Branch data Line data Source code
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 : : * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
32 : : *
33 : : * The contents of this file are subject to the Mozilla Public License Version
34 : : * 1.1 (the "License"); you may not use this file except in compliance with
35 : : * the License or as specified alternatively below. You may obtain a copy of
36 : : * the License at http://www.mozilla.org/MPL/
37 : : *
38 : : * Software distributed under the License is distributed on an "AS IS" basis,
39 : : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 : : * for the specific language governing rights and limitations under the
41 : : * License.
42 : : *
43 : : * Major Contributor(s):
44 : : * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
45 : : *
46 : : * All Rights Reserved.
47 : : *
48 : : * For minor contributions see the git repository.
49 : : *
50 : : * Alternatively, the contents of this file may be used under the terms of
51 : : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 : : * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 : : * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 : : * instead of those above.
55 : : *
56 : : ************************************************************************/
57 : :
58 : : #include <rtl/ustrbuf.hxx>
59 : :
60 : : #include <cppuhelper/typeprovider.hxx>
61 : : #include <cppuhelper/queryinterface.hxx>
62 : :
63 : : #include <com/sun/star/beans/PropertyAttribute.hpp>
64 : :
65 : : #include <com/sun/star/sdbc/XRow.hpp>
66 : : #include <com/sun/star/sdbc/XParameters.hpp>
67 : :
68 : : #include "pq_xview.hxx"
69 : : #include "pq_xviews.hxx"
70 : : #include "pq_statics.hxx"
71 : : #include "pq_tools.hxx"
72 : :
73 : : using osl::MutexGuard;
74 : : using osl::Mutex;
75 : :
76 : : using rtl::OUString;
77 : : using rtl::OUStringBuffer;
78 : : using rtl::OUStringToOString;
79 : :
80 : : using com::sun::star::container::XNameAccess;
81 : : using com::sun::star::container::XIndexAccess;
82 : : using com::sun::star::container::ElementExistException;
83 : : using com::sun::star::container::NoSuchElementException;
84 : :
85 : : using com::sun::star::uno::Reference;
86 : : using com::sun::star::uno::Exception;
87 : : using com::sun::star::uno::UNO_QUERY;
88 : : using com::sun::star::uno::XInterface;
89 : : using com::sun::star::uno::Sequence;
90 : : using com::sun::star::uno::Any;
91 : : using com::sun::star::uno::makeAny;
92 : : using com::sun::star::uno::Type;
93 : : using com::sun::star::uno::RuntimeException;
94 : :
95 : : using com::sun::star::lang::IllegalArgumentException;
96 : : using com::sun::star::lang::IndexOutOfBoundsException;
97 : :
98 : : using com::sun::star::beans::XPropertySetInfo;
99 : : using com::sun::star::beans::XFastPropertySet;
100 : : using com::sun::star::beans::XMultiPropertySet;
101 : : using com::sun::star::beans::XPropertySet;
102 : : using com::sun::star::beans::Property;
103 : :
104 : : using com::sun::star::sdbc::XResultSet;
105 : : using com::sun::star::sdbc::XPreparedStatement;
106 : : using com::sun::star::sdbc::XStatement;
107 : : using com::sun::star::sdbc::XParameters;
108 : : using com::sun::star::sdbc::XRow;
109 : : using com::sun::star::sdbc::SQLException;
110 : :
111 : : namespace pq_sdbc_driver
112 : : {
113 : : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
114 : :
115 : 0 : View::View( const ::rtl::Reference< RefCountedMutex > & refMutex,
116 : : const Reference< com::sun::star::sdbc::XConnection > & connection,
117 : : ConnectionSettings *pSettings)
118 : : : ReflectionBase(
119 : 0 : getStatics().refl.view.implName,
120 : 0 : getStatics().refl.view.serviceNames,
121 : : refMutex,
122 : : connection,
123 : : pSettings,
124 : 0 : * getStatics().refl.view.pProps )
125 : 0 : {}
126 : :
127 : 0 : Reference< XPropertySet > View::createDataDescriptor( ) throw (RuntimeException)
128 : : {
129 : : ViewDescriptor * pView = new ViewDescriptor(
130 : 0 : m_refMutex, m_conn, m_pSettings );
131 : 0 : pView->copyValuesFrom( this );
132 : :
133 : 0 : return Reference< XPropertySet > ( pView );
134 : : }
135 : :
136 : 0 : void View::rename( const ::rtl::OUString& newName )
137 : : throw (::com::sun::star::sdbc::SQLException,
138 : : ::com::sun::star::container::ElementExistException,
139 : : ::com::sun::star::uno::RuntimeException)
140 : : {
141 : 0 : MutexGuard guard( m_refMutex->mutex );
142 : :
143 : 0 : Statics & st = getStatics();
144 : :
145 : 0 : ::rtl::OUString oldName = extractStringProperty(this,st.NAME );
146 : 0 : ::rtl::OUString schema = extractStringProperty(this,st.SCHEMA_NAME );
147 : 0 : ::rtl::OUString fullOldName = concatQualified( schema, oldName );
148 : :
149 : 0 : OUString newTableName;
150 : 0 : OUString newSchemaName;
151 : : // OOo2.0 passes schema + dot + new-table-name while
152 : : // OO1.1.x passes new Name without schema
153 : : // in case name contains a dot, it is interpreted as schema.tablename
154 : 0 : if( newName.indexOf( '.' ) >= 0 )
155 : : {
156 : 0 : splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
157 : : }
158 : : else
159 : : {
160 : 0 : newTableName = newName;
161 : 0 : newSchemaName = schema;
162 : : }
163 : 0 : ::rtl::OUString fullNewName = concatQualified( newSchemaName, newTableName );
164 : :
165 : 0 : if( ! schema.equals( newSchemaName ) )
166 : : {
167 : : try
168 : : {
169 : 0 : OUStringBuffer buf(128);
170 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
171 : 0 : bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
172 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("SET SCHEMA" ) );
173 : 0 : bufferQuoteIdentifier( buf, newSchemaName, m_pSettings );
174 : 0 : Reference< XStatement > statement = m_conn->createStatement();
175 : 0 : statement->executeUpdate( buf.makeStringAndClear() );
176 : 0 : setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, makeAny(newSchemaName) );
177 : 0 : disposeNoThrow( statement );
178 : 0 : schema = newSchemaName;
179 : : }
180 : 0 : catch( com::sun::star::sdbc::SQLException &e )
181 : : {
182 : 0 : OUStringBuffer buf( e.Message );
183 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" ) );
184 : 0 : e.Message = buf.makeStringAndClear();
185 : 0 : throw;
186 : : }
187 : :
188 : : }
189 : 0 : if( ! oldName.equals( newTableName ) )
190 : : {
191 : 0 : OUStringBuffer buf(128);
192 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
193 : 0 : bufferQuoteQualifiedIdentifier( buf, schema, oldName, m_pSettings );
194 : 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("RENAME TO" ) );
195 : 0 : bufferQuoteIdentifier( buf, newTableName, m_pSettings );
196 : 0 : Reference< XStatement > statement = m_conn->createStatement();
197 : 0 : statement->executeUpdate( buf.makeStringAndClear() );
198 : 0 : setPropertyValue_NoBroadcast_public( st.NAME, makeAny(newTableName) );
199 : : }
200 : :
201 : : // inform the container of the name change !
202 : 0 : if( m_pSettings->views.is() )
203 : : {
204 : 0 : m_pSettings->pViewsImpl->rename( fullOldName, fullNewName );
205 : 0 : }
206 : 0 : }
207 : :
208 : 0 : Sequence<Type > View::getTypes() throw( RuntimeException )
209 : : {
210 : : static cppu::OTypeCollection *pCollection;
211 : 0 : if( ! pCollection )
212 : : {
213 : 0 : MutexGuard guard( osl::Mutex::getGlobalMutex() );
214 : 0 : if( !pCollection )
215 : : {
216 : : static cppu::OTypeCollection collection(
217 : 0 : getCppuType( (Reference< com::sun::star::sdbcx::XRename> *) 0 ),
218 : 0 : ReflectionBase::getTypes());
219 : 0 : pCollection = &collection;
220 : 0 : }
221 : : }
222 : 0 : return pCollection->getTypes();
223 : : }
224 : :
225 : 0 : Sequence< sal_Int8> View::getImplementationId() throw( RuntimeException )
226 : : {
227 : 0 : return getStatics().refl.view.implementationId;
228 : : }
229 : :
230 : 0 : Any View::queryInterface( const Type & reqType ) throw (RuntimeException)
231 : : {
232 : 0 : Any ret;
233 : :
234 : 0 : ret = ReflectionBase::queryInterface( reqType );
235 : 0 : if( ! ret.hasValue() )
236 : : ret = ::cppu::queryInterface(
237 : : reqType,
238 : : static_cast< com::sun::star::sdbcx::XRename * > ( this )
239 : 0 : );
240 : 0 : return ret;
241 : : }
242 : :
243 : 0 : ::rtl::OUString View::getName( ) throw (::com::sun::star::uno::RuntimeException)
244 : : {
245 : 0 : Statics & st = getStatics();
246 : : return concatQualified(
247 : : extractStringProperty( this, st.SCHEMA_NAME ),
248 : 0 : extractStringProperty( this, st.NAME ) );
249 : : }
250 : :
251 : 0 : void View::setName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException)
252 : : {
253 : 0 : rename( aName );
254 : 0 : }
255 : :
256 : : //____________________________________________________________________________________________
257 : :
258 : 0 : ViewDescriptor::ViewDescriptor(
259 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
260 : : const Reference< com::sun::star::sdbc::XConnection > & connection,
261 : : ConnectionSettings *pSettings)
262 : : : ReflectionBase(
263 : 0 : getStatics().refl.viewDescriptor.implName,
264 : 0 : getStatics().refl.viewDescriptor.serviceNames,
265 : : refMutex,
266 : : connection,
267 : : pSettings,
268 : 0 : * getStatics().refl.viewDescriptor.pProps )
269 : 0 : {}
270 : :
271 : 0 : Reference< XPropertySet > ViewDescriptor::createDataDescriptor( ) throw (RuntimeException)
272 : : {
273 : : ViewDescriptor * pView = new ViewDescriptor(
274 : 0 : m_refMutex, m_conn, m_pSettings );
275 : 0 : pView->copyValuesFrom( this );
276 : :
277 : 0 : return Reference< XPropertySet > ( pView );
278 : : }
279 : :
280 : :
281 : : }
|