bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xtables.cxx
blob411cc917ea298ba730b538b8e3151c5b31573ca4
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 * This Source Code Form is subject to the terms of the Mozilla Public
32 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
35 ************************************************************************/
37 #include <rtl/ustrbuf.hxx>
38 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
39 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 #include <com/sun/star/sdbc/SQLException.hpp>
41 #include <com/sun/star/sdbc/XRow.hpp>
42 #include <com/sun/star/sdbcx/Privilege.hpp>
43 #include <com/sun/star/sdbcx/KeyType.hpp>
44 #include <com/sun/star/sdbc/KeyRule.hpp>
45 #include <com/sun/star/sdbc/DataType.hpp>
46 #include <cppuhelper/exc_hlp.hxx>
48 #include "pq_xtables.hxx"
49 #include "pq_xviews.hxx"
50 #include "pq_xtable.hxx"
51 #include "pq_statics.hxx"
52 #include "pq_tools.hxx"
54 using osl::MutexGuard;
56 using com::sun::star::beans::XPropertySet;
58 using com::sun::star::uno::Any;
59 using com::sun::star::uno::makeAny;
60 using com::sun::star::uno::UNO_QUERY;
61 using com::sun::star::uno::Reference;
62 using com::sun::star::uno::Sequence;
64 using com::sun::star::container::XEnumerationAccess;
65 using com::sun::star::container::XEnumeration;
67 using com::sun::star::sdbc::XRow;
68 using com::sun::star::sdbc::XStatement;
69 using com::sun::star::sdbc::XResultSet;
70 using com::sun::star::sdbc::XDatabaseMetaData;
71 using com::sun::star::sdbcx::XColumnsSupplier;
72 using com::sun::star::sdbcx::XKeysSupplier;
74 namespace pq_sdbc_driver
76 Tables::Tables(
77 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
78 const css::uno::Reference< css::sdbc::XConnection > & origin,
79 ConnectionSettings *pSettings )
80 : Container( refMutex, origin, pSettings, getStatics().TABLE )
83 Tables::~Tables()
86 void Tables::refresh()
88 try
90 osl::MutexGuard guard( m_xMutex->GetMutex() );
91 Statics & st = getStatics();
93 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
95 Reference< XResultSet > rs =
96 meta->getTables( Any(), st.cPERCENT,st.cPERCENT, Sequence< OUString > () );
98 Reference< XRow > xRow( rs , UNO_QUERY );
100 String2IntMap map;
102 m_values.clear();
103 sal_Int32 tableIndex = 0;
104 while( rs->next() )
106 // if creating all these tables turns out to have too bad performance, we might
107 // instead offer a factory interface
108 Table * pTable =
109 new Table( m_xMutex, m_origin, m_pSettings );
110 Reference< css::beans::XPropertySet > prop = pTable;
112 OUString name = xRow->getString( TABLE_INDEX_NAME+1);
113 OUString schema = xRow->getString( TABLE_INDEX_SCHEMA+1);
114 pTable->setPropertyValue_NoBroadcast_public(
115 st.CATALOG_NAME , makeAny(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
116 pTable->setPropertyValue_NoBroadcast_public( st.NAME , makeAny( name ) );
117 pTable->setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME , makeAny( schema ));
118 pTable->setPropertyValue_NoBroadcast_public(
119 st.TYPE , makeAny( xRow->getString( TABLE_INDEX_TYPE+1) ) );
120 pTable->setPropertyValue_NoBroadcast_public(
121 st.DESCRIPTION , makeAny( xRow->getString( TABLE_INDEX_REMARKS+1) ) );
122 pTable->setPropertyValue_NoBroadcast_public(
123 st.PRIVILEGES ,
124 makeAny( sal_Int32( css::sdbcx::Privilege::SELECT |
125 css::sdbcx::Privilege::INSERT |
126 css::sdbcx::Privilege::UPDATE |
127 css::sdbcx::Privilege::DELETE |
128 css::sdbcx::Privilege::READ |
129 css::sdbcx::Privilege::CREATE |
130 css::sdbcx::Privilege::ALTER |
131 css::sdbcx::Privilege::REFERENCE |
132 css::sdbcx::Privilege::DROP ) ) );
135 m_values.push_back( makeAny( prop ) );
136 OUStringBuffer buf( name.getLength() + schema.getLength() + 1);
137 buf.append( schema ).append( "." ).append( name );
138 map[ buf.makeStringAndClear() ] = tableIndex;
139 ++tableIndex;
142 m_name2index.swap( map );
144 catch ( const css::sdbc::SQLException & e )
146 css::uno::Any anyEx = cppu::getCaughtException();
147 throw css::lang::WrappedTargetRuntimeException( e.Message,
148 e.Context, anyEx );
151 fire( RefreshedBroadcaster( *this ) );
155 static void appendColumnList(
156 OUStringBuffer &buf, const Reference< XColumnsSupplier > & columnSupplier, ConnectionSettings *settings )
158 if( columnSupplier.is() )
160 Reference< XEnumerationAccess > columns( columnSupplier->getColumns(),UNO_QUERY );
161 if( columns.is() )
163 Reference< XEnumeration > xEnum( columns->createEnumeration() );
164 bool first = true;
165 Statics & st = getStatics();
167 while( xEnum.is() && xEnum->hasMoreElements() )
169 if( first )
171 first = false;
173 else
175 buf.append( ", " );
177 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
178 OUString name = extractStringProperty( column, st.NAME );
179 OUString defaultValue = extractStringProperty( column, st.DEFAULT_VALUE );
180 bool isNullable = extractBoolProperty( column, st.IS_NULLABLE );
181 bool isAutoIncrement = extractBoolProperty( column, st.IS_AUTO_INCREMENT );
183 bufferQuoteIdentifier( buf, name, settings );
185 OUString type = sqltype2string( column );
186 if( isAutoIncrement )
188 sal_Int32 dataType = 0;
189 column->getPropertyValue( st.TYPE ) >>= dataType;
190 if( css::sdbc::DataType::INTEGER == dataType )
192 buf.append( " serial ");
193 isNullable = false;
195 else if( css::sdbc::DataType::BIGINT == dataType )
197 buf.append( " serial8 " );
198 isNullable = false;
200 else
201 buf.append( type );
203 else
205 buf.append( type );
207 if( !defaultValue.isEmpty() )
209 bufferQuoteConstant( buf, defaultValue, settings );
212 if( ! isNullable )
213 buf.append( " NOT NULL " );
220 static void appendKeyList(
221 OUStringBuffer & buf, const Reference< XKeysSupplier > &keySupplier, ConnectionSettings *settings )
223 if( keySupplier.is() )
225 Reference< XEnumerationAccess > keys( keySupplier->getKeys(), UNO_QUERY );
226 if(keys.is() )
228 Reference< XEnumeration > xEnum = keys->createEnumeration();
229 while( xEnum.is() && xEnum->hasMoreElements() )
231 buf.append( ", " );
232 Reference< XPropertySet > key( xEnum->nextElement(), UNO_QUERY );
233 bufferKey2TableConstraint( buf, key, settings );
239 void Tables::appendByDescriptor(
240 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
242 osl::MutexGuard guard( m_xMutex->GetMutex() );
243 Reference< XStatement > stmt =
244 m_origin->createStatement();
246 Statics &st = getStatics();
247 OUString name,schema;
248 descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
249 descriptor->getPropertyValue( st.NAME ) >>= name;
251 TransactionGuard transaction( stmt );
253 OUStringBuffer buf( 128 );
254 buf.append( "CREATE TABLE" );
255 bufferQuoteQualifiedIdentifier( buf, schema, name , m_pSettings);
256 buf.append( "(" );
258 // columns
259 Reference< XColumnsSupplier > supplier( descriptor, UNO_QUERY );
260 appendColumnList( buf, supplier, m_pSettings );
262 appendKeyList( buf, Reference< XKeysSupplier >( descriptor, UNO_QUERY ), m_pSettings );
264 buf.append( ") " );
265 // execute the creation !
266 transaction.executeUpdate( buf.makeStringAndClear() );
268 // description ....
269 OUString description = extractStringProperty( descriptor, st.DESCRIPTION );
270 if( !description.isEmpty() )
272 buf.truncate();
273 buf.append( "COMMENT ON TABLE" );
274 bufferQuoteQualifiedIdentifier( buf, schema, name, m_pSettings );
275 buf.append( "IS " );
276 bufferQuoteConstant( buf, description, m_pSettings);
278 transaction.executeUpdate( buf.makeStringAndClear() );
281 // column descriptions
282 if( supplier.is() )
284 Reference< XEnumerationAccess > columns( supplier->getColumns(),UNO_QUERY );
285 if( columns.is() )
287 Reference< XEnumeration > xEnum( columns->createEnumeration() );
288 while( xEnum.is() && xEnum->hasMoreElements() )
290 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
291 description = extractStringProperty( column,st.DESCRIPTION );
292 if( !description.isEmpty() )
294 buf.truncate();
295 buf.append( "COMMENT ON COLUMN " );
296 bufferQuoteQualifiedIdentifier(
297 buf, schema, name, extractStringProperty( column, st.NAME ), m_pSettings );
298 buf.append( "IS " );
299 bufferQuoteConstant( buf, description, m_pSettings );
300 transaction.executeUpdate( buf.makeStringAndClear() );
306 transaction.commit();
308 disposeNoThrow( stmt );
309 // TODO: cheaper recalculate
310 // Container::append( concatQualified( schema, name ), descriptor ); // maintain the lists
311 refresh();
314 void Tables::dropByIndex( sal_Int32 index )
316 osl::MutexGuard guard( m_xMutex->GetMutex() );
317 if( index < 0 || index >= static_cast<sal_Int32>(m_values.size()) )
319 throw css::lang::IndexOutOfBoundsException(
320 "TABLES: Index out of range (allowed 0 to " + OUString::number(m_values.size() -1)
321 + ", got " + OUString::number( index ) + ")",
322 *this );
325 Reference< XPropertySet > set;
326 m_values[index] >>= set;
327 Statics &st = getStatics();
328 OUString name,schema;
329 set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
330 set->getPropertyValue( st.NAME ) >>= name;
331 if( extractStringProperty( set, st.TYPE ) == st.VIEW && m_pSettings->views.is() )
333 m_pSettings->pViewsImpl->dropByName( concatQualified( schema, name ) );
335 else
337 OUStringBuffer update( 128 );
338 update.append( "DROP " );
339 if( extractStringProperty( set, st.TYPE ) == st.VIEW )
340 update.append( "VIEW " );
341 else
342 update.append( "TABLE " );
343 bufferQuoteQualifiedIdentifier( update, schema, name, m_pSettings );
344 Reference< XStatement > stmt = m_origin->createStatement( );
345 DisposeGuard dispGuard( stmt );
346 stmt->executeUpdate( update.makeStringAndClear() );
349 Container::dropByIndex( index );
353 css::uno::Reference< css::beans::XPropertySet > Tables::createDataDescriptor()
355 return new TableDescriptor( m_xMutex, m_origin, m_pSettings );
358 Reference< css::container::XNameAccess > Tables::create(
359 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
360 const css::uno::Reference< css::sdbc::XConnection > & origin,
361 ConnectionSettings *pSettings,
362 Tables **ppTables)
364 *ppTables = new Tables( refMutex, origin, pSettings );
365 Reference< css::container::XNameAccess > ret = *ppTables;
366 (*ppTables)->refresh();
368 return ret;
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */