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 <rtl/ustrbuf.hxx>
39 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
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 <com/sun/star/sdbc/XParameters.hpp>
44 #include <cppuhelper/exc_hlp.hxx>
46 #include "pq_xindexes.hxx"
47 #include "pq_xindex.hxx"
48 #include "pq_statics.hxx"
49 #include "pq_tools.hxx"
51 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::container::XEnumerationAccess
;
63 using com::sun::star::container::XEnumeration
;
66 using com::sun::star::sdbcx::XColumnsSupplier
;
68 using com::sun::star::sdbc::XRow
;
69 using com::sun::star::sdbc::XResultSet
;
70 using com::sun::star::sdbc::XParameters
;
71 using com::sun::star::sdbc::XPreparedStatement
;
73 namespace pq_sdbc_driver
77 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
78 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
79 ConnectionSettings
*pSettings
,
80 const OUString
&schemaName
,
81 const OUString
&tableName
)
82 : Container( refMutex
, origin
, pSettings
, getStatics().KEY
),
83 m_schemaName( schemaName
),
84 m_tableName( tableName
)
91 void Indexes::refresh()
95 SAL_INFO("connectivity.postgresql", "sdbcx.Indexes get refreshed for table " << m_schemaName
<< "." << m_tableName
);
97 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
98 Statics
& st
= getStatics();
100 Int2StringMap column2NameMap
;
101 fillAttnum2attnameMap( column2NameMap
, m_origin
, m_schemaName
, m_tableName
);
103 // see XDatabaseMetaData::getIndexInfo()
104 Reference
< XPreparedStatement
> stmt
= m_origin
->prepareStatement(
105 "SELECT nspname, " // 1
106 "pg_class.relname, " // 2
107 "class2.relname, " // 3
108 "indisclustered, " // 4
110 "indisprimary, " // 6
112 "FROM pg_index INNER JOIN pg_class ON indrelid = pg_class.oid "
113 "INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid "
114 "INNER JOIN pg_class as class2 ON pg_index.indexrelid = class2.oid "
115 "WHERE nspname = ? AND pg_class.relname = ?" );
117 Reference
< XParameters
> params( stmt
, UNO_QUERY
);
118 params
->setString( 1, m_schemaName
);
119 params
->setString( 2, m_tableName
);
120 Reference
< XResultSet
> rs
= stmt
->executeQuery();
122 Reference
< XRow
> row( rs
, UNO_QUERY
);
130 static const sal_Int32 C_INDEXNAME
= 3;
131 static const sal_Int32 C_IS_CLUSTERED
= 4;
132 static const sal_Int32 C_IS_UNIQUE
= 5;
133 static const sal_Int32 C_IS_PRIMARY
= 6;
134 static const sal_Int32 C_COLUMNS
= 7;
135 OUString currentIndexName
= row
->getString( C_INDEXNAME
);
137 new Index( m_xMutex
, m_origin
, m_pSettings
,
138 m_schemaName
, m_tableName
);
140 bool isUnique
= row
->getBoolean( C_IS_UNIQUE
);
141 bool isPrimary
= row
->getBoolean( C_IS_PRIMARY
);
142 bool isClusterd
= row
->getBoolean( C_IS_CLUSTERED
);
143 Reference
< css::beans::XPropertySet
> prop
= pIndex
;
144 pIndex
->setPropertyValue_NoBroadcast_public(
145 st
.IS_UNIQUE
, Any( isUnique
) );
146 pIndex
->setPropertyValue_NoBroadcast_public(
147 st
.IS_PRIMARY_KEY_INDEX
, Any( isPrimary
) );
148 pIndex
->setPropertyValue_NoBroadcast_public(
149 st
.IS_CLUSTERED
, Any( isClusterd
) );
150 pIndex
->setPropertyValue_NoBroadcast_public(
151 st
.NAME
, makeAny( currentIndexName
) );
153 std::vector
< sal_Int32
> seq
= parseIntArray( row
->getString( C_COLUMNS
) );
154 Sequence
< OUString
> columnNames(seq
.size());
155 for( size_t columns
= 0 ; columns
< seq
.size() ; columns
++ )
157 columnNames
[columns
] = column2NameMap
[ seq
[columns
] ];
160 pIndex
->setPropertyValue_NoBroadcast_public(
161 st
.PRIVATE_COLUMN_INDEXES
, makeAny( columnNames
));
164 m_values
.push_back( makeAny( prop
) );
165 map
[ currentIndexName
] = index
;
169 m_name2index
.swap( map
);
171 catch ( css::sdbc::SQLException
& e
)
173 css::uno::Any anyEx
= cppu::getCaughtException();
174 throw css::lang::WrappedTargetRuntimeException( e
.Message
,
178 fire( RefreshedBroadcaster( *this ) );
182 void Indexes::appendByDescriptor(
183 const css::uno::Reference
< css::beans::XPropertySet
>& descriptor
)
185 Statics
& st
= getStatics();
186 OUString name
= extractStringProperty( descriptor
, st
.NAME
);
188 bool isUnique
= extractBoolProperty( descriptor
, st
.IS_UNIQUE
);
190 OUStringBuffer
buf( 128 );
192 buf
.append( "CREATE " );
194 buf
.append( "UNIQUE " );
195 buf
.append( "INDEX " );
196 bufferQuoteIdentifier( buf
, name
, m_pSettings
);
197 buf
.append( " ON " );
198 bufferQuoteQualifiedIdentifier( buf
, m_schemaName
, m_tableName
, m_pSettings
);
202 Reference
< XColumnsSupplier
> columns( descriptor
, UNO_QUERY
);
205 Reference
< XEnumerationAccess
> access( columns
->getColumns(), UNO_QUERY
);
208 Reference
< XEnumeration
> xEnum( access
->createEnumeration() );
210 while( xEnum
.is() && xEnum
->hasMoreElements() )
212 Reference
< XPropertySet
> column( xEnum
->nextElement(), UNO_QUERY
);
221 buf
.append( extractStringProperty( column
, st
.NAME
) );
227 m_origin
->createStatement()->executeUpdate( buf
.makeStringAndClear() );
231 void Indexes::dropByIndex( sal_Int32 index
)
235 osl::MutexGuard
guard( m_xMutex
->GetMutex() );
236 if( index
< 0 || index
>= static_cast<sal_Int32
>(m_values
.size()) )
238 throw css::lang::IndexOutOfBoundsException(
239 "Indexes: Index out of range (allowed 0 to "
240 + OUString::number( m_values
.size() -1 )
241 + ", got " + OUString::number( index
)
246 Reference
< XPropertySet
> set
;
247 m_values
[index
] >>= set
;
248 Statics
&st
= getStatics();
250 OUStringBuffer
buf( 128 );
251 buf
.append( "DROP INDEX " );
252 bufferQuoteIdentifier( buf
, extractStringProperty( set
, st
.NAME
), m_pSettings
);
253 m_origin
->createStatement()->executeUpdate( buf
.makeStringAndClear() );
255 Container::dropByIndex( index
);
259 css::uno::Reference
< css::beans::XPropertySet
> Indexes::createDataDescriptor()
261 return new IndexDescriptor( m_xMutex
, m_origin
, m_pSettings
);
264 Reference
< css::container::XNameAccess
> Indexes::create(
265 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
266 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
267 ConnectionSettings
*pSettings
,
268 const OUString
& schemaName
,
269 const OUString
& tableName
)
271 Indexes
*pIndexes
= new Indexes( refMutex
, origin
, pSettings
, schemaName
, tableName
);
272 Reference
< css::container::XNameAccess
> ret
= pIndexes
;
278 IndexDescriptors::IndexDescriptors(
279 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
280 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
281 ConnectionSettings
*pSettings
)
282 : Container( refMutex
, origin
, pSettings
, getStatics().INDEX
)
285 Reference
< css::container::XNameAccess
> IndexDescriptors::create(
286 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
287 const css::uno::Reference
< css::sdbc::XConnection
> & origin
,
288 ConnectionSettings
*pSettings
)
290 return new IndexDescriptors( refMutex
, origin
, pSettings
);
293 css::uno::Reference
< css::beans::XPropertySet
> IndexDescriptors::createDataDescriptor()
295 return new IndexDescriptor( m_xMutex
, m_origin
, m_pSettings
);
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */