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/sdbc/SQLException.hpp>
44 #include "pq_xview.hxx"
45 #include "pq_xviews.hxx"
46 #include "pq_statics.hxx"
47 #include "pq_tools.hxx"
49 using osl::MutexGuard
;
51 using com::sun::star::uno::Reference
;
52 using com::sun::star::uno::Sequence
;
53 using com::sun::star::uno::Any
;
54 using com::sun::star::uno::makeAny
;
55 using com::sun::star::uno::Type
;
57 using com::sun::star::beans::XPropertySet
;
59 using com::sun::star::sdbc::XStatement
;
60 using com::sun::star::sdbc::SQLException
;
62 namespace pq_sdbc_driver
65 View::View( const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
66 const Reference
< css::sdbc::XConnection
> & connection
,
67 ConnectionSettings
*pSettings
)
69 getStatics().refl
.view
.implName
,
70 getStatics().refl
.view
.serviceNames
,
74 * getStatics().refl
.view
.pProps
)
77 Reference
< XPropertySet
> View::createDataDescriptor( )
79 ViewDescriptor
* pView
= new ViewDescriptor(
80 m_xMutex
, m_conn
, m_pSettings
);
81 pView
->copyValuesFrom( this );
83 return Reference
< XPropertySet
> ( pView
);
86 void View::rename( const OUString
& newName
)
88 MutexGuard
guard( m_xMutex
->GetMutex() );
90 Statics
& st
= getStatics();
92 OUString oldName
= extractStringProperty(this,st
.NAME
);
93 OUString schema
= extractStringProperty(this,st
.SCHEMA_NAME
);
94 OUString fullOldName
= concatQualified( schema
, oldName
);
96 OUString newTableName
;
97 OUString newSchemaName
;
98 // OOo2.0 passes schema + dot + new-table-name while
99 // OO1.1.x passes new Name without schema
100 // in case name contains a dot, it is interpreted as schema.tablename
101 if( newName
.indexOf( '.' ) >= 0 )
103 splitConcatenatedIdentifier( newName
, &newSchemaName
, &newTableName
);
107 newTableName
= newName
;
108 newSchemaName
= schema
;
110 OUString fullNewName
= concatQualified( newSchemaName
, newTableName
);
112 if( schema
!= newSchemaName
)
116 OUStringBuffer
buf(128);
117 buf
.append( "ALTER TABLE" );
118 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
, m_pSettings
);
119 buf
.append( "SET SCHEMA" );
120 bufferQuoteIdentifier( buf
, newSchemaName
, m_pSettings
);
121 Reference
< XStatement
> statement
= m_conn
->createStatement();
122 statement
->executeUpdate( buf
.makeStringAndClear() );
123 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, makeAny(newSchemaName
) );
124 disposeNoThrow( statement
);
125 schema
= newSchemaName
;
127 catch( css::sdbc::SQLException
&e
)
129 OUString
buf( e
.Message
+ "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" );
135 if( oldName
!= newTableName
)
137 OUStringBuffer
buf(128);
138 buf
.append( "ALTER TABLE" );
139 bufferQuoteQualifiedIdentifier( buf
, schema
, oldName
, m_pSettings
);
140 buf
.append( "RENAME TO" );
141 bufferQuoteIdentifier( buf
, newTableName
, m_pSettings
);
142 Reference
< XStatement
> statement
= m_conn
->createStatement();
143 statement
->executeUpdate( buf
.makeStringAndClear() );
144 setPropertyValue_NoBroadcast_public( st
.NAME
, makeAny(newTableName
) );
147 // inform the container of the name change !
148 if( m_pSettings
->views
.is() )
150 m_pSettings
->pViewsImpl
->rename( fullOldName
, fullNewName
);
154 Sequence
<Type
> View::getTypes()
156 static cppu::OTypeCollection
collection(
157 cppu::UnoType
<css::sdbcx::XRename
>::get(),
158 ReflectionBase::getTypes());
160 return collection
.getTypes();
163 Sequence
< sal_Int8
> View::getImplementationId()
165 return css::uno::Sequence
<sal_Int8
>();
168 Any
View::queryInterface( const Type
& reqType
)
170 Any ret
= ReflectionBase::queryInterface( reqType
);
171 if( ! ret
.hasValue() )
172 ret
= ::cppu::queryInterface(
174 static_cast< css::sdbcx::XRename
* > ( this )
179 OUString
View::getName( )
181 Statics
& st
= getStatics();
182 return concatQualified(
183 extractStringProperty( this, st
.SCHEMA_NAME
),
184 extractStringProperty( this, st
.NAME
) );
187 void View::setName( const OUString
& aName
)
193 ViewDescriptor::ViewDescriptor(
194 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
195 const Reference
< css::sdbc::XConnection
> & connection
,
196 ConnectionSettings
*pSettings
)
198 getStatics().refl
.viewDescriptor
.implName
,
199 getStatics().refl
.viewDescriptor
.serviceNames
,
203 * getStatics().refl
.viewDescriptor
.pProps
)
206 Reference
< XPropertySet
> ViewDescriptor::createDataDescriptor( )
208 ViewDescriptor
* pView
= new ViewDescriptor(
209 m_xMutex
, m_conn
, m_pSettings
);
210 pView
->copyValuesFrom( this );
212 return Reference
< XPropertySet
> ( pView
);
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */