Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_resultset.cxx
blob3fbc6d47337dba425f86771ad8016f414ed3d558
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 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
33 * The contents of this file are subject to the Mozilla Public License Version
34 * 1.1 (the "License"); you may not use this file except in compliance with
35 * the License or as specified alternatively below. You may obtain a copy of
36 * the License at http://www.mozilla.org/MPL/
38 * Software distributed under the License is distributed on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 * for the specific language governing rights and limitations under the
41 * License.
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
50 * Alternatively, the contents of this file may be used under the terms of
51 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 * instead of those above.
56 ************************************************************************/
58 #include "pq_resultset.hxx"
59 #include "pq_resultsetmetadata.hxx"
61 #include <com/sun/star/sdbc/FetchDirection.hpp>
62 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
63 #include <com/sun/star/sdbc/ResultSetType.hpp>
64 #include <com/sun/star/sdbc/DataType.hpp>
66 using rtl::OUString;
67 using rtl::OUStringToOString;
69 using osl::MutexGuard;
71 using com::sun::star::uno::RuntimeException;
72 using com::sun::star::uno::Any;
73 using com::sun::star::uno::makeAny;
74 using com::sun::star::uno::Reference;
75 using com::sun::star::uno::XInterface;
77 using com::sun::star::sdbc::SQLException;
78 using com::sun::star::sdbc::XResultSetMetaData;
81 namespace pq_sdbc_driver
83 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
85 void ResultSet::checkClosed()
86 throw ( com::sun::star::sdbc::SQLException, com::sun::star::uno::RuntimeException )
88 if( ! m_result )
90 throw SQLException( ASCII_STR( "pq_resultset: already closed" ),
91 *this, OUString(), 1, Any() );
94 if( ! m_ppSettings || ! *m_ppSettings || ! (*m_ppSettings)->pConnection )
96 throw SQLException( ASCII_STR( "pq_resultset: statement has been closed already" ),
97 *this, OUString(), 1, Any() );
102 ResultSet::ResultSet( const ::rtl::Reference< RefCountedMutex > & refMutex,
103 const Reference< XInterface > & owner,
104 ConnectionSettings **ppSettings,
105 PGresult * result,
106 const rtl::OUString &schema,
107 const rtl::OUString &table)
108 : BaseResultSet(
109 refMutex, owner, PQntuples( result ),
110 PQnfields( result ),(*ppSettings)->tc ),
111 m_result( result ),
112 m_schema( schema ),
113 m_table( table ),
114 m_ppSettings( ppSettings )
116 // LEM TODO: shouldn't these things be inherited from the statement or something like that?
117 sal_Bool b = sal_False;
118 // Positioned update/delete not supported, so no cursor name
119 // Fetch direction and size are cursor-specific things, so not used now.
120 // Fetch size not set
121 m_props[ BASERESULTSET_FETCH_DIRECTION ] = makeAny(
122 com::sun::star::sdbc::FetchDirection::UNKNOWN);
123 // No escape processing for now
124 m_props[ BASERESULTSET_ESCAPE_PROCESSING ] = Any( &b, getBooleanCppuType() );
125 // Bookmarks not implemented for now
126 m_props[ BASERESULTSET_IS_BOOKMARKABLE ] = Any( &b, getBooleanCppuType() );
127 m_props[ BASERESULTSET_RESULT_SET_CONCURRENCY ] = makeAny(
128 com::sun::star::sdbc::ResultSetConcurrency::READ_ONLY );
129 m_props[ BASERESULTSET_RESULT_SET_TYPE ] = makeAny(
130 com::sun::star::sdbc::ResultSetType::SCROLL_INSENSITIVE );
134 Any ResultSet::getValue( sal_Int32 columnIndex )
136 Any ret;
137 if( PQgetisnull( m_result, m_row, columnIndex -1 ) )
139 m_wasNull = true;
141 else
143 m_wasNull = false;
144 ret <<= ::rtl::OUString(
145 PQgetvalue( m_result, m_row , columnIndex -1 ) ,
146 PQgetlength( m_result, m_row , columnIndex -1 ) ,
147 (*m_ppSettings)->encoding );
150 return ret;
153 ResultSet::~ResultSet()
156 void ResultSet::close( ) throw (SQLException, RuntimeException)
158 Reference< XInterface > owner;
160 MutexGuard guard( m_refMutex->mutex );
161 if( m_result )
163 PQclear(m_result );
164 m_result = 0;
165 m_row = -1;
167 owner = m_owner;
168 m_owner.clear();
172 Reference< XResultSetMetaData > ResultSet::getMetaData( ) throw (SQLException, RuntimeException)
174 MutexGuard guard( m_refMutex->mutex );
175 checkClosed();
176 return new ResultSetMetaData(
177 m_refMutex, this, this, m_ppSettings, m_result, m_schema, m_table );
180 sal_Int32 ResultSet::findColumn( const ::rtl::OUString& columnName )
181 throw (SQLException, RuntimeException)
183 MutexGuard guard( m_refMutex->mutex );
184 checkClosed();
185 return PQfnumber(
186 m_result,
187 OUStringToOString( columnName, (*m_ppSettings)->encoding ).getStr())
191 static bool isNumber( const char * data, sal_Int32 len )
193 bool ret = false;
194 if( len )
196 ret = true;
197 for( int i = 0 ; i < len ; i ++ )
199 if( ( data[i] >= '0' && data[i] <= '9' ) ||
200 data[i] == '-' || data[i] == '+' || data[i] == '.' || data[i] == ',' )
202 if( data[i] == '-' && i != 0 && i != len-1 )
204 // no number, maybe a date
205 ret = false;
206 break;
209 else
211 ret = false;
212 break;
216 return ret;
219 static bool isInteger( const char * data, sal_Int32 len )
221 bool ret = false;
222 if( len )
224 ret = true;
225 for( int i = 0 ; i < len ; i ++ )
227 if( ( data[i] >= '0' && data[i] <= '9' ) ||
228 data[i] == '-' || data[i] == '+' )
230 if( data[i] == '-' && i != 0 && i != len-1 )
232 // no number, maybe a date
233 ret = false;
234 break;
237 else
239 ret = false;
240 break;
244 return ret;
247 static bool isDate( const char * data, sal_Int32 len )
249 return 10 == len &&
250 '-' == data[4] &&
251 '-' == data[7] &&
252 isInteger( &(data[0]),4 ) &&
253 isInteger( &(data[5]),2) &&
254 isInteger( &(data[8]),2 );
257 static bool isTime( const char * data, sal_Int32 len )
259 return 8 == len &&
260 ':' == data[2] &&
261 ':' == data[5] &&
262 isInteger( &(data[0]),2 ) &&
263 isInteger( &(data[3]),2) &&
264 isInteger( &(data[6]),2 );
268 static bool isTimestamp( const char * data, sal_Int32 len )
270 return len == 19 && isDate( data, 10) && isTime( &(data[11]),8 );
273 sal_Int32 ResultSet::guessDataType( sal_Int32 column )
275 // we don't look into more than 100 rows ...
276 sal_Int32 ret = com::sun::star::sdbc::DataType::INTEGER;
278 int maxRows = ( m_rowCount > 100 ? 100 : m_rowCount );
279 for( int i = 0 ; i < maxRows ; i ++ )
281 if( ! PQgetisnull( m_result, i , column-1 ) )
283 const char * p = PQgetvalue( m_result, i , column -1 );
284 int len = PQgetlength( m_result, i , column -1 );
286 if( com::sun::star::sdbc::DataType::INTEGER == ret )
288 if( ! isInteger( p,len ) )
289 ret = com::sun::star::sdbc::DataType::NUMERIC;
291 if( com::sun::star::sdbc::DataType::NUMERIC == ret )
293 if( ! isNumber( p,len ) )
295 ret = com::sun::star::sdbc::DataType::DATE;
298 if( com::sun::star::sdbc::DataType::DATE == ret )
300 if( ! isDate( p,len ) )
302 ret = com::sun::star::sdbc::DataType::TIME;
305 if( com::sun::star::sdbc::DataType::TIME == ret )
307 if( ! isTime( p,len ) )
309 ret = com::sun::star::sdbc::DataType::TIMESTAMP;
312 if( com::sun::star::sdbc::DataType::TIMESTAMP == ret )
314 if( ! isTimestamp( p,len ) )
316 ret = com::sun::star::sdbc::DataType::LONGVARCHAR;
317 break;
322 return ret;