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 * 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/.
35 ************************************************************************/
37 #include <rtl/ref.hxx>
38 #include <rtl/ustrbuf.hxx>
40 #include <cppuhelper/typeprovider.hxx>
41 #include <cppuhelper/queryinterface.hxx>
43 #include <com/sun/star/sdbc/SQLException.hpp>
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"
54 using osl::MutexGuard
;
56 using com::sun::star::container::XNameAccess
;
57 using com::sun::star::container::XIndexAccess
;
59 using com::sun::star::uno::Reference
;
60 using com::sun::star::uno::UNO_QUERY
;
61 using com::sun::star::uno::Sequence
;
62 using com::sun::star::uno::Any
;
63 using com::sun::star::uno::Type
;
65 using com::sun::star::beans::XPropertySet
;
67 using com::sun::star::sdbc::XStatement
;
68 using com::sun::star::sdbc::SQLException
;
70 namespace pq_sdbc_driver
72 Table::Table( const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
73 const Reference
< css::sdbc::XConnection
> & connection
,
74 ConnectionSettings
*pSettings
)
76 getStatics().refl
.table
.implName
,
77 getStatics().refl
.table
.serviceNames
,
81 * getStatics().refl
.table
.pProps
)
84 Reference
< XPropertySet
> Table::createDataDescriptor( )
86 rtl::Reference
<TableDescriptor
> pTable
= new TableDescriptor(
87 m_xMutex
, m_conn
, m_pSettings
);
88 pTable
->copyValuesFrom( this );
90 return Reference
< XPropertySet
> ( pTable
);
93 Reference
< XNameAccess
> Table::getColumns( )
95 if( ! m_columns
.is() )
97 m_columns
= Columns::create(
101 extractStringProperty( this, getStatics().SCHEMA_NAME
),
102 extractStringProperty( this, getStatics().NAME
),
108 Reference
< XNameAccess
> Table::getIndexes()
110 if( ! m_indexes
.is() )
112 m_indexes
= ::pq_sdbc_driver::Indexes::create(
116 extractStringProperty( this, getStatics().SCHEMA_NAME
),
117 extractStringProperty( this, getStatics().NAME
) );
122 Reference
< XIndexAccess
> Table::getKeys( )
126 m_keys
= ::pq_sdbc_driver::Keys::create(
130 extractStringProperty( this, getStatics().SCHEMA_NAME
),
131 extractStringProperty( this, getStatics().NAME
) );
136 void Table::rename( const OUString
& newName
)
138 MutexGuard
guard( m_xMutex
->GetMutex() );
139 Statics
& st
= getStatics();
141 OUString oldName
= extractStringProperty(this,st
.NAME
);
142 OUString schema
= extractStringProperty(this,st
.SCHEMA_NAME
);
143 OUString fullOldName
= concatQualified( schema
, oldName
);
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 )
152 splitConcatenatedIdentifier( newName
, &newSchemaName
, &newTableName
);
156 newTableName
= newName
;
157 newSchemaName
= schema
;
159 OUString fullNewName
= concatQualified( newSchemaName
, newTableName
);
161 if( extractStringProperty( this, st
.TYPE
) == st
.VIEW
&& m_pSettings
->views
.is() )
163 // maintain view list (really strange API !)
164 Any a
= m_pSettings
->pViewsImpl
->getByName( fullOldName
);
165 Reference
< css::sdbcx::XRename
> Xrename
;
169 Xrename
->rename( newName
);
170 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, Any(newSchemaName
) );
175 if( newSchemaName
!= schema
)
177 // try new schema name first
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
;
191 catch( css::sdbc::SQLException
&e
)
193 e
.Message
+= "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)";
198 if( newTableName
!= oldName
) // might also be just the change of a schema name
200 OUStringBuffer
buf(128);
201 buf
.append( "ALTER TABLE" );
202 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
, m_pSettings
);
203 buf
.append( "RENAME TO" );
204 bufferQuoteIdentifier( buf
, newTableName
, m_pSettings
);
205 Reference
< XStatement
> statement
= m_conn
->createStatement();
206 statement
->executeUpdate( buf
.makeStringAndClear() );
207 disposeNoThrow( statement
);
210 setPropertyValue_NoBroadcast_public( st
.NAME
, Any(newTableName
) );
211 // inform the container of the name change !
212 if( m_pSettings
->tables
.is() )
214 m_pSettings
->pTablesImpl
->rename( fullOldName
, fullNewName
);
218 void Table::alterColumnByName(
219 const OUString
& colName
,
220 const Reference
< XPropertySet
>& descriptor
)
222 Reference
< css::container::XNameAccess
> columns
= getColumns();
224 OUString newName
= extractStringProperty(descriptor
, getStatics().NAME
);
225 ::pq_sdbc_driver::alterColumnByDescriptor(
226 extractStringProperty( this, getStatics().SCHEMA_NAME
),
227 extractStringProperty( this, getStatics().NAME
),
229 m_conn
->createStatement(),
230 Reference
< css::beans::XPropertySet
>( columns
->getByName( colName
), UNO_QUERY
) ,
233 if( colName
!= newName
)
235 // m_pColumns->rename( colName, newName );
236 m_pColumns
->refresh();
240 void Table::alterColumnByIndex(
242 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
244 Reference
< css::container::XIndexAccess
> columns( getColumns(), UNO_QUERY
);
245 Reference
< css::beans::XPropertySet
> column(columns
->getByIndex( index
), UNO_QUERY
);
246 ::pq_sdbc_driver::alterColumnByDescriptor(
247 extractStringProperty( this, getStatics().SCHEMA_NAME
),
248 extractStringProperty( this, getStatics().NAME
),
250 m_conn
->createStatement(),
253 m_pColumns
->refresh();
256 Sequence
<Type
> Table::getTypes()
258 static cppu::OTypeCollection
collection(
259 cppu::UnoType
<css::sdbcx::XIndexesSupplier
>::get(),
260 cppu::UnoType
<css::sdbcx::XKeysSupplier
>::get(),
261 cppu::UnoType
<css::sdbcx::XColumnsSupplier
>::get(),
262 cppu::UnoType
<css::sdbcx::XRename
>::get(),
263 cppu::UnoType
<css::sdbcx::XAlterTable
>::get(),
264 ReflectionBase::getTypes());
266 return collection
.getTypes();
269 Sequence
< sal_Int8
> Table::getImplementationId()
271 return css::uno::Sequence
<sal_Int8
>();
274 Any
Table::queryInterface( const Type
& reqType
)
276 Any ret
= ReflectionBase::queryInterface( reqType
);
277 if( ! ret
.hasValue() )
278 ret
= ::cppu::queryInterface(
280 static_cast< css::sdbcx::XIndexesSupplier
* > ( this ),
281 static_cast< css::sdbcx::XKeysSupplier
* > ( this ),
282 static_cast< css::sdbcx::XColumnsSupplier
* > ( this ),
283 static_cast< css::sdbcx::XRename
* > ( this ),
284 static_cast< css::sdbcx::XAlterTable
* > ( this )
289 OUString
Table::getName( )
291 Statics
& st
= getStatics();
292 return concatQualified(
293 extractStringProperty( this, st
.SCHEMA_NAME
),
294 extractStringProperty( this, st
.NAME
) );
297 void Table::setName( const OUString
& aName
)
303 TableDescriptor::TableDescriptor(
304 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
305 const Reference
< css::sdbc::XConnection
> & connection
,
306 ConnectionSettings
*pSettings
)
308 getStatics().refl
.tableDescriptor
.implName
,
309 getStatics().refl
.tableDescriptor
.serviceNames
,
313 * getStatics().refl
.tableDescriptor
.pProps
)
317 Reference
< XNameAccess
> TableDescriptor::getColumns( )
319 if( ! m_columns
.is() )
321 m_columns
= new ColumnDescriptors(m_xMutex
, m_conn
, m_pSettings
);
326 Reference
< XNameAccess
> TableDescriptor::getIndexes()
328 if( ! m_indexes
.is() )
330 m_indexes
= ::pq_sdbc_driver::IndexDescriptors::create(
338 Reference
< XIndexAccess
> TableDescriptor::getKeys( )
342 m_keys
= ::pq_sdbc_driver::KeyDescriptors::create(
351 Sequence
<Type
> TableDescriptor::getTypes()
353 static cppu::OTypeCollection
collection(
354 cppu::UnoType
<css::sdbcx::XIndexesSupplier
>::get(),
355 cppu::UnoType
<css::sdbcx::XKeysSupplier
>::get(),
356 cppu::UnoType
<css::sdbcx::XColumnsSupplier
>::get(),
357 ReflectionBase::getTypes());
359 return collection
.getTypes();
362 Sequence
< sal_Int8
> TableDescriptor::getImplementationId()
364 return css::uno::Sequence
<sal_Int8
>();
367 Any
TableDescriptor::queryInterface( const Type
& reqType
)
369 Any ret
= ReflectionBase::queryInterface( reqType
);
370 if( ! ret
.hasValue() )
371 ret
= ::cppu::queryInterface(
373 static_cast< css::sdbcx::XIndexesSupplier
* > ( this ),
374 static_cast< css::sdbcx::XKeysSupplier
* > ( this ),
375 static_cast< css::sdbcx::XColumnsSupplier
* > ( this ));
380 Reference
< XPropertySet
> TableDescriptor::createDataDescriptor( )
382 rtl::Reference
<TableDescriptor
> pTable
= new TableDescriptor(
383 m_xMutex
, m_conn
, m_pSettings
);
386 pTable
->m_values
= m_values
;
388 return Reference
< XPropertySet
> ( pTable
);
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */