LibreOffice Module connectivity (master) 1
pq_xtable.cxx
Go to the documentation of this file.
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 <rtl/ref.hxx>
38#include <rtl/ustrbuf.hxx>
39
42
43#include <com/sun/star/sdbc/SQLException.hpp>
44
45#include "pq_xtable.hxx"
46#include "pq_xtables.hxx"
47#include "pq_xviews.hxx"
48#include "pq_xindexes.hxx"
49#include "pq_xkeys.hxx"
50#include "pq_xcolumns.hxx"
51#include "pq_tools.hxx"
52#include "pq_statics.hxx"
53
54using osl::MutexGuard;
55
56using com::sun::star::container::XNameAccess;
57using com::sun::star::container::XIndexAccess;
58
60using com::sun::star::uno::UNO_QUERY;
62using com::sun::star::uno::Any;
63using com::sun::star::uno::Type;
64
66
67using com::sun::star::sdbc::XStatement;
68using com::sun::star::sdbc::SQLException;
69
70namespace pq_sdbc_driver
71{
72Table::Table( const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
73 const Reference< css::sdbc::XConnection > & connection,
74 ConnectionSettings *pSettings)
75 : ReflectionBase(
77 getStatics().refl.table.serviceNames,
78 refMutex,
79 connection,
80 pSettings,
81 * getStatics().refl.table.pProps )
82{}
83
84Reference< XPropertySet > Table::createDataDescriptor( )
85{
87 m_xMutex, m_conn, m_pSettings );
88 pTable->copyValuesFrom( this );
89
90 return Reference< XPropertySet > ( pTable );
91}
92
93Reference< XNameAccess > Table::getColumns( )
94{
95 if( ! m_columns.is() )
96 {
97 m_columns = Columns::create(
98 m_xMutex,
99 m_conn,
100 m_pSettings,
101 extractStringProperty( this, getStatics().SCHEMA_NAME ),
103 &m_pColumns);
104 }
105 return m_columns;
106}
107
108Reference< XNameAccess > Table::getIndexes()
109{
110 if( ! m_indexes.is() )
111 {
113 m_xMutex,
114 m_conn,
115 m_pSettings,
116 extractStringProperty( this, getStatics().SCHEMA_NAME ),
118 }
119 return m_indexes;
120}
121
123{
124 if( ! m_keys.is() )
125 {
127 m_xMutex,
128 m_conn,
129 m_pSettings,
130 extractStringProperty( this, getStatics().SCHEMA_NAME ),
132 }
133 return m_keys;
134}
135
136void Table::rename( const OUString& newName )
137{
138 MutexGuard guard( m_xMutex->GetMutex() );
139 Statics & st = getStatics();
140
141 OUString oldName = extractStringProperty(this,st.NAME );
142 OUString schema = extractStringProperty(this,st.SCHEMA_NAME );
143 OUString fullOldName = concatQualified( schema, oldName );
144
145 OUString newTableName;
146 OUString newSchemaName;
147 // OOo2.0 passes schema + dot + new-table-name while
148 // OO1.1.x passes new Name without schema
149 // in case name contains a dot, it is interpreted as schema.tablename
150 if( newName.indexOf( '.' ) >= 0 )
151 {
152 splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
153 }
154 else
155 {
156 newTableName = newName;
157 newSchemaName = schema;
158 }
159 OUString fullNewName = concatQualified( newSchemaName, newTableName );
160
161 if( extractStringProperty( this, st.TYPE ) == st.VIEW && m_pSettings->views.is() )
162 {
163 // maintain view list (really strange API !)
164 Any a = m_pSettings->pViewsImpl->getByName( fullOldName );
166 a >>= Xrename;
167 if( Xrename.is() )
168 {
169 Xrename->rename( newName );
170 setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, Any(newSchemaName) );
171 }
172 }
173 else
174 {
175 if( newSchemaName != schema )
176 {
177 // try new schema name first
178 try
179 {
180 OUStringBuffer buf(128);
181 buf.append( "ALTER TABLE" );
182 bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
183 buf.append( "SET SCHEMA" );
184 bufferQuoteIdentifier( buf, newSchemaName, m_pSettings );
185 Reference< XStatement > statement = m_conn->createStatement();
186 statement->executeUpdate( buf.makeStringAndClear() );
187 setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, Any(newSchemaName) );
188 disposeNoThrow( statement );
189 schema = newSchemaName;
190 }
191 catch( css::sdbc::SQLException &e )
192 {
193 OUString buf( e.Message + "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" );
194 e.Message = buf;
195 throw;
196 }
197
198 }
199 if( newTableName != oldName ) // might also be just the change of a schema name
200 {
201 OUStringBuffer buf(128);
202 buf.append( "ALTER TABLE" );
203 bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
204 buf.append( "RENAME TO" );
205 bufferQuoteIdentifier( buf, newTableName, m_pSettings );
206 Reference< XStatement > statement = m_conn->createStatement();
207 statement->executeUpdate( buf.makeStringAndClear() );
208 disposeNoThrow( statement );
209 }
210 }
211 setPropertyValue_NoBroadcast_public( st.NAME, Any(newTableName) );
212 // inform the container of the name change !
213 if( m_pSettings->tables.is() )
214 {
215 m_pSettings->pTablesImpl->rename( fullOldName, fullNewName );
216 }
217}
218
219void Table::alterColumnByName(
220 const OUString& colName,
221 const Reference< XPropertySet >& descriptor )
222{
224
225 OUString newName = extractStringProperty(descriptor, getStatics().NAME );
227 extractStringProperty( this, getStatics().SCHEMA_NAME ),
228 extractStringProperty( this, getStatics().NAME ),
229 m_pSettings,
230 m_conn->createStatement(),
231 Reference< css::beans::XPropertySet>( columns->getByName( colName ), UNO_QUERY) ,
232 descriptor );
233
234 if( colName != newName )
235 {
236// m_pColumns->rename( colName, newName );
237 m_pColumns->refresh();
238 }
239}
240
241void Table::alterColumnByIndex(
242 sal_Int32 index,
243 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
244{
245 Reference< css::container::XIndexAccess > columns( getColumns(), UNO_QUERY );
246 Reference< css::beans::XPropertySet> column(columns->getByIndex( index ), UNO_QUERY );
248 extractStringProperty( this, getStatics().SCHEMA_NAME ),
250 m_pSettings,
251 m_conn->createStatement(),
252 column,
253 descriptor );
254 m_pColumns->refresh();
255}
256
257Sequence<Type > Table::getTypes()
258{
259 static cppu::OTypeCollection collection(
265 ReflectionBase::getTypes());
266
267 return collection.getTypes();
268}
269
270Sequence< sal_Int8> Table::getImplementationId()
271{
272 return css::uno::Sequence<sal_Int8>();
273}
274
275Any Table::queryInterface( const Type & reqType )
276{
277 Any ret = ReflectionBase::queryInterface( reqType );
278 if( ! ret.hasValue() )
279 ret = ::cppu::queryInterface(
280 reqType,
281 static_cast< css::sdbcx::XIndexesSupplier * > ( this ),
282 static_cast< css::sdbcx::XKeysSupplier * > ( this ),
283 static_cast< css::sdbcx::XColumnsSupplier * > ( this ),
284 static_cast< css::sdbcx::XRename * > ( this ),
285 static_cast< css::sdbcx::XAlterTable * > ( this )
286 );
287 return ret;
288}
289
290OUString Table::getName( )
291{
292 Statics & st = getStatics();
293 return concatQualified(
295 extractStringProperty( this, st.NAME ) );
296}
297
298void Table::setName( const OUString& aName )
299{
300 rename( aName );
301}
302
303
304TableDescriptor::TableDescriptor(
305 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
306 const Reference< css::sdbc::XConnection > & connection,
307 ConnectionSettings *pSettings)
309 getStatics().refl.tableDescriptor.implName,
310 getStatics().refl.tableDescriptor.serviceNames,
311 refMutex,
312 connection,
313 pSettings,
314 * getStatics().refl.tableDescriptor.pProps )
315{
316}
317
319{
320 if( ! m_columns.is() )
321 {
323 }
324 return m_columns;
325}
326
328{
329 if( ! m_indexes.is() )
330 {
332 m_xMutex,
333 m_conn,
335 }
336 return m_indexes;
337}
338
340{
341 if( ! m_keys.is() )
342 {
344 m_xMutex,
345 m_conn,
346 m_pSettings );
347 }
348 return m_keys;
349}
350
351
353{
354 static cppu::OTypeCollection collection(
359
360 return collection.getTypes();
361}
362
364{
365 return css::uno::Sequence<sal_Int8>();
366}
367
369{
370 Any ret = ReflectionBase::queryInterface( reqType );
371 if( ! ret.hasValue() )
372 ret = ::cppu::queryInterface(
373 reqType,
374 static_cast< css::sdbcx::XIndexesSupplier * > ( this ),
375 static_cast< css::sdbcx::XKeysSupplier * > ( this ),
376 static_cast< css::sdbcx::XColumnsSupplier * > ( this ));
377 return ret;
378}
379
380
382{
385
386 // TODO: deep copies
387 pTable->m_values = m_values;
388
389 return Reference< XPropertySet > ( pTable );
390}
391
392}
393
394/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NAME
css::uno::Sequence< css::uno::Type > SAL_CALL getTypes()
static css::uno::Reference< css::container::XNameAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
static css::uno::Reference< css::container::XNameAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, const OUString &schemaName, const OUString &tableName)
static css::uno::Reference< css::container::XIndexAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xkeys.cxx:280
static css::uno::Reference< css::container::XIndexAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, const OUString &schemaName, const OUString &tableName)
Definition: pq_xkeys.cxx:260
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &reqType) override
Definition: pq_xbase.cxx:163
css::uno::Reference< css::sdbc::XConnection > m_conn
Definition: pq_xbase.hxx:62
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
Definition: pq_xbase.hxx:61
std::vector< css::uno::Any > m_values
Definition: pq_xbase.hxx:65
ConnectionSettings * m_pSettings
Definition: pq_xbase.hxx:63
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: pq_xbase.cxx:152
virtual css::uno::Reference< css::container::XIndexAccess > SAL_CALL getKeys() override
Definition: pq_xtable.cxx:339
TableDescriptor(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &connection, ConnectionSettings *pSettings)
Definition: pq_xtable.cxx:304
css::uno::Reference< css::container::XNameAccess > m_columns
Definition: pq_xtable.hxx:120
css::uno::Reference< css::container::XNameAccess > m_indexes
Definition: pq_xtable.hxx:122
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getIndexes() override
Definition: pq_xtable.cxx:327
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
Definition: pq_xtable.cxx:381
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: pq_xtable.cxx:352
css::uno::Reference< css::container::XIndexAccess > m_keys
Definition: pq_xtable.hxx:121
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
Definition: pq_xtable.cxx:363
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getColumns() override
Definition: pq_xtable.cxx:318
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &reqType) override
Definition: pq_xtable.cxx:368
Table(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &connection, ConnectionSettings *pSettings)
OUString aName
uno_Any a
@ table
class SAL_NO_VTABLE XPropertySet
Type
OUString newName(std::u16string_view aNewPrefix, std::u16string_view aOldPrefix, std::u16string_view old_Name)
index
Statics & getStatics()
Definition: pq_statics.cxx:112
void bufferQuoteQualifiedIdentifier(OUStringBuffer &buf, std::u16string_view schema, std::u16string_view table, ConnectionSettings *settings)
Definition: pq_tools.cxx:181
OUString concatQualified(std::u16string_view a, std::u16string_view b)
Definition: pq_tools.cxx:90
OUString extractStringProperty(const Reference< XPropertySet > &descriptor, const OUString &name)
Definition: pq_tools.cxx:204
void splitConcatenatedIdentifier(std::u16string_view source, OUString *first, OUString *second)
Definition: pq_tools.cxx:548
void disposeNoThrow(const css::uno::Reference< css::uno::XInterface > &r)
Definition: pq_tools.cxx:235
void bufferQuoteIdentifier(OUStringBuffer &buf, std::u16string_view toQuote, ConnectionSettings *settings)
Definition: pq_tools.cxx:175
void alterColumnByDescriptor(std::u16string_view schemaName, std::u16string_view tableName, ConnectionSettings *settings, const Reference< XStatement > &stmt, const css::uno::Reference< css::beans::XPropertySet > &past, const css::uno::Reference< css::beans::XPropertySet > &future)
rtl::Reference< Connection > m_conn
Columns columns
const char * implName