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/ustrbuf.hxx>
39 #include <cppuhelper/typeprovider.hxx>
40 #include <cppuhelper/queryinterface.hxx>
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
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::makeAny
;
64 using com::sun::star::uno::Type
;
66 using com::sun::star::beans::XPropertySet
;
68 using com::sun::star::sdbc::XStatement
;
69 using com::sun::star::sdbc::SQLException
;
71 namespace pq_sdbc_driver
73 Table::Table( const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
74 const Reference
< css::sdbc::XConnection
> & connection
,
75 ConnectionSettings
*pSettings
)
77 getStatics().refl
.table
.implName
,
78 getStatics().refl
.table
.serviceNames
,
82 * getStatics().refl
.table
.pProps
),
86 Reference
< XPropertySet
> Table::createDataDescriptor( )
88 TableDescriptor
* pTable
= new TableDescriptor(
89 m_xMutex
, m_conn
, m_pSettings
);
90 pTable
->copyValuesFrom( this );
92 return Reference
< XPropertySet
> ( pTable
);
95 Reference
< XNameAccess
> Table::getColumns( )
97 if( ! m_columns
.is() )
99 m_columns
= Columns::create(
103 extractStringProperty( this, getStatics().SCHEMA_NAME
),
104 extractStringProperty( this, getStatics().NAME
),
110 Reference
< XNameAccess
> Table::getIndexes()
112 if( ! m_indexes
.is() )
114 m_indexes
= ::pq_sdbc_driver::Indexes::create(
118 extractStringProperty( this, getStatics().SCHEMA_NAME
),
119 extractStringProperty( this, getStatics().NAME
) );
124 Reference
< XIndexAccess
> Table::getKeys( )
128 m_keys
= ::pq_sdbc_driver::Keys::create(
132 extractStringProperty( this, getStatics().SCHEMA_NAME
),
133 extractStringProperty( this, getStatics().NAME
) );
138 void Table::rename( const OUString
& newName
)
140 MutexGuard
guard( m_xMutex
->GetMutex() );
141 Statics
& st
= getStatics();
143 OUString oldName
= extractStringProperty(this,st
.NAME
);
144 OUString schema
= extractStringProperty(this,st
.SCHEMA_NAME
);
145 OUString fullOldName
= concatQualified( schema
, oldName
);
147 OUString newTableName
;
148 OUString newSchemaName
;
149 // OOo2.0 passes schema + dot + new-table-name while
150 // OO1.1.x passes new Name without schema
151 // in case name contains a dot, it is interpreted as schema.tablename
152 if( newName
.indexOf( '.' ) >= 0 )
154 splitConcatenatedIdentifier( newName
, &newSchemaName
, &newTableName
);
158 newTableName
= newName
;
159 newSchemaName
= schema
;
161 OUString fullNewName
= concatQualified( newSchemaName
, newTableName
);
163 if( extractStringProperty( this, st
.TYPE
) == st
.VIEW
&& m_pSettings
->views
.is() )
165 // maintain view list (really strange API !)
166 Any a
= m_pSettings
->pViewsImpl
->getByName( fullOldName
);
167 Reference
< css::sdbcx::XRename
> Xrename
;
171 Xrename
->rename( newName
);
172 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, makeAny(newSchemaName
) );
177 if( newSchemaName
!= schema
)
179 // try new schema name first
182 OUStringBuffer
buf(128);
183 buf
.append( "ALTER TABLE" );
184 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
, m_pSettings
);
185 buf
.append( "SET SCHEMA" );
186 bufferQuoteIdentifier( buf
, newSchemaName
, m_pSettings
);
187 Reference
< XStatement
> statement
= m_conn
->createStatement();
188 statement
->executeUpdate( buf
.makeStringAndClear() );
189 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, makeAny(newSchemaName
) );
190 disposeNoThrow( statement
);
191 schema
= newSchemaName
;
193 catch( css::sdbc::SQLException
&e
)
195 OUString
buf( e
.Message
+ "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" );
201 if( newTableName
!= oldName
) // might also be just the change of a schema name
203 OUStringBuffer
buf(128);
204 buf
.append( "ALTER TABLE" );
205 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
, m_pSettings
);
206 buf
.append( "RENAME TO" );
207 bufferQuoteIdentifier( buf
, newTableName
, m_pSettings
);
208 Reference
< XStatement
> statement
= m_conn
->createStatement();
209 statement
->executeUpdate( buf
.makeStringAndClear() );
210 disposeNoThrow( statement
);
213 setPropertyValue_NoBroadcast_public( st
.NAME
, makeAny(newTableName
) );
214 // inform the container of the name change !
215 if( m_pSettings
->tables
.is() )
217 m_pSettings
->pTablesImpl
->rename( fullOldName
, fullNewName
);
221 void Table::alterColumnByName(
222 const OUString
& colName
,
223 const Reference
< XPropertySet
>& descriptor
)
225 Reference
< css::container::XNameAccess
> columns
=
226 Reference
< css::container::XNameAccess
> ( getColumns(), UNO_QUERY
);
228 OUString newName
= extractStringProperty(descriptor
, getStatics().NAME
);
229 ::pq_sdbc_driver::alterColumnByDescriptor(
230 extractStringProperty( this, getStatics().SCHEMA_NAME
),
231 extractStringProperty( this, getStatics().NAME
),
233 m_conn
->createStatement(),
234 Reference
< css::beans::XPropertySet
>( columns
->getByName( colName
), UNO_QUERY
) ,
237 if( colName
!= newName
)
239 // m_pColumns->rename( colName, newName );
240 m_pColumns
->refresh();
244 void Table::alterColumnByIndex(
246 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
248 Reference
< css::container::XIndexAccess
> columns
=
249 Reference
< css::container::XIndexAccess
>( getColumns(), UNO_QUERY
);
250 Reference
< css::beans::XPropertySet
> column(columns
->getByIndex( index
), UNO_QUERY
);
251 ::pq_sdbc_driver::alterColumnByDescriptor(
252 extractStringProperty( this, getStatics().SCHEMA_NAME
),
253 extractStringProperty( this, getStatics().NAME
),
255 m_conn
->createStatement(),
258 m_pColumns
->refresh();
261 Sequence
<Type
> Table::getTypes()
263 static cppu::OTypeCollection
collection(
264 cppu::UnoType
<css::sdbcx::XIndexesSupplier
>::get(),
265 cppu::UnoType
<css::sdbcx::XKeysSupplier
>::get(),
266 cppu::UnoType
<css::sdbcx::XColumnsSupplier
>::get(),
267 cppu::UnoType
<css::sdbcx::XRename
>::get(),
268 cppu::UnoType
<css::sdbcx::XAlterTable
>::get(),
269 ReflectionBase::getTypes());
271 return collection
.getTypes();
274 Sequence
< sal_Int8
> Table::getImplementationId()
276 return css::uno::Sequence
<sal_Int8
>();
279 Any
Table::queryInterface( const Type
& reqType
)
281 Any ret
= ReflectionBase::queryInterface( reqType
);
282 if( ! ret
.hasValue() )
283 ret
= ::cppu::queryInterface(
285 static_cast< css::sdbcx::XIndexesSupplier
* > ( this ),
286 static_cast< css::sdbcx::XKeysSupplier
* > ( this ),
287 static_cast< css::sdbcx::XColumnsSupplier
* > ( this ),
288 static_cast< css::sdbcx::XRename
* > ( this ),
289 static_cast< css::sdbcx::XAlterTable
* > ( this )
294 OUString
Table::getName( )
296 Statics
& st
= getStatics();
297 return concatQualified(
298 extractStringProperty( this, st
.SCHEMA_NAME
),
299 extractStringProperty( this, st
.NAME
) );
302 void Table::setName( const OUString
& aName
)
308 TableDescriptor::TableDescriptor(
309 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
310 const Reference
< css::sdbc::XConnection
> & connection
,
311 ConnectionSettings
*pSettings
)
313 getStatics().refl
.tableDescriptor
.implName
,
314 getStatics().refl
.tableDescriptor
.serviceNames
,
318 * getStatics().refl
.tableDescriptor
.pProps
)
322 Reference
< XNameAccess
> TableDescriptor::getColumns( )
324 if( ! m_columns
.is() )
326 m_columns
= new ColumnDescriptors(m_xMutex
, m_conn
, m_pSettings
);
331 Reference
< XNameAccess
> TableDescriptor::getIndexes()
333 if( ! m_indexes
.is() )
335 m_indexes
= ::pq_sdbc_driver::IndexDescriptors::create(
343 Reference
< XIndexAccess
> TableDescriptor::getKeys( )
347 m_keys
= ::pq_sdbc_driver::KeyDescriptors::create(
356 Sequence
<Type
> TableDescriptor::getTypes()
358 static cppu::OTypeCollection
collection(
359 cppu::UnoType
<css::sdbcx::XIndexesSupplier
>::get(),
360 cppu::UnoType
<css::sdbcx::XKeysSupplier
>::get(),
361 cppu::UnoType
<css::sdbcx::XColumnsSupplier
>::get(),
362 ReflectionBase::getTypes());
364 return collection
.getTypes();
367 Sequence
< sal_Int8
> TableDescriptor::getImplementationId()
369 return css::uno::Sequence
<sal_Int8
>();
372 Any
TableDescriptor::queryInterface( const Type
& reqType
)
374 Any ret
= ReflectionBase::queryInterface( reqType
);
375 if( ! ret
.hasValue() )
376 ret
= ::cppu::queryInterface(
378 static_cast< css::sdbcx::XIndexesSupplier
* > ( this ),
379 static_cast< css::sdbcx::XKeysSupplier
* > ( this ),
380 static_cast< css::sdbcx::XColumnsSupplier
* > ( this ));
385 Reference
< XPropertySet
> TableDescriptor::createDataDescriptor( )
387 TableDescriptor
* pTable
= new TableDescriptor(
388 m_xMutex
, m_conn
, m_pSettings
);
391 pTable
->m_values
= m_values
;
393 return Reference
< XPropertySet
> ( pTable
);
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */