Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xkeycolumns.cxx
blob0fc25059e810600f9d5f8d3b71c91db86cedb11d
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 <rtl/strbuf.hxx>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <com/sun/star/sdbc/XParameters.hpp>
42 #include <com/sun/star/sdbc/DataType.hpp>
43 #include <com/sun/star/sdbc/ColumnValue.hpp>
45 #include "pq_xcolumns.hxx"
46 #include "pq_xkeycolumns.hxx"
47 #include "pq_xkeycolumn.hxx"
48 #include "pq_statics.hxx"
49 #include "pq_tools.hxx"
51 using osl::MutexGuard;
54 using com::sun::star::beans::XPropertySet;
56 using com::sun::star::uno::Any;
57 using com::sun::star::uno::makeAny;
58 using com::sun::star::uno::UNO_QUERY;
59 using com::sun::star::uno::Type;
60 using com::sun::star::uno::XInterface;
61 using com::sun::star::uno::Reference;
62 using com::sun::star::uno::Sequence;
63 using com::sun::star::uno::RuntimeException;
65 using com::sun::star::container::NoSuchElementException;
66 using com::sun::star::lang::WrappedTargetException;
68 using com::sun::star::sdbc::XRow;
69 using com::sun::star::sdbc::XCloseable;
70 using com::sun::star::sdbc::XStatement;
71 using com::sun::star::sdbc::XResultSet;
72 using com::sun::star::sdbc::XParameters;
73 using com::sun::star::sdbc::XPreparedStatement;
74 using com::sun::star::sdbc::XDatabaseMetaData;
75 using com::sun::star::sdbc::SQLException;
77 namespace pq_sdbc_driver
80 KeyColumns::KeyColumns(
81 const ::rtl::Reference< RefCountedMutex > & refMutex,
82 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
83 ConnectionSettings *pSettings,
84 const OUString &schemaName,
85 const OUString &tableName,
86 const Sequence< OUString > &columnNames,
87 const Sequence< OUString > &foreignColumnNames )
88 : Container( refMutex, origin, pSettings, "KEY_COLUMN" ),
89 m_schemaName( schemaName ),
90 m_tableName( tableName ),
91 m_columnNames( columnNames ),
92 m_foreignColumnNames( foreignColumnNames )
95 KeyColumns::~KeyColumns()
99 void KeyColumns::refresh()
100 throw (::com::sun::star::uno::RuntimeException, std::exception)
104 if( isLog( m_pSettings, LogLevel::INFO ) )
106 OStringBuffer buf;
107 buf.append( "sdbcx.KeyColumns get refreshed for table " );
108 buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
109 buf.append( "." );
110 buf.append( OUStringToOString( m_tableName, m_pSettings->encoding ) );
111 log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
114 osl::MutexGuard guard( m_refMutex->mutex );
116 Statics &st = getStatics();
117 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
119 Reference< XResultSet > rs =
120 meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
122 DisposeGuard disposeIt( rs );
123 Reference< XRow > xRow( rs , UNO_QUERY );
125 String2IntMap map;
127 m_values = Sequence< com::sun::star::uno::Any > ();
128 sal_Int32 columnIndex = 0;
129 while( rs->next() )
131 OUString columnName = xRow->getString( 4 );
133 int keyindex;
134 for( keyindex = 0 ; keyindex < m_columnNames.getLength() ; keyindex ++ )
136 if( columnName == m_columnNames[keyindex] )
137 break;
139 if( m_columnNames.getLength() == keyindex )
140 continue;
142 KeyColumn * pKeyColumn =
143 new KeyColumn( m_refMutex, m_origin, m_pSettings );
144 Reference< com::sun::star::beans::XPropertySet > prop = pKeyColumn;
146 OUString name = columnMetaData2SDBCX( pKeyColumn, xRow );
147 if( keyindex < m_foreignColumnNames.getLength() )
149 pKeyColumn->setPropertyValue_NoBroadcast_public(
150 st.RELATED_COLUMN, makeAny( m_foreignColumnNames[keyindex]) );
154 const int currentColumnIndex = columnIndex++;
155 assert(currentColumnIndex == m_values.getLength());
156 m_values.realloc( columnIndex );
157 m_values[currentColumnIndex] = makeAny( prop );
158 map[ name ] = currentColumnIndex;
161 m_name2index.swap( map );
163 catch ( com::sun::star::sdbc::SQLException & e )
165 throw RuntimeException( e.Message , e.Context );
168 fire( RefreshedBroadcaster( *this ) );
172 // void alterColumnByDescriptor(
173 // const OUString & schemaName,
174 // const OUString & tableName,
175 // rtl_TextEncoding encoding,
176 // const Reference< XStatement > &stmt,
177 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & past,
178 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & future)
179 // {
180 // Statics & st = getStatics();
182 // // if( past->getPropertyValue( st.TABLE_NAME ) != future->getPropertyValue( st.TABLE_NAME ) ||
183 // // past->getPropertyValue( st.SCHEMA_NAME ) != future->getPropertyValue( st.SCHEMA_NAME ))
184 // // {
185 // // OUStringBuffer buf(128);
186 // // buf.append( "Can't move column " );
187 // // buf.append( extractStringProperty( past, st.COLUMN_NAME ) );
188 // // buf.append( " from table " );
189 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
190 // // buf.append( " to table " );
191 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
192 // // throw SQLException( buf.makeStringAndClear(), Reference< XInterface > () );
193 // // }
195 // // OUString tableName = extractStringProperty( past, st.TABLE_NAME );
196 // // OUString schemaName = extractStringProperty( past, st.SCHEMA_NAME );
197 // OUString pastColumnName = extractStringProperty( past, st.NAME );
198 // OUString futureColumnName = extractStringProperty( future, st.NAME );
199 // OUString pastTypeName = sqltype2string( past );
200 // OUString futureTypeName = sqltype2string( future );
202 // TransactionGuard transaction( stmt );
204 // OUStringBuffer buf( 128 );
205 // if( ! pastColumnName.getLength())
206 // {
207 // // create a new column
208 // buf.append( "ALTER TABLE" );
209 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
210 // buf.append( "ADD COLUMN" );
211 // bufferQuoteIdentifier( buf, futureColumnName );
212 // buf.append( futureTypeName );
213 // transaction.executeUpdate( buf.makeStringAndClear() );
214 // }
215 // else
216 // {
217 // if( pastTypeName != futureTypeName )
218 // {
219 // throw RuntimeException(
220 // "Can't modify column types, drop the column and create a new one",
221 // Reference< XInterface > () );
222 // }
224 // if( pastColumnName != futureColumnName )
225 // {
226 // buf.append( "ALTER TABLE" );
227 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
228 // buf.append( "RENAME COLUMN" );
229 // bufferQuoteIdentifier( buf, pastColumnName );
230 // buf.append( "TO" );
231 // bufferQuoteIdentifier( buf, futureColumnName );
232 // transaction.executeUpdate( buf.makeStringAndClear() );
233 // }
234 // }
236 // OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
237 // OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
238 // if( futureDefaultValue != pastDefaultValue )
239 // {
240 // buf = OUStringBuffer( 128 );
241 // buf.append( "ALTER TABLE" );
242 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
243 // buf.append( "ALTER COLUMN" );
244 // bufferQuoteIdentifier( buf, futureColumnName );
245 // buf.append( "SET DEFAULT " );
246 // // default value is not quoted, caller needs to quote himself (otherwise
247 // // how to pass e.g. nextval('something' ) ????
248 // buf.append( futureDefaultValue );
249 // // bufferQuoteConstant( buf, defaultValue, encoding );
250 // transaction.executeUpdate( buf.makeStringAndClear() );
251 // }
253 // sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
254 // sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
255 // if( futureNullable != pastNullable )
256 // {
257 // buf = OUStringBuffer( 128 );
258 // buf.append( "ALTER TABLE" );
259 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
260 // buf.append( "ALTER COLUMN" );
261 // bufferQuoteIdentifier( buf, futureColumnName );
262 // if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
263 // {
264 // buf.append( "SET" );
265 // }
266 // else
267 // {
268 // buf.append( "DROP" );
269 // }
270 // buf.append( " NOT NULL" );
271 // transaction.executeUpdate( buf.makeStringAndClear() );
272 // }
274 // OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
275 // OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
276 // if( futureComment != pastComment )
277 // {
278 // buf = OUStringBuffer( 128 );
279 // buf.append( "COMMENT ON COLUMN" );
280 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName );
281 // buf.append( "IS " );
282 // bufferQuoteConstant( buf, futureComment,encoding);
283 // transaction.executeUpdate( buf.makeStringAndClear() );
284 // }
285 // transaction.commit();
286 // }
288 void KeyColumns::appendByDescriptor(
289 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& future )
290 throw (::com::sun::star::sdbc::SQLException,
291 ::com::sun::star::container::ElementExistException,
292 ::com::sun::star::uno::RuntimeException, std::exception)
294 (void) future;
295 throw com::sun::star::sdbc::SQLException(
296 "KeyColumns::appendByDescriptor not implemented yet",
297 *this, OUString(), 1, Any() );
299 // osl::MutexGuard guard( m_refMutex->mutex );
300 // Statics & st = getStatics();
301 // Reference< XPropertySet > past = createDataDescriptor();
302 // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
303 // alterColumnByDescriptor(
304 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
309 void KeyColumns::dropByIndex( sal_Int32 index )
310 throw (::com::sun::star::sdbc::SQLException,
311 ::com::sun::star::lang::IndexOutOfBoundsException,
312 ::com::sun::star::uno::RuntimeException, std::exception)
314 (void) index;
315 throw com::sun::star::sdbc::SQLException(
316 "KeyColumns::dropByIndex not implemented yet",
317 *this, OUString(), 1, Any() );
318 // osl::MutexGuard guard( m_refMutex->mutex );
319 // if( index < 0 || index >= m_values.getLength() )
320 // {
321 // OUStringBuffer buf( 128 );
322 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
323 // buf.append((sal_Int32)(m_values.getLength() -1) );
324 // buf.appendAscii( ", got " );
325 // buf.append( index );
326 // buf.appendAscii( ")" );
327 // throw com::sun::star::lang::IndexOutOfBoundsException(
328 // buf.makeStringAndClear(), *this );
329 // }
331 // Reference< XPropertySet > set;
332 // m_values[index] >>= set;
333 // Statics &st = getStatics();
334 // OUString name;
335 // set->getPropertyValue( st.NAME ) >>= name;
337 // OUStringBuffer update( 128 );
338 // update.appendAscii( "ALTER TABLE ONLY");
339 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
340 // update.appendAscii( "DROP COLUMN" );
341 // bufferQuoteIdentifier( update, name );
342 // Reference< XStatement > stmt = m_origin->createStatement( );
343 // DisposeGuard disposeIt( stmt );
344 // stmt->executeUpdate( update.makeStringAndClear() );
349 Reference< ::com::sun::star::beans::XPropertySet > KeyColumns::createDataDescriptor()
350 throw (::com::sun::star::uno::RuntimeException, std::exception)
352 return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
355 Reference< com::sun::star::container::XNameAccess > KeyColumns::create(
356 const ::rtl::Reference< RefCountedMutex > & refMutex,
357 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
358 ConnectionSettings *pSettings,
359 const OUString &schemaName,
360 const OUString &tableName,
361 const Sequence< OUString > &columnNames ,
362 const Sequence< OUString > &foreignColumnNames )
364 KeyColumns *pKeyColumns = new KeyColumns(
365 refMutex, origin, pSettings, schemaName, tableName, columnNames, foreignColumnNames );
366 Reference< com::sun::star::container::XNameAccess > ret = pKeyColumns;
367 pKeyColumns->refresh();
369 return ret;
373 KeyColumnDescriptors::KeyColumnDescriptors(
374 const ::rtl::Reference< RefCountedMutex > & refMutex,
375 const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
376 ConnectionSettings *pSettings )
377 : Container( refMutex, origin, pSettings, "KEY_COLUMN" )
380 Reference< ::com::sun::star::beans::XPropertySet > KeyColumnDescriptors::createDataDescriptor()
381 throw (::com::sun::star::uno::RuntimeException, std::exception)
383 return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */