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 <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>
43 #include "pq_xcolumns.hxx"
44 #include "pq_xkeycolumns.hxx"
45 #include "pq_xkeycolumn.hxx"
46 #include "pq_statics.hxx"
47 #include "pq_tools.hxx"
49 using osl::MutexGuard
;
51 using com::sun::star::beans::XPropertySet
;
53 using com::sun::star::uno::Any
;
54 using com::sun::star::uno::makeAny
;
55 using com::sun::star::uno::UNO_QUERY
;
56 using com::sun::star::uno::Reference
;
57 using com::sun::star::uno::Sequence
;
59 using com::sun::star::sdbc::XRow
;
60 using com::sun::star::sdbc::XResultSet
;
61 using com::sun::star::sdbc::XDatabaseMetaData
;
62 using com::sun::star::sdbc::SQLException
;
64 namespace pq_sdbc_driver
67 KeyColumns::KeyColumns(
68 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
69 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
70 ConnectionSettings
*pSettings
,
71 const OUString
&schemaName
,
72 const OUString
&tableName
,
73 const Sequence
< OUString
> &columnNames
,
74 const Sequence
< OUString
> &foreignColumnNames
)
75 : Container( refMutex
, origin
, pSettings
, "KEY_COLUMN" ),
76 m_schemaName( schemaName
),
77 m_tableName( tableName
),
78 m_columnNames( columnNames
),
79 m_foreignColumnNames( foreignColumnNames
)
82 KeyColumns::~KeyColumns()
86 void KeyColumns::refresh()
90 SAL_INFO("connectivity.postgresql", "sdbcx.KeyColumns get refreshed for table " << m_schemaName
<< "." << m_tableName
);
92 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
94 Statics
&st
= getStatics();
95 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
97 Reference
< XResultSet
> rs
=
98 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
100 DisposeGuard
disposeIt( rs
);
101 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
106 sal_Int32 columnIndex
= 0;
109 OUString columnName
= xRow
->getString( 4 );
112 for( keyindex
= 0 ; keyindex
< m_columnNames
.getLength() ; keyindex
++ )
114 if( columnName
== m_columnNames
[keyindex
] )
117 if( m_columnNames
.getLength() == keyindex
)
120 KeyColumn
* pKeyColumn
=
121 new KeyColumn( m_xMutex
, m_origin
, m_pSettings
);
122 Reference
< css::beans::XPropertySet
> prop
= pKeyColumn
;
124 OUString name
= columnMetaData2SDBCX( pKeyColumn
, xRow
);
125 if( keyindex
< m_foreignColumnNames
.getLength() )
127 pKeyColumn
->setPropertyValue_NoBroadcast_public(
128 st
.RELATED_COLUMN
, makeAny( m_foreignColumnNames
[keyindex
]) );
132 m_values
.push_back( makeAny( prop
) );
133 map
[ name
] = columnIndex
;
137 m_name2index
.swap( map
);
139 catch ( css::sdbc::SQLException
& e
)
141 css::uno::Any anyEx
= cppu::getCaughtException();
142 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
146 fire( RefreshedBroadcaster( *this ) );
150 void KeyColumns::appendByDescriptor(
151 const css::uno::Reference
< css::beans::XPropertySet
>& )
153 throw css::sdbc::SQLException(
154 "KeyColumns::appendByDescriptor not implemented yet",
155 *this, OUString(), 1, Any() );
157 // osl::MutexGuard guard( m_xMutex->GetMutex() );
158 // Statics & st = getStatics();
159 // Reference< XPropertySet > past = createDataDescriptor();
160 // past->setPropertyValue( st.IS_NULLABLE, makeAny( css::sdbc::ColumnValue::NULLABLE ) );
161 // alterColumnByDescriptor(
162 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
167 void KeyColumns::dropByIndex( sal_Int32
)
169 throw css::sdbc::SQLException(
170 "KeyColumns::dropByIndex not implemented yet",
171 *this, OUString(), 1, Any() );
172 // osl::MutexGuard guard( m_xMutex->GetMutex() );
173 // if( index < 0 || index >= m_values.getLength() )
175 // OUStringBuffer buf( 128 );
176 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
177 // buf.append((sal_Int32)(m_values.getLength() -1) );
178 // buf.appendAscii( ", got " );
179 // buf.append( index );
180 // buf.appendAscii( ")" );
181 // throw css::lang::IndexOutOfBoundsException(
182 // buf.makeStringAndClear(), *this );
185 // Reference< XPropertySet > set;
186 // m_values[index] >>= set;
187 // Statics &st = getStatics();
189 // set->getPropertyValue( st.NAME ) >>= name;
191 // OUStringBuffer update( 128 );
192 // update.appendAscii( "ALTER TABLE ONLY");
193 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
194 // update.appendAscii( "DROP COLUMN" );
195 // bufferQuoteIdentifier( update, name );
196 // Reference< XStatement > stmt = m_origin->createStatement( );
197 // DisposeGuard disposeIt( stmt );
198 // stmt->executeUpdate( update.makeStringAndClear() );
203 Reference
< css::beans::XPropertySet
> KeyColumns::createDataDescriptor()
205 return new KeyColumnDescriptor( m_xMutex
, m_origin
, m_pSettings
);
208 Reference
< css::container::XNameAccess
> KeyColumns::create(
209 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
210 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
211 ConnectionSettings
*pSettings
,
212 const OUString
&schemaName
,
213 const OUString
&tableName
,
214 const Sequence
< OUString
> &columnNames
,
215 const Sequence
< OUString
> &foreignColumnNames
)
217 KeyColumns
*pKeyColumns
= new KeyColumns(
218 refMutex
, origin
, pSettings
, schemaName
, tableName
, columnNames
, foreignColumnNames
);
219 Reference
< css::container::XNameAccess
> ret
= pKeyColumns
;
220 pKeyColumns
->refresh();
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
, "KEY_COLUMN" )
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: */