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