LibreOffice Module connectivity (master) 1
pq_xkeys.cxx
Go to the documentation of this file.
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 <sal/config.h>
38
39#include <string_view>
40
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>
52#include <o3tl/safeint.hxx>
53#include <utility>
54
55#include "pq_xkeys.hxx"
56#include "pq_xkey.hxx"
57#include "pq_statics.hxx"
58#include "pq_tools.hxx"
59
60using osl::MutexGuard;
61
62
63using css::beans::XPropertySet;
64
65using com::sun::star::uno::Any;
66using com::sun::star::uno::UNO_QUERY;
68
69
70using com::sun::star::sdbc::XRow;
71using com::sun::star::sdbc::XStatement;
72using com::sun::star::sdbc::XResultSet;
73using com::sun::star::sdbc::XParameters;
74using com::sun::star::sdbc::XPreparedStatement;
75
76namespace pq_sdbc_driver
77{
78
80 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
81 const css::uno::Reference< css::sdbc::XConnection > & origin,
82 ConnectionSettings *pSettings,
83 OUString schemaName,
84 OUString tableName)
85 : Container( refMutex, origin, pSettings, getStatics().KEY ),
86 m_schemaName(std::move( schemaName )),
87 m_tableName(std::move( tableName ))
88{}
89
91{}
92
93static sal_Int32 string2keytype( std::u16string_view type )
94{
95 sal_Int32 ret = css::sdbcx::KeyType::UNIQUE;
96 if ( type == u"p" )
97 ret = css::sdbcx::KeyType::PRIMARY;
98 else if ( type == u"f" )
99 ret = css::sdbcx::KeyType::FOREIGN;
100 return ret;
101}
102
103static sal_Int32 string2keyrule( std::u16string_view rule )
104{
105 sal_Int32 ret = css::sdbc::KeyRule::NO_ACTION;
106 if( rule == u"r" )
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;
114 return ret;
115}
116
118{
119 try
120 {
121 SAL_INFO("connectivity.postgresql", "sdbcx.Keys get refreshed for table " << m_schemaName << "." << m_tableName);
122
123 osl::MutexGuard guard( m_xMutex->GetMutex() );
124 Statics & st = getStatics();
125
126 Int2StringMap mainMap;
128
129 Reference< XPreparedStatement > stmt = m_origin->prepareStatement(
130 "SELECT conname, " // 1
131 "contype, " // 2
132 "confupdtype, " // 3
133 "confdeltype, " // 4
134 "class2.relname, " // 5
135 "nmsp2.nspname, " // 6
136 "conkey," // 7
137 "confkey " // 8
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 = ?" );
143
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();
148
149 Reference< XRow > xRow( rs , UNO_QUERY );
150
152 m_values.clear();
153 int keyIndex = 0;
154 while( rs->next() )
155 {
159
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(
170 Any(
172 mainMap,
173 string2intarray( xRow->getString( 7 ) ) ) ) );
174
175 if( css::sdbcx::KeyType::FOREIGN == keyType )
176 {
177 OUString buf = xRow->getString( 6 ) + "." + xRow->getString( 5 );
178 pKey->setPropertyValue_NoBroadcast_public(
179 st.REFERENCED_TABLE, Any( buf ) );
180
181 Int2StringMap foreignMap;
182 fillAttnum2attnameMap( foreignMap, m_origin, xRow->getString(6), xRow->getString(5));
183 pKey->setPropertyValue_NoBroadcast_public(
185 Any(
187 foreignMap,
188 string2intarray( xRow->getString(8) ) ) ) );
189 }
190
191
192 {
193 map[ xRow->getString( 1 ) ] = keyIndex;
194 m_values.push_back( Any( prop ) );
195 ++keyIndex;
196 }
197 }
198 m_name2index.swap( map );
199 }
200 catch ( css::sdbc::SQLException & e )
201 {
202 css::uno::Any anyEx = cppu::getCaughtException();
203 throw css::lang::WrappedTargetRuntimeException( e.Message,
204 e.Context, anyEx );
205 }
206
207 fire( RefreshedBroadcaster( *this ) );
208}
209
210
212 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
213{
214 osl::MutexGuard guard( m_xMutex->GetMutex() );
215
216 OUStringBuffer buf( 128 );
217 buf.append( "ALTER TABLE " );
219 buf.append( " ADD " );
220 bufferKey2TableConstraint( buf, descriptor, m_pSettings );
221
223 m_origin->createStatement();
224 stmt->executeUpdate( buf.makeStringAndClear() );
225}
226
227
228void Keys::dropByIndex( sal_Int32 index )
229{
230 osl::MutexGuard guard( m_xMutex->GetMutex() );
231 if( index < 0 || o3tl::make_unsigned(index) >= m_values.size() )
232 {
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 ) + ")",
236 *this );
237 }
238
239
241 m_values[index] >>= set;
242
243 OUStringBuffer buf( 128 );
244 buf.append( "ALTER TABLE " );
246 buf.append( " DROP CONSTRAINT " );
248 m_origin->createStatement()->executeUpdate( buf.makeStringAndClear() );
249
250
252}
253
254
255css::uno::Reference< css::beans::XPropertySet > Keys::createDataDescriptor()
256{
258}
259
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)
266{
267 rtl::Reference<Keys> pKeys = new Keys( refMutex, origin, pSettings, schemaName, tableName );
268 pKeys->refresh();
269
270 return pKeys;
271}
272
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 )
278{}
279
281 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
282 const css::uno::Reference< css::sdbc::XConnection > & origin,
283 ConnectionSettings *pSettings)
284{
285 return new KeyDescriptors( refMutex, origin, pSettings );
286}
287
288css::uno::Reference< css::beans::XPropertySet > KeyDescriptors::createDataDescriptor()
289{
291}
292
293};
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NAME
ConnectionSettings * m_pSettings
void fire(const EventBroadcastHelper &helper)
css::uno::Reference< css::sdbc::XConnection > m_origin
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
std::vector< css::uno::Any > m_values
KeyDescriptors(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xkeys.cxx:273
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
Definition: pq_xkeys.cxx:288
static css::uno::Reference< css::container::XIndexAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
Definition: pq_xkeys.cxx:280
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
Definition: pq_xkeys.cxx:255
Keys(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, OUString schemaName, OUString tableName)
Definition: pq_xkeys.cxx:79
virtual void SAL_CALL refresh() override
Definition: pq_xkeys.cxx:117
OUString m_tableName
Definition: pq_xkeys.hxx:46
OUString m_schemaName
Definition: pq_xkeys.hxx:45
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
Definition: pq_xkeys.cxx:228
virtual void SAL_CALL appendByDescriptor(const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
Definition: pq_xkeys.cxx:211
static css::uno::Reference< css::container::XIndexAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, const OUString &schemaName, const OUString &tableName)
Definition: pq_xkeys.cxx:260
virtual ~Keys() override
Definition: pq_xkeys.cxx:90
float u
#define SAL_INFO(area, stream)
void set(css::uno::UnoInterfaceReference const &value)
Any SAL_CALL getCaughtException()
index
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Statics & getStatics()
Definition: pq_statics.cxx:112
void bufferQuoteQualifiedIdentifier(OUStringBuffer &buf, std::u16string_view schema, std::u16string_view table, ConnectionSettings *settings)
Definition: pq_tools.cxx:181
css::uno::Sequence< sal_Int32 > string2intarray(const OUString &str)
Definition: pq_tools.cxx:843
std::unordered_map< OUString, sal_Int32 > String2IntMap
OUString extractStringProperty(const Reference< XPropertySet > &descriptor, const OUString &name)
Definition: pq_tools.cxx:204
Sequence< OUString > convertMappedIntArray2StringArray(const Int2StringMap &map, const Sequence< sal_Int32 > &intArray)
Definition: pq_tools.cxx:917
void bufferQuoteIdentifier(OUStringBuffer &buf, std::u16string_view toQuote, ConnectionSettings *settings)
Definition: pq_tools.cxx:175
static sal_Int32 string2keyrule(std::u16string_view rule)
Definition: pq_xkeys.cxx:103
static sal_Int32 string2keytype(std::u16string_view type)
Definition: pq_xkeys.cxx:93
std::unordered_map< sal_Int32, OUString > Int2StringMap
void bufferKey2TableConstraint(OUStringBuffer &buf, const Reference< XPropertySet > &key, ConnectionSettings *settings)
Definition: pq_tools.cxx:991
void fillAttnum2attnameMap(Int2StringMap &map, const Reference< css::sdbc::XConnection > &conn, const OUString &schema, const OUString &table)
Definition: pq_tools.cxx:707
sal_Int32 type
Definition: pq_statics.cxx:60
const char * tableName
Definition: pq_statics.cxx:57
std::map< OUString, rtl::Reference< Entity > > map
OUString PRIVATE_FOREIGN_COLUMNS
Definition: pq_statics.hxx:186