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>
39 #include "pq_resultsetmetadata.hxx"
40 #include "pq_resultset.hxx"
41 #include "pq_tools.hxx"
42 #include "pq_statics.hxx"
44 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
45 #include <com/sun/star/sdbc/ColumnValue.hpp>
46 #include <com/sun/star/sdbc/SQLException.hpp>
47 #include <com/sun/star/sdbc/XRow.hpp>
51 using osl::MutexGuard
;
54 using com::sun::star::uno::Any
;
55 using com::sun::star::uno::Exception
;
56 using com::sun::star::uno::Reference
;
57 using com::sun::star::uno::UNO_QUERY
;
60 using com::sun::star::sdbc::SQLException
;
61 using com::sun::star::sdbc::XStatement
;
62 using com::sun::star::sdbc::XRow
;
63 using com::sun::star::sdbc::XResultSet
;
64 using com::sun::star::sdbcx::XColumnsSupplier
;
65 using com::sun::star::sdbcx::XTablesSupplier
;
67 using com::sun::star::beans::XPropertySet
;
68 using com::sun::star::container::XNameAccess
;
71 namespace pq_sdbc_driver
74 // struct ColumnMetaData
76 // OUString tableName;
77 // OUString schemaTableName;
79 // css::sdbc::DataType type;
80 // sal_Int32 precision;
82 // sal_Bool isCurrency;
83 // sal_Bool isNullable;
84 // sal_Bool isAutoIncrement;
85 // sal_Bool isReadOnly;
89 // is not exported by the postgres header
90 const static int PQ_VARHDRSZ
= sizeof( sal_Int32
);
92 static void extractPrecisionAndScale( sal_Int32 atttypmod
, sal_Int32
*precision
, sal_Int32
*scale
)
94 if( atttypmod
< PQ_VARHDRSZ
)
101 if( atttypmod
& 0xffff0000 )
103 *precision
= ( ( atttypmod
- PQ_VARHDRSZ
) >> 16 ) & 0xffff;
104 *scale
= (atttypmod
- PQ_VARHDRSZ
) & 0xffff;
108 *precision
= atttypmod
- PQ_VARHDRSZ
;
114 ResultSetMetaData::ResultSetMetaData(
115 const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
116 const css::uno::Reference
< css::sdbc::XResultSet
> & origin
,
117 ResultSet
* pResultSet
,
118 ConnectionSettings
**ppSettings
,
119 PGresult
const *pResult
,
120 const OUString
&schemaName
,
121 const OUString
&tableName
) :
122 m_xMutex( refMutex
),
123 m_ppSettings( ppSettings
),
125 m_tableName( tableName
),
126 m_schemaName( schemaName
),
127 m_colDesc( PQnfields( pResult
) ),
128 m_pResultSet( pResultSet
),
129 m_checkedForTable( false ),
130 m_checkedForTypes( false ),
131 m_colCount( PQnfields( pResult
) )
134 // extract all needed information from the result object, so that we don't
135 // need it anymore after this call !
136 for( int col
= 0; col
< m_colCount
; col
++ )
138 sal_Int32 size
= PQfsize( pResult
, col
);
139 size
= -1 == size
? 25 : size
;
140 m_colDesc
[col
].displaySize
= size
;
142 extractPrecisionAndScale(
143 PQfmod( pResult
, col
),
144 & ( m_colDesc
[col
].precision
),
145 & ( m_colDesc
[col
].scale
) );
146 char *name
= PQfname( pResult
, col
);
147 m_colDesc
[col
].name
= OUString( name
, strlen(name
) , ConnectionSettings::encoding
);
148 m_colDesc
[col
].typeOid
= PQftype( pResult
, col
);
149 m_colDesc
[col
].type
= css::sdbc::DataType::LONGVARCHAR
;
153 void ResultSetMetaData::checkForTypes()
155 if( ! m_checkedForTypes
)
157 Reference
< XStatement
> stmt
=
158 extractConnectionFromStatement( m_origin
->getStatement() )->createStatement();
159 DisposeGuard
guard( stmt
);
160 OUStringBuffer
buf(128);
161 buf
.append( "SELECT oid, typname, typtype FROM pg_type WHERE ");
162 for( int i
= 0 ; i
< m_colCount
; i
++ )
165 buf
.append( " OR " );
166 int oid
= m_colDesc
[i
].typeOid
;
167 buf
.append( "oid=" );
168 buf
.append( static_cast<sal_Int32
>(oid
) );
170 Reference
< XResultSet
> rs
= stmt
->executeQuery( buf
.makeStringAndClear() );
171 Reference
< XRow
> xRow( rs
, UNO_QUERY
);
174 Oid oid
= xRow
->getInt( 1 );
175 OUString typeName
= xRow
->getString( 2 );
176 OUString typType
= xRow
->getString( 3 );
178 sal_Int32 type
= typeNameToDataType( typeName
, typType
);
180 for( sal_Int32 j
= 0; j
< m_colCount
; j
++ )
182 if( m_colDesc
[j
].typeOid
== oid
)
184 m_colDesc
[j
].typeName
= typeName
;
185 m_colDesc
[j
].type
= type
;
189 m_checkedForTypes
= true;
193 void ResultSetMetaData::checkTable()
195 if( ! m_checkedForTable
)
197 m_checkedForTable
= true;
198 if( m_tableName
.getLength() )
201 Reference
< css::container::XNameAccess
> tables
= (*m_ppSettings
)->tables
;
205 Reference
< XTablesSupplier
> supplier
=
206 Reference
< XTablesSupplier
> (
207 extractConnectionFromStatement( m_origin
->getStatement() ), UNO_QUERY
);
209 tables
= supplier
->getTables();
213 const OUString
name (getTableName ( 1 ));
214 const OUString
schema (getSchemaName( 1 ));
215 const OUString
composedName( schema
.isEmpty() ? name
: (schema
+ "." + name
) );
216 tables
->getByName( composedName
) >>= m_table
;
222 sal_Int32
ResultSetMetaData::getIntColumnProperty( const OUString
& name
, int index
, int def
)
224 sal_Int32 ret
= def
; // give defensive answers, when data is not available
227 MutexGuard
guard( m_xMutex
->GetMutex() );
228 checkColumnIndex( index
);
229 Reference
< XPropertySet
> set
= getColumnByIndex( index
);
233 set
->getPropertyValue( name
) >>= ret
;
236 catch( css::uno::Exception
& )
242 bool ResultSetMetaData::getBoolColumnProperty( const OUString
& name
, int index
, bool def
)
247 MutexGuard
guard( m_xMutex
->GetMutex() );
248 checkColumnIndex( index
);
249 Reference
< XPropertySet
> set
= getColumnByIndex( index
);
252 set
->getPropertyValue( name
) >>= ret
;
255 catch( css::uno::Exception
& )
262 Reference
< css::beans::XPropertySet
> ResultSetMetaData::getColumnByIndex( int index
)
264 Reference
< XPropertySet
> ret
;
268 OUString columnName
= getColumnName( index
);
269 Reference
< XColumnsSupplier
> supplier( m_table
, UNO_QUERY
);
272 Reference
< XNameAccess
> columns
= supplier
->getColumns();
273 if( columns
.is() && columns
->hasByName( columnName
) )
275 columns
->getByName( columnName
) >>= ret
;
283 sal_Int32
ResultSetMetaData::getColumnCount( )
288 sal_Bool
ResultSetMetaData::isAutoIncrement( sal_Int32 column
)
291 bool ret
= getBoolColumnProperty( getStatics().IS_AUTO_INCREMENT
, column
, false );
295 sal_Bool
ResultSetMetaData::isCaseSensitive( sal_Int32
)
297 return true; // ??? hmm, numeric types or
300 sal_Bool
ResultSetMetaData::isSearchable( sal_Int32
)
302 return true; // mmm, what types are not searchable ?
305 sal_Bool
ResultSetMetaData::isCurrency( sal_Int32 column
)
307 return getBoolColumnProperty( getStatics().IS_CURRENCY
, column
, false );
310 sal_Int32
ResultSetMetaData::isNullable( sal_Int32 column
)
312 return getIntColumnProperty(
313 getStatics().IS_NULLABLE
, column
, css::sdbc::ColumnValue::NULLABLE_UNKNOWN
);
316 sal_Bool
ResultSetMetaData::isSigned( sal_Int32
)
321 sal_Int32
ResultSetMetaData::getColumnDisplaySize( sal_Int32 column
)
323 MutexGuard
guard( m_xMutex
->GetMutex() );
324 checkColumnIndex( column
);
325 return m_colDesc
[column
-1].displaySize
;
328 OUString
ResultSetMetaData::getColumnLabel( sal_Int32 column
)
330 return getColumnName( column
);
333 OUString
ResultSetMetaData::getColumnName( sal_Int32 column
)
335 MutexGuard
guard( m_xMutex
->GetMutex() );
336 checkColumnIndex( column
);
338 return m_colDesc
[column
-1].name
;
341 OUString
ResultSetMetaData::getSchemaName( sal_Int32
)
346 sal_Int32
ResultSetMetaData::getPrecision( sal_Int32 column
)
348 MutexGuard
guard( m_xMutex
->GetMutex() );
349 checkColumnIndex( column
);
350 return m_colDesc
[column
-1].precision
;
353 sal_Int32
ResultSetMetaData::getScale( sal_Int32 column
)
355 MutexGuard
guard( m_xMutex
->GetMutex() );
356 checkColumnIndex( column
);
357 return m_colDesc
[column
-1].scale
;
360 OUString
ResultSetMetaData::getTableName( sal_Int32
)
362 // LEM TODO This is very fishy.. Should probably return the table to which that column belongs!
366 OUString
ResultSetMetaData::getCatalogName( sal_Int32
)
368 // can do this through XConnection.getCatalog() !
371 sal_Int32
ResultSetMetaData::getColumnType( sal_Int32 column
)
373 int ret
= getIntColumnProperty( getStatics().TYPE
, column
, -100 );
377 if( css::sdbc::DataType::LONGVARCHAR
== m_colDesc
[column
-1].type
&& m_pResultSet
)
378 m_colDesc
[column
-1].type
= m_pResultSet
->guessDataType( column
);
379 ret
= m_colDesc
[column
-1].type
;
384 OUString
ResultSetMetaData::getColumnTypeName( sal_Int32 column
)
386 OUString ret
; // give defensive answers, when data is not available
389 MutexGuard
guard( m_xMutex
->GetMutex() );
390 checkColumnIndex( column
);
391 Reference
< XPropertySet
> set
= getColumnByIndex( column
);
395 set
->getPropertyValue( getStatics().TYPE_NAME
) >>= ret
;
400 ret
= m_colDesc
[column
-1].typeName
;
403 catch( css::uno::Exception
& )
410 sal_Bool
ResultSetMetaData::isReadOnly( sal_Int32
)
415 sal_Bool
ResultSetMetaData::isWritable( sal_Int32 column
)
417 return ! isReadOnly( column
); // what's the sense if this method ?
420 sal_Bool
ResultSetMetaData::isDefinitelyWritable( sal_Int32 column
)
422 return isWritable(column
); // uhh, now it becomes really esoteric ....
424 OUString
ResultSetMetaData::getColumnServiceName( sal_Int32
)
429 void ResultSetMetaData::checkColumnIndex(sal_Int32 columnIndex
)
431 if( columnIndex
< 1 || columnIndex
> m_colCount
)
434 "pq_resultsetmetadata: index out of range (expected 1 to "
435 + OUString::number( m_colCount
) + ", got " + OUString::number( columnIndex
),
436 *this, OUString(), 1, Any() );
442 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */