Branch data 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/KeyRule.hpp>
64 : : #include <com/sun/star/sdbcx/KeyType.hpp>
65 : :
66 : : #include "pq_xkeys.hxx"
67 : : #include "pq_xkey.hxx"
68 : : #include "pq_statics.hxx"
69 : : #include "pq_tools.hxx"
70 : :
71 : : using osl::MutexGuard;
72 : :
73 : : using rtl::OUString;
74 : : using rtl::OUStringBuffer;
75 : : using rtl::OUStringToOString;
76 : :
77 : : using com::sun::star::beans::XPropertySet;
78 : :
79 : : using com::sun::star::uno::Any;
80 : : using com::sun::star::uno::makeAny;
81 : : using com::sun::star::uno::UNO_QUERY;
82 : : using com::sun::star::uno::Type;
83 : : using com::sun::star::uno::XInterface;
84 : : using com::sun::star::uno::Reference;
85 : : using com::sun::star::uno::Sequence;
86 : : using com::sun::star::uno::RuntimeException;
87 : :
88 : : using com::sun::star::container::NoSuchElementException;
89 : : using com::sun::star::lang::WrappedTargetException;
90 : :
91 : : using com::sun::star::sdbc::XRow;
92 : : using com::sun::star::sdbc::XCloseable;
93 : : using com::sun::star::sdbc::XStatement;
94 : : using com::sun::star::sdbc::XResultSet;
95 : : using com::sun::star::sdbc::XParameters;
96 : : using com::sun::star::sdbc::XPreparedStatement;
97 : : using com::sun::star::sdbc::XDatabaseMetaData;
98 : :
99 : : namespace pq_sdbc_driver
100 : : {
101 : : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
102 : :
103 : 0 : Keys::Keys(
104 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
105 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
106 : : ConnectionSettings *pSettings,
107 : : const rtl::OUString &schemaName,
108 : : const rtl::OUString &tableName)
109 : 0 : : Container( refMutex, origin, pSettings, getStatics().KEY ),
110 : : m_schemaName( schemaName ),
111 : 0 : m_tableName( tableName )
112 : 0 : {}
113 : :
114 : 0 : Keys::~Keys()
115 : 0 : {}
116 : :
117 : 0 : static sal_Int32 string2keytype( const rtl::OUString &type )
118 : : {
119 : 0 : sal_Int32 ret = com::sun::star::sdbcx::KeyType::UNIQUE;
120 : 0 : if ( type == "p" )
121 : 0 : ret = com::sun::star::sdbcx::KeyType::PRIMARY;
122 : 0 : else if ( type == "f" )
123 : 0 : ret = com::sun::star::sdbcx::KeyType::FOREIGN;
124 : 0 : return ret;
125 : : }
126 : :
127 : 0 : static sal_Int32 string2keyrule( const rtl::OUString & rule )
128 : : {
129 : 0 : sal_Int32 ret = com::sun::star::sdbc::KeyRule::NO_ACTION;
130 : 0 : if( rule.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "r" ) ) == 0 )
131 : 0 : ret = com::sun::star::sdbc::KeyRule::RESTRICT;
132 : 0 : else if( rule.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "c" ) ) == 0 )
133 : 0 : ret = com::sun::star::sdbc::KeyRule::CASCADE;
134 : 0 : else if( rule.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "n" ) ) == 0 )
135 : 0 : ret = com::sun::star::sdbc::KeyRule::SET_NULL;
136 : 0 : else if( rule.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "d" ) ) == 0 )
137 : 0 : ret = com::sun::star::sdbc::KeyRule::SET_DEFAULT;
138 : 0 : return ret;
139 : : }
140 : :
141 : :
142 : :
143 : : // static void fillAttnum2attnameMap(
144 : : // Int2StringMap &map,
145 : : // const Reference< com::sun::star::sdbc::XConnection > &conn,
146 : : // const rtl::OUString &schema,
147 : : // const rtl::OUString &table )
148 : : // {
149 : : // Reference< XPreparedStatement > prep = conn->prepareStatement(
150 : : // ASCII_STR( "SELECT attname,attnum "
151 : : // "FROM pg_attribute "
152 : : // "INNER JOIN pg_class ON attrelid = pg_class.oid "
153 : : // "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
154 : : // "WHERE relname=? AND nspname=?" ) );
155 : :
156 : : // Reference< XParameters > paras( prep, UNO_QUERY );
157 : : // paras->setString( 1 , table );
158 : : // paras->setString( 2 , schema );
159 : : // Reference< XResultSet > rs = prep->executeQuery();
160 : :
161 : : // Reference< XRow > xRow( rs , UNO_QUERY );
162 : : // while( rs->next() )
163 : : // {
164 : : // map[ xRow->getInt(2) ] = xRow->getString(1);
165 : : // }
166 : : // }
167 : :
168 : : // static Sequence< rtl::OUString > resolveColumnNames(
169 : : // const Int2StringMap &map, const rtl::OUString &array )
170 : : // {
171 : : // Sequence< sal_Int32 > intArray = string2intarray( array );
172 : : // Sequence< ::rtl::OUString > ret( intArray.getLength() );
173 : : // for( int i = 0; i < intArray.getLength() ; i ++ )
174 : : // {
175 : : // Int2StringMap::const_iterator ii = map.find( intArray[i] );
176 : : // if( ii != map.end() )
177 : : // ret[i] = ii->second;
178 : : // }
179 : : // return ret;
180 : : // }
181 : :
182 : 0 : void Keys::refresh()
183 : : throw (::com::sun::star::uno::RuntimeException)
184 : : {
185 : : try
186 : : {
187 : 0 : if( isLog( m_pSettings, LogLevel::INFO ) )
188 : : {
189 : 0 : rtl::OStringBuffer buf;
190 : 0 : buf.append( "sdbcx.Keys get refreshed for table " );
191 : 0 : buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
192 : 0 : buf.append( "." );
193 : 0 : buf.append( OUStringToOString( m_tableName,m_pSettings->encoding ) );
194 : 0 : log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
195 : : }
196 : :
197 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
198 : 0 : Statics & st = getStatics();
199 : :
200 : 0 : Int2StringMap mainMap;
201 : 0 : fillAttnum2attnameMap( mainMap, m_origin, m_schemaName, m_tableName );
202 : :
203 : 0 : Reference< XPreparedStatement > stmt = m_origin->prepareStatement(
204 : : ASCII_STR(
205 : : "SELECT conname, " // 1
206 : : "contype, " // 2
207 : : "confupdtype, " // 3
208 : : "confdeltype, " // 4
209 : : "class2.relname, " // 5
210 : : "nmsp2.nspname, " // 6
211 : : "conkey," // 7
212 : : "confkey " // 8
213 : : "FROM pg_constraint INNER JOIN pg_class ON conrelid = pg_class.oid "
214 : : "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
215 : : "LEFT JOIN pg_class AS class2 ON confrelid = class2.oid "
216 : : "LEFT JOIN pg_namespace AS nmsp2 ON class2.relnamespace=nmsp2.oid "
217 : 0 : "WHERE pg_class.relname = ? AND pg_namespace.nspname = ?" ) );
218 : :
219 : 0 : Reference< XParameters > paras( stmt, UNO_QUERY );
220 : 0 : paras->setString( 1 , m_tableName );
221 : 0 : paras->setString( 2 , m_schemaName );
222 : 0 : Reference< XResultSet > rs = stmt->executeQuery();
223 : :
224 : 0 : Reference< XRow > xRow( rs , UNO_QUERY );
225 : :
226 : 0 : String2IntMap map;
227 : 0 : m_values = Sequence< com::sun::star::uno::Any > ();
228 : 0 : int keyIndex = 0;
229 : 0 : while( rs->next() )
230 : : {
231 : : Key * pKey =
232 : 0 : new Key( m_refMutex, m_origin, m_pSettings , m_schemaName, m_tableName );
233 : 0 : Reference< com::sun::star::beans::XPropertySet > prop = pKey;
234 : :
235 : : pKey->setPropertyValue_NoBroadcast_public(
236 : 0 : st.NAME, makeAny( xRow->getString( 1 ) ) );
237 : 0 : sal_Int32 keyType = string2keytype( xRow->getString(2) );
238 : 0 : pKey->setPropertyValue_NoBroadcast_public( st.TYPE, makeAny( keyType ) );
239 : : pKey->setPropertyValue_NoBroadcast_public(
240 : 0 : st.UPDATE_RULE, makeAny( string2keyrule( xRow->getString(3) ) ) );
241 : : pKey->setPropertyValue_NoBroadcast_public(
242 : 0 : st.DELETE_RULE, makeAny( string2keyrule( xRow->getString(4) ) ) );
243 : : pKey->setPropertyValue_NoBroadcast_public(
244 : : st.PRIVATE_COLUMNS,
245 : : makeAny(
246 : : convertMappedIntArray2StringArray(
247 : : mainMap,
248 : 0 : string2intarray( xRow->getString( 7 ) ) ) ) );
249 : :
250 : 0 : if( com::sun::star::sdbcx::KeyType::FOREIGN == keyType )
251 : : {
252 : 0 : OUStringBuffer buf( 128 );
253 : 0 : buf.append( xRow->getString( 6 ) );
254 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "." ) );
255 : 0 : buf.append( xRow->getString( 5 ) );
256 : : pKey->setPropertyValue_NoBroadcast_public(
257 : 0 : st.REFERENCED_TABLE, makeAny( buf.makeStringAndClear() ) );
258 : :
259 : 0 : Int2StringMap foreignMap;
260 : 0 : fillAttnum2attnameMap( foreignMap, m_origin, xRow->getString(6), xRow->getString(5));
261 : : pKey->setPropertyValue_NoBroadcast_public(
262 : : st.PRIVATE_FOREIGN_COLUMNS,
263 : : makeAny(
264 : : convertMappedIntArray2StringArray(
265 : : foreignMap,
266 : 0 : string2intarray( xRow->getString(8) ) ) ) );
267 : : }
268 : :
269 : :
270 : : {
271 : 0 : const int currentKeyIndex = keyIndex++;
272 : 0 : map[ xRow->getString( 1 ) ] = currentKeyIndex;
273 : : assert(currentKeyIndex == m_values.getLength());
274 : 0 : m_values.realloc( keyIndex );
275 : 0 : m_values[currentKeyIndex] = makeAny( prop );
276 : : }
277 : 0 : }
278 : 0 : m_name2index.swap( map );
279 : : }
280 : 0 : catch ( com::sun::star::sdbc::SQLException & e )
281 : : {
282 : 0 : throw RuntimeException( e.Message , e.Context );
283 : : }
284 : :
285 : 0 : fire( RefreshedBroadcaster( *this ) );
286 : 0 : }
287 : :
288 : :
289 : 0 : void Keys::appendByDescriptor(
290 : : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
291 : : throw (::com::sun::star::sdbc::SQLException,
292 : : ::com::sun::star::container::ElementExistException,
293 : : ::com::sun::star::uno::RuntimeException)
294 : : {
295 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
296 : :
297 : 0 : OUStringBuffer buf( 128 );
298 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE " ) );
299 : 0 : bufferQuoteQualifiedIdentifier( buf, m_schemaName, m_tableName, m_pSettings );
300 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " ADD " ) );
301 : 0 : bufferKey2TableConstraint( buf, descriptor, m_pSettings );
302 : :
303 : : Reference< XStatement > stmt =
304 : 0 : m_origin->createStatement();
305 : 0 : stmt->executeUpdate( buf.makeStringAndClear() );
306 : 0 : }
307 : :
308 : :
309 : 0 : void Keys::dropByIndex( sal_Int32 index )
310 : : throw (::com::sun::star::sdbc::SQLException,
311 : : ::com::sun::star::lang::IndexOutOfBoundsException,
312 : : ::com::sun::star::uno::RuntimeException)
313 : : {
314 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
315 : 0 : if( index < 0 || index >= m_values.getLength() )
316 : : {
317 : 0 : OUStringBuffer buf( 128 );
318 : 0 : buf.appendAscii( "TABLES: Index out of range (allowed 0 to " );
319 : 0 : buf.append( (sal_Int32)(m_values.getLength() -1) );
320 : 0 : buf.appendAscii( ", got " );
321 : 0 : buf.append( index );
322 : 0 : buf.appendAscii( ")" );
323 : : throw com::sun::star::lang::IndexOutOfBoundsException(
324 : 0 : buf.makeStringAndClear(), *this );
325 : : }
326 : :
327 : :
328 : 0 : Reference< XPropertySet > set;
329 : 0 : m_values[index] >>= set;
330 : :
331 : 0 : OUStringBuffer buf( 128 );
332 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE " ) );
333 : 0 : bufferQuoteQualifiedIdentifier( buf, m_schemaName, m_tableName, m_pSettings );
334 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " DROP CONSTRAINT " ) );
335 : 0 : bufferQuoteIdentifier( buf, extractStringProperty( set , getStatics().NAME ), m_pSettings );
336 : 0 : m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
337 : :
338 : :
339 : 0 : Container::dropByIndex( index );
340 : 0 : }
341 : :
342 : :
343 : 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Keys::createDataDescriptor()
344 : : throw (::com::sun::star::uno::RuntimeException)
345 : : {
346 : 0 : return new KeyDescriptor( m_refMutex, m_origin, m_pSettings );
347 : : }
348 : :
349 : 0 : Reference< com::sun::star::container::XIndexAccess > Keys::create(
350 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
351 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
352 : : ConnectionSettings *pSettings,
353 : : const rtl::OUString & schemaName,
354 : : const rtl::OUString & tableName)
355 : : {
356 : 0 : Keys *pKeys = new Keys( refMutex, origin, pSettings, schemaName, tableName );
357 : 0 : Reference< com::sun::star::container::XIndexAccess > ret = pKeys;
358 : 0 : pKeys->refresh();
359 : :
360 : 0 : return ret;
361 : : }
362 : : //_________________________________________________________________________________________
363 : 0 : KeyDescriptors::KeyDescriptors(
364 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
365 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
366 : : ConnectionSettings *pSettings)
367 : 0 : : Container( refMutex, origin, pSettings, getStatics().KEY )
368 : 0 : {}
369 : :
370 : 0 : Reference< com::sun::star::container::XIndexAccess > KeyDescriptors::create(
371 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
372 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
373 : : ConnectionSettings *pSettings)
374 : : {
375 : 0 : return new KeyDescriptors( refMutex, origin, pSettings );
376 : : }
377 : :
378 : 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > KeyDescriptors::createDataDescriptor()
379 : : throw (::com::sun::star::uno::RuntimeException)
380 : : {
381 : 0 : return new KeyDescriptor( m_refMutex, m_origin, m_pSettings );
382 : : }
383 : :
384 : : };
|