1 /*************************************************************************
3 * $RCSfile: pq_xkeycolumns.cxx,v $
7 * last change: $Author: jbu $ $Date: 2004/08/29 08:33:31 $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Joerg Budischewski
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Joerg Budischewski
60 ************************************************************************/
64 #include <rtl/ustrbuf.hxx>
65 #include <rtl/strbuf.hxx>
67 #include <com/sun/star/sdbc/XRow.hpp>
68 #include <com/sun/star/sdbc/XParameters.hpp>
69 #include <com/sun/star/sdbc/DataType.hpp>
70 #include <com/sun/star/sdbc/ColumnValue.hpp>
72 #include "pq_xcolumns.hxx"
73 #include "pq_xkeycolumns.hxx"
74 #include "pq_xkeycolumn.hxx"
75 #include "pq_statics.hxx"
76 #include "pq_tools.hxx"
78 using osl::MutexGuard
;
81 using rtl::OUStringBuffer
;
82 using rtl::OUStringToOString
;
84 using com::sun::star::beans::XPropertySet
;
86 using com::sun::star::uno::Any
;
87 using com::sun::star::uno::makeAny
;
88 using com::sun::star::uno::UNO_QUERY
;
89 using com::sun::star::uno::Type
;
90 using com::sun::star::uno::XInterface
;
91 using com::sun::star::uno::Reference
;
92 using com::sun::star::uno::Sequence
;
93 using com::sun::star::uno::RuntimeException
;
95 using com::sun::star::container::NoSuchElementException
;
96 using com::sun::star::lang::WrappedTargetException
;
98 using com::sun::star::sdbc::XRow
;
99 using com::sun::star::sdbc::XCloseable
;
100 using com::sun::star::sdbc::XStatement
;
101 using com::sun::star::sdbc::XResultSet
;
102 using com::sun::star::sdbc::XParameters
;
103 using com::sun::star::sdbc::XPreparedStatement
;
104 using com::sun::star::sdbc::XDatabaseMetaData
;
105 using com::sun::star::sdbc::SQLException
;
107 namespace pq_sdbc_driver
109 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
111 KeyColumns::KeyColumns(
112 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
113 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
114 ConnectionSettings
*pSettings
,
115 const rtl::OUString
&schemaName
,
116 const rtl::OUString
&tableName
,
117 const Sequence
< rtl::OUString
> &columnNames
,
118 const Sequence
< rtl::OUString
> &foreignColumnNames
)
119 : Container( refMutex
, origin
, pSettings
, ASCII_STR( "KEY_COLUMN" ) ),
120 m_schemaName( schemaName
),
121 m_tableName( tableName
),
122 m_columnNames( columnNames
),
123 m_foreignColumnNames( foreignColumnNames
)
126 KeyColumns::~KeyColumns()
130 void KeyColumns::refresh()
131 throw (::com::sun::star::uno::RuntimeException
)
135 if( isLog( m_pSettings
, LogLevel::INFO
) )
137 rtl::OStringBuffer buf
;
138 buf
.append( "sdbcx.KeyColumns get refreshed for table " );
139 buf
.append( OUStringToOString( m_schemaName
, m_pSettings
->encoding
) );
141 buf
.append( OUStringToOString( m_tableName
, m_pSettings
->encoding
) );
142 log( m_pSettings
, LogLevel::INFO
, buf
.makeStringAndClear().getStr() );
145 osl::MutexGuard
guard( m_refMutex
->mutex
);
147 Statics
&st
= getStatics();
148 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
150 Reference
< XResultSet
> rs
=
151 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
153 DisposeGuard
disposeIt( rs
);
154 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
158 std::vector
< Any
, Allocator
< Any
> > vec
;
159 sal_Int32 columnIndex
= 0;
162 OUString columnName
= xRow
->getString( 4 );
165 for( keyindex
= 0 ; keyindex
< m_columnNames
.getLength() ; keyindex
++ )
167 if( columnName
== m_columnNames
[keyindex
] )
170 if( m_columnNames
.getLength() == keyindex
)
173 KeyColumn
* pKeyColumn
=
174 new KeyColumn( m_refMutex
, m_origin
, m_pSettings
);
175 Reference
< com::sun::star::beans::XPropertySet
> prop
= pKeyColumn
;
177 OUString name
= columnMetaData2SDBCX( pKeyColumn
, xRow
);
178 if( keyindex
< m_foreignColumnNames
.getLength() )
180 pKeyColumn
->setPropertyValue_NoBroadcast_public(
181 st
.RELATED_COLUMN
, makeAny( m_foreignColumnNames
[keyindex
]) );
184 vec
.push_back( makeAny( prop
) );
185 map
[ name
] = columnIndex
;
188 m_values
= Sequence
< com::sun::star::uno::Any
> ( & vec
[0] , vec
.size() );
189 m_name2index
.swap( map
);
191 catch ( com::sun::star::sdbc::SQLException
& e
)
193 throw RuntimeException( e
.Message
, e
.Context
);
196 fire( RefreshedBroadcaster( *this ) );
200 // void alterColumnByDescriptor(
201 // const OUString & schemaName,
202 // const OUString & tableName,
203 // rtl_TextEncoding encoding,
204 // const Reference< XStatement > &stmt,
205 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & past,
206 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & future)
208 // Statics & st = getStatics();
210 // // if( past->getPropertyValue( st.TABLE_NAME ) != future->getPropertyValue( st.TABLE_NAME ) ||
211 // // past->getPropertyValue( st.SCHEMA_NAME ) != future->getPropertyValue( st.SCHEMA_NAME ))
213 // // OUStringBuffer buf(128);
214 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Can't move column " ) );
215 // // buf.append( extractStringProperty( past, st.COLUMN_NAME ) );
216 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " from table " ) );
217 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
218 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " to table " ) );
219 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
220 // // throw SQLException( buf.makeStringAndClear(), Reference< XInterface > () );
223 // // OUString tableName = extractStringProperty( past, st.TABLE_NAME );
224 // // OUString schemaName = extractStringProperty( past, st.SCHEMA_NAME );
225 // OUString pastColumnName = extractStringProperty( past, st.NAME );
226 // OUString futureColumnName = extractStringProperty( future, st.NAME );
227 // OUString pastTypeName = sqltype2string( past );
228 // OUString futureTypeName = sqltype2string( future );
230 // TransactionGuard transaction( stmt );
232 // OUStringBuffer buf( 128 );
233 // if( ! pastColumnName.getLength())
235 // // create a new column
236 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
237 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
238 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ADD COLUMN" ) );
239 // bufferQuoteIdentifier( buf, futureColumnName );
240 // buf.append( futureTypeName );
241 // transaction.executeUpdate( buf.makeStringAndClear() );
245 // if( pastTypeName != futureTypeName )
247 // throw RuntimeException(
248 // ASCII_STR( "Can't modify column types, drop the column and create a new one" ),
249 // Reference< XInterface > () );
252 // if( pastColumnName != futureColumnName )
254 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
255 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
256 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "RENAME COLUMN" ) );
257 // bufferQuoteIdentifier( buf, pastColumnName );
258 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "TO" ) );
259 // bufferQuoteIdentifier( buf, futureColumnName );
260 // transaction.executeUpdate( buf.makeStringAndClear() );
264 // OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
265 // OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
266 // if( futureDefaultValue != pastDefaultValue )
268 // buf = OUStringBuffer( 128 );
269 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
270 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
271 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
272 // bufferQuoteIdentifier( buf, futureColumnName );
273 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET DEFAULT " ) );
274 // // default value is not quoted, caller needs to quote himself (otherwise
275 // // how to pass e.g. nextval('something' ) ????
276 // buf.append( futureDefaultValue );
277 // // bufferQuoteConstant( buf, defaultValue, encoding );
278 // transaction.executeUpdate( buf.makeStringAndClear() );
281 // sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
282 // sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
283 // if( futureNullable != pastNullable )
285 // buf = OUStringBuffer( 128 );
286 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
287 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
288 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
289 // bufferQuoteIdentifier( buf, futureColumnName );
290 // if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
292 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET" ) );
296 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "DROP" ) );
298 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NOT NULL" ) );
299 // transaction.executeUpdate( buf.makeStringAndClear() );
302 // OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
303 // OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
304 // if( futureComment != pastComment )
306 // buf = OUStringBuffer( 128 );
307 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
308 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName );
309 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
310 // bufferQuoteConstant( buf, futureComment,encoding);
311 // transaction.executeUpdate( buf.makeStringAndClear() );
313 // transaction.commit();
316 void KeyColumns::appendByDescriptor(
317 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& future
)
318 throw (::com::sun::star::sdbc::SQLException
,
319 ::com::sun::star::container::ElementExistException
,
320 ::com::sun::star::uno::RuntimeException
)
322 throw com::sun::star::sdbc::SQLException(
323 ASCII_STR( "KeyColumns::appendByDescriptor not implemented yet" ),
324 *this, OUString(), 1, Any() );
326 // osl::MutexGuard guard( m_refMutex->mutex );
327 // Statics & st = getStatics();
328 // Reference< XPropertySet > past = createDataDescriptor();
329 // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
330 // alterColumnByDescriptor(
331 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
336 void KeyColumns::dropByIndex( sal_Int32 index
)
337 throw (::com::sun::star::sdbc::SQLException
,
338 ::com::sun::star::lang::IndexOutOfBoundsException
,
339 ::com::sun::star::uno::RuntimeException
)
341 throw com::sun::star::sdbc::SQLException(
342 ASCII_STR( "KeyColumns::dropByIndex not implemented yet" ),
343 *this, OUString(), 1, Any() );
344 // osl::MutexGuard guard( m_refMutex->mutex );
345 // if( index < 0 || index >= m_values.getLength() )
347 // OUStringBuffer buf( 128 );
348 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
349 // buf.append((sal_Int32)(m_values.getLength() -1) );
350 // buf.appendAscii( ", got " );
351 // buf.append( index );
352 // buf.appendAscii( ")" );
353 // throw com::sun::star::lang::IndexOutOfBoundsException(
354 // buf.makeStringAndClear(), *this );
357 // Reference< XPropertySet > set;
358 // m_values[index] >>= set;
359 // Statics &st = getStatics();
361 // set->getPropertyValue( st.NAME ) >>= name;
363 // OUStringBuffer update( 128 );
364 // update.appendAscii( "ALTER TABLE ONLY");
365 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
366 // update.appendAscii( "DROP COLUMN" );
367 // bufferQuoteIdentifier( update, name );
368 // Reference< XStatement > stmt = m_origin->createStatement( );
369 // DisposeGuard disposeIt( stmt );
370 // stmt->executeUpdate( update.makeStringAndClear() );
375 Reference
< ::com::sun::star::beans::XPropertySet
> KeyColumns::createDataDescriptor()
376 throw (::com::sun::star::uno::RuntimeException
)
378 return new KeyColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);
381 Reference
< com::sun::star::container::XNameAccess
> KeyColumns::create(
382 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
383 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
384 ConnectionSettings
*pSettings
,
385 const rtl::OUString
&schemaName
,
386 const rtl::OUString
&tableName
,
387 const Sequence
< rtl::OUString
> &columnNames
,
388 const Sequence
< rtl::OUString
> &foreignColumnNames
)
390 KeyColumns
*pKeyColumns
= new KeyColumns(
391 refMutex
, origin
, pSettings
, schemaName
, tableName
, columnNames
, foreignColumnNames
);
392 Reference
< com::sun::star::container::XNameAccess
> ret
= pKeyColumns
;
393 pKeyColumns
->refresh();
398 //_______________________________________________________________________________________
399 KeyColumnDescriptors::KeyColumnDescriptors(
400 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
401 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
402 ConnectionSettings
*pSettings
)
403 : Container( refMutex
, origin
, pSettings
, ASCII_STR( "KEY_COLUMN" ) )
406 Reference
< ::com::sun::star::beans::XPropertySet
> KeyColumnDescriptors::createDataDescriptor()
407 throw (::com::sun::star::uno::RuntimeException
)
409 return new KeyColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);