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/config.h>
39 #include <string_view>
41 #include <sal/log.hxx>
42 #include <rtl/ref.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
45 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
46 #include <com/sun/star/sdbc/SQLException.hpp>
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #include <com/sun/star/sdbc/XParameters.hpp>
49 #include <com/sun/star/sdbc/KeyRule.hpp>
50 #include <com/sun/star/sdbcx/KeyType.hpp>
51 #include <cppuhelper/exc_hlp.hxx>
52 #include <o3tl/safeint.hxx>
55 #include "pq_xkeys.hxx"
56 #include "pq_xkey.hxx"
57 #include "pq_statics.hxx"
58 #include "pq_tools.hxx"
60 using osl::MutexGuard
;
63 using css::beans::XPropertySet
;
65 using com::sun::star::uno::Any
;
66 using com::sun::star::uno::UNO_QUERY
;
67 using com::sun::star::uno::Reference
;
70 using com::sun::star::sdbc::XRow
;
71 using com::sun::star::sdbc::XStatement
;
72 using com::sun::star::sdbc::XResultSet
;
73 using com::sun::star::sdbc::XParameters
;
74 using com::sun::star::sdbc::XPreparedStatement
;
76 namespace pq_sdbc_driver
80 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
81 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
82 ConnectionSettings
*pSettings
,
85 : Container( refMutex
, origin
, pSettings
, getStatics().KEY
),
86 m_schemaName(std::move( schemaName
)),
87 m_tableName(std::move( tableName
))
93 static sal_Int32
string2keytype( std::u16string_view type
)
95 sal_Int32 ret
= css::sdbcx::KeyType::UNIQUE
;
97 ret
= css::sdbcx::KeyType::PRIMARY
;
98 else if ( type
== u
"f" )
99 ret
= css::sdbcx::KeyType::FOREIGN
;
103 static sal_Int32
string2keyrule( std::u16string_view rule
)
105 sal_Int32 ret
= css::sdbc::KeyRule::NO_ACTION
;
107 ret
= css::sdbc::KeyRule::RESTRICT
;
108 else if( rule
== u
"c" )
109 ret
= css::sdbc::KeyRule::CASCADE
;
110 else if( rule
== u
"n" )
111 ret
= css::sdbc::KeyRule::SET_NULL
;
112 else if( rule
== u
"d" )
113 ret
= css::sdbc::KeyRule::SET_DEFAULT
;
121 SAL_INFO("connectivity.postgresql", "sdbcx.Keys get refreshed for table " << m_schemaName
<< "." << m_tableName
);
123 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
124 Statics
& st
= getStatics();
126 Int2StringMap mainMap
;
127 fillAttnum2attnameMap( mainMap
, m_origin
, m_schemaName
, m_tableName
);
129 Reference
< XPreparedStatement
> stmt
= m_origin
->prepareStatement(
130 u
"SELECT conname, " // 1
134 "class2.relname, " // 5
135 "nmsp2.nspname, " // 6
138 "FROM pg_constraint INNER JOIN pg_class ON conrelid = pg_class.oid "
139 "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
140 "LEFT JOIN pg_class AS class2 ON confrelid = class2.oid "
141 "LEFT JOIN pg_namespace AS nmsp2 ON class2.relnamespace=nmsp2.oid "
142 "WHERE pg_class.relname = ? AND pg_namespace.nspname = ?"_ustr
);
144 Reference
< XParameters
> paras( stmt
, UNO_QUERY
);
145 paras
->setString( 1 , m_tableName
);
146 paras
->setString( 2 , m_schemaName
);
147 Reference
< XResultSet
> rs
= stmt
->executeQuery();
149 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
156 rtl::Reference
<Key
> pKey
=
157 new Key( m_xMutex
, m_origin
, m_pSettings
, m_schemaName
, m_tableName
);
158 Reference
< css::beans::XPropertySet
> prop
= pKey
;
160 pKey
->setPropertyValue_NoBroadcast_public(
161 st
.NAME
, Any( xRow
->getString( 1 ) ) );
162 sal_Int32 keyType
= string2keytype( xRow
->getString(2) );
163 pKey
->setPropertyValue_NoBroadcast_public( st
.TYPE
, Any( keyType
) );
164 pKey
->setPropertyValue_NoBroadcast_public(
165 st
.UPDATE_RULE
, Any( string2keyrule( xRow
->getString(3) ) ) );
166 pKey
->setPropertyValue_NoBroadcast_public(
167 st
.DELETE_RULE
, Any( string2keyrule( xRow
->getString(4) ) ) );
168 pKey
->setPropertyValue_NoBroadcast_public(
171 convertMappedIntArray2StringArray(
173 string2intarray( xRow
->getString( 7 ) ) ) ) );
175 if( css::sdbcx::KeyType::FOREIGN
== keyType
)
177 OUString buf
= xRow
->getString( 6 ) + "." + xRow
->getString( 5 );
178 pKey
->setPropertyValue_NoBroadcast_public(
179 st
.REFERENCED_TABLE
, Any( buf
) );
181 Int2StringMap foreignMap
;
182 fillAttnum2attnameMap( foreignMap
, m_origin
, xRow
->getString(6), xRow
->getString(5));
183 pKey
->setPropertyValue_NoBroadcast_public(
184 st
.PRIVATE_FOREIGN_COLUMNS
,
186 convertMappedIntArray2StringArray(
188 string2intarray( xRow
->getString(8) ) ) ) );
193 map
[ xRow
->getString( 1 ) ] = keyIndex
;
194 m_values
.push_back( Any( prop
) );
198 m_name2index
.swap( map
);
200 catch ( css::sdbc::SQLException
& e
)
202 css::uno::Any anyEx
= cppu::getCaughtException();
203 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
207 fire( RefreshedBroadcaster( *this ) );
211 void Keys::appendByDescriptor(
212 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
214 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
216 OUStringBuffer
buf( 128 );
217 buf
.append( "ALTER TABLE " );
218 bufferQuoteQualifiedIdentifier( buf
, m_schemaName
, m_tableName
, m_pSettings
);
219 buf
.append( " ADD " );
220 bufferKey2TableConstraint( buf
, descriptor
, m_pSettings
);
222 Reference
< XStatement
> stmt
=
223 m_origin
->createStatement();
224 stmt
->executeUpdate( buf
.makeStringAndClear() );
228 void Keys::dropByIndex( sal_Int32 index
)
230 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
231 if( index
< 0 || o3tl::make_unsigned(index
) >= m_values
.size() )
233 throw css::lang::IndexOutOfBoundsException(
234 "TABLES: Index out of range (allowed 0 to " + OUString::number(m_values
.size() -1)
235 + ", got " + OUString::number( index
) + ")",
240 Reference
< XPropertySet
> set
;
241 m_values
[index
] >>= set
;
243 OUStringBuffer
buf( 128 );
244 buf
.append( "ALTER TABLE " );
245 bufferQuoteQualifiedIdentifier( buf
, m_schemaName
, m_tableName
, m_pSettings
);
246 buf
.append( " DROP CONSTRAINT " );
247 bufferQuoteIdentifier( buf
, extractStringProperty( set
, getStatics().NAME
), m_pSettings
);
248 m_origin
->createStatement()->executeUpdate( buf
.makeStringAndClear() );
251 Container::dropByIndex( index
);
255 css::uno::Reference
< css::beans::XPropertySet
> Keys::createDataDescriptor()
257 return new KeyDescriptor( m_xMutex
, m_origin
, m_pSettings
);
260 Reference
< css::container::XIndexAccess
> Keys::create(
261 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
262 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
263 ConnectionSettings
*pSettings
,
264 const OUString
& schemaName
,
265 const OUString
& tableName
)
267 rtl::Reference
<Keys
> pKeys
= new Keys( refMutex
, origin
, pSettings
, schemaName
, tableName
);
273 KeyDescriptors::KeyDescriptors(
274 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
275 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
276 ConnectionSettings
*pSettings
)
277 : Container( refMutex
, origin
, pSettings
, getStatics().KEY
)
280 Reference
< css::container::XIndexAccess
> KeyDescriptors::create(
281 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
282 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
283 ConnectionSettings
*pSettings
)
285 return new KeyDescriptors( refMutex
, origin
, pSettings
);
288 css::uno::Reference
< css::beans::XPropertySet
> KeyDescriptors::createDataDescriptor()
290 return new KeyDescriptor( m_xMutex
, m_origin
, m_pSettings
);
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */