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 <vector>
59 : :
60 : : #include <rtl/ustrbuf.hxx>
61 : : #include <rtl/strbuf.hxx>
62 : :
63 : : #include <com/sun/star/sdbc/XRow.hpp>
64 : : #include <com/sun/star/sdbc/XParameters.hpp>
65 : : #include <com/sun/star/sdbc/DataType.hpp>
66 : : #include <com/sun/star/sdbc/ColumnValue.hpp>
67 : :
68 : : #include "pq_xcolumns.hxx"
69 : : #include "pq_xindexcolumns.hxx"
70 : : #include "pq_xindexcolumn.hxx"
71 : : #include "pq_statics.hxx"
72 : : #include "pq_tools.hxx"
73 : :
74 : : using osl::MutexGuard;
75 : :
76 : : using rtl::OUString;
77 : : using rtl::OUStringBuffer;
78 : : using rtl::OUStringToOString;
79 : :
80 : : using com::sun::star::beans::XPropertySet;
81 : :
82 : : using com::sun::star::uno::Any;
83 : : using com::sun::star::uno::makeAny;
84 : : using com::sun::star::uno::UNO_QUERY;
85 : : using com::sun::star::uno::Type;
86 : : using com::sun::star::uno::XInterface;
87 : : using com::sun::star::uno::Reference;
88 : : using com::sun::star::uno::Sequence;
89 : : using com::sun::star::uno::RuntimeException;
90 : :
91 : : using com::sun::star::container::NoSuchElementException;
92 : : using com::sun::star::lang::WrappedTargetException;
93 : :
94 : : using com::sun::star::sdbc::XRow;
95 : : using com::sun::star::sdbc::XCloseable;
96 : : using com::sun::star::sdbc::XStatement;
97 : : using com::sun::star::sdbc::XResultSet;
98 : : using com::sun::star::sdbc::XParameters;
99 : : using com::sun::star::sdbc::XPreparedStatement;
100 : : using com::sun::star::sdbc::XDatabaseMetaData;
101 : : using com::sun::star::sdbc::SQLException;
102 : :
103 : : namespace pq_sdbc_driver
104 : : {
105 : : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
106 : :
107 : 0 : IndexColumns::IndexColumns(
108 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
109 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
110 : : ConnectionSettings *pSettings,
111 : : const rtl::OUString &schemaName,
112 : : const rtl::OUString &tableName,
113 : : const rtl::OUString &indexName,
114 : : const com::sun::star::uno::Sequence< rtl::OUString > &columns )
115 : : : Container( refMutex, origin, pSettings, ASCII_STR( "INDEX_COLUMN" ) ),
116 : : m_schemaName( schemaName ),
117 : : m_tableName( tableName ),
118 : : m_indexName( indexName ),
119 : 0 : m_columns( columns )
120 : 0 : {}
121 : :
122 : 0 : IndexColumns::~IndexColumns()
123 : 0 : {}
124 : :
125 : 0 : static sal_Int32 findInSequence( const Sequence< rtl::OUString > & seq , const rtl::OUString &str)
126 : : {
127 : : int index;
128 : 0 : for( index = 0 ; index < seq.getLength() ; index ++ )
129 : : {
130 : 0 : if( str == seq[index] )
131 : 0 : break;
132 : : }
133 : 0 : return index;
134 : : }
135 : :
136 : 0 : void IndexColumns::refresh()
137 : : throw (::com::sun::star::uno::RuntimeException)
138 : : {
139 : : try
140 : : {
141 : 0 : if( isLog( m_pSettings, LogLevel::INFO ) )
142 : : {
143 : 0 : rtl::OStringBuffer buf;
144 : 0 : buf.append( "sdbcx.IndexColumns get refreshed for index " );
145 : 0 : buf.append( OUStringToOString( m_indexName, m_pSettings->encoding ) );
146 : 0 : log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
147 : : }
148 : :
149 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
150 : :
151 : 0 : Statics &st = getStatics();
152 : 0 : Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
153 : :
154 : : Reference< XResultSet > rs =
155 : 0 : meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
156 : :
157 : 0 : DisposeGuard disposeIt( rs );
158 : 0 : Reference< XRow > xRow( rs , UNO_QUERY );
159 : 0 : m_values = Sequence< Any >( m_columns.getLength() );
160 : :
161 : 0 : while( rs->next() )
162 : : {
163 : 0 : OUString columnName = xRow->getString( 4 );
164 : :
165 : 0 : sal_Int32 index = findInSequence( m_columns, columnName );
166 : 0 : if( index >= m_columns.getLength() )
167 : 0 : continue;
168 : :
169 : : IndexColumn * pIndexColumn =
170 : 0 : new IndexColumn( m_refMutex, m_origin, m_pSettings );
171 : 0 : Reference< com::sun::star::beans::XPropertySet > prop = pIndexColumn;
172 : :
173 : 0 : columnMetaData2SDBCX( pIndexColumn, xRow );
174 : : pIndexColumn->setPropertyValue_NoBroadcast_public(
175 : 0 : st.IS_ASCENDING , makeAny( (sal_Bool ) sal_False ) );
176 : :
177 : 0 : m_values[ index ] = makeAny( prop );
178 : 0 : m_name2index[ columnName ] = index;
179 : 0 : }
180 : : }
181 : 0 : catch ( com::sun::star::sdbc::SQLException & e )
182 : : {
183 : 0 : throw RuntimeException( e.Message , e.Context );
184 : : }
185 : :
186 : 0 : fire( RefreshedBroadcaster( *this ) );
187 : 0 : }
188 : :
189 : :
190 : 0 : void IndexColumns::appendByDescriptor(
191 : : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& future )
192 : : throw (::com::sun::star::sdbc::SQLException,
193 : : ::com::sun::star::container::ElementExistException,
194 : : ::com::sun::star::uno::RuntimeException)
195 : : {
196 : 0 : OUString name = extractStringProperty( future, getStatics().NAME );
197 : : throw com::sun::star::sdbc::SQLException(
198 : : ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.appendByDescriptor not yet implemented" ),
199 : 0 : *this, OUString(), 1, Any() );
200 : : // osl::MutexGuard guard( m_refMutex->mutex );
201 : : // Statics & st = getStatics();
202 : : // Reference< XPropertySet > past = createDataDescriptor();
203 : : // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
204 : : // alterColumnByDescriptor(
205 : : // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
206 : :
207 : : }
208 : :
209 : 0 : void IndexColumns::dropByName( const ::rtl::OUString& elementName )
210 : : throw (::com::sun::star::sdbc::SQLException,
211 : : ::com::sun::star::container::NoSuchElementException,
212 : : ::com::sun::star::uno::RuntimeException)
213 : : {
214 : : (void) elementName;
215 : : throw com::sun::star::sdbc::SQLException(
216 : : ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByName not yet implemented" ),
217 : 0 : *this, OUString(), 1, Any() );
218 : : // String2IntMap::const_iterator ii = m_name2index.find( elementName );
219 : : // if( ii == m_name2index.end() )
220 : : // {
221 : : // OUStringBuffer buf( 128 );
222 : : // buf.appendAscii( "Column " );
223 : : // buf.append( elementName );
224 : : // buf.appendAscii( " is unknown in table " );
225 : : // buf.append( m_schemaName );
226 : : // buf.appendAscii( "." );
227 : : // buf.append( m_tableName );
228 : : // buf.appendAscii( ", so it can't be dropped" );
229 : : // throw com::sun::star::container::NoSuchElementException(
230 : : // buf.makeStringAndClear(), *this );
231 : : // }
232 : : // dropByIndex( ii->second );
233 : : }
234 : :
235 : 0 : void IndexColumns::dropByIndex( sal_Int32 index )
236 : : throw (::com::sun::star::sdbc::SQLException,
237 : : ::com::sun::star::lang::IndexOutOfBoundsException,
238 : : ::com::sun::star::uno::RuntimeException)
239 : : {
240 : : (void) index;
241 : : throw com::sun::star::sdbc::SQLException(
242 : : ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByIndex not yet implemented" ),
243 : 0 : *this, OUString(), 1, Any() );
244 : : // osl::MutexGuard guard( m_refMutex->mutex );
245 : : // if( index < 0 || index >= m_values.getLength() )
246 : : // {
247 : : // OUStringBuffer buf( 128 );
248 : : // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
249 : : // buf.append((sal_Int32)(m_values.getLength() -1) );
250 : : // buf.appendAscii( ", got " );
251 : : // buf.append( index );
252 : : // buf.appendAscii( ")" );
253 : : // throw com::sun::star::lang::IndexOutOfBoundsException(
254 : : // buf.makeStringAndClear(), *this );
255 : : // }
256 : :
257 : : // Reference< XPropertySet > set;
258 : : // m_values[index] >>= set;
259 : : // Statics &st = getStatics();
260 : : // OUString name;
261 : : // set->getPropertyValue( st.NAME ) >>= name;
262 : :
263 : : // OUStringBuffer update( 128 );
264 : : // update.appendAscii( "ALTER TABLE ONLY");
265 : : // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
266 : : // update.appendAscii( "DROP COLUMN" );
267 : : // bufferQuoteIdentifier( update, name );
268 : : // Reference< XStatement > stmt = m_origin->createStatement( );
269 : : // DisposeGuard disposeIt( stmt );
270 : : // stmt->executeUpdate( update.makeStringAndClear() );
271 : :
272 : : }
273 : :
274 : :
275 : 0 : Reference< ::com::sun::star::beans::XPropertySet > IndexColumns::createDataDescriptor()
276 : : throw (::com::sun::star::uno::RuntimeException)
277 : : {
278 : 0 : return new IndexColumnDescriptor( m_refMutex, m_origin, m_pSettings );
279 : : }
280 : :
281 : 0 : Reference< com::sun::star::container::XNameAccess > IndexColumns::create(
282 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
283 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
284 : : ConnectionSettings *pSettings,
285 : : const rtl::OUString &schemaName,
286 : : const rtl::OUString &tableName,
287 : : const rtl::OUString &indexName,
288 : : const Sequence< rtl::OUString > &columns )
289 : : {
290 : : IndexColumns *pIndexColumns = new IndexColumns(
291 : 0 : refMutex, origin, pSettings, schemaName, tableName, indexName, columns );
292 : 0 : Reference< com::sun::star::container::XNameAccess > ret = pIndexColumns;
293 : 0 : pIndexColumns->refresh();
294 : :
295 : 0 : return ret;
296 : : }
297 : :
298 : : //_________________________________________________________________________________________
299 : 0 : IndexColumnDescriptors::IndexColumnDescriptors(
300 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
301 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
302 : : ConnectionSettings *pSettings)
303 : 0 : : Container( refMutex, origin, pSettings, getStatics().INDEX_COLUMN )
304 : 0 : {}
305 : :
306 : 0 : Reference< com::sun::star::container::XNameAccess > IndexColumnDescriptors::create(
307 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
308 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
309 : : ConnectionSettings *pSettings)
310 : : {
311 : 0 : return new IndexColumnDescriptors( refMutex, origin, pSettings );
312 : : }
313 : :
314 : 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > IndexColumnDescriptors::createDataDescriptor()
315 : : throw (::com::sun::star::uno::RuntimeException)
316 : : {
317 : 0 : return new IndexColumnDescriptor( m_refMutex, m_origin, m_pSettings );
318 : : }
319 : :
320 : : };
|