1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "connectivity/sdbcx/VTable.hxx"
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include "connectivity/sdbcx/VIndex.hxx"
29 #include <comphelper/sequence.hxx>
30 #include "connectivity/sdbcx/VCollection.hxx"
31 #include "TConnection.hxx"
32 #include "connectivity/sdbcx/VColumn.hxx"
33 #include "connectivity/sdbcx/VKey.hxx"
34 #include "connectivity/dbtools.hxx"
35 #include <connectivity/dbexception.hxx>
36 
37 // -------------------------------------------------------------------------
38 using namespace ::connectivity;
39 using namespace ::connectivity::sdbcx;
40 using namespace ::dbtools;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
47 
48 // -----------------------------------------------------------------------------
49 ::rtl::OUString SAL_CALL OTable::getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException)
50 {
51 	if(isNew())
52 		return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.VTableDescriptor");
53 	return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Table");
54 }
55 
56 // -----------------------------------------------------------------------------
57 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL OTable::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
58 {
59 	::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1);
60 	if(isNew())
61 		aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.TableDescriptor");
62 	else
63 		aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Table");
64 
65 	return aSupported;
66 }
67 // -----------------------------------------------------------------------------
68 sal_Bool SAL_CALL OTable::supportsService( const ::rtl::OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException)
69 {
70 	Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
71 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
72 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
73 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
74 		;
75 
76 	return pSupported != pEnd;
77 }
78 // -------------------------------------------------------------------------
79 OTable::OTable(OCollection*	_pTables,
80 			   sal_Bool _bCase)
81 			   : OTableDescriptor_BASE(m_aMutex)
82 				,ODescriptor(OTableDescriptor_BASE::rBHelper,_bCase,sal_True)
83 				,m_pKeys(NULL)
84 				,m_pColumns(NULL)
85 				,m_pIndexes(NULL)
86 				,m_pTables(_pTables)
87 {
88 }
89 // -----------------------------------------------------------------------------
90 OTable::OTable( OCollection*	_pTables,
91 				sal_Bool _bCase,
92 				const ::rtl::OUString& _Name,		const ::rtl::OUString& _Type,
93 				const ::rtl::OUString& _Description,const ::rtl::OUString& _SchemaName,
94 				const ::rtl::OUString& _CatalogName) :	OTableDescriptor_BASE(m_aMutex)
95 				,ODescriptor(OTableDescriptor_BASE::rBHelper,_bCase)
96                 ,m_CatalogName(_CatalogName)
97                 ,m_SchemaName(_SchemaName)
98                 ,m_Description(_Description)
99                 ,m_Type(_Type)
100 				,m_pKeys(NULL)
101 				,m_pColumns(NULL)
102 				,m_pIndexes(NULL)
103                 ,m_pTables(_pTables)
104 {
105 	m_Name = _Name;
106 }
107 // -------------------------------------------------------------------------
108 OTable::~OTable()
109 {
110 	delete m_pKeys;
111 	delete m_pColumns;
112 	delete m_pIndexes;
113 }
114 // -------------------------------------------------------------------------
115 void OTable::construct()
116 {
117 	ODescriptor::construct();
118 
119 	sal_Int32 nAttrib = isNew() ? 0 : PropertyAttribute::READONLY;
120 
121 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CATALOGNAME),		PROPERTY_ID_CATALOGNAME,nAttrib,&m_CatalogName,	::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
122 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME),		PROPERTY_ID_SCHEMANAME,	nAttrib,&m_SchemaName,	::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
123 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION),		PROPERTY_ID_DESCRIPTION,nAttrib,&m_Description,	::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
124 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),			PROPERTY_ID_TYPE,		nAttrib,&m_Type,		::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
125 }
126 // -----------------------------------------------------------------------------
127 void SAL_CALL OTable::acquire() throw()
128 {
129 	OTableDescriptor_BASE::acquire();
130 }
131 // -----------------------------------------------------------------------------
132 void SAL_CALL OTable::release() throw()
133 {
134 	OTableDescriptor_BASE::release();
135 }
136 
137 // -------------------------------------------------------------------------
138 Any SAL_CALL OTable::queryInterface( const Type & rType ) throw(RuntimeException)
139 {
140 	Any aRet = ODescriptor::queryInterface( rType);
141 	if(!aRet.hasValue())
142 	{
143 		if(!isNew())
144 			aRet = OTable_BASE::queryInterface( rType);
145 		if(isNew() && (rType == getCppuType( (Reference<XIndexesSupplier>*)0)))
146 			return Any();
147 		if(!aRet.hasValue())
148 			aRet = OTableDescriptor_BASE::queryInterface( rType);
149 	}
150 	return aRet;
151 }
152 // -------------------------------------------------------------------------
153 Sequence< Type > SAL_CALL OTable::getTypes(  ) throw(RuntimeException)
154 {
155 	if(isNew())
156 		return ::comphelper::concatSequences(ODescriptor::getTypes(),OTableDescriptor_BASE::getTypes());
157 	return ::comphelper::concatSequences(ODescriptor::getTypes(),OTableDescriptor_BASE::getTypes(),OTable_BASE::getTypes());
158 }
159 // -------------------------------------------------------------------------
160 void SAL_CALL OTable::disposing(void)
161 {
162 	ODescriptor::disposing();
163 
164 	::osl::MutexGuard aGuard(m_aMutex);
165 
166 	if(m_pKeys)
167 		m_pKeys->disposing();
168 	if(m_pColumns)
169 		m_pColumns->disposing();
170 	if(m_pIndexes)
171 		m_pIndexes->disposing();
172 
173 	m_pTables = NULL;
174 }
175 // -----------------------------------------------------------------------------
176 // XColumnsSupplier
177 Reference< XNameAccess > SAL_CALL OTable::getColumns(  ) throw(RuntimeException)
178 {
179 	::osl::MutexGuard aGuard(m_aMutex);
180 	checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
181 
182 	try
183 	{
184 		if ( !m_pColumns )
185 			refreshColumns();
186 	}
187 	catch( const RuntimeException& )
188 	{
189 		// allowed to leave this method
190 		throw;
191 	}
192 	catch( const Exception& )
193 	{
194 		// allowed
195 	}
196 
197 	return m_pColumns;
198 }
199 
200 // -------------------------------------------------------------------------
201 // XKeysSupplier
202 Reference< XIndexAccess > SAL_CALL OTable::getKeys(  ) throw(RuntimeException)
203 {
204 	::osl::MutexGuard aGuard(m_aMutex);
205 	checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
206 
207     Reference< XIndexAccess > xKeys;
208 
209 	try
210 	{
211         if ( !m_pKeys )
212 		    refreshKeys();
213         xKeys = m_pKeys;
214 	}
215 	catch( const RuntimeException& )
216 	{
217 		// allowed to leave this method
218 		throw;
219 	}
220 	catch( const Exception& )
221 	{
222 		// allowed
223 	}
224 
225 	return xKeys;
226 }
227 // -----------------------------------------------------------------------------
228 cppu::IPropertyArrayHelper* OTable::createArrayHelper( sal_Int32 /*_nId*/ ) const
229 {
230     return doCreateArrayHelper();
231 }
232 // -------------------------------------------------------------------------
233 cppu::IPropertyArrayHelper & OTable::getInfoHelper()
234 {
235 	return *const_cast<OTable*>(this)->getArrayHelper(isNew() ? 1 : 0);
236 }
237 // -------------------------------------------------------------------------
238 Reference< XPropertySet > SAL_CALL OTable::createDataDescriptor(  ) throw(RuntimeException)
239 {
240 	::osl::MutexGuard aGuard(m_aMutex);
241 	checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
242 
243 	OTable* pTable = new OTable(m_pTables,isCaseSensitive(),m_Name,m_Type,m_Description,m_SchemaName,m_CatalogName);
244 	pTable->setNew(sal_True);
245 	return pTable;
246 }
247 // -------------------------------------------------------------------------
248 // XIndexesSupplier
249 Reference< XNameAccess > SAL_CALL OTable::getIndexes(  ) throw(RuntimeException)
250 {
251 	::osl::MutexGuard aGuard(m_aMutex);
252 	checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
253 
254 	try
255 	{
256 		if ( !m_pIndexes )
257 			refreshIndexes();
258 	}
259 	catch( const RuntimeException& )
260 	{
261 		// allowed to leave this method
262 		throw;
263 	}
264 	catch( const Exception& )
265 	{
266 		// allowed
267 	}
268 
269 	return m_pIndexes;
270 }
271 // -------------------------------------------------------------------------
272 // XRename
273 void SAL_CALL OTable::rename( const ::rtl::OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
274 {
275 	::osl::MutexGuard aGuard(m_aMutex);
276 	checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
277 
278 	const ::rtl::OUString sOldComposedName = getName();
279 	const Reference< XDatabaseMetaData> xMetaData = getMetaData();
280 	if ( xMetaData.is() )
281 		::dbtools::qualifiedNameComponents(xMetaData,newName,m_CatalogName,m_SchemaName,m_Name,::dbtools::eInDataManipulation);
282 	else
283 		m_Name = newName;
284 
285 	m_pTables->renameObject(sOldComposedName,newName);
286 }
287 // -----------------------------------------------------------------------------
288 Reference< XDatabaseMetaData> OTable::getMetaData() const
289 {
290 	return NULL;
291 }
292 // -------------------------------------------------------------------------
293 // XAlterTable
294 void SAL_CALL OTable::alterColumnByName( const ::rtl::OUString& /*colName*/, const Reference< XPropertySet >& /*descriptor*/ ) throw(SQLException, NoSuchElementException, RuntimeException)
295 {
296     throwFeatureNotImplementedException( "XAlterTable::alterColumnByName", *this );
297 }
298 // -------------------------------------------------------------------------
299 void SAL_CALL OTable::alterColumnByIndex( sal_Int32 /*index*/, const Reference< XPropertySet >& /*descriptor*/ ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException)
300 {
301     throwFeatureNotImplementedException( "XAlterTable::alterColumnByIndex", *this );
302 }
303 // -------------------------------------------------------------------------
304 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OTable::getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException)
305 {
306 	return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
307 }
308 // -----------------------------------------------------------------------------
309 ::rtl::OUString SAL_CALL OTable::getName() throw(::com::sun::star::uno::RuntimeException)
310 {
311 	// this is only correct for tables who haven't a schema or catalog name
312 	OSL_ENSURE(!m_CatalogName.getLength(),"getName(): forgot to overload getName()!");
313 	OSL_ENSURE(!m_SchemaName.getLength(),"getName(): forgot to overload getName()!");
314 	return m_Name;
315 }
316 // -----------------------------------------------------------------------------
317 void SAL_CALL OTable::setName( const ::rtl::OUString& /*aName*/ ) throw(::com::sun::star::uno::RuntimeException)
318 {
319 }
320 // -----------------------------------------------------------------------------
321 void OTable::refreshColumns()
322 {
323 }
324 // -----------------------------------------------------------------------------
325 void OTable::refreshKeys()
326 {
327 }
328 // -----------------------------------------------------------------------------
329 void OTable::refreshIndexes()
330 {
331 }
332 // -----------------------------------------------------------------------------
333