nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xindexes.cxx
blob2f6df914f0a26f1ba0884e37d0fbc9d460076601
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,
18 * MA 02111-1307 USA
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 * 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/.
35 ************************************************************************/
37 #include <sal/log.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
40 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
41 #include <com/sun/star/sdbc/SQLException.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <com/sun/star/sdbc/XParameters.hpp>
44 #include <cppuhelper/exc_hlp.hxx>
46 #include "pq_xindexes.hxx"
47 #include "pq_xindex.hxx"
48 #include "pq_statics.hxx"
49 #include "pq_tools.hxx"
51 using osl::MutexGuard;
54 using com::sun::star::beans::XPropertySet;
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::Reference;
60 using com::sun::star::uno::Sequence;
62 using com::sun::star::container::XEnumerationAccess;
63 using com::sun::star::container::XEnumeration;
66 using com::sun::star::sdbcx::XColumnsSupplier;
68 using com::sun::star::sdbc::XRow;
69 using com::sun::star::sdbc::XResultSet;
70 using com::sun::star::sdbc::XParameters;
71 using com::sun::star::sdbc::XPreparedStatement;
73 namespace pq_sdbc_driver
76 Indexes::Indexes(
77 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
78 const css::uno::Reference< css::sdbc::XConnection > & origin,
79 ConnectionSettings *pSettings,
80 const OUString &schemaName,
81 const OUString &tableName)
82 : Container( refMutex, origin, pSettings, getStatics().KEY ),
83 m_schemaName( schemaName ),
84 m_tableName( tableName )
88 Indexes::~Indexes()
91 void Indexes::refresh()
93 try
95 SAL_INFO("connectivity.postgresql", "sdbcx.Indexes get refreshed for table " << m_schemaName << "." << m_tableName);
97 osl::MutexGuard guard( m_xMutex->GetMutex() );
98 Statics & st = getStatics();
100 Int2StringMap column2NameMap;
101 fillAttnum2attnameMap( column2NameMap, m_origin, m_schemaName, m_tableName );
103 // see XDatabaseMetaData::getIndexInfo()
104 Reference< XPreparedStatement > stmt = m_origin->prepareStatement(
105 "SELECT nspname, " // 1
106 "pg_class.relname, " // 2
107 "class2.relname, " // 3
108 "indisclustered, " // 4
109 "indisunique, " // 5
110 "indisprimary, " // 6
111 "indkey " // 7
112 "FROM pg_index INNER JOIN pg_class ON indrelid = pg_class.oid "
113 "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
114 "INNER JOIN pg_class as class2 ON pg_index.indexrelid = class2.oid "
115 "WHERE nspname = ? AND pg_class.relname = ?" );
117 Reference< XParameters > params( stmt, UNO_QUERY);
118 params->setString( 1, m_schemaName );
119 params->setString( 2, m_tableName );
120 Reference< XResultSet > rs = stmt->executeQuery();
122 Reference< XRow > row( rs, UNO_QUERY );
123 String2IntMap map;
124 m_values.clear();
125 sal_Int32 index = 0;
126 while( rs->next() )
128 // C_SCHEMA = 1
129 // C_TABLENAME = 2
130 static const sal_Int32 C_INDEXNAME = 3;
131 static const sal_Int32 C_IS_CLUSTERED = 4;
132 static const sal_Int32 C_IS_UNIQUE = 5;
133 static const sal_Int32 C_IS_PRIMARY = 6;
134 static const sal_Int32 C_COLUMNS = 7;
135 OUString currentIndexName = row->getString( C_INDEXNAME );
136 Index *pIndex =
137 new Index( m_xMutex, m_origin, m_pSettings,
138 m_schemaName, m_tableName );
140 bool isUnique = row->getBoolean( C_IS_UNIQUE );
141 bool isPrimary = row->getBoolean( C_IS_PRIMARY );
142 bool isClusterd = row->getBoolean( C_IS_CLUSTERED );
143 Reference< css::beans::XPropertySet > prop = pIndex;
144 pIndex->setPropertyValue_NoBroadcast_public(
145 st.IS_UNIQUE, Any( isUnique ) );
146 pIndex->setPropertyValue_NoBroadcast_public(
147 st.IS_PRIMARY_KEY_INDEX, Any( isPrimary ) );
148 pIndex->setPropertyValue_NoBroadcast_public(
149 st.IS_CLUSTERED, Any( isClusterd ) );
150 pIndex->setPropertyValue_NoBroadcast_public(
151 st.NAME, makeAny( currentIndexName ) );
153 std::vector< sal_Int32 > seq = parseIntArray( row->getString( C_COLUMNS ) );
154 Sequence< OUString > columnNames(seq.size());
155 for( size_t columns = 0 ; columns < seq.size() ; columns ++ )
157 columnNames[columns] = column2NameMap[ seq[columns] ];
160 pIndex->setPropertyValue_NoBroadcast_public(
161 st.PRIVATE_COLUMN_INDEXES, makeAny( columnNames ));
164 m_values.push_back( makeAny( prop ) );
165 map[ currentIndexName ] = index;
166 ++index;
169 m_name2index.swap( map );
171 catch ( css::sdbc::SQLException & e )
173 css::uno::Any anyEx = cppu::getCaughtException();
174 throw css::lang::WrappedTargetRuntimeException( e.Message,
175 e.Context, anyEx );
178 fire( RefreshedBroadcaster( *this ) );
182 void Indexes::appendByDescriptor(
183 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
185 Statics & st = getStatics();
186 OUString name = extractStringProperty( descriptor, st.NAME );
188 bool isUnique = extractBoolProperty( descriptor, st.IS_UNIQUE );
190 OUStringBuffer buf( 128 );
192 buf.append( "CREATE " );
193 if( isUnique )
194 buf.append( "UNIQUE " );
195 buf.append( "INDEX " );
196 bufferQuoteIdentifier( buf, name, m_pSettings );
197 buf.append( " ON " );
198 bufferQuoteQualifiedIdentifier( buf, m_schemaName, m_tableName, m_pSettings );
200 buf.append( " ( " );
202 Reference< XColumnsSupplier > columns( descriptor, UNO_QUERY );
203 if( columns.is() )
205 Reference< XEnumerationAccess > access( columns->getColumns(), UNO_QUERY );
206 if( access.is() )
208 Reference< XEnumeration > xEnum( access->createEnumeration() );
209 bool first = true;
210 while( xEnum.is() && xEnum->hasMoreElements() )
212 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
213 if( first )
215 first = false;
217 else
219 buf.append( ", " );
221 buf.append( extractStringProperty( column, st.NAME ) );
225 buf.append( " ) " );
227 m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
228 refresh();
231 void Indexes::dropByIndex( sal_Int32 index )
235 osl::MutexGuard guard( m_xMutex->GetMutex() );
236 if( index < 0 || index >= static_cast<sal_Int32>(m_values.size()) )
238 throw css::lang::IndexOutOfBoundsException(
239 "Indexes: Index out of range (allowed 0 to "
240 + OUString::number( m_values.size() -1 )
241 + ", got " + OUString::number( index )
242 + ")",
243 *this );
246 Reference< XPropertySet > set;
247 m_values[index] >>= set;
248 Statics &st = getStatics();
250 OUStringBuffer buf( 128 );
251 buf.append( "DROP INDEX " );
252 bufferQuoteIdentifier( buf, extractStringProperty( set, st.NAME ), m_pSettings );
253 m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
255 Container::dropByIndex( index );
259 css::uno::Reference< css::beans::XPropertySet > Indexes::createDataDescriptor()
261 return new IndexDescriptor( m_xMutex, m_origin, m_pSettings );
264 Reference< css::container::XNameAccess > Indexes::create(
265 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
266 const css::uno::Reference< css::sdbc::XConnection > & origin,
267 ConnectionSettings *pSettings,
268 const OUString & schemaName,
269 const OUString & tableName)
271 Indexes *pIndexes = new Indexes( refMutex, origin, pSettings, schemaName, tableName );
272 Reference< css::container::XNameAccess > ret = pIndexes;
273 pIndexes->refresh();
274 return ret;
278 IndexDescriptors::IndexDescriptors(
279 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
280 const css::uno::Reference< css::sdbc::XConnection > & origin,
281 ConnectionSettings *pSettings)
282 : Container( refMutex, origin, pSettings, getStatics().INDEX )
285 Reference< css::container::XNameAccess > IndexDescriptors::create(
286 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
287 const css::uno::Reference< css::sdbc::XConnection > & origin,
288 ConnectionSettings *pSettings)
290 return new IndexDescriptors( refMutex, origin, pSettings );
293 css::uno::Reference< css::beans::XPropertySet > IndexDescriptors::createDataDescriptor()
295 return new IndexDescriptor( m_xMutex, m_origin, m_pSettings );
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */