tdf#144694 In direct SQL dialog, activate options 'Run SQL command
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xindexcolumns.cxx
blob5237f64e9b453cfc95749818661b73624e2cb10b
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>
39 #include <string_view>
40 #include <utility>
41 #include <vector>
43 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
44 #include <com/sun/star/sdbc/SQLException.hpp>
45 #include <com/sun/star/sdbc/XRow.hpp>
46 #include <cppuhelper/exc_hlp.hxx>
47 #include <rtl/ref.hxx>
49 #include "pq_xcolumns.hxx"
50 #include "pq_xindexcolumns.hxx"
51 #include "pq_xindexcolumn.hxx"
52 #include "pq_statics.hxx"
53 #include "pq_tools.hxx"
55 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::sdbc::XRow;
65 using com::sun::star::sdbc::XResultSet;
66 using com::sun::star::sdbc::XDatabaseMetaData;
67 using com::sun::star::sdbc::SQLException;
69 namespace pq_sdbc_driver
72 IndexColumns::IndexColumns(
73 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
74 const css::uno::Reference< css::sdbc::XConnection > & origin,
75 ConnectionSettings *pSettings,
76 OUString schemaName,
77 OUString tableName,
78 OUString indexName,
79 const css::uno::Sequence< OUString > &columns )
80 : Container( refMutex, origin, pSettings, u"INDEX_COLUMN"_ustr ),
81 m_schemaName(std::move( schemaName )),
82 m_tableName(std::move( tableName )),
83 m_indexName(std::move( indexName )),
84 m_columns( columns )
87 IndexColumns::~IndexColumns()
90 static sal_Int32 findInSequence( const Sequence< OUString > & seq , std::u16string_view str)
92 int index;
93 for( index = 0 ; index < seq.getLength() ; index ++ )
95 if( str == seq[index] )
96 break;
98 return index;
101 void IndexColumns::refresh()
105 SAL_INFO("connectivity.postgresql", "sdbcx.IndexColumns get refreshed for index " << m_indexName);
107 osl::MutexGuard guard( m_xMutex->GetMutex() );
109 Statics &st = getStatics();
110 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
112 Reference< XResultSet > rs =
113 meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
115 DisposeGuard disposeIt( rs );
116 Reference< XRow > xRow( rs , UNO_QUERY );
117 m_values.clear();
118 m_values.resize( m_columns.getLength() );
120 while( rs->next() )
122 OUString columnName = xRow->getString( 4 );
124 sal_Int32 index = findInSequence( m_columns, columnName );
125 if( index >= m_columns.getLength() )
126 continue;
128 rtl::Reference<IndexColumn> pIndexColumn =
129 new IndexColumn( m_xMutex, m_origin, m_pSettings );
130 Reference< css::beans::XPropertySet > prop = pIndexColumn;
132 columnMetaData2SDBCX( pIndexColumn.get(), xRow );
133 pIndexColumn->setPropertyValue_NoBroadcast_public(
134 st.IS_ASCENDING , Any( false ) );
136 m_values[ index ] <<= prop;
137 m_name2index[ columnName ] = index;
140 catch ( css::sdbc::SQLException & e )
142 css::uno::Any anyEx = cppu::getCaughtException();
143 throw css::lang::WrappedTargetRuntimeException( e.Message,
144 e.Context, anyEx );
147 fire( RefreshedBroadcaster( *this ) );
151 void IndexColumns::appendByDescriptor(
152 const css::uno::Reference< css::beans::XPropertySet >& /*future*/ )
154 throw css::sdbc::SQLException(
155 u"SDBC-POSTGRESQL: IndexesColumns.appendByDescriptor not yet implemented"_ustr,
156 *this, OUString(), 1, Any() );
157 // osl::MutexGuard guard( m_xMutex->GetMutex() );
158 // Statics & st = getStatics();
159 // Reference< XPropertySet > past = createDataDescriptor();
160 // past->setPropertyValue( st.IS_NULLABLE, makeAny( css::sdbc::ColumnValue::NULLABLE ) );
161 // alterColumnByDescriptor(
162 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
166 void IndexColumns::dropByName( const OUString& )
168 throw css::sdbc::SQLException(
169 u"SDBC-POSTGRESQL: IndexesColumns.dropByName not yet implemented"_ustr,
170 *this, OUString(), 1, Any() );
171 // String2IntMap::const_iterator ii = m_name2index.find( elementName );
172 // if( ii == m_name2index.end() )
173 // {
174 // OUStringBuffer buf( 128 );
175 // buf.appendAscii( "Column " );
176 // buf.append( elementName );
177 // buf.appendAscii( " is unknown in table " );
178 // buf.append( m_schemaName );
179 // buf.appendAscii( "." );
180 // buf.append( m_tableName );
181 // buf.appendAscii( ", so it can't be dropped" );
182 // throw css::container::NoSuchElementException(
183 // buf.makeStringAndClear(), *this );
184 // }
185 // dropByIndex( ii->second );
188 void IndexColumns::dropByIndex( sal_Int32 )
190 throw css::sdbc::SQLException(
191 u"SDBC-POSTGRESQL: IndexesColumns.dropByIndex not yet implemented"_ustr,
192 *this, OUString(), 1, Any() );
193 // osl::MutexGuard guard( m_xMutex->GetMutex() );
194 // if( index < 0 || index >= m_values.getLength() )
195 // {
196 // OUStringBuffer buf( 128 );
197 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
198 // buf.append((sal_Int32)(m_values.getLength() -1) );
199 // buf.appendAscii( ", got " );
200 // buf.append( index );
201 // buf.appendAscii( ")" );
202 // throw css::lang::IndexOutOfBoundsException(
203 // buf.makeStringAndClear(), *this );
204 // }
206 // Reference< XPropertySet > set;
207 // m_values[index] >>= set;
208 // Statics &st = getStatics();
209 // OUString name;
210 // set->getPropertyValue( st.NAME ) >>= name;
212 // OUStringBuffer update( 128 );
213 // update.appendAscii( "ALTER TABLE ONLY");
214 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
215 // update.appendAscii( "DROP COLUMN" );
216 // bufferQuoteIdentifier( update, name );
217 // Reference< XStatement > stmt = m_origin->createStatement( );
218 // DisposeGuard disposeIt( stmt );
219 // stmt->executeUpdate( update.makeStringAndClear() );
224 Reference< css::beans::XPropertySet > IndexColumns::createDataDescriptor()
226 return new IndexColumnDescriptor( m_xMutex, m_origin, m_pSettings );
229 Reference< css::container::XNameAccess > IndexColumns::create(
230 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
231 const css::uno::Reference< css::sdbc::XConnection > & origin,
232 ConnectionSettings *pSettings,
233 const OUString &schemaName,
234 const OUString &tableName,
235 const OUString &indexName,
236 const Sequence< OUString > &columns )
238 rtl::Reference<IndexColumns> pIndexColumns = new IndexColumns(
239 refMutex, origin, pSettings, schemaName, tableName, indexName, columns );
240 pIndexColumns->refresh();
242 return pIndexColumns;
246 IndexColumnDescriptors::IndexColumnDescriptors(
247 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
248 const css::uno::Reference< css::sdbc::XConnection > & origin,
249 ConnectionSettings *pSettings)
250 : Container( refMutex, origin, pSettings, getStatics().INDEX_COLUMN )
253 Reference< css::container::XNameAccess > IndexColumnDescriptors::create(
254 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
255 const css::uno::Reference< css::sdbc::XConnection > & origin,
256 ConnectionSettings *pSettings)
258 return new IndexColumnDescriptors( refMutex, origin, pSettings );
261 css::uno::Reference< css::beans::XPropertySet > IndexColumnDescriptors::createDataDescriptor()
263 return new IndexColumnDescriptor( m_xMutex, m_origin, m_pSettings );
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */