Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xtables.cxx
blobb7e20f785bf73faf7412bc6ed7386f30633fa8bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * Effective License of whole file:
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.
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.
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
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
25 * Copyright: 2000 by Sun Microsystems, Inc.
27 * Contributor(s): Joerg Budischewski
29 * All parts contributed on or after August 2011:
31 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
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/
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.
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
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.
56 ************************************************************************/
58 #include <rtl/ustrbuf.hxx>
60 #include <com/sun/star/sdbc/XRow.hpp>
61 #include <com/sun/star/sdbc/XParameters.hpp>
62 #include <com/sun/star/sdbcx/Privilege.hpp>
63 #include <com/sun/star/sdbcx/KeyType.hpp>
64 #include <com/sun/star/sdbc/KeyRule.hpp>
65 #include <com/sun/star/sdbc/DataType.hpp>
67 #include "pq_xtables.hxx"
68 #include "pq_xviews.hxx"
69 #include "pq_xtable.hxx"
70 #include "pq_statics.hxx"
71 #include "pq_tools.hxx"
73 using osl::MutexGuard;
75 using rtl::OUString;
76 using rtl::OUStringBuffer;
77 using rtl::OUStringToOString;
79 using com::sun::star::beans::XPropertySet;
81 using com::sun::star::uno::Any;
82 using com::sun::star::uno::makeAny;
83 using com::sun::star::uno::UNO_QUERY;
84 using com::sun::star::uno::Type;
85 using com::sun::star::uno::XInterface;
86 using com::sun::star::uno::Reference;
87 using com::sun::star::uno::Sequence;
88 using com::sun::star::uno::RuntimeException;
90 using com::sun::star::container::NoSuchElementException;
91 using com::sun::star::container::XEnumerationAccess;
92 using com::sun::star::container::XEnumeration;
93 using com::sun::star::lang::WrappedTargetException;
95 using com::sun::star::sdbc::XRow;
96 // using com::sun::star::sdbc::DataType;
97 using com::sun::star::sdbc::XCloseable;
98 using com::sun::star::sdbc::XStatement;
99 using com::sun::star::sdbc::XResultSet;
100 using com::sun::star::sdbc::XParameters;
101 using com::sun::star::sdbc::XPreparedStatement;
102 using com::sun::star::sdbc::XDatabaseMetaData;
103 using com::sun::star::sdbcx::XColumnsSupplier;
104 using com::sun::star::sdbcx::XKeysSupplier;
105 using com::sun::star::sdbcx::XViewsSupplier;
106 // using com::sun::star::sdbcx::Privilege;
108 namespace pq_sdbc_driver
110 Tables::Tables(
111 const ::rtl::Reference< RefCountedMutex > & refMutex,
112 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
113 ConnectionSettings *pSettings )
114 : Container( refMutex, origin, pSettings, getStatics().TABLE )
117 Tables::~Tables()
120 void Tables::refresh()
121 throw (::com::sun::star::uno::RuntimeException)
125 osl::MutexGuard guard( m_refMutex->mutex );
126 Statics & st = getStatics();
128 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
130 Reference< XResultSet > rs =
131 meta->getTables( Any(), st.cPERCENT,st.cPERCENT, Sequence< OUString > () );
133 Reference< XRow > xRow( rs , UNO_QUERY );
135 String2IntMap map;
137 m_values = Sequence< com::sun::star::uno::Any > ();
138 sal_Int32 tableIndex = 0;
139 while( rs->next() )
141 // if creating all these tables turns out to have too bad performance, we might
142 // instead offer a factory interface
143 Table * pTable =
144 new Table( m_refMutex, m_origin, m_pSettings );
145 Reference< com::sun::star::beans::XPropertySet > prop = pTable;
147 OUString name = xRow->getString( TABLE_INDEX_NAME+1);
148 OUString schema = xRow->getString( TABLE_INDEX_SCHEMA+1);
149 pTable->setPropertyValue_NoBroadcast_public(
150 st.CATALOG_NAME , makeAny(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
151 pTable->setPropertyValue_NoBroadcast_public( st.NAME , makeAny( name ) );
152 pTable->setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME , makeAny( schema ));
153 pTable->setPropertyValue_NoBroadcast_public(
154 st.TYPE , makeAny( xRow->getString( TABLE_INDEX_TYPE+1) ) );
155 pTable->setPropertyValue_NoBroadcast_public(
156 st.DESCRIPTION , makeAny( xRow->getString( TABLE_INDEX_REMARKS+1) ) );
157 pTable->setPropertyValue_NoBroadcast_public(
158 st.PRIVILEGES ,
159 makeAny( (sal_Int32)
160 ( com::sun::star::sdbcx::Privilege::SELECT |
161 com::sun::star::sdbcx::Privilege::INSERT |
162 com::sun::star::sdbcx::Privilege::UPDATE |
163 com::sun::star::sdbcx::Privilege::DELETE |
164 com::sun::star::sdbcx::Privilege::READ |
165 com::sun::star::sdbcx::Privilege::CREATE |
166 com::sun::star::sdbcx::Privilege::ALTER |
167 com::sun::star::sdbcx::Privilege::REFERENCE |
168 com::sun::star::sdbcx::Privilege::DROP ) ) );
171 const int currentTableIndex = tableIndex++;
172 assert(currentTableIndex == m_values.getLength());
173 m_values.realloc( tableIndex );
174 m_values[currentTableIndex] = makeAny( prop );
175 OUStringBuffer buf( name.getLength() + schema.getLength() + 1);
176 buf.append( schema ).appendAscii( "." ).append( name );
177 map[ buf.makeStringAndClear() ] = currentTableIndex;
180 m_name2index.swap( map );
182 catch ( const com::sun::star::sdbc::SQLException & e )
184 throw RuntimeException( e.Message , e.Context );
187 fire( RefreshedBroadcaster( *this ) );
191 static void appendColumnList(
192 OUStringBuffer &buf, const Reference< XColumnsSupplier > & columnSupplier, ConnectionSettings *settings )
194 if( columnSupplier.is() )
196 Reference< XEnumerationAccess > columns( columnSupplier->getColumns(),UNO_QUERY );
197 if( columns.is() )
199 Reference< XEnumeration > xEnum( columns->createEnumeration() );
200 bool first = true;
201 Statics & st = getStatics();
203 while( xEnum.is() && xEnum->hasMoreElements() )
205 if( first )
207 first = false;
209 else
211 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ", " ) );
213 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
214 OUString name = extractStringProperty( column, st.NAME );
215 OUString defaultValue = extractStringProperty( column, st.DEFAULT_VALUE );
216 sal_Bool isNullable = extractBoolProperty( column, st.IS_NULLABLE );
217 sal_Bool isAutoIncrement = extractBoolProperty( column, st.IS_AUTO_INCREMENT );
219 bufferQuoteIdentifier( buf, name, settings );
221 OUString type = sqltype2string( column );
222 if( isAutoIncrement )
224 sal_Int32 dataType = 0;
225 column->getPropertyValue( st.TYPE ) >>= dataType;
226 if( com::sun::star::sdbc::DataType::INTEGER == dataType )
228 buf.appendAscii( " serial ");
229 isNullable = sal_False;
231 else if( com::sun::star::sdbc::DataType::BIGINT == dataType )
233 buf.appendAscii( " serial8 " );
234 isNullable = sal_False;
236 else
237 buf.append( type );
239 else
241 buf.append( type );
243 if( defaultValue.getLength() )
245 bufferQuoteConstant( buf, defaultValue, settings );
248 if( ! isNullable )
249 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NULL " ) );
250 // else
251 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NOT NULL " ) );
258 static void appendKeyList(
259 OUStringBuffer & buf, const Reference< XKeysSupplier > &keySupplier, ConnectionSettings *settings )
261 if( keySupplier.is() )
263 Reference< XEnumerationAccess > keys( keySupplier->getKeys(), UNO_QUERY );
264 if(keys.is() )
266 Reference< XEnumeration > xEnum = keys->createEnumeration();
267 while( xEnum.is() && xEnum->hasMoreElements() )
269 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ", " ) );
270 Reference< XPropertySet > key( xEnum->nextElement(), UNO_QUERY );
271 bufferKey2TableConstraint( buf, key, settings );
277 void Tables::appendByDescriptor(
278 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
279 throw (::com::sun::star::sdbc::SQLException,
280 ::com::sun::star::container::ElementExistException,
281 ::com::sun::star::uno::RuntimeException)
283 osl::MutexGuard guard( m_refMutex->mutex );
284 Reference< XStatement > stmt =
285 m_origin->createStatement();
287 Statics &st = getStatics();
288 OUString name,schema;
289 descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
290 descriptor->getPropertyValue( st.NAME ) >>= name;
292 TransactionGuard transaction( stmt );
294 OUStringBuffer buf( 128 );
295 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("CREATE TABLE" ) );
296 bufferQuoteQualifiedIdentifier( buf, schema, name , m_pSettings);
297 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "(" ) );
299 // columns
300 Reference< XColumnsSupplier > supplier( descriptor, UNO_QUERY );
301 appendColumnList( buf, supplier, m_pSettings );
303 appendKeyList( buf, Reference< XKeysSupplier >( descriptor, UNO_QUERY ), m_pSettings );
305 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ") " ) );
306 // execute the creation !
307 transaction.executeUpdate( buf.makeStringAndClear() );
309 // description ....
310 OUString description = extractStringProperty( descriptor, st.DESCRIPTION );
311 if( description.getLength() )
313 buf = OUStringBuffer( 128 );
314 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON TABLE" ) );
315 bufferQuoteQualifiedIdentifier( buf, schema, name, m_pSettings );
316 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
317 bufferQuoteConstant( buf, description, m_pSettings);
319 transaction.executeUpdate( buf.makeStringAndClear() );
322 // column descriptions
323 if( supplier.is() )
325 Reference< XEnumerationAccess > columns( supplier->getColumns(),UNO_QUERY );
326 if( columns.is() )
328 Reference< XEnumeration > xEnum( columns->createEnumeration() );
329 while( xEnum.is() && xEnum->hasMoreElements() )
331 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
332 // help text seems to be used by OOo rather than Description
333 // OUString description = extractStringProperty( column, st.HELP_TEXT );
334 OUString helpText = extractStringProperty( column,st.DESCRIPTION );
335 if( description.getLength() )
337 buf = OUStringBuffer( 128 );
338 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN " ) );
339 bufferQuoteQualifiedIdentifier(
340 buf, schema, name, extractStringProperty( column, st.NAME ), m_pSettings );
341 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
342 bufferQuoteConstant( buf, description, m_pSettings );
343 transaction.executeUpdate( buf.makeStringAndClear() );
349 transaction.commit();
351 disposeNoThrow( stmt );
352 // TODO: cheaper recalculate
353 // Container::append( concatQualified( schema, name ), descriptor ); // maintain the lists
354 refresh();
356 // increase the vector
357 // sal_Int32 index = m_values.getLength();
358 // m_values.realloc( index + 1 );
360 // Table * pTable =
361 // new Table( m_refMutex, m_origin, m_pSettings, false /*modifiable*/ );
362 // Reference< com::sun::star::beans::XPropertySet > prop = pTable;
363 // copyProperties( pTable, descriptor );
364 // m_values[index] = makeAny( prop );
365 // OUStringBuffer buf( name.getLength() + 1 + schema.getLength() );
366 // buf.append( schema ).appendAscii( "." ).append( name );
367 // m_name2index[ buf.makeStringAndClear() ] = index;
370 // void Tables::dropByName( const ::rtl::OUString& elementName )
371 // throw (::com::sun::star::sdbc::SQLException,
372 // ::com::sun::star::container::NoSuchElementException,
373 // ::com::sun::star::uno::RuntimeException)
374 // {
375 // String2IntMap::const_iterator ii = m_name2index.find( elementName );
376 // if( ii == m_name2index.end() )
377 // {
378 // OUStringBuffer buf( 128 );
379 // buf.appendAscii( "Table " );
380 // buf.append( elementName );
381 // buf.appendAscii( " is unknown, so it can't be dropped" );
382 // throw com::sun::star::container::NoSuchElementException(
383 // buf.makeStringAndClear(), *this );
384 // }
385 // dropByIndex( ii->second );
386 // }
388 void Tables::dropByIndex( sal_Int32 index )
389 throw (::com::sun::star::sdbc::SQLException,
390 ::com::sun::star::lang::IndexOutOfBoundsException,
391 ::com::sun::star::uno::RuntimeException)
393 osl::MutexGuard guard( m_refMutex->mutex );
394 if( index < 0 || index >= m_values.getLength() )
396 OUStringBuffer buf( 128 );
397 buf.appendAscii( "TABLES: Index out of range (allowed 0 to " );
398 buf.append( (sal_Int32) (m_values.getLength() -1) );
399 buf.appendAscii( ", got " );
400 buf.append( index );
401 buf.appendAscii( ")" );
402 throw com::sun::star::lang::IndexOutOfBoundsException(
403 buf.makeStringAndClear(), *this );
406 Reference< XPropertySet > set;
407 m_values[index] >>= set;
408 Statics &st = getStatics();
409 OUString name,schema;
410 set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
411 set->getPropertyValue( st.NAME ) >>= name;
412 if( extractStringProperty( set, st.TYPE ).equals( st.VIEW ) && m_pSettings->views.is() )
414 m_pSettings->pViewsImpl->dropByName( concatQualified( schema, name ) );
416 else
418 OUStringBuffer update( 128 );
419 update.appendAscii( RTL_CONSTASCII_STRINGPARAM( "DROP " ) );
420 if( extractStringProperty( set, st.TYPE ).equals( st.VIEW ) )
421 update.appendAscii( RTL_CONSTASCII_STRINGPARAM( "VIEW " ) );
422 else
423 update.appendAscii( RTL_CONSTASCII_STRINGPARAM( "TABLE " ) );
424 bufferQuoteQualifiedIdentifier( update, schema, name, m_pSettings );
425 Reference< XStatement > stmt = m_origin->createStatement( );
426 DisposeGuard dispGuard( stmt );
427 stmt->executeUpdate( update.makeStringAndClear() );
430 Container::dropByIndex( index );
434 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Tables::createDataDescriptor()
435 throw (::com::sun::star::uno::RuntimeException)
437 return new TableDescriptor( m_refMutex, m_origin, m_pSettings );
440 Reference< com::sun::star::container::XNameAccess > Tables::create(
441 const ::rtl::Reference< RefCountedMutex > & refMutex,
442 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
443 ConnectionSettings *pSettings,
444 Tables **ppTables)
446 *ppTables = new Tables( refMutex, origin, pSettings );
447 Reference< com::sun::star::container::XNameAccess > ret = *ppTables;
448 (*ppTables)->refresh();
450 return ret;
453 void Tables::disposing()
455 Container::disposing();