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>
40 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
41 #include <com/sun/star/sdbc/SQLException.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <cppuhelper/exc_hlp.hxx>
45 #include "pq_xcolumns.hxx"
46 #include "pq_xindexcolumns.hxx"
47 #include "pq_xindexcolumn.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::makeAny
;
57 using com::sun::star::uno::UNO_QUERY
;
58 using com::sun::star::uno::Reference
;
59 using com::sun::star::uno::Sequence
;
61 using com::sun::star::sdbc::XRow
;
62 using com::sun::star::sdbc::XResultSet
;
63 using com::sun::star::sdbc::XDatabaseMetaData
;
64 using com::sun::star::sdbc::SQLException
;
66 namespace pq_sdbc_driver
69 IndexColumns::IndexColumns(
70 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
71 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
72 ConnectionSettings
*pSettings
,
73 const OUString
&schemaName
,
74 const OUString
&tableName
,
75 const OUString
&indexName
,
76 const css::uno::Sequence
< OUString
> &columns
)
77 : Container( refMutex
, origin
, pSettings
, "INDEX_COLUMN" ),
78 m_schemaName( schemaName
),
79 m_tableName( tableName
),
80 m_indexName( indexName
),
84 IndexColumns::~IndexColumns()
87 static sal_Int32
findInSequence( const Sequence
< OUString
> & seq
, const OUString
&str
)
90 for( index
= 0 ; index
< seq
.getLength() ; index
++ )
92 if( str
== seq
[index
] )
98 void IndexColumns::refresh()
102 SAL_INFO("connectivity.postgresql", "sdbcx.IndexColumns get refreshed for index " << m_indexName
);
104 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
106 Statics
&st
= getStatics();
107 Reference
< XDatabaseMetaData
> meta
= m_origin
->getMetaData();
109 Reference
< XResultSet
> rs
=
110 meta
->getColumns( Any(), m_schemaName
, m_tableName
, st
.cPERCENT
);
112 DisposeGuard
disposeIt( rs
);
113 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
115 m_values
.resize( m_columns
.getLength() );
119 OUString columnName
= xRow
->getString( 4 );
121 sal_Int32 index
= findInSequence( m_columns
, columnName
);
122 if( index
>= m_columns
.getLength() )
125 IndexColumn
* pIndexColumn
=
126 new IndexColumn( m_xMutex
, m_origin
, m_pSettings
);
127 Reference
< css::beans::XPropertySet
> prop
= pIndexColumn
;
129 columnMetaData2SDBCX( pIndexColumn
, xRow
);
130 pIndexColumn
->setPropertyValue_NoBroadcast_public(
131 st
.IS_ASCENDING
, makeAny( false ) );
133 m_values
[ index
] <<= prop
;
134 m_name2index
[ columnName
] = index
;
137 catch ( css::sdbc::SQLException
& e
)
139 css::uno::Any anyEx
= cppu::getCaughtException();
140 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
144 fire( RefreshedBroadcaster( *this ) );
148 void IndexColumns::appendByDescriptor(
149 const css::uno::Reference
< css::beans::XPropertySet
>& /*future*/ )
151 throw css::sdbc::SQLException(
152 "SDBC-POSTGRESQL: IndexesColumns.appendByDescriptor not yet implemented",
153 *this, OUString(), 1, Any() );
154 // osl::MutexGuard guard( m_xMutex->GetMutex() );
155 // Statics & st = getStatics();
156 // Reference< XPropertySet > past = createDataDescriptor();
157 // past->setPropertyValue( st.IS_NULLABLE, makeAny( css::sdbc::ColumnValue::NULLABLE ) );
158 // alterColumnByDescriptor(
159 // m_schemaName, m_tableName, m_pSettings->encoding, m_origin->createStatement() , past, future );
163 void IndexColumns::dropByName( const OUString
& )
165 throw css::sdbc::SQLException(
166 "SDBC-POSTGRESQL: IndexesColumns.dropByName not yet implemented",
167 *this, OUString(), 1, Any() );
168 // String2IntMap::const_iterator ii = m_name2index.find( elementName );
169 // if( ii == m_name2index.end() )
171 // OUStringBuffer buf( 128 );
172 // buf.appendAscii( "Column " );
173 // buf.append( elementName );
174 // buf.appendAscii( " is unknown in table " );
175 // buf.append( m_schemaName );
176 // buf.appendAscii( "." );
177 // buf.append( m_tableName );
178 // buf.appendAscii( ", so it can't be dropped" );
179 // throw css::container::NoSuchElementException(
180 // buf.makeStringAndClear(), *this );
182 // dropByIndex( ii->second );
185 void IndexColumns::dropByIndex( sal_Int32
)
187 throw css::sdbc::SQLException(
188 "SDBC-POSTGRESQL: IndexesColumns.dropByIndex not yet implemented",
189 *this, OUString(), 1, Any() );
190 // osl::MutexGuard guard( m_xMutex->GetMutex() );
191 // if( index < 0 || index >= m_values.getLength() )
193 // OUStringBuffer buf( 128 );
194 // buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
195 // buf.append((sal_Int32)(m_values.getLength() -1) );
196 // buf.appendAscii( ", got " );
197 // buf.append( index );
198 // buf.appendAscii( ")" );
199 // throw css::lang::IndexOutOfBoundsException(
200 // buf.makeStringAndClear(), *this );
203 // Reference< XPropertySet > set;
204 // m_values[index] >>= set;
205 // Statics &st = getStatics();
207 // set->getPropertyValue( st.NAME ) >>= name;
209 // OUStringBuffer update( 128 );
210 // update.appendAscii( "ALTER TABLE ONLY");
211 // bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName );
212 // update.appendAscii( "DROP COLUMN" );
213 // bufferQuoteIdentifier( update, name );
214 // Reference< XStatement > stmt = m_origin->createStatement( );
215 // DisposeGuard disposeIt( stmt );
216 // stmt->executeUpdate( update.makeStringAndClear() );
221 Reference
< css::beans::XPropertySet
> IndexColumns::createDataDescriptor()
223 return new IndexColumnDescriptor( m_xMutex
, m_origin
, m_pSettings
);
226 Reference
< css::container::XNameAccess
> IndexColumns::create(
227 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
228 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
229 ConnectionSettings
*pSettings
,
230 const OUString
&schemaName
,
231 const OUString
&tableName
,
232 const OUString
&indexName
,
233 const Sequence
< OUString
> &columns
)
235 IndexColumns
*pIndexColumns
= new IndexColumns(
236 refMutex
, origin
, pSettings
, schemaName
, tableName
, indexName
, columns
);
237 Reference
< css::container::XNameAccess
> ret
= pIndexColumns
;
238 pIndexColumns
->refresh();
244 IndexColumnDescriptors::IndexColumnDescriptors(
245 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
246 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
247 ConnectionSettings
*pSettings
)
248 : Container( refMutex
, origin
, pSettings
, getStatics().INDEX_COLUMN
)
251 Reference
< css::container::XNameAccess
> IndexColumnDescriptors::create(
252 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
253 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
254 ConnectionSettings
*pSettings
)
256 return new IndexColumnDescriptors( refMutex
, origin
, pSettings
);
259 css::uno::Reference
< css::beans::XPropertySet
> IndexColumnDescriptors::createDataDescriptor()
261 return new IndexColumnDescriptor( m_xMutex
, m_origin
, m_pSettings
);
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */