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() );
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 : // }
222 :
223 : // if( pastColumnName != futureColumnName )
224 : // {
225 : // buf.append( "ALTER TABLE" );
226 : // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
227 : // buf.append( "RENAME COLUMN" );
228 : // bufferQuoteIdentifier( buf, pastColumnName );
229 : // buf.append( "TO" );
230 : // bufferQuoteIdentifier( buf, futureColumnName );
231 : // transaction.executeUpdate( buf.makeStringAndClear() );
232 : // }
233 : // }
234 :
235 : // OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
236 : // OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
237 : // if( futureDefaultValue != pastDefaultValue )
238 : // {
239 : // buf = OUStringBuffer( 128 );
240 : // buf.append( "ALTER TABLE" );
241 : // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
242 : // buf.append( "ALTER COLUMN" );
243 : // bufferQuoteIdentifier( buf, futureColumnName );
244 : // buf.append( "SET DEFAULT " );
245 : // // default value is not quoted, caller needs to quote himself (otherwise
246 : // // how to pass e.g. nextval('something' ) ????
247 : // buf.append( futureDefaultValue );
248 : // // bufferQuoteConstant( buf, defaultValue, encoding );
249 : // transaction.executeUpdate( buf.makeStringAndClear() );
250 : // }
251 :
252 : // sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
253 : // sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
254 : // if( futureNullable != pastNullable )
255 : // {
256 : // buf = OUStringBuffer( 128 );
257 : // buf.append( "ALTER TABLE" );
258 : // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName );
259 : // buf.append( "ALTER COLUMN" );
260 : // bufferQuoteIdentifier( buf, futureColumnName );
261 : // if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
262 : // {
263 : // buf.append( "SET" );
264 : // }
265 : // else
266 : // {
267 : // buf.append( "DROP" );
268 : // }
269 : // buf.append( " NOT NULL" );
270 : // transaction.executeUpdate( buf.makeStringAndClear() );
271 : // }
272 :
273 : // OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
274 : // OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
275 : // if( futureComment != pastComment )
276 : // {
277 : // buf = OUStringBuffer( 128 );
278 : // buf.append( "COMMENT ON COLUMN" );
279 : // bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName );
280 : // buf.append( "IS " );
281 : // bufferQuoteConstant( buf, futureComment,encoding);
282 : // transaction.executeUpdate( buf.makeStringAndClear() );
283 : // }
284 : // transaction.commit();
285 : // }
286 :
287 0 : void KeyColumns::appendByDescriptor(
288 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& future )
289 : throw (::com::sun::star::sdbc::SQLException,
290 : ::com::sun::star::container::ElementExistException,
291 : ::com::sun::star::uno::RuntimeException, std::exception)
292 : {
293 : (void) future;
294 : throw com::sun::star::sdbc::SQLException(
295 : "KeyColumns::appendByDescriptor not implemented yet",
296 0 : *this, OUString(), 1, Any() );
297 :
298 : // osl::MutexGuard guard( m_refMutex->mutex );
299 : // Statics & st = getStatics();
300 : // Reference< XPropertySet > past = createDataDescriptor();
301 : // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
302 : // alterColumnByDescriptor(
303 : // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
304 :
305 : }
306 :
307 :
308 0 : void KeyColumns::dropByIndex( sal_Int32 index )
309 : throw (::com::sun::star::sdbc::SQLException,
310 : ::com::sun::star::lang::IndexOutOfBoundsException,
311 : ::com::sun::star::uno::RuntimeException, std::exception)
312 : {
313 : (void) index;
314 : throw com::sun::star::sdbc::SQLException(
315 : "KeyColumns::dropByIndex not implemented yet",
316 0 : *this, OUString(), 1, Any() );
317 : // osl::MutexGuard guard( m_refMutex->mutex );
318 : // if( index < 0 || index >= m_values.getLength() )
319 : // {
320 : // OUStringBuffer buf( 128 );
321 : // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
322 : // buf.append((sal_Int32)(m_values.getLength() -1) );
323 : // buf.appendAscii( ", got " );
324 : // buf.append( index );
325 : // buf.appendAscii( ")" );
326 : // throw com::sun::star::lang::IndexOutOfBoundsException(
327 : // buf.makeStringAndClear(), *this );
328 : // }
329 :
330 : // Reference< XPropertySet > set;
331 : // m_values[index] >>= set;
332 : // Statics &st = getStatics();
333 : // OUString name;
334 : // set->getPropertyValue( st.NAME ) >>= name;
335 :
336 : // OUStringBuffer update( 128 );
337 : // update.appendAscii( "ALTER TABLE ONLY");
338 : // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
339 : // update.appendAscii( "DROP COLUMN" );
340 : // bufferQuoteIdentifier( update, name );
341 : // Reference< XStatement > stmt = m_origin->createStatement( );
342 : // DisposeGuard disposeIt( stmt );
343 : // stmt->executeUpdate( update.makeStringAndClear() );
344 :
345 : }
346 :
347 :
348 0 : Reference< ::com::sun::star::beans::XPropertySet > KeyColumns::createDataDescriptor()
349 : throw (::com::sun::star::uno::RuntimeException, std::exception)
350 : {
351 0 : return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
352 : }
353 :
354 0 : Reference< com::sun::star::container::XNameAccess > KeyColumns::create(
355 : const ::rtl::Reference< RefCountedMutex > & refMutex,
356 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
357 : ConnectionSettings *pSettings,
358 : const OUString &schemaName,
359 : const OUString &tableName,
360 : const Sequence< OUString > &columnNames ,
361 : const Sequence< OUString > &foreignColumnNames )
362 : {
363 : KeyColumns *pKeyColumns = new KeyColumns(
364 0 : refMutex, origin, pSettings, schemaName, tableName, columnNames, foreignColumnNames );
365 0 : Reference< com::sun::star::container::XNameAccess > ret = pKeyColumns;
366 0 : pKeyColumns->refresh();
367 :
368 0 : return ret;
369 : }
370 :
371 :
372 0 : KeyColumnDescriptors::KeyColumnDescriptors(
373 : const ::rtl::Reference< RefCountedMutex > & refMutex,
374 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
375 : ConnectionSettings *pSettings )
376 0 : : Container( refMutex, origin, pSettings, "KEY_COLUMN" )
377 0 : {}
378 :
379 0 : Reference< ::com::sun::star::beans::XPropertySet > KeyColumnDescriptors::createDataDescriptor()
380 : throw (::com::sun::star::uno::RuntimeException, std::exception)
381 : {
382 0 : return new KeyColumnDescriptor( m_refMutex, m_origin, m_pSettings );
383 : }
384 : };
385 :
386 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|