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 <rtl/ustrbuf.hxx>
38 #include <rtl/strbuf.hxx>
39 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 #include <com/sun/star/sdbc/SQLException.hpp>
41 #include <com/sun/star/sdbc/XRow.hpp>
42 #include <com/sun/star/sdbc/DataType.hpp>
43 #include <com/sun/star/sdbc/ColumnValue.hpp>
44 #include <cppuhelper/exc_hlp.hxx>
46 #include "pq_xcolumns.hxx"
47 #include "pq_xkeycolumns.hxx"
48 #include "pq_xkeycolumn.hxx"
49 #include "pq_statics.hxx"
50 #include "pq_tools.hxx"
52 using osl::MutexGuard
;
54 using com::sun::star::beans::XPropertySet
;
56 using com::sun::star::uno::Any
;
57 using com::sun::star::uno::makeAny
;
58 using com::sun::star::uno::UNO_QUERY
;
59 using com::sun::star::uno::Reference
;
60 using com::sun::star::uno::Sequence
;
62 using com::sun::star::sdbc::XRow
;
63 using com::sun::star::sdbc::XResultSet
;
64 using com::sun::star::sdbc::XDatabaseMetaData
;
65 using com::sun::star::sdbc::SQLException
;
67 namespace pq_sdbc_driver
70 KeyColumns::KeyColumns(
71 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
72 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
73 ConnectionSettings
*pSettings
,
74 const OUString
&schemaName
,
75 const OUString
&tableName
,
76 const Sequence
< OUString
> &columnNames
,
77 const Sequence
< OUString
> &foreignColumnNames
)
78 : Container( refMutex
, origin
, pSettings
, "KEY_COLUMN" ),
79 m_schemaName( schemaName
),
80 m_tableName( tableName
),
81 m_columnNames( columnNames
),
82 m_foreignColumnNames( foreignColumnNames
)
85 KeyColumns::~KeyColumns()
89 void KeyColumns::refresh()
93 if (isLog(m_pSettings
, LogLevel::Info
))
96 buf
.append( "sdbcx.KeyColumns get refreshed for table " );
97 buf
.append( OUStringToOString( m_schemaName
, ConnectionSettings::encoding
) );
99 buf
.append( OUStringToOString( m_tableName
, ConnectionSettings::encoding
) );
100 log( m_pSettings
, LogLevel::Info
, buf
.makeStringAndClear().getStr() );
103 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
105 Statics
&st
= getStatics();
106 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
108 Reference
< XResultSet
> rs
=
109 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
111 DisposeGuard
disposeIt( rs
);
112 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
117 sal_Int32 columnIndex
= 0;
120 OUString columnName
= xRow
->getString( 4 );
123 for( keyindex
= 0 ; keyindex
< m_columnNames
.getLength() ; keyindex
++ )
125 if( columnName
== m_columnNames
[keyindex
] )
128 if( m_columnNames
.getLength() == keyindex
)
131 KeyColumn
* pKeyColumn
=
132 new KeyColumn( m_xMutex
, m_origin
, m_pSettings
);
133 Reference
< css::beans::XPropertySet
> prop
= pKeyColumn
;
135 OUString name
= columnMetaData2SDBCX( pKeyColumn
, xRow
);
136 if( keyindex
< m_foreignColumnNames
.getLength() )
138 pKeyColumn
->setPropertyValue_NoBroadcast_public(
139 st
.RELATED_COLUMN
, makeAny( m_foreignColumnNames
[keyindex
]) );
143 m_values
.push_back( makeAny( prop
) );
144 map
[ name
] = columnIndex
;
148 m_name2index
.swap( map
);
150 catch ( css::sdbc::SQLException
& e
)
152 css::uno::Any anyEx
= cppu::getCaughtException();
153 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
157 fire( RefreshedBroadcaster( *this ) );
161 void KeyColumns::appendByDescriptor(
162 const css::uno::Reference
< css::beans::XPropertySet
>& )
164 throw css::sdbc::SQLException(
165 "KeyColumns::appendByDescriptor not implemented yet",
166 *this, OUString(), 1, Any() );
168 // osl::MutexGuard guard( m_xMutex->GetMutex() );
169 // Statics & st = getStatics();
170 // Reference< XPropertySet > past = createDataDescriptor();
171 // past->setPropertyValue( st.IS_NULLABLE, makeAny( css::sdbc::ColumnValue::NULLABLE ) );
172 // alterColumnByDescriptor(
173 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
178 void KeyColumns::dropByIndex( sal_Int32
)
180 throw css::sdbc::SQLException(
181 "KeyColumns::dropByIndex not implemented yet",
182 *this, OUString(), 1, Any() );
183 // osl::MutexGuard guard( m_xMutex->GetMutex() );
184 // if( index < 0 || index >= m_values.getLength() )
186 // OUStringBuffer buf( 128 );
187 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
188 // buf.append((sal_Int32)(m_values.getLength() -1) );
189 // buf.appendAscii( ", got " );
190 // buf.append( index );
191 // buf.appendAscii( ")" );
192 // throw css::lang::IndexOutOfBoundsException(
193 // buf.makeStringAndClear(), *this );
196 // Reference< XPropertySet > set;
197 // m_values[index] >>= set;
198 // Statics &st = getStatics();
200 // set->getPropertyValue( st.NAME ) >>= name;
202 // OUStringBuffer update( 128 );
203 // update.appendAscii( "ALTER TABLE ONLY");
204 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
205 // update.appendAscii( "DROP COLUMN" );
206 // bufferQuoteIdentifier( update, name );
207 // Reference< XStatement > stmt = m_origin->createStatement( );
208 // DisposeGuard disposeIt( stmt );
209 // stmt->executeUpdate( update.makeStringAndClear() );
214 Reference
< css::beans::XPropertySet
> KeyColumns::createDataDescriptor()
216 return new KeyColumnDescriptor( m_xMutex
, m_origin
, m_pSettings
);
219 Reference
< css::container::XNameAccess
> KeyColumns::create(
220 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
221 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
222 ConnectionSettings
*pSettings
,
223 const OUString
&schemaName
,
224 const OUString
&tableName
,
225 const Sequence
< OUString
> &columnNames
,
226 const Sequence
< OUString
> &foreignColumnNames
)
228 KeyColumns
*pKeyColumns
= new KeyColumns(
229 refMutex
, origin
, pSettings
, schemaName
, tableName
, columnNames
, foreignColumnNames
);
230 Reference
< css::container::XNameAccess
> ret
= pKeyColumns
;
231 pKeyColumns
->refresh();
237 KeyColumnDescriptors::KeyColumnDescriptors(
238 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
239 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
240 ConnectionSettings
*pSettings
)
241 : Container( refMutex
, origin
, pSettings
, "KEY_COLUMN" )
244 Reference
< css::beans::XPropertySet
> KeyColumnDescriptors::createDataDescriptor()
246 return new KeyColumnDescriptor( m_xMutex
, m_origin
, m_pSettings
);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */