1 /*************************************************************************
3 * $RCSfile: pq_xindexcolumns.cxx,v $
7 * last change: $Author: jbu $ $Date: 2004/08/29 08:33:31 $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Joerg Budischewski
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Joerg Budischewski
60 ************************************************************************/
64 #include <rtl/ustrbuf.hxx>
65 #include <rtl/strbuf.hxx>
67 #include <com/sun/star/sdbc/XRow.hpp>
68 #include <com/sun/star/sdbc/XParameters.hpp>
69 #include <com/sun/star/sdbc/DataType.hpp>
70 #include <com/sun/star/sdbc/ColumnValue.hpp>
72 #include "pq_xcolumns.hxx"
73 #include "pq_xindexcolumns.hxx"
74 #include "pq_xindexcolumn.hxx"
75 #include "pq_statics.hxx"
76 #include "pq_tools.hxx"
78 using osl::MutexGuard
;
81 using rtl::OUStringBuffer
;
82 using rtl::OUStringToOString
;
84 using com::sun::star::beans::XPropertySet
;
86 using com::sun::star::uno::Any
;
87 using com::sun::star::uno::makeAny
;
88 using com::sun::star::uno::UNO_QUERY
;
89 using com::sun::star::uno::Type
;
90 using com::sun::star::uno::XInterface
;
91 using com::sun::star::uno::Reference
;
92 using com::sun::star::uno::Sequence
;
93 using com::sun::star::uno::RuntimeException
;
95 using com::sun::star::container::NoSuchElementException
;
96 using com::sun::star::lang::WrappedTargetException
;
98 using com::sun::star::sdbc::XRow
;
99 using com::sun::star::sdbc::XCloseable
;
100 using com::sun::star::sdbc::XStatement
;
101 using com::sun::star::sdbc::XResultSet
;
102 using com::sun::star::sdbc::XParameters
;
103 using com::sun::star::sdbc::XPreparedStatement
;
104 using com::sun::star::sdbc::XDatabaseMetaData
;
105 using com::sun::star::sdbc::SQLException
;
107 namespace pq_sdbc_driver
109 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
111 IndexColumns::IndexColumns(
112 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
113 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
114 ConnectionSettings
*pSettings
,
115 const rtl::OUString
&schemaName
,
116 const rtl::OUString
&tableName
,
117 const rtl::OUString
&indexName
,
118 const com::sun::star::uno::Sequence
< rtl::OUString
> &columns
)
119 : Container( refMutex
, origin
, pSettings
, ASCII_STR( "INDEX_COLUMN" ) ),
120 m_schemaName( schemaName
),
121 m_tableName( tableName
),
122 m_columns( columns
),
123 m_indexName( indexName
)
126 IndexColumns::~IndexColumns()
129 static sal_Int32
findInSequence( const Sequence
< rtl::OUString
> & seq
, const rtl::OUString
&str
)
132 for( index
= 0 ; index
< seq
.getLength() ; index
++ )
134 if( str
== seq
[index
] )
140 void IndexColumns::refresh()
141 throw (::com::sun::star::uno::RuntimeException
)
145 if( isLog( m_pSettings
, LogLevel::INFO
) )
147 rtl::OStringBuffer buf
;
148 buf
.append( "sdbcx.IndexColumns get refreshed for index " );
149 buf
.append( OUStringToOString( m_indexName
, m_pSettings
->encoding
) );
150 log( m_pSettings
, LogLevel::INFO
, buf
.makeStringAndClear().getStr() );
153 osl::MutexGuard
guard( m_refMutex
->mutex
);
155 Statics
&st
= getStatics();
156 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
158 Reference
< XResultSet
> rs
=
159 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
161 DisposeGuard
disposeIt( rs
);
162 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
163 m_values
= Sequence
< Any
>( m_columns
.getLength() );
167 OUString columnName
= xRow
->getString( 4 );
169 sal_Int32 index
= findInSequence( m_columns
, columnName
);
170 if( index
>= m_columns
.getLength() )
173 IndexColumn
* pIndexColumn
=
174 new IndexColumn( m_refMutex
, m_origin
, m_pSettings
);
175 Reference
< com::sun::star::beans::XPropertySet
> prop
= pIndexColumn
;
177 columnMetaData2SDBCX( pIndexColumn
, xRow
);
178 pIndexColumn
->setPropertyValue_NoBroadcast_public(
179 st
.IS_ASCENDING
, makeAny( (sal_Bool
) sal_False
) );
181 m_values
[ index
] = makeAny( prop
);
182 m_name2index
[ columnName
] = index
;
185 catch ( com::sun::star::sdbc::SQLException
& e
)
187 throw RuntimeException( e
.Message
, e
.Context
);
190 fire( RefreshedBroadcaster( *this ) );
194 void IndexColumns::appendByDescriptor(
195 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& future
)
196 throw (::com::sun::star::sdbc::SQLException
,
197 ::com::sun::star::container::ElementExistException
,
198 ::com::sun::star::uno::RuntimeException
)
200 OUString name
= extractStringProperty( future
, getStatics().NAME
);
201 throw com::sun::star::sdbc::SQLException(
202 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.appendByDescriptor not yet implemented" ),
203 *this, OUString(), 1, Any() );
204 // osl::MutexGuard guard( m_refMutex->mutex );
205 // Statics & st = getStatics();
206 // Reference< XPropertySet > past = createDataDescriptor();
207 // past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
208 // alterColumnByDescriptor(
209 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
213 void IndexColumns::dropByName( const ::rtl::OUString
& elementName
)
214 throw (::com::sun::star::sdbc::SQLException
,
215 ::com::sun::star::container::NoSuchElementException
,
216 ::com::sun::star::uno::RuntimeException
)
218 throw com::sun::star::sdbc::SQLException(
219 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByName not yet implemented" ),
220 *this, OUString(), 1, Any() );
221 // String2IntMap::const_iterator ii = m_name2index.find( elementName );
222 // if( ii == m_name2index.end() )
224 // OUStringBuffer buf( 128 );
225 // buf.appendAscii( "Column " );
226 // buf.append( elementName );
227 // buf.appendAscii( " is unknown in table " );
228 // buf.append( m_schemaName );
229 // buf.appendAscii( "." );
230 // buf.append( m_tableName );
231 // buf.appendAscii( ", so it can't be dropped" );
232 // throw com::sun::star::container::NoSuchElementException(
233 // buf.makeStringAndClear(), *this );
235 // dropByIndex( ii->second );
238 void IndexColumns::dropByIndex( sal_Int32 index
)
239 throw (::com::sun::star::sdbc::SQLException
,
240 ::com::sun::star::lang::IndexOutOfBoundsException
,
241 ::com::sun::star::uno::RuntimeException
)
243 throw com::sun::star::sdbc::SQLException(
244 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByIndex not yet implemented" ),
245 *this, OUString(), 1, Any() );
246 // osl::MutexGuard guard( m_refMutex->mutex );
247 // if( index < 0 || index >= m_values.getLength() )
249 // OUStringBuffer buf( 128 );
250 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
251 // buf.append((sal_Int32)(m_values.getLength() -1) );
252 // buf.appendAscii( ", got " );
253 // buf.append( index );
254 // buf.appendAscii( ")" );
255 // throw com::sun::star::lang::IndexOutOfBoundsException(
256 // buf.makeStringAndClear(), *this );
259 // Reference< XPropertySet > set;
260 // m_values[index] >>= set;
261 // Statics &st = getStatics();
263 // set->getPropertyValue( st.NAME ) >>= name;
265 // OUStringBuffer update( 128 );
266 // update.appendAscii( "ALTER TABLE ONLY");
267 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
268 // update.appendAscii( "DROP COLUMN" );
269 // bufferQuoteIdentifier( update, name );
270 // Reference< XStatement > stmt = m_origin->createStatement( );
271 // DisposeGuard disposeIt( stmt );
272 // stmt->executeUpdate( update.makeStringAndClear() );
277 Reference
< ::com::sun::star::beans::XPropertySet
> IndexColumns::createDataDescriptor()
278 throw (::com::sun::star::uno::RuntimeException
)
280 return new IndexColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);
283 Reference
< com::sun::star::container::XNameAccess
> IndexColumns::create(
284 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
285 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
286 ConnectionSettings
*pSettings
,
287 const rtl::OUString
&schemaName
,
288 const rtl::OUString
&tableName
,
289 const rtl::OUString
&indexName
,
290 const Sequence
< rtl::OUString
> &columns
)
292 IndexColumns
*pIndexColumns
= new IndexColumns(
293 refMutex
, origin
, pSettings
, schemaName
, tableName
, indexName
, columns
);
294 Reference
< com::sun::star::container::XNameAccess
> ret
= pIndexColumns
;
295 pIndexColumns
->refresh();
300 //_________________________________________________________________________________________
301 IndexColumnDescriptors::IndexColumnDescriptors(
302 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
303 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
304 ConnectionSettings
*pSettings
)
305 : Container( refMutex
, origin
, pSettings
, getStatics().INDEX_COLUMN
)
308 Reference
< com::sun::star::container::XNameAccess
> IndexColumnDescriptors::create(
309 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
310 const ::com::sun::star::uno::Reference
< com::sun::star::sdbc::XConnection
> & origin
,
311 ConnectionSettings
*pSettings
)
313 return new IndexColumnDescriptors( refMutex
, origin
, pSettings
);
316 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> IndexColumnDescriptors::createDataDescriptor()
317 throw (::com::sun::star::uno::RuntimeException
)
319 return new IndexColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);