Use correct object
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xindexes.cxx
blob6c44c8875604ca837b2c538f9886d25ce18bc612
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/ref.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
41 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
42 #include <com/sun/star/sdbc/SQLException.hpp>
43 #include <com/sun/star/sdbc/XRow.hpp>
44 #include <com/sun/star/sdbc/XParameters.hpp>
45 #include <cppuhelper/exc_hlp.hxx>
46 #include <o3tl/safeint.hxx>
47 #include <utility>
49 #include "pq_xindexes.hxx"
50 #include "pq_xindex.hxx"
51 #include "pq_statics.hxx"
52 #include "pq_tools.hxx"
54 using osl::MutexGuard;
57 using com::sun::star::beans::XPropertySet;
59 using com::sun::star::uno::Any;
60 using com::sun::star::uno::UNO_QUERY;
61 using com::sun::star::uno::Reference;
62 using com::sun::star::uno::Sequence;
64 using com::sun::star::container::XEnumerationAccess;
65 using com::sun::star::container::XEnumeration;
68 using com::sun::star::sdbcx::XColumnsSupplier;
70 using com::sun::star::sdbc::XRow;
71 using com::sun::star::sdbc::XResultSet;
72 using com::sun::star::sdbc::XParameters;
73 using com::sun::star::sdbc::XPreparedStatement;
75 namespace pq_sdbc_driver
78 Indexes::Indexes(
79 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
80 const css::uno::Reference< css::sdbc::XConnection > & origin,
81 ConnectionSettings *pSettings,
82 OUString schemaName,
83 OUString tableName)
84 : Container( refMutex, origin, pSettings, getStatics().KEY ),
85 m_schemaName(std::move( schemaName )),
86 m_tableName(std::move( tableName ))
90 Indexes::~Indexes()
93 void Indexes::refresh()
95 try
97 SAL_INFO("connectivity.postgresql", "sdbcx.Indexes get refreshed for table " << m_schemaName << "." << m_tableName);
99 osl::MutexGuard guard( m_xMutex->GetMutex() );
100 Statics & st = getStatics();
102 Int2StringMap column2NameMap;
103 fillAttnum2attnameMap( column2NameMap, m_origin, m_schemaName, m_tableName );
105 // see XDatabaseMetaData::getIndexInfo()
106 Reference< XPreparedStatement > stmt = m_origin->prepareStatement(
107 u"SELECT nspname, " // 1
108 "pg_class.relname, " // 2
109 "class2.relname, " // 3
110 "indisclustered, " // 4
111 "indisunique, " // 5
112 "indisprimary, " // 6
113 "indkey " // 7
114 "FROM pg_index INNER JOIN pg_class ON indrelid = pg_class.oid "
115 "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
116 "INNER JOIN pg_class as class2 ON pg_index.indexrelid = class2.oid "
117 "WHERE nspname = ? AND pg_class.relname = ?"_ustr );
119 Reference< XParameters > params( stmt, UNO_QUERY);
120 params->setString( 1, m_schemaName );
121 params->setString( 2, m_tableName );
122 Reference< XResultSet > rs = stmt->executeQuery();
124 Reference< XRow > row( rs, UNO_QUERY );
125 String2IntMap map;
126 m_values.clear();
127 sal_Int32 index = 0;
128 while( rs->next() )
130 // C_SCHEMA = 1
131 // C_TABLENAME = 2
132 static const sal_Int32 C_INDEXNAME = 3;
133 static const sal_Int32 C_IS_CLUSTERED = 4;
134 static const sal_Int32 C_IS_UNIQUE = 5;
135 static const sal_Int32 C_IS_PRIMARY = 6;
136 static const sal_Int32 C_COLUMNS = 7;
137 OUString currentIndexName = row->getString( C_INDEXNAME );
138 rtl::Reference<Index> pIndex =
139 new Index( m_xMutex, m_origin, m_pSettings,
140 m_schemaName, m_tableName );
142 bool isUnique = row->getBoolean( C_IS_UNIQUE );
143 bool isPrimary = row->getBoolean( C_IS_PRIMARY );
144 bool isClusterd = row->getBoolean( C_IS_CLUSTERED );
145 Reference< css::beans::XPropertySet > prop = pIndex;
146 pIndex->setPropertyValue_NoBroadcast_public(
147 st.IS_UNIQUE, Any( isUnique ) );
148 pIndex->setPropertyValue_NoBroadcast_public(
149 st.IS_PRIMARY_KEY_INDEX, Any( isPrimary ) );
150 pIndex->setPropertyValue_NoBroadcast_public(
151 st.IS_CLUSTERED, Any( isClusterd ) );
152 pIndex->setPropertyValue_NoBroadcast_public(
153 st.NAME, Any( currentIndexName ) );
155 std::vector< sal_Int32 > seq = parseIntArray( row->getString( C_COLUMNS ) );
156 Sequence< OUString > columnNames(seq.size());
157 auto columnNamesRange = asNonConstRange(columnNames);
158 for( size_t columns = 0 ; columns < seq.size() ; columns ++ )
160 columnNamesRange[columns] = column2NameMap[ seq[columns] ];
163 pIndex->setPropertyValue_NoBroadcast_public(
164 st.PRIVATE_COLUMN_INDEXES, Any( columnNames ));
167 m_values.emplace_back(prop);
168 map[ currentIndexName ] = index;
169 ++index;
172 m_name2index.swap( map );
174 catch ( css::sdbc::SQLException & e )
176 css::uno::Any anyEx = cppu::getCaughtException();
177 throw css::lang::WrappedTargetRuntimeException( e.Message,
178 e.Context, anyEx );
181 fire( RefreshedBroadcaster( *this ) );
185 void Indexes::appendByDescriptor(
186 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
188 Statics & st = getStatics();
189 OUString name = extractStringProperty( descriptor, st.NAME );
191 bool isUnique = extractBoolProperty( descriptor, st.IS_UNIQUE );
193 OUStringBuffer buf( 128 );
195 buf.append( "CREATE " );
196 if( isUnique )
197 buf.append( "UNIQUE " );
198 buf.append( "INDEX " );
199 bufferQuoteIdentifier( buf, name, m_pSettings );
200 buf.append( " ON " );
201 bufferQuoteQualifiedIdentifier( buf, m_schemaName, m_tableName, m_pSettings );
203 buf.append( " ( " );
205 Reference< XColumnsSupplier > columns( descriptor, UNO_QUERY );
206 if( columns.is() )
208 Reference< XEnumerationAccess > access( columns->getColumns(), UNO_QUERY );
209 if( access.is() )
211 Reference< XEnumeration > xEnum( access->createEnumeration() );
212 bool first = true;
213 while( xEnum.is() && xEnum->hasMoreElements() )
215 Reference< XPropertySet > column( xEnum->nextElement(), UNO_QUERY );
216 if( first )
218 first = false;
220 else
222 buf.append( ", " );
224 buf.append( extractStringProperty( column, st.NAME ) );
228 buf.append( " ) " );
230 m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
231 refresh();
234 void Indexes::dropByIndex( sal_Int32 index )
238 osl::MutexGuard guard( m_xMutex->GetMutex() );
239 if( index < 0 || o3tl::make_unsigned(index) >= m_values.size() )
241 throw css::lang::IndexOutOfBoundsException(
242 "Indexes: Index out of range (allowed 0 to "
243 + OUString::number( m_values.size() -1 )
244 + ", got " + OUString::number( index )
245 + ")",
246 *this );
249 Reference< XPropertySet > set;
250 m_values[index] >>= set;
251 Statics &st = getStatics();
253 OUStringBuffer buf( 128 );
254 buf.append( "DROP INDEX " );
255 bufferQuoteIdentifier( buf, extractStringProperty( set, st.NAME ), m_pSettings );
256 m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
258 Container::dropByIndex( index );
262 css::uno::Reference< css::beans::XPropertySet > Indexes::createDataDescriptor()
264 return new IndexDescriptor( m_xMutex, m_origin, m_pSettings );
267 Reference< css::container::XNameAccess > Indexes::create(
268 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
269 const css::uno::Reference< css::sdbc::XConnection > & origin,
270 ConnectionSettings *pSettings,
271 const OUString & schemaName,
272 const OUString & tableName)
274 rtl::Reference<Indexes> pIndexes
275 = new Indexes( refMutex, origin, pSettings, schemaName, tableName );
276 pIndexes->refresh();
277 return pIndexes;
281 IndexDescriptors::IndexDescriptors(
282 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
283 const css::uno::Reference< css::sdbc::XConnection > & origin,
284 ConnectionSettings *pSettings)
285 : Container( refMutex, origin, pSettings, getStatics().INDEX )
288 Reference< css::container::XNameAccess > IndexDescriptors::create(
289 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
290 const css::uno::Reference< css::sdbc::XConnection > & origin,
291 ConnectionSettings *pSettings)
293 return new IndexDescriptors( refMutex, origin, pSettings );
296 css::uno::Reference< css::beans::XPropertySet > IndexDescriptors::createDataDescriptor()
298 return new IndexDescriptor( m_xMutex, m_origin, m_pSettings );
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */