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 : * 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/.
34 : *
35 : ************************************************************************/
36 :
37 : #include <rtl/ustrbuf.hxx>
38 : #include <rtl/strbuf.hxx>
39 :
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>
44 :
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"
50 :
51 : using osl::MutexGuard;
52 :
53 :
54 : using com::sun::star::beans::XPropertySet;
55 :
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;
64 :
65 : using com::sun::star::container::NoSuchElementException;
66 : using com::sun::star::lang::WrappedTargetException;
67 :
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;
76 :
77 : namespace pq_sdbc_driver
78 : {
79 :
80 0 : 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 0 : m_foreignColumnNames( foreignColumnNames )
93 0 : {}
94 :
95 0 : KeyColumns::~KeyColumns()
96 0 : {}
97 :
98 :
99 0 : void KeyColumns::refresh()
100 : throw (::com::sun::star::uno::RuntimeException, std::exception)
101 : {
102 : try
103 : {
104 0 : if( isLog( m_pSettings, LogLevel::INFO ) )
105 : {
106 0 : OStringBuffer buf;
107 0 : buf.append( "sdbcx.KeyColumns get refreshed for table " );
108 0 : buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
109 0 : buf.append( "." );
110 0 : buf.append( OUStringToOString( m_tableName, m_pSettings->encoding ) );
111 0 : log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
112 : }
113 :
114 0 : osl::MutexGuard guard( m_refMutex->mutex );
115 :
116 0 : Statics &st = getStatics();
117 0 : Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
118 :
119 : Reference< XResultSet > rs =
120 0 : meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
121 :
122 0 : DisposeGuard disposeIt( rs );
123 0 : Reference< XRow > xRow( rs , UNO_QUERY );
124 :
125 0 : String2IntMap map;
126 :
127 0 : m_values = Sequence< com::sun::star::uno::Any > ();
128 0 : sal_Int32 columnIndex = 0;
129 0 : while( rs->next() )
130 : {
131 0 : OUString columnName = xRow->getString( 4 );
132 :
133 : int keyindex;
134 0 : for( keyindex = 0 ; keyindex < m_columnNames.getLength() ; keyindex ++ )
135 : {
136 0 : if( columnName == m_columnNames[keyindex] )
137 0 : break;
138 : }
139 0 : if( m_columnNames.getLength() == keyindex )
140 0 : continue;
141 :
142 : KeyColumn * pKeyColumn =
143 0 : new KeyColumn( m_refMutex, m_origin, m_pSettings );
144 0 : Reference< com::sun::star::beans::XPropertySet > prop = pKeyColumn;
145 :
146 0 : OUString name = columnMetaData2SDBCX( pKeyColumn, xRow );
147 0 : if( keyindex < m_foreignColumnNames.getLength() )
148 : {
149 : pKeyColumn->setPropertyValue_NoBroadcast_public(
150 0 : st.RELATED_COLUMN, makeAny( m_foreignColumnNames[keyindex]) );
151 : }
152 :
153 : {
154 0 : const int currentColumnIndex = columnIndex++;
155 : assert(currentColumnIndex == m_values.getLength());
156 0 : m_values.realloc( columnIndex );
157 0 : m_values[currentColumnIndex] = makeAny( prop );
158 0 : map[ name ] = currentColumnIndex;
159 : }
160 0 : }
161 0 : m_name2index.swap( map );
162 : }
163 0 : catch ( com::sun::star::sdbc::SQLException & e )
164 : {
165 0 : throw RuntimeException( e.Message , e.Context );
166 : }
167 :
168 0 : fire( RefreshedBroadcaster( *this ) );
169 0 : }
170 :
171 :
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();
181 :
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 : // // }
194 :
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 );
201 :
202 : // TransactionGuard transaction( stmt );
203 :
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 : // }
223 :
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 : // }
235 :
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 : // }
252 :
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 : // }
273 :
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 : // }
287 :
288 0 : 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)
293 : {
294 : (void) future;
295 : throw com::sun::star::sdbc::SQLException(
296 : "KeyColumns::appendByDescriptor not implemented yet",
297 0 : *this, OUString(), 1, Any() );
298 :
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 );
305 :
306 : }
307 :
308 :
309 0 : 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)
313 : {
314 : (void) index;
315 : throw com::sun::star::sdbc::SQLException(
316 : "KeyColumns::dropByIndex not implemented yet",
317 0 : *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 : // }
330 :
331 : // Reference< XPropertySet > set;
332 : // m_values[index] >>= set;
333 : // Statics &st = getStatics();
334 : // OUString name;
335 : // set->getPropertyValue( st.NAME ) >>= name;
336 :
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() );
345 :
346 : }
347 :
348 :
349 0 : Reference< ::com::sun::star::beans::XPropertySet > KeyColumns::createDataDescriptor()
350 : throw (::com::sun::star::uno::RuntimeException, std::exception)
351 : {
352 0 : return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
353 : }
354 :
355 0 : 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 )
363 : {
364 : KeyColumns *pKeyColumns = new KeyColumns(
365 0 : refMutex, origin, pSettings, schemaName, tableName, columnNames, foreignColumnNames );
366 0 : Reference< com::sun::star::container::XNameAccess > ret = pKeyColumns;
367 0 : pKeyColumns->refresh();
368 :
369 0 : return ret;
370 : }
371 :
372 :
373 0 : 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 0 : : Container( refMutex, origin, pSettings, "KEY_COLUMN" )
378 0 : {}
379 :
380 0 : Reference< ::com::sun::star::beans::XPropertySet > KeyColumnDescriptors::createDataDescriptor()
381 : throw (::com::sun::star::uno::RuntimeException, std::exception)
382 : {
383 0 : return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
384 : }
385 : };
386 :
387 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|