Use correct object
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_xkeycolumns.cxx
blobf44899d41a2ff1b6c7e18af8beb60f7739a29551
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,
18 * MA 02111-1307 USA
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 <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
39 #include <com/sun/star/sdbc/SQLException.hpp>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include <rtl/ref.hxx>
43 #include <utility>
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"
51 using osl::MutexGuard;
53 using com::sun::star::beans::XPropertySet;
55 using com::sun::star::uno::Any;
56 using com::sun::star::uno::UNO_QUERY;
57 using com::sun::star::uno::Reference;
58 using com::sun::star::uno::Sequence;
60 using com::sun::star::sdbc::XRow;
61 using com::sun::star::sdbc::XResultSet;
62 using com::sun::star::sdbc::XDatabaseMetaData;
63 using com::sun::star::sdbc::SQLException;
65 namespace pq_sdbc_driver
68 KeyColumns::KeyColumns(
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, u"KEY_COLUMN"_ustr ),
77 m_schemaName(std::move( schemaName )),
78 m_tableName(std::move( tableName )),
79 m_columnNames( columnNames ),
80 m_foreignColumnNames( foreignColumnNames )
83 KeyColumns::~KeyColumns()
87 void KeyColumns::refresh()
89 try
91 SAL_INFO("connectivity.postgresql", "sdbcx.KeyColumns get refreshed for table " << m_schemaName << "." << m_tableName);
93 osl::MutexGuard guard( m_xMutex->GetMutex() );
95 Statics &st = getStatics();
96 Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
98 Reference< XResultSet > rs =
99 meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
101 DisposeGuard disposeIt( rs );
102 Reference< XRow > xRow( rs , UNO_QUERY );
104 String2IntMap map;
106 m_values.clear();
107 sal_Int32 columnIndex = 0;
108 while( rs->next() )
110 OUString columnName = xRow->getString( 4 );
112 int keyindex;
113 for( keyindex = 0 ; keyindex < m_columnNames.getLength() ; keyindex ++ )
115 if( columnName == m_columnNames[keyindex] )
116 break;
118 if( m_columnNames.getLength() == keyindex )
119 continue;
121 rtl::Reference<KeyColumn> pKeyColumn =
122 new KeyColumn( m_xMutex, m_origin, m_pSettings );
123 Reference< css::beans::XPropertySet > prop = pKeyColumn;
125 OUString name = columnMetaData2SDBCX( pKeyColumn.get(), xRow );
126 if( keyindex < m_foreignColumnNames.getLength() )
128 pKeyColumn->setPropertyValue_NoBroadcast_public(
129 st.RELATED_COLUMN, Any( m_foreignColumnNames[keyindex]) );
133 m_values.emplace_back(prop);
134 map[ name ] = columnIndex;
135 ++columnIndex;
138 m_name2index.swap( map );
140 catch ( css::sdbc::SQLException & e )
142 css::uno::Any anyEx = cppu::getCaughtException();
143 throw css::lang::WrappedTargetRuntimeException( e.Message,
144 e.Context, anyEx );
147 fire( RefreshedBroadcaster( *this ) );
151 void KeyColumns::appendByDescriptor(
152 const css::uno::Reference< css::beans::XPropertySet >& )
154 throw css::sdbc::SQLException(
155 u"KeyColumns::appendByDescriptor not implemented yet"_ustr,
156 *this, OUString(), 1, Any() );
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 );
168 void KeyColumns::dropByIndex( sal_Int32 )
170 throw css::sdbc::SQLException(
171 u"KeyColumns::dropByIndex not implemented yet"_ustr,
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 // }
186 // Reference< XPropertySet > set;
187 // m_values[index] >>= set;
188 // Statics &st = getStatics();
189 // OUString name;
190 // set->getPropertyValue( st.NAME ) >>= name;
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() );
204 Reference< css::beans::XPropertySet > KeyColumns::createDataDescriptor()
206 return new KeyColumnDescriptor( m_xMutex, m_origin, m_pSettings );
209 Reference< css::container::XNameAccess > KeyColumns::create(
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 )
218 rtl::Reference<KeyColumns> pKeyColumns = new KeyColumns(
219 refMutex, origin, pSettings, schemaName, tableName, columnNames, foreignColumnNames );
220 pKeyColumns->refresh();
222 return pKeyColumns;
226 KeyColumnDescriptors::KeyColumnDescriptors(
227 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
228 const css::uno::Reference< css::sdbc::XConnection > & origin,
229 ConnectionSettings *pSettings )
230 : Container( refMutex, origin, pSettings, u"KEY_COLUMN"_ustr )
233 Reference< css::beans::XPropertySet > KeyColumnDescriptors::createDataDescriptor()
235 return new KeyColumnDescriptor( m_xMutex, m_origin, m_pSettings );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */