Use correct object
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xview.cxx
blobc2e936e9a314f6f6e44ebf6af470a7bd4742fb7c
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,
18 * MA 02111-1307 USA
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_xview.hxx"
46 #include "pq_xviews.hxx"
47 #include "pq_statics.hxx"
48 #include "pq_tools.hxx"
50 using osl::MutexGuard;
52 using com::sun::star::uno::Reference;
53 using com::sun::star::uno::Sequence;
54 using com::sun::star::uno::Any;
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)
68 : ReflectionBase(
69 getStatics().refl.view.implName,
70 getStatics().refl.view.serviceNames,
71 refMutex,
72 connection,
73 pSettings,
74 * getStatics().refl.view.pProps )
77 Reference< XPropertySet > View::createDataDescriptor( )
79 rtl::Reference<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 );
105 else
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, Any(newSchemaName) );
124 disposeNoThrow( statement );
125 schema = newSchemaName;
127 catch( css::sdbc::SQLException &e )
129 e.Message += "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)";
130 throw;
134 if( oldName != newTableName )
136 OUStringBuffer buf(128);
137 buf.append( "ALTER TABLE" );
138 bufferQuoteQualifiedIdentifier( buf, schema, oldName, m_pSettings );
139 buf.append( "RENAME TO" );
140 bufferQuoteIdentifier( buf, newTableName, m_pSettings );
141 Reference< XStatement > statement = m_conn->createStatement();
142 statement->executeUpdate( buf.makeStringAndClear() );
143 setPropertyValue_NoBroadcast_public( st.NAME, Any(newTableName) );
146 // inform the container of the name change !
147 if( m_pSettings->views.is() )
149 m_pSettings->pViewsImpl->rename( fullOldName, fullNewName );
153 Sequence<Type > View::getTypes()
155 static cppu::OTypeCollection collection(
156 cppu::UnoType<css::sdbcx::XRename>::get(),
157 ReflectionBase::getTypes());
159 return collection.getTypes();
162 Sequence< sal_Int8> View::getImplementationId()
164 return css::uno::Sequence<sal_Int8>();
167 Any View::queryInterface( const Type & reqType )
169 Any ret = ReflectionBase::queryInterface( reqType );
170 if( ! ret.hasValue() )
171 ret = ::cppu::queryInterface(
172 reqType,
173 static_cast< css::sdbcx::XRename * > ( this )
175 return ret;
178 OUString View::getName( )
180 Statics & st = getStatics();
181 return concatQualified(
182 extractStringProperty( this, st.SCHEMA_NAME ),
183 extractStringProperty( this, st.NAME ) );
186 void View::setName( const OUString& aName )
188 rename( aName );
192 ViewDescriptor::ViewDescriptor(
193 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
194 const Reference< css::sdbc::XConnection > & connection,
195 ConnectionSettings *pSettings)
196 : ReflectionBase(
197 getStatics().refl.viewDescriptor.implName,
198 getStatics().refl.viewDescriptor.serviceNames,
199 refMutex,
200 connection,
201 pSettings,
202 * getStatics().refl.viewDescriptor.pProps )
205 Reference< XPropertySet > ViewDescriptor::createDataDescriptor( )
207 rtl::Reference<ViewDescriptor> pView = new ViewDescriptor(
208 m_xMutex, m_conn, m_pSettings );
209 pView->copyValuesFrom( this );
211 return Reference< XPropertySet > ( pView );
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */