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 "pq_resultset.hxx"
38 #include "pq_resultsetmetadata.hxx"
40 #include <connectivity/dbexception.hxx>
42 #include <com/sun/star/sdbc/FetchDirection.hpp>
43 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
44 #include <com/sun/star/sdbc/ResultSetType.hpp>
45 #include <com/sun/star/sdbc/DataType.hpp>
46 #include <com/sun/star/sdbc/SQLException.hpp>
50 using osl::MutexGuard
;
52 using com::sun::star::uno::Any
;
53 using com::sun::star::uno::Reference
;
54 using com::sun::star::uno::XInterface
;
56 using com::sun::star::sdbc::SQLException
;
57 using com::sun::star::sdbc::XResultSetMetaData
;
60 namespace pq_sdbc_driver
63 void ResultSet::checkClosed()
67 throw SQLException( u
"pq_resultset: already closed"_ustr
,
68 *this, OUString(), 1, Any() );
71 if( ! m_ppSettings
|| ! *m_ppSettings
|| ! (*m_ppSettings
)->pConnection
)
73 throw SQLException( u
"pq_resultset: statement has been closed already"_ustr
,
74 *this, OUString(), 1, Any() );
79 ResultSet::ResultSet( const ::rtl::Reference
< comphelper::RefCountedMutex
> & refMutex
,
80 const Reference
< XInterface
> & owner
,
81 ConnectionSettings
**ppSettings
,
86 refMutex
, owner
, PQntuples( result
),
87 PQnfields( result
),(*ppSettings
)->tc
),
89 m_schema(std::move( schema
)),
90 m_table(std::move( table
)),
91 m_ppSettings( ppSettings
)
93 // LEM TODO: shouldn't these things be inherited from the statement or something like that?
94 // Positioned update/delete not supported, so no cursor name
95 // Fetch direction and size are cursor-specific things, so not used now.
97 m_props
[ BASERESULTSET_FETCH_DIRECTION
] <<= css::sdbc::FetchDirection::UNKNOWN
;
98 // No escape processing for now
99 m_props
[ BASERESULTSET_ESCAPE_PROCESSING
] <<= false;
100 // Bookmarks not implemented for now
101 m_props
[ BASERESULTSET_IS_BOOKMARKABLE
] <<= false;
102 m_props
[ BASERESULTSET_RESULT_SET_CONCURRENCY
] <<= css::sdbc::ResultSetConcurrency::READ_ONLY
;
103 m_props
[ BASERESULTSET_RESULT_SET_TYPE
] <<= css::sdbc::ResultSetType::SCROLL_INSENSITIVE
;
107 Any
ResultSet::getValue( sal_Int32 columnIndex
)
110 if( PQgetisnull( m_result
, m_row
, columnIndex
-1 ) )
118 PQgetvalue( m_result
, m_row
, columnIndex
-1 ) ,
119 PQgetlength( m_result
, m_row
, columnIndex
-1 ) ,
120 ConnectionSettings::encoding
);
126 ResultSet::~ResultSet()
129 void ResultSet::close( )
131 Reference
< XInterface
> owner
;
133 MutexGuard
guard( m_xMutex
->GetMutex() );
145 Reference
< XResultSetMetaData
> ResultSet::getMetaData( )
147 MutexGuard
guard( m_xMutex
->GetMutex() );
149 return new ResultSetMetaData(
150 m_xMutex
, this, this, m_ppSettings
, m_result
, m_schema
, m_table
);
153 sal_Int32
ResultSet::findColumn( const OUString
& columnName
)
155 MutexGuard
guard( m_xMutex
->GetMutex() );
157 sal_Int32 res
= PQfnumber( m_result
,
158 OUStringToOString( columnName
, ConnectionSettings::encoding
).getStr());
159 /* PQfnumber return -1 for not found, which is what we want
160 * other than that we use col number as 1-based not 0-based */
167 ::dbtools::throwInvalidColumnException( columnName
, *this );
172 static bool isNumber( const char * data
, sal_Int32 len
)
178 for( int i
= 0 ; i
< len
; i
++ )
180 if( ( data
[i
] >= '0' && data
[i
] <= '9' ) ||
181 data
[i
] == '-' || data
[i
] == '+' || data
[i
] == '.' || data
[i
] == ',' )
183 if( data
[i
] == '-' && i
!= 0 && i
!= len
-1 )
185 // no number, maybe a date
200 static bool isInteger( const char * data
, sal_Int32 len
)
206 for( int i
= 0 ; i
< len
; i
++ )
208 if( ( data
[i
] >= '0' && data
[i
] <= '9' ) ||
209 data
[i
] == '-' || data
[i
] == '+' )
211 if( data
[i
] == '-' && i
!= 0 && i
!= len
-1 )
213 // no number, maybe a date
228 static bool isDate( const char * data
, sal_Int32 len
)
233 isInteger( &(data
[0]),4 ) &&
234 isInteger( &(data
[5]),2) &&
235 isInteger( &(data
[8]),2 );
238 static bool isTime( const char * data
, sal_Int32 len
)
243 isInteger( &(data
[0]),2 ) &&
244 isInteger( &(data
[3]),2) &&
245 isInteger( &(data
[6]),2 );
249 static bool isTimestamp( const char * data
, sal_Int32 len
)
251 return len
== 19 && isDate( data
, 10) && isTime( &(data
[11]),8 );
254 sal_Int32
ResultSet::guessDataType( sal_Int32 column
)
256 // we don't look into more than 100 rows ...
257 sal_Int32 ret
= css::sdbc::DataType::INTEGER
;
259 int maxRows
= std::min
<sal_Int32
>( m_rowCount
, 100 );
260 for( int i
= 0 ; i
< maxRows
; i
++ )
262 if( ! PQgetisnull( m_result
, i
, column
-1 ) )
264 const char * p
= PQgetvalue( m_result
, i
, column
-1 );
265 int len
= PQgetlength( m_result
, i
, column
-1 );
267 if( css::sdbc::DataType::INTEGER
== ret
)
269 if( ! isInteger( p
,len
) )
270 ret
= css::sdbc::DataType::NUMERIC
;
272 if( css::sdbc::DataType::NUMERIC
== ret
)
274 if( ! isNumber( p
,len
) )
276 ret
= css::sdbc::DataType::DATE
;
279 if( css::sdbc::DataType::DATE
== ret
)
281 if( ! isDate( p
,len
) )
283 ret
= css::sdbc::DataType::TIME
;
286 if( css::sdbc::DataType::TIME
== ret
)
288 if( ! isTime( p
,len
) )
290 ret
= css::sdbc::DataType::TIMESTAMP
;
293 if( css::sdbc::DataType::TIMESTAMP
== ret
)
295 if( ! isTimestamp( p
,len
) )
297 ret
= css::sdbc::DataType::LONGVARCHAR
;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */