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,
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 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
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/
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
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
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.
56 ************************************************************************/
60 #include <rtl/ustrbuf.hxx>
61 #include <rtl/strbuf.hxx>
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>
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"
74 using osl::MutexGuard
;
77 using rtl::OUStringBuffer
;
78 using rtl::OUStringToOString
;
80 using com::sun::star::beans::XPropertySet
;
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
;
91 using com::sun::star::container::NoSuchElementException
;
92 using com::sun::star::lang::WrappedTargetException
;
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
;
103 namespace pq_sdbc_driver
105 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
107 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
),
122 IndexColumns::~IndexColumns()
125 static sal_Int32
findInSequence( const Sequence
< rtl::OUString
> & seq
, const rtl::OUString
&str
)
128 for( index
= 0 ; index
< seq
.getLength() ; index
++ )
130 if( str
== seq
[index
] )
136 void IndexColumns::refresh()
137 throw (::com::sun::star::uno::RuntimeException
)
141 if( isLog( m_pSettings
, LogLevel::INFO
) )
143 rtl::OStringBuffer buf
;
144 buf
.append( "sdbcx.IndexColumns get refreshed for index " );
145 buf
.append( OUStringToOString( m_indexName
, m_pSettings
->encoding
) );
146 log( m_pSettings
, LogLevel::INFO
, buf
.makeStringAndClear().getStr() );
149 osl::MutexGuard
guard( m_refMutex
->mutex
);
151 Statics
&st
= getStatics();
152 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
154 Reference
< XResultSet
> rs
=
155 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
157 DisposeGuard
disposeIt( rs
);
158 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
159 m_values
= Sequence
< Any
>( m_columns
.getLength() );
163 OUString columnName
= xRow
->getString( 4 );
165 sal_Int32 index
= findInSequence( m_columns
, columnName
);
166 if( index
>= m_columns
.getLength() )
169 IndexColumn
* pIndexColumn
=
170 new IndexColumn( m_refMutex
, m_origin
, m_pSettings
);
171 Reference
< com::sun::star::beans::XPropertySet
> prop
= pIndexColumn
;
173 columnMetaData2SDBCX( pIndexColumn
, xRow
);
174 pIndexColumn
->setPropertyValue_NoBroadcast_public(
175 st
.IS_ASCENDING
, makeAny( (sal_Bool
) sal_False
) );
177 m_values
[ index
] = makeAny( prop
);
178 m_name2index
[ columnName
] = index
;
181 catch ( com::sun::star::sdbc::SQLException
& e
)
183 throw RuntimeException( e
.Message
, e
.Context
);
186 fire( RefreshedBroadcaster( *this ) );
190 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
)
196 OUString name
= extractStringProperty( future
, getStatics().NAME
);
197 throw com::sun::star::sdbc::SQLException(
198 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.appendByDescriptor not yet implemented" ),
199 *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 );
209 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
)
215 throw com::sun::star::sdbc::SQLException(
216 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByName not yet implemented" ),
217 *this, OUString(), 1, Any() );
218 // String2IntMap::const_iterator ii = m_name2index.find( elementName );
219 // if( ii == m_name2index.end() )
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 );
232 // dropByIndex( ii->second );
235 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
)
241 throw com::sun::star::sdbc::SQLException(
242 ASCII_STR( "SDBC-POSTGRESQL: IndexesColumns.dropByIndex not yet implemented" ),
243 *this, OUString(), 1, Any() );
244 // osl::MutexGuard guard( m_refMutex->mutex );
245 // if( index < 0 || index >= m_values.getLength() )
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 );
257 // Reference< XPropertySet > set;
258 // m_values[index] >>= set;
259 // Statics &st = getStatics();
261 // set->getPropertyValue( st.NAME ) >>= name;
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() );
275 Reference
< ::com::sun::star::beans::XPropertySet
> IndexColumns::createDataDescriptor()
276 throw (::com::sun::star::uno::RuntimeException
)
278 return new IndexColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);
281 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
)
290 IndexColumns
*pIndexColumns
= new IndexColumns(
291 refMutex
, origin
, pSettings
, schemaName
, tableName
, indexName
, columns
);
292 Reference
< com::sun::star::container::XNameAccess
> ret
= pIndexColumns
;
293 pIndexColumns
->refresh();
298 //_________________________________________________________________________________________
299 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 : Container( refMutex
, origin
, pSettings
, getStatics().INDEX_COLUMN
)
306 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
)
311 return new IndexColumnDescriptors( refMutex
, origin
, pSettings
);
314 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> IndexColumnDescriptors::createDataDescriptor()
315 throw (::com::sun::star::uno::RuntimeException
)
317 return new IndexColumnDescriptor( m_refMutex
, m_origin
, m_pSettings
);