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 * 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 <sal/log.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
40 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
41 #include <com/sun/star/sdbc/SQLException.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <com/sun/star/sdbc/XParameters.hpp>
44 #include <com/sun/star/sdbc/KeyRule.hpp>
45 #include <com/sun/star/sdbcx/KeyType.hpp>
46 #include <cppuhelper/exc_hlp.hxx>
48 #include "pq_xkeys.hxx"
49 #include "pq_xkey.hxx"
50 #include "pq_statics.hxx"
51 #include "pq_tools.hxx"
53 using osl::MutexGuard
;
56 using css::beans::XPropertySet
;
58 using com::sun::star::uno::makeAny
;
59 using com::sun::star::uno::UNO_QUERY
;
60 using com::sun::star::uno::Reference
;
63 using com::sun::star::sdbc::XRow
;
64 using com::sun::star::sdbc::XStatement
;
65 using com::sun::star::sdbc::XResultSet
;
66 using com::sun::star::sdbc::XParameters
;
67 using com::sun::star::sdbc::XPreparedStatement
;
69 namespace pq_sdbc_driver
73 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
74 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
75 ConnectionSettings
*pSettings
,
76 const OUString
&schemaName
,
77 const OUString
&tableName
)
78 : Container( refMutex
, origin
, pSettings
, getStatics().KEY
),
79 m_schemaName( schemaName
),
80 m_tableName( tableName
)
86 static sal_Int32
string2keytype( const OUString
&type
)
88 sal_Int32 ret
= css::sdbcx::KeyType::UNIQUE
;
90 ret
= css::sdbcx::KeyType::PRIMARY
;
91 else if ( type
== "f" )
92 ret
= css::sdbcx::KeyType::FOREIGN
;
96 static sal_Int32
string2keyrule( const OUString
& rule
)
98 sal_Int32 ret
= css::sdbc::KeyRule::NO_ACTION
;
100 ret
= css::sdbc::KeyRule::RESTRICT
;
101 else if( rule
== "c" )
102 ret
= css::sdbc::KeyRule::CASCADE
;
103 else if( rule
== "n" )
104 ret
= css::sdbc::KeyRule::SET_NULL
;
105 else if( rule
== "d" )
106 ret
= css::sdbc::KeyRule::SET_DEFAULT
;
114 SAL_INFO("connectivity.postgresql", "sdbcx.Keys get refreshed for table " << m_schemaName
<< "." << m_tableName
);
116 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
117 Statics
& st
= getStatics();
119 Int2StringMap mainMap
;
120 fillAttnum2attnameMap( mainMap
, m_origin
, m_schemaName
, m_tableName
);
122 Reference
< XPreparedStatement
> stmt
= m_origin
->prepareStatement(
123 "SELECT conname, " // 1
127 "class2.relname, " // 5
128 "nmsp2.nspname, " // 6
131 "FROM pg_constraint INNER JOIN pg_class ON conrelid = pg_class.oid "
132 "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
133 "LEFT JOIN pg_class AS class2 ON confrelid = class2.oid "
134 "LEFT JOIN pg_namespace AS nmsp2 ON class2.relnamespace=nmsp2.oid "
135 "WHERE pg_class.relname = ? AND pg_namespace.nspname = ?" );
137 Reference
< XParameters
> paras( stmt
, UNO_QUERY
);
138 paras
->setString( 1 , m_tableName
);
139 paras
->setString( 2 , m_schemaName
);
140 Reference
< XResultSet
> rs
= stmt
->executeQuery();
142 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
150 new Key( m_xMutex
, m_origin
, m_pSettings
, m_schemaName
, m_tableName
);
151 Reference
< css::beans::XPropertySet
> prop
= pKey
;
153 pKey
->setPropertyValue_NoBroadcast_public(
154 st
.NAME
, makeAny( xRow
->getString( 1 ) ) );
155 sal_Int32 keyType
= string2keytype( xRow
->getString(2) );
156 pKey
->setPropertyValue_NoBroadcast_public( st
.TYPE
, makeAny( keyType
) );
157 pKey
->setPropertyValue_NoBroadcast_public(
158 st
.UPDATE_RULE
, makeAny( string2keyrule( xRow
->getString(3) ) ) );
159 pKey
->setPropertyValue_NoBroadcast_public(
160 st
.DELETE_RULE
, makeAny( string2keyrule( xRow
->getString(4) ) ) );
161 pKey
->setPropertyValue_NoBroadcast_public(
164 convertMappedIntArray2StringArray(
166 string2intarray( xRow
->getString( 7 ) ) ) ) );
168 if( css::sdbcx::KeyType::FOREIGN
== keyType
)
170 OUStringBuffer
buf( 128 );
171 buf
.append( xRow
->getString( 6 ) ).append( "." ).append( xRow
->getString( 5 ) );
172 pKey
->setPropertyValue_NoBroadcast_public(
173 st
.REFERENCED_TABLE
, makeAny( buf
.makeStringAndClear() ) );
175 Int2StringMap foreignMap
;
176 fillAttnum2attnameMap( foreignMap
, m_origin
, xRow
->getString(6), xRow
->getString(5));
177 pKey
->setPropertyValue_NoBroadcast_public(
178 st
.PRIVATE_FOREIGN_COLUMNS
,
180 convertMappedIntArray2StringArray(
182 string2intarray( xRow
->getString(8) ) ) ) );
187 map
[ xRow
->getString( 1 ) ] = keyIndex
;
188 m_values
.push_back( makeAny( prop
) );
192 m_name2index
.swap( map
);
194 catch ( css::sdbc::SQLException
& e
)
196 css::uno::Any anyEx
= cppu::getCaughtException();
197 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
201 fire( RefreshedBroadcaster( *this ) );
205 void Keys::appendByDescriptor(
206 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
208 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
210 OUStringBuffer
buf( 128 );
211 buf
.append( "ALTER TABLE " );
212 bufferQuoteQualifiedIdentifier( buf
, m_schemaName
, m_tableName
, m_pSettings
);
213 buf
.append( " ADD " );
214 bufferKey2TableConstraint( buf
, descriptor
, m_pSettings
);
216 Reference
< XStatement
> stmt
=
217 m_origin
->createStatement();
218 stmt
->executeUpdate( buf
.makeStringAndClear() );
222 void Keys::dropByIndex( sal_Int32 index
)
224 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
225 if( index
< 0 || index
>= static_cast<sal_Int32
>(m_values
.size()) )
227 throw css::lang::IndexOutOfBoundsException(
228 "TABLES: Index out of range (allowed 0 to " + OUString::number(m_values
.size() -1)
229 + ", got " + OUString::number( index
) + ")",
234 Reference
< XPropertySet
> set
;
235 m_values
[index
] >>= set
;
237 OUStringBuffer
buf( 128 );
238 buf
.append( "ALTER TABLE " );
239 bufferQuoteQualifiedIdentifier( buf
, m_schemaName
, m_tableName
, m_pSettings
);
240 buf
.append( " DROP CONSTRAINT " );
241 bufferQuoteIdentifier( buf
, extractStringProperty( set
, getStatics().NAME
), m_pSettings
);
242 m_origin
->createStatement()->executeUpdate( buf
.makeStringAndClear() );
245 Container::dropByIndex( index
);
249 css::uno::Reference
< css::beans::XPropertySet
> Keys::createDataDescriptor()
251 return new KeyDescriptor( m_xMutex
, m_origin
, m_pSettings
);
254 Reference
< css::container::XIndexAccess
> Keys::create(
255 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
256 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
257 ConnectionSettings
*pSettings
,
258 const OUString
& schemaName
,
259 const OUString
& tableName
)
261 Keys
*pKeys
= new Keys( refMutex
, origin
, pSettings
, schemaName
, tableName
);
262 Reference
< css::container::XIndexAccess
> ret
= pKeys
;
268 KeyDescriptors::KeyDescriptors(
269 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
270 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
271 ConnectionSettings
*pSettings
)
272 : Container( refMutex
, origin
, pSettings
, getStatics().KEY
)
275 Reference
< css::container::XIndexAccess
> KeyDescriptors::create(
276 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
277 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
278 ConnectionSettings
*pSettings
)
280 return new KeyDescriptors( refMutex
, origin
, pSettings
);
283 css::uno::Reference
< css::beans::XPropertySet
> KeyDescriptors::createDataDescriptor()
285 return new KeyDescriptor( m_xMutex
, m_origin
, m_pSettings
);
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */