Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * Effective License of whole file:
5 : *
6 : * This library is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public
8 : * License version 2.1, as published by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 : * MA 02111-1307 USA
19 : *
20 : * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 : *
22 : * The Contents of this file are made available subject to the terms of
23 : * the GNU Lesser General Public License Version 2.1
24 : *
25 : * Copyright: 2000 by Sun Microsystems, Inc.
26 : *
27 : * Contributor(s): Joerg Budischewski
28 : *
29 : * All parts contributed on or after August 2011:
30 : *
31 : * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
32 : *
33 : * The contents of this file are subject to the Mozilla Public License Version
34 : * 1.1 (the "License"); you may not use this file except in compliance with
35 : * the License or as specified alternatively below. You may obtain a copy of
36 : * the License at http://www.mozilla.org/MPL/
37 : *
38 : * Software distributed under the License is distributed on an "AS IS" basis,
39 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 : * for the specific language governing rights and limitations under the
41 : * License.
42 : *
43 : * Major Contributor(s):
44 : * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
45 : *
46 : * All Rights Reserved.
47 : *
48 : * For minor contributions see the git repository.
49 : *
50 : * Alternatively, the contents of this file may be used under the terms of
51 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 : * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 : * instead of those above.
55 : *
56 : ************************************************************************/
57 :
58 : #include <rtl/ustrbuf.hxx>
59 :
60 : #include <cppuhelper/typeprovider.hxx>
61 : #include <cppuhelper/queryinterface.hxx>
62 :
63 : #include <com/sun/star/beans/PropertyAttribute.hpp>
64 :
65 : #include <com/sun/star/sdbc/XRow.hpp>
66 : #include <com/sun/star/sdbc/XParameters.hpp>
67 :
68 : #include "pq_xtable.hxx"
69 : #include "pq_xtables.hxx"
70 : #include "pq_xviews.hxx"
71 : #include "pq_xindexes.hxx"
72 : #include "pq_xkeys.hxx"
73 : #include "pq_xcolumns.hxx"
74 : #include "pq_tools.hxx"
75 : #include "pq_statics.hxx"
76 :
77 : using osl::MutexGuard;
78 : using osl::Mutex;
79 :
80 : using rtl::OUString;
81 : using rtl::OUStringBuffer;
82 : using rtl::OUStringToOString;
83 :
84 : using com::sun::star::container::XNameAccess;
85 : using com::sun::star::container::XIndexAccess;
86 : using com::sun::star::container::ElementExistException;
87 : using com::sun::star::container::NoSuchElementException;
88 :
89 : using com::sun::star::uno::Reference;
90 : using com::sun::star::uno::Exception;
91 : using com::sun::star::uno::UNO_QUERY;
92 : using com::sun::star::uno::XInterface;
93 : using com::sun::star::uno::Sequence;
94 : using com::sun::star::uno::Any;
95 : using com::sun::star::uno::makeAny;
96 : using com::sun::star::uno::Type;
97 : using com::sun::star::uno::RuntimeException;
98 :
99 : using com::sun::star::lang::IllegalArgumentException;
100 : using com::sun::star::lang::IndexOutOfBoundsException;
101 :
102 : using com::sun::star::beans::XPropertySetInfo;
103 : using com::sun::star::beans::XFastPropertySet;
104 : using com::sun::star::beans::XMultiPropertySet;
105 : using com::sun::star::beans::XPropertySet;
106 : using com::sun::star::beans::Property;
107 :
108 : using com::sun::star::sdbc::XResultSet;
109 : using com::sun::star::sdbc::XPreparedStatement;
110 : using com::sun::star::sdbc::XStatement;
111 : using com::sun::star::sdbc::XParameters;
112 : using com::sun::star::sdbc::XRow;
113 : using com::sun::star::sdbc::SQLException;
114 :
115 : namespace pq_sdbc_driver
116 : {
117 0 : Table::Table( const ::rtl::Reference< RefCountedMutex > & refMutex,
118 : const Reference< com::sun::star::sdbc::XConnection > & connection,
119 : ConnectionSettings *pSettings)
120 : : ReflectionBase(
121 0 : getStatics().refl.table.implName,
122 0 : getStatics().refl.table.serviceNames,
123 : refMutex,
124 : connection,
125 : pSettings,
126 0 : * getStatics().refl.table.pProps ),
127 0 : m_pColumns( 0 )
128 0 : {}
129 :
130 0 : Reference< XPropertySet > Table::createDataDescriptor( ) throw (RuntimeException)
131 : {
132 : TableDescriptor * pTable = new TableDescriptor(
133 0 : m_refMutex, m_conn, m_pSettings );
134 0 : pTable->copyValuesFrom( this );
135 :
136 0 : return Reference< XPropertySet > ( pTable );
137 : }
138 :
139 0 : Reference< XNameAccess > Table::getColumns( ) throw (::com::sun::star::uno::RuntimeException)
140 : {
141 0 : if( ! m_columns.is() )
142 : {
143 : m_columns = Columns::create(
144 : m_refMutex,
145 : m_conn,
146 : m_pSettings,
147 0 : extractStringProperty( this, getStatics().SCHEMA_NAME ),
148 0 : extractStringProperty( this, getStatics().NAME ),
149 0 : &m_pColumns);
150 : }
151 0 : return m_columns;
152 : }
153 :
154 0 : Reference< XNameAccess > Table::getIndexes() throw (::com::sun::star::uno::RuntimeException)
155 : {
156 0 : if( ! m_indexes.is() )
157 : {
158 : m_indexes = ::pq_sdbc_driver::Indexes::create(
159 : m_refMutex,
160 : m_conn,
161 : m_pSettings,
162 0 : extractStringProperty( this, getStatics().SCHEMA_NAME ),
163 0 : extractStringProperty( this, getStatics().NAME ) );
164 : }
165 0 : return m_indexes;
166 : }
167 :
168 0 : Reference< XIndexAccess > Table::getKeys( ) throw (::com::sun::star::uno::RuntimeException)
169 : {
170 0 : if( ! m_keys.is() )
171 : {
172 : m_keys = ::pq_sdbc_driver::Keys::create(
173 : m_refMutex,
174 : m_conn,
175 : m_pSettings,
176 0 : extractStringProperty( this, getStatics().SCHEMA_NAME ),
177 0 : extractStringProperty( this, getStatics().NAME ) );
178 : }
179 0 : return m_keys;
180 : }
181 :
182 0 : void Table::rename( const ::rtl::OUString& newName )
183 : throw (::com::sun::star::sdbc::SQLException,
184 : ::com::sun::star::container::ElementExistException,
185 : ::com::sun::star::uno::RuntimeException)
186 : {
187 0 : MutexGuard guard( m_refMutex->mutex );
188 0 : Statics & st = getStatics();
189 :
190 0 : ::rtl::OUString oldName = extractStringProperty(this,st.NAME );
191 0 : ::rtl::OUString schema = extractStringProperty(this,st.SCHEMA_NAME );
192 0 : ::rtl::OUString fullOldName = concatQualified( schema, oldName );
193 :
194 0 : OUString newTableName;
195 0 : OUString newSchemaName;
196 : // OOo2.0 passes schema + dot + new-table-name while
197 : // OO1.1.x passes new Name without schema
198 : // in case name contains a dot, it is interpreted as schema.tablename
199 0 : if( newName.indexOf( '.' ) >= 0 )
200 : {
201 0 : splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
202 : }
203 : else
204 : {
205 0 : newTableName = newName;
206 0 : newSchemaName = schema;
207 : }
208 0 : ::rtl::OUString fullNewName = concatQualified( newSchemaName, newTableName );
209 :
210 0 : if( extractStringProperty( this, st.TYPE ).equals( st.VIEW ) && m_pSettings->views.is() )
211 : {
212 : // maintain view list (really strange API !)
213 0 : Any a = m_pSettings->pViewsImpl->getByName( fullOldName );
214 0 : Reference< com::sun::star::sdbcx::XRename > Xrename;
215 0 : a >>= Xrename;
216 0 : if( Xrename.is() )
217 : {
218 0 : Xrename->rename( newName );
219 0 : setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, makeAny(newSchemaName) );
220 0 : }
221 : }
222 : else
223 : {
224 0 : if( ! newSchemaName.equals(schema) )
225 : {
226 : // try new schema name first
227 : try
228 : {
229 0 : OUStringBuffer buf(128);
230 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
231 0 : bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
232 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("SET SCHEMA" ) );
233 0 : bufferQuoteIdentifier( buf, newSchemaName, m_pSettings );
234 0 : Reference< XStatement > statement = m_conn->createStatement();
235 0 : statement->executeUpdate( buf.makeStringAndClear() );
236 0 : setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, makeAny(newSchemaName) );
237 0 : disposeNoThrow( statement );
238 0 : schema = newSchemaName;
239 : }
240 0 : catch( com::sun::star::sdbc::SQLException &e )
241 : {
242 0 : OUStringBuffer buf( e.Message );
243 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" ) );
244 0 : e.Message = buf.makeStringAndClear();
245 0 : throw;
246 : }
247 :
248 : }
249 0 : if( ! newTableName.equals( oldName ) ) // might also be just the change of a schema name
250 : {
251 0 : OUStringBuffer buf(128);
252 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
253 0 : bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
254 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("RENAME TO" ) );
255 0 : bufferQuoteIdentifier( buf, newTableName, m_pSettings );
256 0 : Reference< XStatement > statement = m_conn->createStatement();
257 0 : statement->executeUpdate( buf.makeStringAndClear() );
258 0 : disposeNoThrow( statement );
259 : }
260 : }
261 0 : setPropertyValue_NoBroadcast_public( st.NAME, makeAny(newTableName) );
262 : // inform the container of the name change !
263 0 : if( m_pSettings->tables.is() )
264 : {
265 0 : m_pSettings->pTablesImpl->rename( fullOldName, fullNewName );
266 0 : }
267 0 : }
268 :
269 0 : void Table::alterColumnByName(
270 : const ::rtl::OUString& colName,
271 : const Reference< XPropertySet >& descriptor )
272 : throw (SQLException,NoSuchElementException,RuntimeException)
273 : {
274 : Reference< com::sun::star::container::XNameAccess > colums =
275 0 : Reference< com::sun::star::container::XNameAccess > ( getColumns(), UNO_QUERY );
276 :
277 0 : OUString newName = extractStringProperty(descriptor, getStatics().NAME );
278 : ::pq_sdbc_driver::alterColumnByDescriptor(
279 0 : extractStringProperty( this, getStatics().SCHEMA_NAME ),
280 0 : extractStringProperty( this, getStatics().NAME ),
281 : m_pSettings,
282 0 : m_conn->createStatement(),
283 0 : Reference< com::sun::star::beans::XPropertySet>( colums->getByName( colName ), UNO_QUERY) ,
284 0 : descriptor );
285 :
286 0 : if( colName != newName )
287 : {
288 : // m_pColumns->rename( colName, newName );
289 0 : m_pColumns->refresh();
290 0 : }
291 0 : }
292 :
293 0 : void Table::alterColumnByIndex(
294 : sal_Int32 index,
295 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
296 : throw (SQLException,IndexOutOfBoundsException,RuntimeException)
297 : {
298 : Reference< com::sun::star::container::XIndexAccess > colums =
299 0 : Reference< com::sun::star::container::XIndexAccess>( getColumns(), UNO_QUERY );
300 0 : Reference< com::sun::star::beans::XPropertySet> column(colums->getByIndex( index ), UNO_QUERY );
301 0 : OUString oldName = extractStringProperty( column, getStatics().NAME );
302 0 : OUString newName = extractStringProperty( descriptor, getStatics().NAME );
303 : ::pq_sdbc_driver::alterColumnByDescriptor(
304 0 : extractStringProperty( this, getStatics().SCHEMA_NAME ),
305 0 : extractStringProperty( this, getStatics().NAME ),
306 : m_pSettings,
307 0 : m_conn->createStatement(),
308 : column,
309 0 : descriptor );
310 : // m_pColumns->rename( oldName, newName );
311 0 : m_pColumns->refresh();
312 0 : }
313 :
314 0 : Sequence<Type > Table::getTypes() throw( RuntimeException )
315 : {
316 : static cppu::OTypeCollection *pCollection;
317 0 : if( ! pCollection )
318 : {
319 0 : MutexGuard guard( osl::Mutex::getGlobalMutex() );
320 0 : if( !pCollection )
321 : {
322 : static cppu::OTypeCollection collection(
323 0 : getCppuType( (Reference< com::sun::star::sdbcx::XIndexesSupplier> *) 0 ),
324 0 : getCppuType( (Reference< com::sun::star::sdbcx::XKeysSupplier> *) 0 ),
325 0 : getCppuType( (Reference< com::sun::star::sdbcx::XColumnsSupplier> *) 0 ),
326 0 : getCppuType( (Reference< com::sun::star::sdbcx::XRename> *) 0 ),
327 0 : getCppuType( (Reference< com::sun::star::sdbcx::XAlterTable> *) 0 ),
328 0 : ReflectionBase::getTypes());
329 0 : pCollection = &collection;
330 0 : }
331 : }
332 0 : return pCollection->getTypes();
333 : }
334 :
335 0 : Sequence< sal_Int8> Table::getImplementationId() throw( RuntimeException )
336 : {
337 0 : return getStatics().refl.table.implementationId;
338 : }
339 :
340 0 : Any Table::queryInterface( const Type & reqType ) throw (RuntimeException)
341 : {
342 0 : Any ret;
343 :
344 0 : ret = ReflectionBase::queryInterface( reqType );
345 0 : if( ! ret.hasValue() )
346 : ret = ::cppu::queryInterface(
347 : reqType,
348 : static_cast< com::sun::star::sdbcx::XIndexesSupplier * > ( this ),
349 : static_cast< com::sun::star::sdbcx::XKeysSupplier * > ( this ),
350 : static_cast< com::sun::star::sdbcx::XColumnsSupplier * > ( this ),
351 : static_cast< com::sun::star::sdbcx::XRename * > ( this ),
352 : static_cast< com::sun::star::sdbcx::XAlterTable * > ( this )
353 0 : );
354 0 : return ret;
355 : }
356 :
357 0 : ::com::sun::star::uno::Any Table::getPropertyValue(const ::rtl::OUString& aPropertyName)
358 : throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
359 : {
360 0 : return ReflectionBase::getPropertyValue( aPropertyName );
361 : }
362 :
363 :
364 0 : ::rtl::OUString Table::getName( ) throw (::com::sun::star::uno::RuntimeException)
365 : {
366 0 : Statics & st = getStatics();
367 : return concatQualified(
368 : extractStringProperty( this, st.SCHEMA_NAME ),
369 0 : extractStringProperty( this, st.NAME ) );
370 : }
371 :
372 0 : void Table::setName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException)
373 : {
374 0 : rename( aName );
375 0 : }
376 :
377 :
378 :
379 : //________________________________________________________________________
380 0 : TableDescriptor::TableDescriptor(
381 : const ::rtl::Reference< RefCountedMutex > & refMutex,
382 : const Reference< com::sun::star::sdbc::XConnection > & connection,
383 : ConnectionSettings *pSettings)
384 : : ReflectionBase(
385 0 : getStatics().refl.tableDescriptor.implName,
386 0 : getStatics().refl.tableDescriptor.serviceNames,
387 : refMutex,
388 : connection,
389 : pSettings,
390 0 : * getStatics().refl.tableDescriptor.pProps )
391 : {
392 0 : }
393 :
394 0 : Reference< XNameAccess > TableDescriptor::getColumns( ) throw (::com::sun::star::uno::RuntimeException)
395 : {
396 0 : if( ! m_columns.is() )
397 : {
398 0 : m_columns = new ColumnDescriptors(m_refMutex, m_conn, m_pSettings );
399 : }
400 0 : return m_columns;
401 : }
402 :
403 0 : Reference< XNameAccess > TableDescriptor::getIndexes() throw (::com::sun::star::uno::RuntimeException)
404 : {
405 0 : if( ! m_indexes.is() )
406 : {
407 : m_indexes = ::pq_sdbc_driver::IndexDescriptors::create(
408 : m_refMutex,
409 : m_conn,
410 0 : m_pSettings);
411 : }
412 0 : return m_indexes;
413 : }
414 :
415 0 : Reference< XIndexAccess > TableDescriptor::getKeys( ) throw (::com::sun::star::uno::RuntimeException)
416 : {
417 0 : if( ! m_keys.is() )
418 : {
419 : m_keys = ::pq_sdbc_driver::KeyDescriptors::create(
420 : m_refMutex,
421 : m_conn,
422 0 : m_pSettings );
423 : }
424 0 : return m_keys;
425 : }
426 :
427 :
428 0 : Sequence<Type > TableDescriptor::getTypes() throw( RuntimeException )
429 : {
430 : static cppu::OTypeCollection *pCollection;
431 0 : if( ! pCollection )
432 : {
433 0 : MutexGuard guard( osl::Mutex::getGlobalMutex() );
434 0 : if( !pCollection )
435 : {
436 : static cppu::OTypeCollection collection(
437 0 : getCppuType( (Reference< com::sun::star::sdbcx::XIndexesSupplier> *) 0 ),
438 0 : getCppuType( (Reference< com::sun::star::sdbcx::XKeysSupplier> *) 0 ),
439 0 : getCppuType( (Reference< com::sun::star::sdbcx::XColumnsSupplier> *) 0 ),
440 0 : ReflectionBase::getTypes());
441 0 : pCollection = &collection;
442 0 : }
443 : }
444 0 : return pCollection->getTypes();
445 : }
446 :
447 0 : Sequence< sal_Int8> TableDescriptor::getImplementationId() throw( RuntimeException )
448 : {
449 0 : return getStatics().refl.tableDescriptor.implementationId;
450 : }
451 :
452 0 : Any TableDescriptor::queryInterface( const Type & reqType ) throw (RuntimeException)
453 : {
454 0 : Any ret;
455 :
456 0 : ret = ReflectionBase::queryInterface( reqType );
457 0 : if( ! ret.hasValue() )
458 : ret = ::cppu::queryInterface(
459 : reqType,
460 : static_cast< com::sun::star::sdbcx::XIndexesSupplier * > ( this ),
461 : static_cast< com::sun::star::sdbcx::XKeysSupplier * > ( this ),
462 0 : static_cast< com::sun::star::sdbcx::XColumnsSupplier * > ( this ));
463 0 : return ret;
464 : }
465 :
466 :
467 0 : Reference< XPropertySet > TableDescriptor::createDataDescriptor( ) throw (RuntimeException)
468 : {
469 : TableDescriptor * pTable = new TableDescriptor(
470 0 : m_refMutex, m_conn, m_pSettings );
471 :
472 : // TODO: deep copies
473 0 : pTable->m_values = m_values;
474 :
475 0 : return Reference< XPropertySet > ( pTable );
476 : }
477 :
478 : }
|