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,
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
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>
59 #include <rtl/strbuf.hxx>
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>
66 #include "pq_xcolumns.hxx"
67 #include "pq_xkeycolumns.hxx"
68 #include "pq_xkeycolumn.hxx"
69 #include "pq_statics.hxx"
70 #include "pq_tools.hxx"
72 using osl::MutexGuard
;
75 using rtl::OUStringBuffer
;
76 using rtl::OUStringToOString
;
78 using com::sun::star::beans::XPropertySet
;
80 using com::sun::star::uno::Any
;
81 using com::sun::star::uno::makeAny
;
82 using com::sun::star::uno::UNO_QUERY
;
83 using com::sun::star::uno::Type
;
84 using com::sun::star::uno::XInterface
;
85 using com::sun::star::uno::Reference
;
86 using com::sun::star::uno::Sequence
;
87 using com::sun::star::uno::RuntimeException
;
89 using com::sun::star::container::NoSuchElementException
;
90 using com::sun::star::lang::WrappedTargetException
;
92 using com::sun::star::sdbc::XRow
;
93 using com::sun::star::sdbc::XCloseable
;
94 using com::sun::star::sdbc::XStatement
;
95 using com::sun::star::sdbc::XResultSet
;
96 using com::sun::star::sdbc::XParameters
;
97 using com::sun::star::sdbc::XPreparedStatement
;
98 using com::sun::star::sdbc::XDatabaseMetaData
;
99 using com::sun::star::sdbc::SQLException
;
101 namespace pq_sdbc_driver
103 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
105 KeyColumns::KeyColumns(
106 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
107 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
108 ConnectionSettings
*pSettings
,
109 const rtl::OUString
&schemaName
,
110 const rtl::OUString
&tableName
,
111 const Sequence
< rtl::OUString
> &columnNames
,
112 const Sequence
< rtl::OUString
> &foreignColumnNames
)
113 : Container( refMutex
, origin
, pSettings
, ASCII_STR( "KEY_COLUMN" ) ),
114 m_schemaName( schemaName
),
115 m_tableName( tableName
),
116 m_columnNames( columnNames
),
117 m_foreignColumnNames( foreignColumnNames
)
120 KeyColumns::~KeyColumns()
124 void KeyColumns::refresh()
125 throw (::com::sun::star::uno::RuntimeException
)
129 if( isLog( m_pSettings
, LogLevel::INFO
) )
131 rtl::OStringBuffer buf
;
132 buf
.append( "sdbcx.KeyColumns get refreshed for table " );
133 buf
.append( OUStringToOString( m_schemaName
, m_pSettings
->encoding
) );
135 buf
.append( OUStringToOString( m_tableName
, m_pSettings
->encoding
) );
136 log( m_pSettings
, LogLevel::INFO
, buf
.makeStringAndClear().getStr() );
139 osl::MutexGuard
guard( m_refMutex
->mutex
);
141 Statics
&st
= getStatics();
142 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
144 Reference
< XResultSet
> rs
=
145 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
147 DisposeGuard
disposeIt( rs
);
148 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
152 m_values
= Sequence
< com::sun::star::uno::Any
> ();
153 sal_Int32 columnIndex
= 0;
156 OUString columnName
= xRow
->getString( 4 );
159 for( keyindex
= 0 ; keyindex
< m_columnNames
.getLength() ; keyindex
++ )
161 if( columnName
== m_columnNames
[keyindex
] )
164 if( m_columnNames
.getLength() == keyindex
)
167 KeyColumn
* pKeyColumn
=
168 new KeyColumn( m_refMutex
, m_origin
, m_pSettings
);
169 Reference
< com::sun::star::beans::XPropertySet
> prop
= pKeyColumn
;
171 OUString name
= columnMetaData2SDBCX( pKeyColumn
, xRow
);
172 if( keyindex
< m_foreignColumnNames
.getLength() )
174 pKeyColumn
->setPropertyValue_NoBroadcast_public(
175 st
.RELATED_COLUMN
, makeAny( m_foreignColumnNames
[keyindex
]) );
179 const int currentColumnIndex
= columnIndex
++;
180 assert(currentColumnIndex
== m_values
.getLength());
181 m_values
.realloc( columnIndex
);
182 m_values
[currentColumnIndex
] = makeAny( prop
);
183 map
[ name
] = currentColumnIndex
;
186 m_name2index
.swap( map
);
188 catch ( com::sun::star::sdbc::SQLException
& e
)
190 throw RuntimeException( e
.Message
, e
.Context
);
193 fire( RefreshedBroadcaster( *this ) );
197 // void alterColumnByDescriptor(
198 // const OUString & schemaName,
199 // const OUString & tableName,
200 // rtl_TextEncoding encoding,
201 // const Reference< XStatement > &stmt,
202 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & past,
203 // const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & future)
205 // Statics & st = getStatics();
207 // // if( past->getPropertyValue( st.TABLE_NAME ) != future->getPropertyValue( st.TABLE_NAME ) ||
208 // // past->getPropertyValue( st.SCHEMA_NAME ) != future->getPropertyValue( st.SCHEMA_NAME ))
210 // // OUStringBuffer buf(128);
211 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Can't move column " ) );
212 // // buf.append( extractStringProperty( past, st.COLUMN_NAME ) );
213 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " from table " ) );
214 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
215 // // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " to table " ) );
216 // // buf.append( extractStringProperty( past, st.TABLE_NAME ) );
217 // // throw SQLException( buf.makeStringAndClear(), Reference< XInterface > () );
220 // // OUString tableName = extractStringProperty( past, st.TABLE_NAME );
221 // // OUString schemaName = extractStringProperty( past, st.SCHEMA_NAME );
222 // OUString pastColumnName = extractStringProperty( past, st.NAME );
223 // OUString futureColumnName = extractStringProperty( future, st.NAME );
224 // OUString pastTypeName = sqltype2string( past );
225 // OUString futureTypeName = sqltype2string( future );
227 // TransactionGuard transaction( stmt );
229 // OUStringBuffer buf( 128 );
230 // if( ! pastColumnName.getLength())
232 // // create a new column
233 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
234 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
235 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ADD COLUMN" ) );
236 // bufferQuoteIdentifier( buf, futureColumnName );
237 // buf.append( futureTypeName );
238 // transaction.executeUpdate( buf.makeStringAndClear() );
242 // if( pastTypeName != futureTypeName )
244 // throw RuntimeException(
245 // ASCII_STR( "Can't modify column types, drop the column and create a new one" ),
246 // Reference< XInterface > () );
249 // if( pastColumnName != futureColumnName )
251 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
252 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
253 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "RENAME COLUMN" ) );
254 // bufferQuoteIdentifier( buf, pastColumnName );
255 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "TO" ) );
256 // bufferQuoteIdentifier( buf, futureColumnName );
257 // transaction.executeUpdate( buf.makeStringAndClear() );
261 // OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
262 // OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
263 // if( futureDefaultValue != pastDefaultValue )
265 // buf = OUStringBuffer( 128 );
266 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
267 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
268 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
269 // bufferQuoteIdentifier( buf, futureColumnName );
270 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET DEFAULT " ) );
271 // // default value is not quoted, caller needs to quote himself (otherwise
272 // // how to pass e.g. nextval('something' ) ????
273 // buf.append( futureDefaultValue );
274 // // bufferQuoteConstant( buf, defaultValue, encoding );
275 // transaction.executeUpdate( buf.makeStringAndClear() );
278 // sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
279 // sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
280 // if( futureNullable != pastNullable )
282 // buf = OUStringBuffer( 128 );
283 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
284 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
285 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
286 // bufferQuoteIdentifier( buf, futureColumnName );
287 // if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
289 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET" ) );
293 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "DROP" ) );
295 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NOT NULL" ) );
296 // transaction.executeUpdate( buf.makeStringAndClear() );
299 // OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
300 // OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
301 // if( futureComment != pastComment )
303 // buf = OUStringBuffer( 128 );
304 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
305 // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName );
306 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
307 // bufferQuoteConstant( buf, futureComment,encoding);
308 // transaction.executeUpdate( buf.makeStringAndClear() );
310 // transaction.commit();
313 void KeyColumns::appendByDescriptor(
314 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& future
)
315 throw (::com::sun::star::sdbc::SQLException
,
316 ::com::sun::star::container::ElementExistException
,
317 ::com::sun::star::uno::RuntimeException
)
320 throw com::sun::star::sdbc::SQLException(
321 ASCII_STR( "KeyColumns::appendByDescriptor not implemented yet" ),
322 *this, OUString(), 1, Any() );
324 // osl::MutexGuard guard( m_refMutex->mutex );
325 // Statics & st = getStatics();
326 // Reference< XPropertySet > past = createDataDescriptor();
327 // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
328 // alterColumnByDescriptor(
329 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
334 void KeyColumns::dropByIndex( sal_Int32 index
)
335 throw (::com::sun::star::sdbc::SQLException
,
336 ::com::sun::star::lang::IndexOutOfBoundsException
,
337 ::com::sun::star::uno::RuntimeException
)
340 throw com::sun::star::sdbc::SQLException(
341 ASCII_STR( "KeyColumns::dropByIndex not implemented yet" ),
342 *this, OUString(), 1, Any() );
343 // osl::MutexGuard guard( m_refMutex->mutex );
344 // if( index < 0 || index >= m_values.getLength() )
346 // OUStringBuffer buf( 128 );
347 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
348 // buf.append((sal_Int32)(m_values.getLength() -1) );
349 // buf.appendAscii( ", got " );
350 // buf.append( index );
351 // buf.appendAscii( ")" );
352 // throw com::sun::star::lang::IndexOutOfBoundsException(
353 // buf.makeStringAndClear(), *this );
356 // Reference< XPropertySet > set;
357 // m_values[index] >>= set;
358 // Statics &st = getStatics();
360 // set->getPropertyValue( st.NAME ) >>= name;
362 // OUStringBuffer update( 128 );
363 // update.appendAscii( "ALTER TABLE ONLY");
364 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
365 // update.appendAscii( "DROP COLUMN" );
366 // bufferQuoteIdentifier( update, name );
367 // Reference< XStatement > stmt = m_origin->createStatement( );
368 // DisposeGuard disposeIt( stmt );
369 // stmt->executeUpdate( update.makeStringAndClear() );
374 Reference
< ::com::sun::star::beans::XPropertySet
> KeyColumns::createDataDescriptor()
375 throw (::com::sun::star::uno::RuntimeException
)
377 return new KeyColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);
380 Reference
< com::sun::star::container::XNameAccess
> KeyColumns::create(
381 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
382 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
383 ConnectionSettings
*pSettings
,
384 const rtl::OUString
&schemaName
,
385 const rtl::OUString
&tableName
,
386 const Sequence
< rtl::OUString
> &columnNames
,
387 const Sequence
< rtl::OUString
> &foreignColumnNames
)
389 KeyColumns
*pKeyColumns
= new KeyColumns(
390 refMutex
, origin
, pSettings
, schemaName
, tableName
, columnNames
, foreignColumnNames
);
391 Reference
< com::sun::star::container::XNameAccess
> ret
= pKeyColumns
;
392 pKeyColumns
->refresh();
397 //_______________________________________________________________________________________
398 KeyColumnDescriptors::KeyColumnDescriptors(
399 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
400 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
401 ConnectionSettings
*pSettings
)
402 : Container( refMutex
, origin
, pSettings
, ASCII_STR( "KEY_COLUMN" ) )
405 Reference
< ::com::sun::star::beans::XPropertySet
> KeyColumnDescriptors::createDataDescriptor()
406 throw (::com::sun::star::uno::RuntimeException
)
408 return new KeyColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);