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/KeyRule.hpp>
43 : #include <com/sun/star/sdbcx/KeyType.hpp>
44 :
45 : #include "pq_xindexes.hxx"
46 : #include "pq_xindex.hxx"
47 : #include "pq_statics.hxx"
48 : #include "pq_tools.hxx"
49 :
50 : using osl::MutexGuard;
51 :
52 :
53 : using com::sun::star::beans::XPropertySet;
54 :
55 : using com::sun::star::uno::Any;
56 : using com::sun::star::uno::makeAny;
57 : using com::sun::star::uno::UNO_QUERY;
58 : using com::sun::star::uno::Type;
59 : using com::sun::star::uno::XInterface;
60 : using com::sun::star::uno::Reference;
61 : using com::sun::star::uno::Sequence;
62 : using com::sun::star::uno::RuntimeException;
63 :
64 : using com::sun::star::container::NoSuchElementException;
65 : using com::sun::star::container::XEnumerationAccess;
66 : using com::sun::star::container::XEnumeration;
67 :
68 : using com::sun::star::lang::WrappedTargetException;
69 :
70 : using com::sun::star::sdbcx::XColumnsSupplier;
71 :
72 : using com::sun::star::sdbc::XRow;
73 : using com::sun::star::sdbc::XCloseable;
74 : using com::sun::star::sdbc::XStatement;
75 : using com::sun::star::sdbc::XResultSet;
76 : using com::sun::star::sdbc::XParameters;
77 : using com::sun::star::sdbc::XPreparedStatement;
78 : using com::sun::star::sdbc::XDatabaseMetaData;
79 :
80 : namespace pq_sdbc_driver
81 : {
82 :
83 0 : Indexes::Indexes(
84 : const ::rtl::Reference< RefCountedMutex > & refMutex,
85 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
86 : ConnectionSettings *pSettings,
87 : const OUString &schemaName,
88 : const OUString &tableName)
89 0 : : Container( refMutex, origin, pSettings, getStatics().KEY ),
90 : m_schemaName( schemaName ),
91 0 : m_tableName( tableName )
92 : {
93 0 : }
94 :
95 0 : Indexes::~Indexes()
96 0 : {}
97 :
98 0 : void Indexes::refresh()
99 : throw (::com::sun::star::uno::RuntimeException, std::exception)
100 : {
101 : try
102 : {
103 0 : if( isLog( m_pSettings, LogLevel::INFO ) )
104 : {
105 0 : OStringBuffer buf;
106 0 : buf.append( "sdbcx.Indexes get refreshed for table " );
107 0 : buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
108 0 : buf.append( "." );
109 0 : buf.append( OUStringToOString( m_tableName,m_pSettings->encoding ) );
110 0 : log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
111 : }
112 :
113 0 : osl::MutexGuard guard( m_refMutex->mutex );
114 0 : Statics & st = getStatics();
115 :
116 0 : Int2StringMap column2NameMap;
117 0 : fillAttnum2attnameMap( column2NameMap, m_origin, m_schemaName, m_tableName );
118 :
119 : // see XDatabaseMetaData::getIndexInfo()
120 0 : Reference< XPreparedStatement > stmt = m_origin->prepareStatement(
121 : "SELECT nspname, " // 1
122 : "pg_class.relname, " // 2
123 : "class2.relname, " // 3
124 : "indisclustered, " // 4
125 : "indisunique, " // 5
126 : "indisprimary, " // 6
127 : "indkey " // 7
128 : "FROM pg_index INNER JOIN pg_class ON indrelid = pg_class.oid "
129 : "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
130 : "INNER JOIN pg_class as class2 ON pg_index.indexrelid = class2.oid "
131 0 : "WHERE nspname = ? AND pg_class.relname = ?" );
132 :
133 0 : Reference< XParameters > params( stmt, UNO_QUERY);
134 0 : params->setString( 1, m_schemaName );
135 0 : params->setString( 2, m_tableName );
136 0 : Reference< XResultSet > rs = stmt->executeQuery();
137 :
138 0 : Reference< XRow > row( rs, UNO_QUERY );
139 0 : String2IntMap map;
140 0 : m_values = Sequence< com::sun::star::uno::Any > ();
141 0 : sal_Int32 index = 0;
142 0 : while( rs->next() )
143 : {
144 : static const sal_Int32 C_SCHEMA = 1;
145 : static const sal_Int32 C_TABLENAME = 2;
146 : static const sal_Int32 C_INDEXNAME = 3;
147 : static const sal_Int32 C_IS_CLUSTERED = 4;
148 : static const sal_Int32 C_IS_UNIQUE = 5;
149 : static const sal_Int32 C_IS_PRIMARY = 6;
150 : static const sal_Int32 C_COLUMNS = 7;
151 0 : OUString currentIndexName = row->getString( C_INDEXNAME );
152 : Index *pIndex =
153 : new Index( m_refMutex, m_origin, m_pSettings,
154 0 : m_schemaName, m_tableName );
155 :
156 : (void) C_SCHEMA; (void) C_TABLENAME;
157 0 : sal_Bool isUnique = row->getBoolean( C_IS_UNIQUE );
158 0 : sal_Bool isPrimary = row->getBoolean( C_IS_PRIMARY );
159 0 : sal_Bool isClusterd = row->getBoolean( C_IS_CLUSTERED );
160 0 : Reference< com::sun::star::beans::XPropertySet > prop = pIndex;
161 : pIndex->setPropertyValue_NoBroadcast_public(
162 0 : st.IS_UNIQUE, Any( &isUnique, getBooleanCppuType() ) );
163 : pIndex->setPropertyValue_NoBroadcast_public(
164 0 : st.IS_PRIMARY_KEY_INDEX, Any( &isPrimary, getBooleanCppuType() ) );
165 : pIndex->setPropertyValue_NoBroadcast_public(
166 0 : st.IS_CLUSTERED, Any( &isClusterd, getBooleanCppuType() ) );
167 : pIndex->setPropertyValue_NoBroadcast_public(
168 0 : st.NAME, makeAny( currentIndexName ) );
169 :
170 0 : Sequence< sal_Int32 > seq = parseIntArray( row->getString( C_COLUMNS ) );
171 0 : Sequence< OUString > columnNames(seq.getLength());
172 0 : for( int columns = 0 ; columns < seq.getLength() ; columns ++ )
173 : {
174 0 : columnNames[columns] = column2NameMap[ seq[columns] ];
175 : }
176 :
177 : pIndex->setPropertyValue_NoBroadcast_public(
178 0 : st.PRIVATE_COLUMN_INDEXES, makeAny( columnNames ));
179 :
180 : {
181 0 : const int currentIndex = index++;
182 : assert(currentIndex == m_values.getLength());
183 0 : m_values.realloc( index );
184 0 : m_values[currentIndex] = makeAny( prop );
185 0 : map[ currentIndexName ] = currentIndex;
186 : }
187 0 : }
188 0 : m_name2index.swap( map );
189 : }
190 0 : catch ( com::sun::star::sdbc::SQLException & e )
191 : {
192 0 : throw RuntimeException( e.Message , e.Context );
193 : }
194 :
195 0 : fire( RefreshedBroadcaster( *this ) );
196 0 : }
197 :
198 :
199 0 : void Indexes::appendByDescriptor(
200 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
201 : throw (::com::sun::star::sdbc::SQLException,
202 : ::com::sun::star::container::ElementExistException,
203 : ::com::sun::star::uno::RuntimeException, std::exception)
204 : {
205 0 : Statics & st = getStatics();
206 0 : OUString name = extractStringProperty( descriptor, st.NAME );
207 :
208 0 : bool isUnique = extractBoolProperty( descriptor, st.IS_UNIQUE );
209 :
210 0 : OUStringBuffer buf( 128 );
211 :
212 0 : buf.append( "CREATE " );
213 0 : if( isUnique )
214 0 : buf.append( "UNIQUE " );
215 0 : buf.append( "INDEX " );
216 0 : bufferQuoteIdentifier( buf, name, m_pSettings );
217 0 : buf.append( " ON " );
218 0 : bufferQuoteQualifiedIdentifier( buf, m_schemaName, m_tableName, m_pSettings );
219 :
220 0 : buf.append( " ( " );
221 :
222 0 : Reference< XColumnsSupplier > columns( descriptor, UNO_QUERY );
223 0 : if( columns.is() )
224 : {
225 0 : Reference< XEnumerationAccess > access( columns->getColumns(), UNO_QUERY );
226 0 : if( access.is() )
227 : {
228 0 : Reference< XEnumeration > xEnum( access->createEnumeration() );
229 0 : bool first = true;
230 0 : while( xEnum.is() && xEnum->hasMoreElements() )
231 : {
232 0 : Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
233 0 : if( first )
234 : {
235 0 : first = false;
236 : }
237 : else
238 : {
239 0 : buf.append( ", " );
240 : }
241 0 : buf.append( extractStringProperty( column, st.NAME ) );
242 0 : }
243 0 : }
244 : }
245 0 : buf.append( " ) " );
246 :
247 0 : m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
248 0 : refresh();
249 0 : }
250 :
251 0 : void Indexes::dropByIndex( sal_Int32 index )
252 : throw (::com::sun::star::sdbc::SQLException,
253 : ::com::sun::star::lang::IndexOutOfBoundsException,
254 : ::com::sun::star::uno::RuntimeException, std::exception)
255 : {
256 :
257 :
258 0 : osl::MutexGuard guard( m_refMutex->mutex );
259 0 : if( index < 0 || index >= m_values.getLength() )
260 : {
261 0 : OUStringBuffer buf( 128 );
262 0 : buf.appendAscii( "Indexes: Index out of range (allowed 0 to " );
263 0 : buf.append( (sal_Int32) (m_values.getLength() -1) );
264 0 : buf.appendAscii( ", got " );
265 0 : buf.append( index );
266 0 : buf.appendAscii( ")" );
267 : throw com::sun::star::lang::IndexOutOfBoundsException(
268 0 : buf.makeStringAndClear(), *this );
269 : }
270 :
271 0 : Reference< XPropertySet > set;
272 0 : m_values[index] >>= set;
273 0 : Statics &st = getStatics();
274 :
275 0 : OUStringBuffer buf( 128 );
276 0 : buf.append( "DROP INDEX " );
277 0 : bufferQuoteIdentifier( buf, extractStringProperty( set, st.NAME ), m_pSettings );
278 0 : m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
279 :
280 0 : Container::dropByIndex( index );
281 0 : }
282 :
283 :
284 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Indexes::createDataDescriptor()
285 : throw (::com::sun::star::uno::RuntimeException, std::exception)
286 : {
287 0 : return new IndexDescriptor( m_refMutex, m_origin, m_pSettings );
288 : }
289 :
290 0 : Reference< com::sun::star::container::XNameAccess > Indexes::create(
291 : const ::rtl::Reference< RefCountedMutex > & refMutex,
292 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
293 : ConnectionSettings *pSettings,
294 : const OUString & schemaName,
295 : const OUString & tableName)
296 : {
297 0 : Indexes *pIndexes = new Indexes( refMutex, origin, pSettings, schemaName, tableName );
298 0 : Reference< com::sun::star::container::XNameAccess > ret = pIndexes;
299 0 : pIndexes->refresh();
300 0 : return ret;
301 : }
302 :
303 :
304 :
305 0 : IndexDescriptors::IndexDescriptors(
306 : const ::rtl::Reference< RefCountedMutex > & refMutex,
307 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
308 : ConnectionSettings *pSettings)
309 0 : : Container( refMutex, origin, pSettings, getStatics().INDEX )
310 0 : {}
311 :
312 0 : Reference< com::sun::star::container::XNameAccess > IndexDescriptors::create(
313 : const ::rtl::Reference< RefCountedMutex > & refMutex,
314 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
315 : ConnectionSettings *pSettings)
316 : {
317 0 : return new IndexDescriptors( refMutex, origin, pSettings );
318 : }
319 :
320 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > IndexDescriptors::createDataDescriptor()
321 : throw (::com::sun::star::uno::RuntimeException, std::exception)
322 : {
323 0 : return new IndexDescriptor( m_refMutex, m_origin, m_pSettings );
324 : }
325 :
326 : };
327 :
328 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|