1 /*************************************************************************
3 * $RCSfile: pq_xview.cxx,v $
7 * last change: $Author: jbu $ $Date: 2007/01/07 13:50:38 $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Joerg Budischewski
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Joerg Budischewski
60 ************************************************************************/
62 #include <rtl/ustrbuf.hxx>
64 #include <cppuhelper/typeprovider.hxx>
65 #include <cppuhelper/queryinterface.hxx>
67 #include <com/sun/star/beans/PropertyAttribute.hpp>
69 #include <com/sun/star/sdbc/XRow.hpp>
70 #include <com/sun/star/sdbc/XParameters.hpp>
72 #include "pq_xview.hxx"
73 #include "pq_xviews.hxx"
74 #include "pq_statics.hxx"
75 #include "pq_tools.hxx"
77 using osl::MutexGuard
;
81 using rtl::OUStringBuffer
;
82 using rtl::OUStringToOString
;
84 using com::sun::star::container::XNameAccess
;
85 using com::sun::star::container::XIndexAccess
;
86 using com::sun::star::container::ElementExistException
;
87 using com::sun::star::container::NoSuchElementException
;
89 using com::sun::star::uno::Reference
;
90 using com::sun::star::uno::Exception
;
91 using com::sun::star::uno::UNO_QUERY
;
92 using com::sun::star::uno::XInterface
;
93 using com::sun::star::uno::Sequence
;
94 using com::sun::star::uno::Any
;
95 using com::sun::star::uno::makeAny
;
96 using com::sun::star::uno::Type
;
97 using com::sun::star::uno::RuntimeException
;
99 using com::sun::star::lang::IllegalArgumentException
;
100 using com::sun::star::lang::IndexOutOfBoundsException
;
102 using com::sun::star::beans::XPropertySetInfo
;
103 using com::sun::star::beans::XFastPropertySet
;
104 using com::sun::star::beans::XMultiPropertySet
;
105 using com::sun::star::beans::XPropertySet
;
106 using com::sun::star::beans::Property
;
108 using com::sun::star::sdbc::XResultSet
;
109 using com::sun::star::sdbc::XPreparedStatement
;
110 using com::sun::star::sdbc::XStatement
;
111 using com::sun::star::sdbc::XParameters
;
112 using com::sun::star::sdbc::XRow
;
113 using com::sun::star::sdbc::SQLException
;
115 namespace pq_sdbc_driver
117 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
119 View::View( const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
120 const Reference
< com::sun::star::sdbc::XConnection
> & connection
,
121 ConnectionSettings
*pSettings
)
123 getStatics().refl
.view
.implName
,
124 getStatics().refl
.view
.serviceNames
,
128 * getStatics().refl
.view
.pProps
)
131 Reference
< XPropertySet
> View::createDataDescriptor( ) throw (RuntimeException
)
133 ViewDescriptor
* pView
= new ViewDescriptor(
134 m_refMutex
, m_conn
, m_pSettings
);
135 pView
->copyValuesFrom( this );
137 return Reference
< XPropertySet
> ( pView
);
140 void View::rename( const ::rtl::OUString
& newName
)
141 throw (::com::sun::star::sdbc::SQLException
,
142 ::com::sun::star::container::ElementExistException
,
143 ::com::sun::star::uno::RuntimeException
)
145 MutexGuard
guard( m_refMutex
->mutex
);
147 Statics
& st
= getStatics();
149 ::rtl::OUString oldName
= extractStringProperty(this,st
.NAME
);
150 ::rtl::OUString schema
= extractStringProperty(this,st
.SCHEMA_NAME
);
151 ::rtl::OUString fullOldName
= concatQualified( schema
, oldName
);
153 OUString newTableName
;
154 OUString newSchemaName
;
155 // OOo2.0 passes schema + dot + new-table-name while
156 // OO1.1.x passes new Name without schema
157 // in case name contains a dot, it is interpreted as schema.tablename
158 if( newName
.indexOf( '.' ) >= 0 )
160 splitConcatenatedIdentifier( newName
, &newSchemaName
, &newTableName
);
164 newTableName
= newName
;
165 newSchemaName
= schema
;
167 ::rtl::OUString fullNewName
= concatQualified( newSchemaName
, newTableName
);
169 if( ! schema
.equals( newSchemaName
) )
173 OUStringBuffer
buf(128);
174 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
175 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
);
176 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("SET SCHEMA" ) );
177 bufferQuoteIdentifier( buf
, newSchemaName
);
178 Reference
< XStatement
> statement
= m_conn
->createStatement();
179 statement
->executeUpdate( buf
.makeStringAndClear() );
180 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, makeAny(newSchemaName
) );
181 disposeNoThrow( statement
);
182 schema
= newSchemaName
;
184 catch( com::sun::star::sdbc::SQLException
&e
)
186 OUStringBuffer
buf( e
.Message
);
187 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" ) );
188 e
.Message
= buf
.makeStringAndClear();
193 if( ! oldName
.equals( newTableName
) )
195 OUStringBuffer
buf(128);
196 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
197 bufferQuoteQualifiedIdentifier( buf
, schema
, oldName
);
198 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("RENAME TO" ) );
199 bufferQuoteIdentifier( buf
, newTableName
);
200 Reference
< XStatement
> statement
= m_conn
->createStatement();
201 statement
->executeUpdate( buf
.makeStringAndClear() );
202 setPropertyValue_NoBroadcast_public( st
.NAME
, makeAny(newTableName
) );
205 // inform the container of the name change !
206 if( m_pSettings
->views
.is() )
208 m_pSettings
->pViewsImpl
->rename( fullOldName
, fullNewName
);
212 Sequence
<Type
> View::getTypes() throw( RuntimeException
)
214 static cppu::OTypeCollection
*pCollection
;
217 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
220 static cppu::OTypeCollection
collection(
221 getCppuType( (Reference
< com::sun::star::sdbcx::XRename
> *) 0 ),
222 ReflectionBase::getTypes());
223 pCollection
= &collection
;
226 return pCollection
->getTypes();
229 Sequence
< sal_Int8
> View::getImplementationId() throw( RuntimeException
)
231 return getStatics().refl
.view
.implementationId
;
234 Any
View::queryInterface( const Type
& reqType
) throw (RuntimeException
)
238 ret
= ReflectionBase::queryInterface( reqType
);
239 if( ! ret
.hasValue() )
240 ret
= ::cppu::queryInterface(
242 static_cast< com::sun::star::sdbcx::XRename
* > ( this )
247 ::rtl::OUString
View::getName( ) throw (::com::sun::star::uno::RuntimeException
)
249 Statics
& st
= getStatics();
250 return concatQualified(
251 extractStringProperty( this, st
.SCHEMA_NAME
),
252 extractStringProperty( this, st
.NAME
) );
255 void View::setName( const ::rtl::OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
)
260 //____________________________________________________________________________________________
262 ViewDescriptor::ViewDescriptor(
263 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
264 const Reference
< com::sun::star::sdbc::XConnection
> & connection
,
265 ConnectionSettings
*pSettings
)
267 getStatics().refl
.viewDescriptor
.implName
,
268 getStatics().refl
.viewDescriptor
.serviceNames
,
272 * getStatics().refl
.viewDescriptor
.pProps
)
275 Reference
< XPropertySet
> ViewDescriptor::createDataDescriptor( ) throw (RuntimeException
)
277 ViewDescriptor
* pView
= new ViewDescriptor(
278 m_refMutex
, m_conn
, m_pSettings
);
279 pView
->copyValuesFrom( this );
281 return Reference
< XPropertySet
> ( pView
);