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 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
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/
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
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
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.
56 ************************************************************************/
58 #include <rtl/ustrbuf.hxx>
60 #include <cppuhelper/typeprovider.hxx>
61 #include <cppuhelper/queryinterface.hxx>
63 #include <com/sun/star/beans/PropertyAttribute.hpp>
65 #include <com/sun/star/sdbc/XRow.hpp>
66 #include <com/sun/star/sdbc/XParameters.hpp>
68 #include "pq_xview.hxx"
69 #include "pq_xviews.hxx"
70 #include "pq_statics.hxx"
71 #include "pq_tools.hxx"
73 using osl::MutexGuard
;
77 using rtl::OUStringBuffer
;
78 using rtl::OUStringToOString
;
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
;
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
;
95 using com::sun::star::lang::IllegalArgumentException
;
96 using com::sun::star::lang::IndexOutOfBoundsException
;
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
;
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
;
111 namespace pq_sdbc_driver
113 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
115 View::View( const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
116 const Reference
< com::sun::star::sdbc::XConnection
> & connection
,
117 ConnectionSettings
*pSettings
)
119 getStatics().refl
.view
.implName
,
120 getStatics().refl
.view
.serviceNames
,
124 * getStatics().refl
.view
.pProps
)
127 Reference
< XPropertySet
> View::createDataDescriptor( ) throw (RuntimeException
)
129 ViewDescriptor
* pView
= new ViewDescriptor(
130 m_refMutex
, m_conn
, m_pSettings
);
131 pView
->copyValuesFrom( this );
133 return Reference
< XPropertySet
> ( pView
);
136 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
)
141 MutexGuard
guard( m_refMutex
->mutex
);
143 Statics
& st
= getStatics();
145 ::rtl::OUString oldName
= extractStringProperty(this,st
.NAME
);
146 ::rtl::OUString schema
= extractStringProperty(this,st
.SCHEMA_NAME
);
147 ::rtl::OUString fullOldName
= concatQualified( schema
, oldName
);
149 OUString newTableName
;
150 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 if( newName
.indexOf( '.' ) >= 0 )
156 splitConcatenatedIdentifier( newName
, &newSchemaName
, &newTableName
);
160 newTableName
= newName
;
161 newSchemaName
= schema
;
163 ::rtl::OUString fullNewName
= concatQualified( newSchemaName
, newTableName
);
165 if( ! schema
.equals( newSchemaName
) )
169 OUStringBuffer
buf(128);
170 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
171 bufferQuoteQualifiedIdentifier(buf
, schema
, oldName
, m_pSettings
);
172 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("SET SCHEMA" ) );
173 bufferQuoteIdentifier( buf
, newSchemaName
, m_pSettings
);
174 Reference
< XStatement
> statement
= m_conn
->createStatement();
175 statement
->executeUpdate( buf
.makeStringAndClear() );
176 setPropertyValue_NoBroadcast_public( st
.SCHEMA_NAME
, makeAny(newSchemaName
) );
177 disposeNoThrow( statement
);
178 schema
= newSchemaName
;
180 catch( com::sun::star::sdbc::SQLException
&e
)
182 OUStringBuffer
buf( e
.Message
);
183 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "(NOTE: Only postgresql server >= V8.1 support changing a table's schema)" ) );
184 e
.Message
= buf
.makeStringAndClear();
189 if( ! oldName
.equals( newTableName
) )
191 OUStringBuffer
buf(128);
192 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
193 bufferQuoteQualifiedIdentifier( buf
, schema
, oldName
, m_pSettings
);
194 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("RENAME TO" ) );
195 bufferQuoteIdentifier( buf
, newTableName
, m_pSettings
);
196 Reference
< XStatement
> statement
= m_conn
->createStatement();
197 statement
->executeUpdate( buf
.makeStringAndClear() );
198 setPropertyValue_NoBroadcast_public( st
.NAME
, makeAny(newTableName
) );
201 // inform the container of the name change !
202 if( m_pSettings
->views
.is() )
204 m_pSettings
->pViewsImpl
->rename( fullOldName
, fullNewName
);
208 Sequence
<Type
> View::getTypes() throw( RuntimeException
)
210 static cppu::OTypeCollection
*pCollection
;
213 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
216 static cppu::OTypeCollection
collection(
217 getCppuType( (Reference
< com::sun::star::sdbcx::XRename
> *) 0 ),
218 ReflectionBase::getTypes());
219 pCollection
= &collection
;
222 return pCollection
->getTypes();
225 Sequence
< sal_Int8
> View::getImplementationId() throw( RuntimeException
)
227 return getStatics().refl
.view
.implementationId
;
230 Any
View::queryInterface( const Type
& reqType
) throw (RuntimeException
)
234 ret
= ReflectionBase::queryInterface( reqType
);
235 if( ! ret
.hasValue() )
236 ret
= ::cppu::queryInterface(
238 static_cast< com::sun::star::sdbcx::XRename
* > ( this )
243 ::rtl::OUString
View::getName( ) throw (::com::sun::star::uno::RuntimeException
)
245 Statics
& st
= getStatics();
246 return concatQualified(
247 extractStringProperty( this, st
.SCHEMA_NAME
),
248 extractStringProperty( this, st
.NAME
) );
251 void View::setName( const ::rtl::OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
)
256 //____________________________________________________________________________________________
258 ViewDescriptor::ViewDescriptor(
259 const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
260 const Reference
< com::sun::star::sdbc::XConnection
> & connection
,
261 ConnectionSettings
*pSettings
)
263 getStatics().refl
.viewDescriptor
.implName
,
264 getStatics().refl
.viewDescriptor
.serviceNames
,
268 * getStatics().refl
.viewDescriptor
.pProps
)
271 Reference
< XPropertySet
> ViewDescriptor::createDataDescriptor( ) throw (RuntimeException
)
273 ViewDescriptor
* pView
= new ViewDescriptor(
274 m_refMutex
, m_conn
, m_pSettings
);
275 pView
->copyValuesFrom( this );
277 return Reference
< XPropertySet
> ( pView
);