bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_resultsetmetadata.cxx
blob096742687b85de341a4ee1f214d49890d95ac5d6
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,
18 * MA 02111-1307 USA
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>
49 #include <string.h>
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
75 // {
76 // OUString tableName;
77 // OUString schemaTableName;
78 // OUString typeName;
79 // css::sdbc::DataType type;
80 // sal_Int32 precision;
81 // sal_Int32 scale;
82 // sal_Bool isCurrency;
83 // sal_Bool isNullable;
84 // sal_Bool isAutoIncrement;
85 // sal_Bool isReadOnly;
86 // sal_Bool isSigned;
87 // };
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 )
96 *precision = 0;
97 *scale = 0;
99 else
101 if( atttypmod & 0xffff0000 )
103 *precision = ( ( atttypmod - PQ_VARHDRSZ ) >> 16 ) & 0xffff;
104 *scale = (atttypmod - PQ_VARHDRSZ ) & 0xffff;
106 else
108 *precision = atttypmod - PQ_VARHDRSZ;
109 *scale = 0;
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 ),
124 m_origin( origin ),
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 ++ )
164 if( i > 0 )
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 );
172 while( rs->next() )
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;
202 if( ! tables.is() )
205 Reference< XTablesSupplier > supplier =
206 Reference< XTablesSupplier > (
207 extractConnectionFromStatement( m_origin->getStatement() ), UNO_QUERY);
208 if( supplier.is() )
209 tables = supplier->getTables();
211 if( tables.is() )
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 );
231 if( set.is() )
233 set->getPropertyValue( name ) >>= ret;
236 catch( css::uno::Exception & )
239 return ret;
242 bool ResultSetMetaData::getBoolColumnProperty( const OUString & name, int index, bool def )
244 bool ret = def;
247 MutexGuard guard( m_xMutex->GetMutex() );
248 checkColumnIndex( index );
249 Reference< XPropertySet > set = getColumnByIndex( index );
250 if( set.is() )
252 set->getPropertyValue( name ) >>= ret;
255 catch( css::uno::Exception & )
259 return ret;
262 Reference< css::beans::XPropertySet > ResultSetMetaData::getColumnByIndex( int index )
264 Reference< XPropertySet > ret;
265 checkTable();
266 if( m_table.is() )
268 OUString columnName = getColumnName( index );
269 Reference< XColumnsSupplier > supplier( m_table, UNO_QUERY );
270 if( supplier.is() )
272 Reference< XNameAccess > columns = supplier->getColumns();
273 if( columns.is() && columns->hasByName( columnName ) )
275 columns->getByName( columnName ) >>= ret;
279 return ret;
282 // Methods
283 sal_Int32 ResultSetMetaData::getColumnCount( )
285 return m_colCount;
288 sal_Bool ResultSetMetaData::isAutoIncrement( sal_Int32 column )
291 bool ret = getBoolColumnProperty( getStatics().IS_AUTO_INCREMENT, column, false );
292 return ret;
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 )
318 return true;
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 )
343 return m_schemaName;
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!
363 return m_tableName;
366 OUString ResultSetMetaData::getCatalogName( sal_Int32 )
368 // can do this through XConnection.getCatalog() !
369 return OUString();
371 sal_Int32 ResultSetMetaData::getColumnType( sal_Int32 column )
373 int ret = getIntColumnProperty( getStatics().TYPE, column, -100 );
374 if( -100 == ret )
376 checkForTypes();
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;
381 return ret;
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 );
393 if( set.is() )
395 set->getPropertyValue( getStatics().TYPE_NAME ) >>= ret;
397 else
399 checkForTypes();
400 ret = m_colDesc[column-1].typeName;
403 catch( css::uno::Exception & )
406 return ret;
410 sal_Bool ResultSetMetaData::isReadOnly( sal_Int32 )
412 return false;
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 )
426 return OUString();
429 void ResultSetMetaData::checkColumnIndex(sal_Int32 columnIndex)
431 if( columnIndex < 1 || columnIndex > m_colCount )
433 throw SQLException(
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: */