Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <string.h>
21 : #include "hsqldb/HTable.hxx"
22 : #include "hsqldb/HTables.hxx"
23 : #include <com/sun/star/sdbc/XRow.hpp>
24 : #include <com/sun/star/sdbc/XResultSet.hpp>
25 : #include <com/sun/star/sdbcx/KeyType.hpp>
26 : #include <com/sun/star/sdbc/KeyRule.hpp>
27 : #include <cppuhelper/typeprovider.hxx>
28 : #include <com/sun/star/lang/DisposedException.hpp>
29 : #include <com/sun/star/sdbc/ColumnValue.hpp>
30 : #include <com/sun/star/sdbcx/Privilege.hpp>
31 : #include <comphelper/property.hxx>
32 : #include <comphelper/extract.hxx>
33 : #include <comphelper/types.hxx>
34 : #include "connectivity/dbtools.hxx"
35 : #include "connectivity/sdbcx/VColumn.hxx"
36 : #include "connectivity/TKeys.hxx"
37 : #include "connectivity/TIndexes.hxx"
38 : #include "connectivity/TColumnsHelper.hxx"
39 : #include "hsqldb/HCatalog.hxx"
40 : #include "hsqldb/HColumns.hxx"
41 : #include "TConnection.hxx"
42 :
43 : #include <tools/diagnose_ex.h>
44 :
45 :
46 : using namespace ::comphelper;
47 : using namespace connectivity::hsqldb;
48 : using namespace connectivity::sdbcx;
49 : using namespace connectivity;
50 : using namespace ::com::sun::star::uno;
51 : using namespace ::com::sun::star::beans;
52 : using namespace ::com::sun::star::sdbcx;
53 : using namespace ::com::sun::star::sdbc;
54 : using namespace ::com::sun::star::container;
55 : using namespace ::com::sun::star::lang;
56 :
57 0 : OHSQLTable::OHSQLTable( sdbcx::OCollection* _pTables,
58 : const Reference< XConnection >& _xConnection)
59 0 : :OTableHelper(_pTables,_xConnection,sal_True)
60 : {
61 : // we create a new table here, so we should have all the rights or ;-)
62 : m_nPrivileges = Privilege::DROP |
63 : Privilege::REFERENCE |
64 : Privilege::ALTER |
65 : Privilege::CREATE |
66 : Privilege::READ |
67 : Privilege::DELETE |
68 : Privilege::UPDATE |
69 : Privilege::INSERT |
70 0 : Privilege::SELECT;
71 0 : construct();
72 0 : }
73 : // -------------------------------------------------------------------------
74 0 : OHSQLTable::OHSQLTable( sdbcx::OCollection* _pTables,
75 : const Reference< XConnection >& _xConnection,
76 : const ::rtl::OUString& _Name,
77 : const ::rtl::OUString& _Type,
78 : const ::rtl::OUString& _Description ,
79 : const ::rtl::OUString& _SchemaName,
80 : const ::rtl::OUString& _CatalogName,
81 : sal_Int32 _nPrivileges
82 : ) : OTableHelper( _pTables,
83 : _xConnection,
84 : sal_True,
85 : _Name,
86 : _Type,
87 : _Description,
88 : _SchemaName,
89 : _CatalogName)
90 0 : , m_nPrivileges(_nPrivileges)
91 : {
92 0 : construct();
93 0 : }
94 : // -------------------------------------------------------------------------
95 0 : void OHSQLTable::construct()
96 : {
97 0 : OTableHelper::construct();
98 0 : if ( !isNew() )
99 0 : registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRIVILEGES), PROPERTY_ID_PRIVILEGES,PropertyAttribute::READONLY,&m_nPrivileges, ::getCppuType(&m_nPrivileges));
100 0 : }
101 : // -----------------------------------------------------------------------------
102 0 : ::cppu::IPropertyArrayHelper* OHSQLTable::createArrayHelper( sal_Int32 /*_nId*/ ) const
103 : {
104 0 : return doCreateArrayHelper();
105 : }
106 : // -------------------------------------------------------------------------
107 0 : ::cppu::IPropertyArrayHelper & OHSQLTable::getInfoHelper()
108 : {
109 0 : return *static_cast<OHSQLTable_PROP*>(const_cast<OHSQLTable*>(this))->getArrayHelper(isNew() ? 1 : 0);
110 : }
111 : // -----------------------------------------------------------------------------
112 0 : sdbcx::OCollection* OHSQLTable::createColumns(const TStringVector& _rNames)
113 : {
114 0 : OHSQLColumns* pColumns = new OHSQLColumns(*this,sal_True,m_aMutex,_rNames);
115 0 : pColumns->setParent(this);
116 0 : return pColumns;
117 : }
118 : // -----------------------------------------------------------------------------
119 0 : sdbcx::OCollection* OHSQLTable::createKeys(const TStringVector& _rNames)
120 : {
121 0 : return new OKeysHelper(this,m_aMutex,_rNames);
122 : }
123 : // -----------------------------------------------------------------------------
124 0 : sdbcx::OCollection* OHSQLTable::createIndexes(const TStringVector& _rNames)
125 : {
126 0 : return new OIndexesHelper(this,m_aMutex,_rNames);
127 : }
128 : //--------------------------------------------------------------------------
129 0 : Sequence< sal_Int8 > OHSQLTable::getUnoTunnelImplementationId()
130 : {
131 : static ::cppu::OImplementationId * pId = 0;
132 0 : if (! pId)
133 : {
134 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
135 0 : if (! pId)
136 : {
137 0 : static ::cppu::OImplementationId aId;
138 0 : pId = &aId;
139 0 : }
140 : }
141 0 : return pId->getImplementationId();
142 : }
143 :
144 : // com::sun::star::lang::XUnoTunnel
145 : //------------------------------------------------------------------
146 0 : sal_Int64 OHSQLTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
147 : {
148 0 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
149 : ? reinterpret_cast< sal_Int64 >( this )
150 0 : : OTable_TYPEDEF::getSomething(rId);
151 : }
152 : // -------------------------------------------------------------------------
153 : // XAlterTable
154 0 : void SAL_CALL OHSQLTable::alterColumnByName( const ::rtl::OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
155 : {
156 0 : ::osl::MutexGuard aGuard(m_aMutex);
157 : checkDisposed(
158 : #ifdef GCC
159 : ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
160 : #else
161 : rBHelper.bDisposed
162 : #endif
163 0 : );
164 :
165 0 : if ( m_pColumns && !m_pColumns->hasByName(colName) )
166 0 : throw NoSuchElementException(colName,*this);
167 :
168 :
169 0 : if ( !isNew() )
170 : {
171 : // first we have to check what should be altered
172 0 : Reference<XPropertySet> xProp;
173 0 : m_pColumns->getByName(colName) >>= xProp;
174 : // first check the types
175 0 : sal_Int32 nOldType = 0,nNewType = 0,nOldPrec = 0,nNewPrec = 0,nOldScale = 0,nNewScale = 0;
176 0 : ::rtl::OUString sOldTypeName, sNewTypeName;
177 :
178 0 : ::dbtools::OPropertyMap& rProp = OMetaConnection::getPropMap();
179 :
180 : // type/typename
181 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPE)) >>= nOldType;
182 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPE)) >>= nNewType;
183 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPENAME)) >>= sOldTypeName;
184 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPENAME))>>= sNewTypeName;
185 :
186 : // and precsions and scale
187 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_PRECISION)) >>= nOldPrec;
188 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_PRECISION))>>= nNewPrec;
189 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_SCALE)) >>= nOldScale;
190 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_SCALE)) >>= nNewScale;
191 :
192 : // second: check the "is nullable" value
193 0 : sal_Int32 nOldNullable = 0,nNewNullable = 0;
194 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISNULLABLE)) >>= nOldNullable;
195 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISNULLABLE)) >>= nNewNullable;
196 :
197 : // check also the auto_increment
198 0 : sal_Bool bOldAutoIncrement = sal_False,bAutoIncrement = sal_False;
199 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bOldAutoIncrement;
200 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bAutoIncrement;
201 :
202 : // now we should look if the name of the column changed
203 0 : ::rtl::OUString sNewColumnName;
204 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_NAME)) >>= sNewColumnName;
205 0 : if ( !sNewColumnName.equals(colName) )
206 : {
207 0 : const ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
208 :
209 0 : ::rtl::OUString sSql = getAlterTableColumnPart();
210 0 : sSql += ::rtl::OUString(" ALTER COLUMN ");
211 0 : sSql += ::dbtools::quoteName(sQuote,colName);
212 :
213 0 : sSql += ::rtl::OUString(" RENAME TO ");
214 0 : sSql += ::dbtools::quoteName(sQuote,sNewColumnName);
215 :
216 0 : executeStatement(sSql);
217 : }
218 :
219 0 : if ( nOldType != nNewType
220 0 : || sOldTypeName != sNewTypeName
221 : || nOldPrec != nNewPrec
222 : || nOldScale != nNewScale
223 : || nNewNullable != nOldNullable
224 : || bOldAutoIncrement != bAutoIncrement )
225 : {
226 : // special handling because they change the type names to distinguish
227 : // if a column should be an auto_incmrement one
228 0 : if ( bOldAutoIncrement != bAutoIncrement )
229 : {
230 : /// TODO: insert special handling for auto increment "IDENTITY" and primary key
231 : }
232 0 : alterColumnType(nNewType,sNewColumnName,descriptor);
233 : }
234 :
235 : // third: check the default values
236 0 : ::rtl::OUString sNewDefault,sOldDefault;
237 0 : xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)) >>= sOldDefault;
238 0 : descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)) >>= sNewDefault;
239 :
240 0 : if(!sOldDefault.isEmpty())
241 : {
242 0 : dropDefaultValue(colName);
243 0 : if(!sNewDefault.isEmpty() && sOldDefault != sNewDefault)
244 0 : alterDefaultValue(sNewDefault,sNewColumnName);
245 : }
246 0 : else if(sOldDefault.isEmpty() && !sNewDefault.isEmpty())
247 0 : alterDefaultValue(sNewDefault,sNewColumnName);
248 :
249 0 : m_pColumns->refresh();
250 : }
251 : else
252 : {
253 0 : if(m_pColumns)
254 : {
255 0 : m_pColumns->dropByName(colName);
256 0 : m_pColumns->appendByDescriptor(descriptor);
257 : }
258 0 : }
259 :
260 0 : }
261 : // -----------------------------------------------------------------------------
262 0 : void OHSQLTable::alterColumnType(sal_Int32 nNewType,const ::rtl::OUString& _rColName, const Reference<XPropertySet>& _xDescriptor)
263 : {
264 0 : ::rtl::OUString sSql = getAlterTableColumnPart();
265 :
266 0 : sSql += ::rtl::OUString(" ALTER COLUMN ");
267 : #if OSL_DEBUG_LEVEL > 0
268 : try
269 : {
270 : ::rtl::OUString sDescriptorName;
271 : OSL_ENSURE( _xDescriptor.is()
272 : && ( _xDescriptor->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_NAME ) ) >>= sDescriptorName )
273 : && ( sDescriptorName == _rColName ),
274 : "OHSQLTable::alterColumnType: unexpected column name!" );
275 : }
276 : catch( const Exception& )
277 : {
278 : DBG_UNHANDLED_EXCEPTION();
279 : }
280 : #else
281 : (void)_rColName;
282 : #endif
283 :
284 0 : OHSQLColumn* pColumn = new OHSQLColumn(sal_True);
285 0 : Reference<XPropertySet> xProp = pColumn;
286 0 : ::comphelper::copyProperties(_xDescriptor,xProp);
287 0 : xProp->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),makeAny(nNewType));
288 :
289 0 : sSql += ::dbtools::createStandardColumnPart(xProp,getConnection());
290 0 : executeStatement(sSql);
291 0 : }
292 : // -----------------------------------------------------------------------------
293 0 : void OHSQLTable::alterDefaultValue(const ::rtl::OUString& _sNewDefault,const ::rtl::OUString& _rColName)
294 : {
295 0 : ::rtl::OUString sSql = getAlterTableColumnPart();
296 0 : sSql += ::rtl::OUString(" ALTER COLUMN ");
297 :
298 0 : const ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
299 0 : sSql += ::dbtools::quoteName(sQuote,_rColName);
300 0 : sSql += ::rtl::OUString(" SET DEFAULT '") + _sNewDefault;
301 0 : sSql += ::rtl::OUString("'");
302 :
303 0 : executeStatement(sSql);
304 0 : }
305 : // -----------------------------------------------------------------------------
306 0 : void OHSQLTable::dropDefaultValue(const ::rtl::OUString& _rColName)
307 : {
308 0 : ::rtl::OUString sSql = getAlterTableColumnPart();
309 0 : sSql += ::rtl::OUString(" ALTER COLUMN ");
310 :
311 0 : const ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
312 0 : sSql += ::dbtools::quoteName(sQuote,_rColName);
313 0 : sSql += ::rtl::OUString(" DROP DEFAULT");
314 :
315 0 : executeStatement(sSql);
316 0 : }
317 : // -----------------------------------------------------------------------------
318 0 : ::rtl::OUString OHSQLTable::getAlterTableColumnPart()
319 : {
320 0 : ::rtl::OUString sSql( "ALTER TABLE " );
321 0 : const ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
322 :
323 0 : ::rtl::OUString sComposedName( ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, sal_True, ::dbtools::eInTableDefinitions ) );
324 0 : sSql += sComposedName;
325 :
326 0 : return sSql;
327 : }
328 : // -----------------------------------------------------------------------------
329 0 : void OHSQLTable::executeStatement(const ::rtl::OUString& _rStatement )
330 : {
331 0 : ::rtl::OUString sSQL = _rStatement;
332 0 : if(sSQL.lastIndexOf(',') == (sSQL.getLength()-1))
333 0 : sSQL = sSQL.replaceAt(sSQL.getLength()-1,1,::rtl::OUString(")"));
334 :
335 0 : Reference< XStatement > xStmt = getConnection()->createStatement( );
336 0 : if ( xStmt.is() )
337 : {
338 0 : try { xStmt->execute(sSQL); }
339 0 : catch( const Exception& )
340 : {
341 0 : ::comphelper::disposeComponent(xStmt);
342 0 : throw;
343 : }
344 0 : ::comphelper::disposeComponent(xStmt);
345 0 : }
346 0 : }
347 : // -----------------------------------------------------------------------------
348 0 : Sequence< Type > SAL_CALL OHSQLTable::getTypes( ) throw(RuntimeException)
349 : {
350 0 : if ( ! m_Type.compareToAscii("VIEW") )
351 : {
352 0 : Sequence< Type > aTypes = OTableHelper::getTypes();
353 0 : ::std::vector<Type> aOwnTypes;
354 0 : aOwnTypes.reserve(aTypes.getLength());
355 0 : const Type* pIter = aTypes.getConstArray();
356 0 : const Type* pEnd = pIter + aTypes.getLength();
357 0 : for(;pIter != pEnd;++pIter)
358 : {
359 0 : if( *pIter != ::getCppuType((const Reference<XRename>*)0) )
360 : {
361 0 : aOwnTypes.push_back(*pIter);
362 : }
363 : }
364 0 : Type *pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
365 0 : return Sequence< Type >(pTypes, aOwnTypes.size());
366 : }
367 0 : return OTableHelper::getTypes();
368 : }
369 : // -------------------------------------------------------------------------
370 : // XRename
371 0 : void SAL_CALL OHSQLTable::rename( const ::rtl::OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
372 : {
373 0 : ::osl::MutexGuard aGuard(m_aMutex);
374 : checkDisposed(
375 : #ifdef GCC
376 : ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
377 : #else
378 : rBHelper.bDisposed
379 : #endif
380 0 : );
381 :
382 0 : if(!isNew())
383 : {
384 0 : ::rtl::OUString sSql = ::rtl::OUString("ALTER ");
385 0 : if ( m_Type == ::rtl::OUString("VIEW") )
386 0 : sSql += ::rtl::OUString(" VIEW ");
387 : else
388 0 : sSql += ::rtl::OUString(" TABLE ");
389 :
390 0 : ::rtl::OUString sQuote = getMetaData()->getIdentifierQuoteString( );
391 :
392 0 : ::rtl::OUString sCatalog,sSchema,sTable;
393 0 : ::dbtools::qualifiedNameComponents(getMetaData(),newName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
394 :
395 : ::rtl::OUString sComposedName(
396 0 : ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, sal_True, ::dbtools::eInDataManipulation ) );
397 : sSql += sComposedName
398 0 : + ::rtl::OUString(" RENAME TO ");
399 0 : sSql += ::dbtools::composeTableName( getMetaData(), sCatalog, sSchema, sTable, sal_True, ::dbtools::eInDataManipulation );
400 :
401 0 : executeStatement(sSql);
402 :
403 0 : ::connectivity::OTable_TYPEDEF::rename(newName);
404 : }
405 : else
406 0 : ::dbtools::qualifiedNameComponents(getMetaData(),newName,m_CatalogName,m_SchemaName,m_Name,::dbtools::eInTableDefinitions);
407 0 : }
408 :
409 : // -------------------------------------------------------------------------
410 0 : Any SAL_CALL OHSQLTable::queryInterface( const Type & rType ) throw(RuntimeException)
411 : {
412 0 : if( !m_Type.compareToAscii("VIEW") && rType == ::getCppuType((const Reference<XRename>*)0) )
413 0 : return Any();
414 :
415 0 : return OTableHelper::queryInterface(rType);
416 : }
417 : // -------------------------------------------------------------------------
418 :
419 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|