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 : #include <rtl/strbuf.hxx>
60 :
61 : #include <com/sun/star/sdbc/XRow.hpp>
62 : #include <com/sun/star/sdbc/XParameters.hpp>
63 : #include <com/sun/star/sdbc/DataType.hpp>
64 : #include <com/sun/star/sdbc/ColumnValue.hpp>
65 :
66 : #include <cppuhelper/implbase1.hxx>
67 :
68 : #include "pq_xcolumns.hxx"
69 : #include "pq_xcolumn.hxx"
70 : #include "pq_statics.hxx"
71 : #include "pq_tools.hxx"
72 :
73 : using osl::MutexGuard;
74 :
75 : using rtl::OUString;
76 : using rtl::OUStringBuffer;
77 : using rtl::OUStringToOString;
78 :
79 : using com::sun::star::beans::XPropertySet;
80 : using com::sun::star::beans::XPropertyChangeListener;
81 : using com::sun::star::beans::PropertyChangeEvent;
82 :
83 : using com::sun::star::uno::Any;
84 : using com::sun::star::uno::makeAny;
85 : using com::sun::star::uno::UNO_QUERY;
86 : using com::sun::star::uno::Type;
87 : using com::sun::star::uno::XInterface;
88 : using com::sun::star::uno::Reference;
89 : using com::sun::star::uno::Sequence;
90 : using com::sun::star::uno::RuntimeException;
91 :
92 : using com::sun::star::container::NoSuchElementException;
93 : using com::sun::star::lang::WrappedTargetException;
94 :
95 : using com::sun::star::sdbc::XRow;
96 : using com::sun::star::sdbc::XCloseable;
97 : using com::sun::star::sdbc::XStatement;
98 : using com::sun::star::sdbc::XResultSet;
99 : using com::sun::star::sdbc::XParameters;
100 : using com::sun::star::sdbc::XPreparedStatement;
101 : using com::sun::star::sdbc::XDatabaseMetaData;
102 : using com::sun::star::sdbc::SQLException;
103 :
104 : namespace pq_sdbc_driver
105 : {
106 : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
107 :
108 0 : static Any isCurrency( const rtl::OUString & typeName )
109 : {
110 0 : sal_Bool b = typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("money"));
111 0 : return Any( &b, getBooleanCppuType() );
112 : }
113 :
114 : // static sal_Bool isAutoIncrement8( const rtl::OUString & typeName )
115 : // {
116 : // return typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("serial8")) ||
117 : // typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("bigserial"));
118 : // }
119 :
120 0 : static Any isAutoIncrement( const rtl::OUString & defaultValue )
121 : {
122 0 : sal_Bool ret = defaultValue.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "nextval(" ) );
123 : // printf( "%s %d\n",
124 : // OUStringToOString(defaultValue, RTL_TEXTENCODING_ASCII_US).getStr(),
125 : // ret );
126 : // {
127 : // static const char * const serials[] =
128 : // {
129 : // "serial", "serial4", "serial8", "bigserial", 0
130 : // };
131 : // s sal_Bool b = sal_False;
132 : // for( int i = 0; !b && serials[i] ; i ++ )
133 : // {
134 : // b = b || typeName.equalsIgnoreAsciiCaseAscii( serials[i] );
135 : // }
136 0 : return Any ( &ret, getBooleanCppuType() );
137 : }
138 :
139 0 : Columns::Columns(
140 : const ::rtl::Reference< RefCountedMutex > & refMutex,
141 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
142 : ConnectionSettings *pSettings,
143 : const rtl::OUString &schemaName,
144 : const rtl::OUString &tableName)
145 : : Container( refMutex, origin, pSettings, ASCII_STR( "COLUMN" ) ),
146 : m_schemaName( schemaName ),
147 0 : m_tableName( tableName )
148 0 : {}
149 :
150 0 : Columns::~Columns()
151 0 : {}
152 :
153 0 : rtl::OUString columnMetaData2SDBCX(
154 : ReflectionBase *pBase, const com::sun::star::uno::Reference< com::sun::star::sdbc::XRow > &xRow )
155 : {
156 0 : Statics & st = getStatics();
157 :
158 : // 1. TABLE_CAT string => table catalog (may be NULL)
159 : // => not supported
160 : // 2. TABLE_SCHEM string => table schema (may be NULL)
161 : // => pg_namespace.nspname
162 : // 3. TABLE_NAME string => table name
163 : // => pg_class.relname
164 : // 4. COLUMN_NAME string => column name
165 : // => pg_attribure.attname
166 : // 5. DATA_TYPE short => SQL type from java.sql.Types
167 : // => pg_type.typname => sdbc.DataType
168 : // 6. TYPE_NAME string => Data source dependent type name, for a UDT the
169 : // type name is fully qualified
170 : // => pg_type.typname
171 : // 7. COLUMN_SIZE long => column size. For char or date types this is
172 : // the maximum number of characters, for numeric
173 : // or decimal types this is precision.
174 : // => pg_type.typlen ( TODO: What is about variable size ? )
175 : // 8. BUFFER_LENGTH is not used.
176 : // => not used
177 : // 9. DECIMAL_DIGITS long => the number of fractional digits
178 : // => don't know ! TODO !
179 : // 10. NUM_PREC_RADIX long => Radix (typically either 10 or 2)
180 : // => TODO ??
181 : // 11. NULLABLE long => is NULL allowed?
182 : // NO_NULLS - might not allow NULL values
183 : // NULABLE - definitely allows NULL values
184 : // NULLABLE_UNKNOWN - nullability unknown
185 : // => pg_attribute.attnotnull
186 : // 12. REMARKS string => comment describing column (may be NULL )
187 : // => Don't know, there does not seem to exist something like
188 : // that in postgres
189 : // 13. COLUMN_DEF string => default value (may be NULL)
190 : // => pg_type.typdefault
191 : // 14. SQL_DATA_TYPE long => unused
192 : // => empty
193 : // 15. SQL_DATETIME_SUB long => unused
194 : // => empty
195 : // 16. CHAR_OCTET_LENGTH long => for char types the maximum number of
196 : // bytes in the column
197 : // => pg_type.typlen
198 : // 17. ORDINAL_POSITION int => index of column in table (starting at 1)
199 : // pg_attribute.attnum
200 : // 18. IS_NULLABLE string => "NO" means column definitely does not allow
201 : // NULL values; "YES" means the column might
202 : // allow NULL values. An empty string means
203 : // nobody knows.
204 : // => pg_attribute.attnotnull
205 :
206 : static const int COLUMN_NAME = 4;
207 : static const int DATA_TYPE = 5;
208 : static const int TYPE_NAME = 6;
209 : static const int COLUMN_SIZE = 7;
210 : static const int DECIMAL_DIGITS = 9;
211 : static const int IS_NULLABLE = 11;
212 : static const int DESCRIPTION = 12;
213 : static const int DEFAULT_VALUE = 13;
214 :
215 0 : OUString name = xRow->getString( COLUMN_NAME );
216 0 : OUString typeName = xRow->getString( TYPE_NAME );
217 :
218 : pBase->setPropertyValue_NoBroadcast_public(
219 0 : st.NAME, makeAny( name ) );
220 :
221 : pBase->setPropertyValue_NoBroadcast_public(
222 0 : st.TYPE, makeAny( xRow->getInt( DATA_TYPE ) ) );
223 :
224 : pBase->setPropertyValue_NoBroadcast_public(
225 0 : st.TYPE_NAME, makeAny( typeName ) );
226 :
227 : pBase->setPropertyValue_NoBroadcast_public(
228 0 : st.PRECISION, makeAny( xRow->getInt( COLUMN_SIZE ) ) );
229 :
230 : pBase->setPropertyValue_NoBroadcast_public(
231 0 : st.SCALE, makeAny( xRow->getInt( DECIMAL_DIGITS ) ) );
232 :
233 : pBase->setPropertyValue_NoBroadcast_public(
234 0 : st.IS_NULLABLE, makeAny( xRow->getInt( IS_NULLABLE ) ) );
235 :
236 : pBase->setPropertyValue_NoBroadcast_public(
237 0 : st.DEFAULT_VALUE, makeAny( xRow->getString( DEFAULT_VALUE ) ) );
238 :
239 : // pBase->setPropertyValue_NoBroadcast_public(
240 : // st.DESCRIPTION, makeAny( xRow->getString( DESCRIPTION ) ) );
241 :
242 : // if( pBase->getPropertySetInfo()->hasPropertyByName( st.HELP_TEXT ) )
243 : // pBase->setPropertyValue_NoBroadcast_public(
244 : // st.HELP_TEXT, makeAny( xRow->getString( DESCRIPTION ) ) );
245 : // else // for key columns, etc. ...
246 : pBase->setPropertyValue_NoBroadcast_public(
247 0 : st.DESCRIPTION, makeAny( xRow->getString( DESCRIPTION ) ) );
248 :
249 :
250 : // maybe a better criterium than the type name can be found in future
251 : pBase->setPropertyValue_NoBroadcast_public(
252 0 : st.IS_AUTO_INCREMENT, isAutoIncrement(xRow->getString( DEFAULT_VALUE )) );
253 :
254 : pBase->setPropertyValue_NoBroadcast_public(
255 0 : st.IS_CURRENCY, isCurrency( typeName));
256 0 : return name;
257 : }
258 :
259 :
260 : // class CommentChanger : public cppu::WeakImplHelper1< XPropertyChangeListener >
261 : // {
262 : // ::rtl::Reference< RefCountedMutex > m_refMutex;
263 : // ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > m_connection;
264 : // ConnectionSettings *m_pSettings;
265 : // rtl::OUString m_schema;
266 : // rtl::OUString m_table;
267 : // rtl::OUString m_column;
268 :
269 : // public:
270 : // CommentChanger(
271 : // const ::rtl::Reference< RefCountedMutex > & refMutex,
272 : // const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & connection,
273 : // ConnectionSettings *pSettings,
274 : // const rtl::OUString & schema,
275 : // const rtl::OUString & table,
276 : // const rtl::OUString & column ) :
277 : // m_refMutex( refMutex ),
278 : // m_connection( connection ),
279 : // m_pSettings( pSettings ),
280 : // m_schema ( schema ),
281 : // m_table ( table ),
282 : // m_column ( column )
283 : // {}
284 :
285 :
286 : // // Methods
287 : // virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException)
288 : // {
289 : // osl::MutexGuard guard( m_refMutex->mutex );
290 : // m_connection.clear();
291 : // }
292 : // // Methods
293 : // virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException)
294 : // {
295 : // osl::MutexGuard guard( m_refMutex->mutex );
296 : // OUStringBuffer buf( 128 );
297 : // OUString comment;
298 : // evt.NewValue >>= comment;
299 : // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
300 : // bufferQuoteQualifiedIdentifier( buf, m_schema, m_table , m_column );
301 : // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
302 : // bufferQuoteConstant( buf, comment,m_pSettings->encoding);
303 :
304 : // printf( "changing comment of column %s to %s\n",
305 : // OUStringToOString( m_column, RTL_TEXTENCODING_ASCII_US ).getStr(),
306 : // OUStringToOString( comment, RTL_TEXTENCODING_ASCII_US ).getStr() );
307 :
308 : // m_connection->createStatement()->executeUpdate( buf.makeStringAndClear() );
309 : // }
310 : // };
311 :
312 0 : void Columns::refresh()
313 : throw (::com::sun::star::uno::RuntimeException)
314 : {
315 : try
316 : {
317 0 : if( isLog( m_pSettings, LogLevel::INFO ) )
318 : {
319 0 : rtl::OStringBuffer buf;
320 0 : buf.append( "sdbcx.Columns get refreshed for table " );
321 0 : buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
322 0 : buf.append( "." );
323 0 : buf.append( OUStringToOString( m_tableName, m_pSettings->encoding ) );
324 0 : log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
325 : }
326 0 : osl::MutexGuard guard( m_refMutex->mutex );
327 :
328 0 : Statics &st = getStatics();
329 0 : Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
330 :
331 : Reference< XResultSet > rs =
332 0 : meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
333 :
334 0 : DisposeGuard disposeIt( rs );
335 0 : Reference< XRow > xRow( rs , UNO_QUERY );
336 :
337 0 : String2IntMap map;
338 :
339 0 : m_values = Sequence< com::sun::star::uno::Any > ();
340 0 : int columnIndex = 0;
341 0 : while( rs->next() )
342 : {
343 : Column * pColumn =
344 0 : new Column( m_refMutex, m_origin, m_pSettings );
345 0 : Reference< com::sun::star::beans::XPropertySet > prop = pColumn;
346 :
347 0 : OUString name = columnMetaData2SDBCX( pColumn, xRow );
348 : // pColumn->addPropertyChangeListener(
349 : // st.HELP_TEXT,
350 : // new CommentChanger(
351 : // m_refMutex,
352 : // m_origin,
353 : // m_pSettings,
354 : // m_schemaName,
355 : // m_tableName,
356 : // name ) );
357 :
358 : {
359 0 : const int currentColumnIndex = columnIndex++;
360 : assert(currentColumnIndex == m_values.getLength());
361 0 : m_values.realloc( columnIndex );
362 0 : m_values[currentColumnIndex] = makeAny( prop );
363 0 : map[ name ] = currentColumnIndex;
364 : }
365 0 : }
366 0 : m_name2index.swap( map );
367 : }
368 0 : catch ( com::sun::star::sdbc::SQLException & e )
369 : {
370 0 : throw RuntimeException( e.Message , e.Context );
371 : }
372 0 : fire( RefreshedBroadcaster( *this ) );
373 0 : }
374 :
375 :
376 0 : void alterColumnByDescriptor(
377 : const OUString & schemaName,
378 : const OUString & tableName,
379 : ConnectionSettings *settings,
380 : const Reference< XStatement > &stmt,
381 : const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & past,
382 : const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & future)
383 : {
384 0 : Statics & st = getStatics();
385 :
386 : // if( past->getPropertyValue( st.TABLE_NAME ) != future->getPropertyValue( st.TABLE_NAME ) ||
387 : // past->getPropertyValue( st.SCHEMA_NAME ) != future->getPropertyValue( st.SCHEMA_NAME ))
388 : // {
389 : // OUStringBuffer buf(128);
390 : // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Can't move column " ) );
391 : // buf.append( extractStringProperty( past, st.COLUMN_NAME ) );
392 : // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " from table " ) );
393 : // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
394 : // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " to table " ) );
395 : // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
396 : // throw SQLException( buf.makeStringAndClear(), Reference< XInterface > () );
397 : // }
398 :
399 : // OUString tableName = extractStringProperty( past, st.TABLE_NAME );
400 : // OUString schemaName = extractStringProperty( past, st.SCHEMA_NAME );
401 0 : OUString pastColumnName = extractStringProperty( past, st.NAME );
402 0 : OUString futureColumnName = extractStringProperty( future, st.NAME );
403 0 : OUString pastTypeName = sqltype2string( past );
404 0 : OUString futureTypeName = sqltype2string( future );
405 :
406 0 : TransactionGuard transaction( stmt );
407 :
408 0 : OUStringBuffer buf( 128 );
409 0 : if( ! pastColumnName.getLength())
410 : {
411 : // create a new column
412 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
413 0 : bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
414 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ADD COLUMN" ) );
415 0 : bufferQuoteIdentifier( buf, futureColumnName, settings );
416 0 : buf.append( futureTypeName );
417 0 : transaction.executeUpdate( buf.makeStringAndClear() );
418 : }
419 : else
420 : {
421 0 : if( pastTypeName != futureTypeName )
422 : {
423 : throw RuntimeException(
424 : ASCII_STR( "Can't modify column types, drop the column and create a new one" ),
425 0 : Reference< XInterface > () );
426 : }
427 :
428 0 : if( pastColumnName != futureColumnName )
429 : {
430 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
431 0 : bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
432 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "RENAME COLUMN" ) );
433 0 : bufferQuoteIdentifier( buf, pastColumnName, settings );
434 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "TO" ) );
435 0 : bufferQuoteIdentifier( buf, futureColumnName, settings );
436 0 : transaction.executeUpdate( buf.makeStringAndClear() );
437 : }
438 : }
439 :
440 0 : OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
441 0 : OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
442 0 : if( futureDefaultValue != pastDefaultValue )
443 : {
444 0 : buf = OUStringBuffer( 128 );
445 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
446 0 : bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
447 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
448 0 : bufferQuoteIdentifier( buf, futureColumnName, settings );
449 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET DEFAULT " ) );
450 : // LEM TODO: check out
451 : // default value is not quoted, caller needs to quote himself (otherwise
452 : // how to pass e.g. nextval('something' ) ????
453 0 : buf.append( futureDefaultValue );
454 : // bufferQuoteConstant( buf, defaultValue, encoding );
455 0 : transaction.executeUpdate( buf.makeStringAndClear() );
456 : }
457 :
458 0 : sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
459 0 : sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
460 0 : if( futureNullable != pastNullable )
461 : {
462 0 : buf = OUStringBuffer( 128 );
463 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
464 0 : bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
465 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
466 0 : bufferQuoteIdentifier( buf, futureColumnName, settings );
467 0 : if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
468 : {
469 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET" ) );
470 : }
471 : else
472 : {
473 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "DROP" ) );
474 : }
475 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NOT NULL" ) );
476 0 : transaction.executeUpdate( buf.makeStringAndClear() );
477 : }
478 :
479 : // OUString futureComment = extractStringProperty( future, st.HELP_TEXT );
480 : // OUString pastComment = extractStringProperty( past, st.HELP_TEXT );
481 : // printf( "past Comment %s, futureComment %s\n",
482 : // OUStringToOString( pastComment, RTL_TEXTENCODING_ASCII_US ).getStr(),
483 : // OUStringToOString( futureComment, RTL_TEXTENCODING_ASCII_US ).getStr() );
484 0 : OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
485 0 : OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
486 :
487 0 : if( futureComment != pastComment )
488 : {
489 0 : buf = OUStringBuffer( 128 );
490 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
491 0 : bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName, settings );
492 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
493 0 : bufferQuoteConstant( buf, futureComment, settings );
494 0 : transaction.executeUpdate( buf.makeStringAndClear() );
495 : }
496 0 : transaction.commit();
497 0 : }
498 :
499 0 : void Columns::appendByDescriptor(
500 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& future )
501 : throw (::com::sun::star::sdbc::SQLException,
502 : ::com::sun::star::container::ElementExistException,
503 : ::com::sun::star::uno::RuntimeException)
504 : {
505 0 : osl::MutexGuard guard( m_refMutex->mutex );
506 0 : Statics & st = getStatics();
507 0 : Reference< XPropertySet > past = createDataDescriptor();
508 0 : past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
509 : alterColumnByDescriptor(
510 0 : m_schemaName, m_tableName, m_pSettings, m_origin->createStatement() , past, future );
511 :
512 0 : refresh();
513 0 : }
514 :
515 : // void Columns::dropByName( const ::rtl::OUString& elementName )
516 : // throw (::com::sun::star::sdbc::SQLException,
517 : // ::com::sun::star::container::NoSuchElementException,
518 : // ::com::sun::star::uno::RuntimeException)
519 : // {
520 : // String2IntMap::const_iterator ii = m_name2index.find( elementName );
521 : // if( ii == m_name2index.end() )
522 : // {
523 : // OUStringBuffer buf( 128 );
524 : // buf.appendAscii( "Column " );
525 : // buf.append( elementName );
526 : // buf.appendAscii( " is unknown in table " );
527 : // buf.append( m_schemaName );
528 : // buf.appendAscii( "." );
529 : // buf.append( m_tableName );
530 : // buf.appendAscii( ", so it can't be dropped" );
531 : // throw com::sun::star::container::NoSuchElementException(
532 : // buf.makeStringAndClear(), *this );
533 : // }
534 : // dropByIndex( ii->second );
535 : // }
536 :
537 0 : void Columns::dropByIndex( sal_Int32 index )
538 : throw (::com::sun::star::sdbc::SQLException,
539 : ::com::sun::star::lang::IndexOutOfBoundsException,
540 : ::com::sun::star::uno::RuntimeException)
541 : {
542 0 : osl::MutexGuard guard( m_refMutex->mutex );
543 0 : if( index < 0 || index >= m_values.getLength() )
544 : {
545 0 : OUStringBuffer buf( 128 );
546 0 : buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
547 0 : buf.append((sal_Int32)(m_values.getLength() -1) );
548 0 : buf.appendAscii( ", got " );
549 0 : buf.append( index );
550 0 : buf.appendAscii( ")" );
551 : throw com::sun::star::lang::IndexOutOfBoundsException(
552 0 : buf.makeStringAndClear(), *this );
553 : }
554 :
555 0 : Reference< XPropertySet > set;
556 0 : m_values[index] >>= set;
557 0 : Statics &st = getStatics();
558 0 : OUString name;
559 0 : set->getPropertyValue( st.NAME ) >>= name;
560 :
561 0 : OUStringBuffer update( 128 );
562 0 : update.appendAscii( "ALTER TABLE ONLY");
563 0 : bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName, m_pSettings );
564 0 : update.appendAscii( "DROP COLUMN" );
565 0 : bufferQuoteIdentifier( update, name, m_pSettings );
566 0 : Reference< XStatement > stmt = m_origin->createStatement( );
567 0 : DisposeGuard disposeIt( stmt );
568 0 : stmt->executeUpdate( update.makeStringAndClear() );
569 :
570 0 : Container::dropByIndex( index );
571 0 : }
572 :
573 :
574 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Columns::createDataDescriptor()
575 : throw (::com::sun::star::uno::RuntimeException)
576 : {
577 0 : return new ColumnDescriptor( m_refMutex, m_origin, m_pSettings );
578 : }
579 :
580 0 : Reference< com::sun::star::container::XNameAccess > Columns::create(
581 : const ::rtl::Reference< RefCountedMutex > & refMutex,
582 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
583 : ConnectionSettings *pSettings,
584 : const rtl::OUString &schemaName,
585 : const rtl::OUString &tableName,
586 : Columns **ppColumns)
587 : {
588 : *ppColumns = new Columns(
589 0 : refMutex, origin, pSettings, schemaName, tableName );
590 0 : Reference< com::sun::star::container::XNameAccess > ret = *ppColumns;
591 0 : (*ppColumns)->refresh();
592 :
593 0 : return ret;
594 : }
595 :
596 :
597 : //_____________________________________________________________________________________
598 0 : ColumnDescriptors::ColumnDescriptors(
599 : const ::rtl::Reference< RefCountedMutex > & refMutex,
600 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
601 : ConnectionSettings *pSettings )
602 0 : : Container( refMutex, origin, pSettings, ASCII_STR( "COLUMN-DESCRIPTOR" ) )
603 0 : {}
604 :
605 :
606 0 : Reference< ::com::sun::star::beans::XPropertySet > ColumnDescriptors::createDataDescriptor()
607 : throw (::com::sun::star::uno::RuntimeException)
608 : {
609 0 : return new ColumnDescriptor( m_refMutex, m_origin, m_pSettings );
610 : }
611 :
612 : }
|