LibreOffice Module connectivity (master) 1
pq_xkeycolumns.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/log.hxx>
38#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
39#include <com/sun/star/sdbc/SQLException.hpp>
40#include <com/sun/star/sdbc/XRow.hpp>
42#include <rtl/ref.hxx>
43#include <utility>
44
45#include "pq_xcolumns.hxx"
46#include "pq_xkeycolumns.hxx"
47#include "pq_xkeycolumn.hxx"
48#include "pq_statics.hxx"
49#include "pq_tools.hxx"
50
51using osl::MutexGuard;
52
54
55using com::sun::star::uno::Any;
56using com::sun::star::uno::UNO_QUERY;
59
60using com::sun::star::sdbc::XRow;
61using com::sun::star::sdbc::XResultSet;
62using com::sun::star::sdbc::XDatabaseMetaData;
63using com::sun::star::sdbc::SQLException;
64
65namespace pq_sdbc_driver
66{
67
69 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
70 const css::uno::Reference< css::sdbc::XConnection > & origin,
71 ConnectionSettings *pSettings,
72 OUString schemaName,
73 OUString tableName,
74 const Sequence< OUString > &columnNames,
75 const Sequence< OUString > &foreignColumnNames )
76 : Container( refMutex, origin, pSettings, "KEY_COLUMN" ),
77 m_schemaName(std::move( schemaName )),
78 m_tableName(std::move( tableName )),
79 m_columnNames( columnNames ),
80 m_foreignColumnNames( foreignColumnNames )
81{}
82
84{}
85
86
88{
89 try
90 {
91 SAL_INFO("connectivity.postgresql", "sdbcx.KeyColumns get refreshed for table " << m_schemaName << "." << m_tableName);
92
93 osl::MutexGuard guard( m_xMutex->GetMutex() );
94
95 Statics &st = getStatics();
96 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
97
99 meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
100
101 DisposeGuard disposeIt( rs );
102 Reference< XRow > xRow( rs , UNO_QUERY );
103
105
106 m_values.clear();
107 sal_Int32 columnIndex = 0;
108 while( rs->next() )
109 {
110 OUString columnName = xRow->getString( 4 );
111
112 int keyindex;
113 for( keyindex = 0 ; keyindex < m_columnNames.getLength() ; keyindex ++ )
114 {
115 if( columnName == m_columnNames[keyindex] )
116 break;
117 }
118 if( m_columnNames.getLength() == keyindex )
119 continue;
120
121 rtl::Reference<KeyColumn> pKeyColumn =
124
125 OUString name = columnMetaData2SDBCX( pKeyColumn.get(), xRow );
126 if( keyindex < m_foreignColumnNames.getLength() )
127 {
128 pKeyColumn->setPropertyValue_NoBroadcast_public(
129 st.RELATED_COLUMN, Any( m_foreignColumnNames[keyindex]) );
130 }
131
132 {
133 m_values.push_back( Any( prop ) );
134 map[ name ] = columnIndex;
135 ++columnIndex;
136 }
137 }
138 m_name2index.swap( map );
139 }
140 catch ( css::sdbc::SQLException & e )
141 {
142 css::uno::Any anyEx = cppu::getCaughtException();
143 throw css::lang::WrappedTargetRuntimeException( e.Message,
144 e.Context, anyEx );
145 }
146
147 fire( RefreshedBroadcaster( *this ) );
148}
149
150
152 const css::uno::Reference< css::beans::XPropertySet >& )
153{
154 throw css::sdbc::SQLException(
155 "KeyColumns::appendByDescriptor not implemented yet",
156 *this, OUString(), 1, Any() );
157
158// osl::MutexGuard guard( m_xMutex->GetMutex() );
159// Statics & st = getStatics();
160// Reference< XPropertySet > past = createDataDescriptor();
161// past->setPropertyValue( st.IS_NULLABLE, makeAny( css::sdbc::ColumnValue::NULLABLE ) );
162// alterColumnByDescriptor(
163// m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
164
165}
166
167
168void KeyColumns::dropByIndex( sal_Int32 )
169{
170 throw css::sdbc::SQLException(
171 "KeyColumns::dropByIndex not implemented yet",
172 *this, OUString(), 1, Any() );
173// osl::MutexGuard guard( m_xMutex->GetMutex() );
174// if( index < 0 || index >= m_values.getLength() )
175// {
176// OUStringBuffer buf( 128 );
177// buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
178// buf.append((sal_Int32)(m_values.getLength() -1) );
179// buf.appendAscii( ", got " );
180// buf.append( index );
181// buf.appendAscii( ")" );
182// throw css::lang::IndexOutOfBoundsException(
183// buf.makeStringAndClear(), *this );
184// }
185
186// Reference< XPropertySet > set;
187// m_values[index] >>= set;
188// Statics &st = getStatics();
189// OUString name;
190// set->getPropertyValue( st.NAME ) >>= name;
191
192// OUStringBuffer update( 128 );
193// update.appendAscii( "ALTER TABLE ONLY");
194// bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
195// update.appendAscii( "DROP COLUMN" );
196// bufferQuoteIdentifier( update, name );
197// Reference< XStatement > stmt = m_origin->createStatement( );
198// DisposeGuard disposeIt( stmt );
199// stmt->executeUpdate( update.makeStringAndClear() );
200
201}
202
203
205{
207}
208
210 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
211 const css::uno::Reference< css::sdbc::XConnection > & origin,
212 ConnectionSettings *pSettings,
213 const OUString &schemaName,
214 const OUString &tableName,
215 const Sequence< OUString > &columnNames ,
216 const Sequence< OUString > &foreignColumnNames )
217{
218 rtl::Reference<KeyColumns> pKeyColumns = new KeyColumns(
219 refMutex, origin, pSettings, schemaName, tableName, columnNames, foreignColumnNames );
220 pKeyColumns->refresh();
221
222 return pKeyColumns;
223}
224
225
227 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
228 const css::uno::Reference< css::sdbc::XConnection > & origin,
229 ConnectionSettings *pSettings )
230 : Container( refMutex, origin, pSettings, "KEY_COLUMN" )
231{}
232
234{
236}
237};
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ConnectionSettings * m_pSettings
void fire(const EventBroadcastHelper &helper)
css::uno::Reference< css::sdbc::XConnection > m_origin
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
std::vector< css::uno::Any > m_values
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
KeyColumnDescriptors(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings)
virtual void SAL_CALL refresh() override
virtual ~KeyColumns() override
static css::uno::Reference< css::container::XNameAccess > create(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, const OUString &schemaName, const OUString &tableName, const css::uno::Sequence< OUString > &keyColumns, const css::uno::Sequence< OUString > &foreignColumnNames)
virtual void SAL_CALL appendByDescriptor(const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
KeyColumns(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, const css::uno::Reference< css::sdbc::XConnection > &origin, ConnectionSettings *pSettings, OUString schemaName, OUString tableName, const css::uno::Sequence< OUString > &keyColumns, const css::uno::Sequence< OUString > &foreignColumnNames)
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL createDataDescriptor() override
css::uno::Sequence< OUString > m_columnNames
css::uno::Sequence< OUString > m_foreignColumnNames
#define SAL_INFO(area, stream)
class SAL_NO_VTABLE XPropertySet
Any SAL_CALL getCaughtException()
Statics & getStatics()
Definition: pq_statics.cxx:112
std::unordered_map< OUString, sal_Int32 > String2IntMap
OUString columnMetaData2SDBCX(ReflectionBase *pBase, const css::uno::Reference< css::sdbc::XRow > &xRow)
const char * columnName
Definition: pq_statics.cxx:56
OUString name
Definition: pq_statics.cxx:74
const char * tableName
Definition: pq_statics.cxx:57
std::map< OUString, rtl::Reference< Entity > > map