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 : * 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/.
34 : *
35 : ************************************************************************/
36 :
37 : #include <rtl/ustrbuf.hxx>
38 :
39 : #include <cppuhelper/typeprovider.hxx>
40 : #include <cppuhelper/queryinterface.hxx>
41 :
42 : #include <com/sun/star/beans/PropertyAttribute.hpp>
43 :
44 : #include <com/sun/star/sdbc/XRow.hpp>
45 : #include <com/sun/star/sdbc/XParameters.hpp>
46 :
47 : #include "pq_xview.hxx"
48 : #include "pq_xviews.hxx"
49 : #include "pq_statics.hxx"
50 : #include "pq_tools.hxx"
51 :
52 : using osl::MutexGuard;
53 : using osl::Mutex;
54 :
55 : using com::sun::star::container::XNameAccess;
56 : using com::sun::star::container::XIndexAccess;
57 : using com::sun::star::container::ElementExistException;
58 : using com::sun::star::container::NoSuchElementException;
59 :
60 : using com::sun::star::uno::Reference;
61 : using com::sun::star::uno::Exception;
62 : using com::sun::star::uno::UNO_QUERY;
63 : using com::sun::star::uno::XInterface;
64 : using com::sun::star::uno::Sequence;
65 : using com::sun::star::uno::Any;
66 : using com::sun::star::uno::makeAny;
67 : using com::sun::star::uno::Type;
68 : using com::sun::star::uno::RuntimeException;
69 :
70 : using com::sun::star::lang::IllegalArgumentException;
71 : using com::sun::star::lang::IndexOutOfBoundsException;
72 :
73 : using com::sun::star::beans::XPropertySetInfo;
74 : using com::sun::star::beans::XFastPropertySet;
75 : using com::sun::star::beans::XMultiPropertySet;
76 : using com::sun::star::beans::XPropertySet;
77 : using com::sun::star::beans::Property;
78 :
79 : using com::sun::star::sdbc::XResultSet;
80 : using com::sun::star::sdbc::XPreparedStatement;
81 : using com::sun::star::sdbc::XStatement;
82 : using com::sun::star::sdbc::XParameters;
83 : using com::sun::star::sdbc::XRow;
84 : using com::sun::star::sdbc::SQLException;
85 :
86 : namespace pq_sdbc_driver
87 : {
88 :
89 0 : View::View( const ::rtl::Reference< RefCountedMutex > & refMutex,
90 : const Reference< com::sun::star::sdbc::XConnection > & connection,
91 : ConnectionSettings *pSettings)
92 : : ReflectionBase(
93 0 : getStatics().refl.view.implName,
94 0 : getStatics().refl.view.serviceNames,
95 : refMutex,
96 : connection,
97 : pSettings,
98 0 : * getStatics().refl.view.pProps )
99 0 : {}
100 :
101 0 : Reference< XPropertySet > View::createDataDescriptor( ) throw (RuntimeException, std::exception)
102 : {
103 : ViewDescriptor * pView = new ViewDescriptor(
104 0 : m_refMutex, m_conn, m_pSettings );
105 0 : pView->copyValuesFrom( this );
106 :
107 0 : return Reference< XPropertySet > ( pView );
108 : }
109 :
110 0 : void View::rename( const OUString& newName )
111 : throw (::com::sun::star::sdbc::SQLException,
112 : ::com::sun::star::container::ElementExistException,
113 : ::com::sun::star::uno::RuntimeException, std::exception)
114 : {
115 0 : MutexGuard guard( m_refMutex->mutex );
116 :
117 0 : Statics & st = getStatics();
118 :
119 0 : OUString oldName = extractStringProperty(this,st.NAME );
120 0 : OUString schema = extractStringProperty(this,st.SCHEMA_NAME );
121 0 : OUString fullOldName = concatQualified( schema, oldName );
122 :
123 0 : OUString newTableName;
124 0 : OUString newSchemaName;
125 : // OOo2.0 passes schema + dot + new-table-name while
126 : // OO1.1.x passes new Name without schema
127 : // in case name contains a dot, it is interpreted as schema.tablename
128 0 : if( newName.indexOf( '.' ) >= 0 )
129 : {
130 0 : splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
131 : }
132 : else
133 : {
134 0 : newTableName = newName;
135 0 : newSchemaName = schema;
136 : }
137 0 : OUString fullNewName = concatQualified( newSchemaName, newTableName );
138 :
139 0 : if( ! schema.equals( newSchemaName ) )
140 : {
141 : try
142 : {
143 0 : OUStringBuffer buf(128);
144 0 : buf.append( "ALTER TABLE" );
145 0 : bufferQuoteQualifiedIdentifier(buf, schema, oldName, m_pSettings );
146 0 : buf.append( "SET SCHEMA" );
147 0 : bufferQuoteIdentifier( buf, newSchemaName, m_pSettings );
148 0 : Reference< XStatement > statement = m_conn->createStatement();
149 0 : statement->executeUpdate( buf.makeStringAndClear() );
150 0 : setPropertyValue_NoBroadcast_public( st.SCHEMA_NAME, makeAny(newSchemaName) );
151 0 : disposeNoThrow( statement );
152 0 : schema = newSchemaName;
153 : }
154 0 : catch( com::sun::star::sdbc::SQLException &e )
155 : {
156 0 : OUString buf( e.Message + "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" );
157 0 : e.Message = buf;
158 0 : throw;
159 : }
160 :
161 : }
162 0 : if( ! oldName.equals( newTableName ) )
163 : {
164 0 : OUStringBuffer buf(128);
165 0 : buf.appendAscii( "ALTER TABLE" );
166 0 : bufferQuoteQualifiedIdentifier( buf, schema, oldName, m_pSettings );
167 0 : buf.appendAscii( "RENAME TO" );
168 0 : bufferQuoteIdentifier( buf, newTableName, m_pSettings );
169 0 : Reference< XStatement > statement = m_conn->createStatement();
170 0 : statement->executeUpdate( buf.makeStringAndClear() );
171 0 : setPropertyValue_NoBroadcast_public( st.NAME, makeAny(newTableName) );
172 : }
173 :
174 : // inform the container of the name change !
175 0 : if( m_pSettings->views.is() )
176 : {
177 0 : m_pSettings->pViewsImpl->rename( fullOldName, fullNewName );
178 0 : }
179 0 : }
180 :
181 0 : Sequence<Type > View::getTypes() throw( RuntimeException, std::exception )
182 : {
183 : static cppu::OTypeCollection *pCollection;
184 0 : if( ! pCollection )
185 : {
186 0 : MutexGuard guard( osl::Mutex::getGlobalMutex() );
187 0 : if( !pCollection )
188 : {
189 : static cppu::OTypeCollection collection(
190 0 : cppu::UnoType<com::sun::star::sdbcx::XRename>::get(),
191 0 : ReflectionBase::getTypes());
192 0 : pCollection = &collection;
193 0 : }
194 : }
195 0 : return pCollection->getTypes();
196 : }
197 :
198 0 : Sequence< sal_Int8> View::getImplementationId() throw( RuntimeException, std::exception )
199 : {
200 0 : return css::uno::Sequence<sal_Int8>();
201 : }
202 :
203 0 : Any View::queryInterface( const Type & reqType ) throw (RuntimeException, std::exception)
204 : {
205 0 : Any ret;
206 :
207 0 : ret = ReflectionBase::queryInterface( reqType );
208 0 : if( ! ret.hasValue() )
209 0 : ret = ::cppu::queryInterface(
210 : reqType,
211 : static_cast< com::sun::star::sdbcx::XRename * > ( this )
212 0 : );
213 0 : return ret;
214 : }
215 :
216 0 : OUString View::getName( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
217 : {
218 0 : Statics & st = getStatics();
219 : return concatQualified(
220 : extractStringProperty( this, st.SCHEMA_NAME ),
221 0 : extractStringProperty( this, st.NAME ) );
222 : }
223 :
224 0 : void View::setName( const OUString& aName ) throw (::com::sun::star::uno::RuntimeException, std::exception)
225 : {
226 0 : rename( aName );
227 0 : }
228 :
229 :
230 :
231 0 : ViewDescriptor::ViewDescriptor(
232 : const ::rtl::Reference< RefCountedMutex > & refMutex,
233 : const Reference< com::sun::star::sdbc::XConnection > & connection,
234 : ConnectionSettings *pSettings)
235 : : ReflectionBase(
236 0 : getStatics().refl.viewDescriptor.implName,
237 0 : getStatics().refl.viewDescriptor.serviceNames,
238 : refMutex,
239 : connection,
240 : pSettings,
241 0 : * getStatics().refl.viewDescriptor.pProps )
242 0 : {}
243 :
244 0 : Reference< XPropertySet > ViewDescriptor::createDataDescriptor( ) throw (RuntimeException, std::exception)
245 : {
246 : ViewDescriptor * pView = new ViewDescriptor(
247 0 : m_refMutex, m_conn, m_pSettings );
248 0 : pView->copyValuesFrom( this );
249 :
250 0 : return Reference< XPropertySet > ( pView );
251 : }
252 :
253 :
254 : }
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|