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>
48 using osl::MutexGuard
;
50 using com::sun::star::uno::RuntimeException
;
51 using com::sun::star::uno::Any
;
52 using com::sun::star::uno::makeAny
;
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()
64 throw ( com::sun::star::sdbc::SQLException
, com::sun::star::uno::RuntimeException
)
68 throw SQLException( "pq_resultset: already closed",
69 *this, OUString(), 1, Any() );
72 if( ! m_ppSettings
|| ! *m_ppSettings
|| ! (*m_ppSettings
)->pConnection
)
74 throw SQLException( "pq_resultset: statement has been closed already",
75 *this, OUString(), 1, Any() );
80 ResultSet::ResultSet( const ::rtl::Reference
< RefCountedMutex
> & refMutex
,
81 const Reference
< XInterface
> & owner
,
82 ConnectionSettings
**ppSettings
,
84 const OUString
&schema
,
85 const OUString
&table
)
87 refMutex
, owner
, PQntuples( result
),
88 PQnfields( result
),(*ppSettings
)->tc
),
92 m_ppSettings( ppSettings
)
94 // LEM TODO: shouldn't these things be inherited from the statement or something like that?
95 sal_Bool b
= sal_False
;
96 // Positioned update/delete not supported, so no cursor name
97 // Fetch direction and size are cursor-specific things, so not used now.
99 m_props
[ BASERESULTSET_FETCH_DIRECTION
] = makeAny(
100 com::sun::star::sdbc::FetchDirection::UNKNOWN
);
101 // No escape processing for now
102 m_props
[ BASERESULTSET_ESCAPE_PROCESSING
] = Any( &b
, getBooleanCppuType() );
103 // Bookmarks not implemented for now
104 m_props
[ BASERESULTSET_IS_BOOKMARKABLE
] = Any( &b
, getBooleanCppuType() );
105 m_props
[ BASERESULTSET_RESULT_SET_CONCURRENCY
] = makeAny(
106 com::sun::star::sdbc::ResultSetConcurrency::READ_ONLY
);
107 m_props
[ BASERESULTSET_RESULT_SET_TYPE
] = makeAny(
108 com::sun::star::sdbc::ResultSetType::SCROLL_INSENSITIVE
);
112 Any
ResultSet::getValue( sal_Int32 columnIndex
)
115 if( PQgetisnull( m_result
, m_row
, columnIndex
-1 ) )
123 PQgetvalue( m_result
, m_row
, columnIndex
-1 ) ,
124 PQgetlength( m_result
, m_row
, columnIndex
-1 ) ,
125 (*m_ppSettings
)->encoding
);
131 ResultSet::~ResultSet()
134 void ResultSet::close( ) throw (SQLException
, RuntimeException
, std::exception
)
136 Reference
< XInterface
> owner
;
138 MutexGuard
guard( m_refMutex
->mutex
);
150 Reference
< XResultSetMetaData
> ResultSet::getMetaData( ) throw (SQLException
, RuntimeException
, std::exception
)
152 MutexGuard
guard( m_refMutex
->mutex
);
154 return new ResultSetMetaData(
155 m_refMutex
, this, this, m_ppSettings
, m_result
, m_schema
, m_table
);
158 sal_Int32
ResultSet::findColumn( const OUString
& columnName
)
159 throw (SQLException
, RuntimeException
, std::exception
)
161 MutexGuard
guard( m_refMutex
->mutex
);
163 sal_Int32 res
= PQfnumber( m_result
,
164 OUStringToOString( columnName
, (*m_ppSettings
)->encoding
).getStr());
165 /* PQfnumber reurn -1 for not found, which is waht we want
166 * otehr than that we use col number as 1-based not 0-based */
173 ::dbtools::throwInvalidColumnException( columnName
, *this );
179 static bool isNumber( const char * data
, sal_Int32 len
)
185 for( int i
= 0 ; i
< len
; i
++ )
187 if( ( data
[i
] >= '0' && data
[i
] <= '9' ) ||
188 data
[i
] == '-' || data
[i
] == '+' || data
[i
] == '.' || data
[i
] == ',' )
190 if( data
[i
] == '-' && i
!= 0 && i
!= len
-1 )
192 // no number, maybe a date
207 static bool isInteger( const char * data
, sal_Int32 len
)
213 for( int i
= 0 ; i
< len
; i
++ )
215 if( ( data
[i
] >= '0' && data
[i
] <= '9' ) ||
216 data
[i
] == '-' || data
[i
] == '+' )
218 if( data
[i
] == '-' && i
!= 0 && i
!= len
-1 )
220 // no number, maybe a date
235 static bool isDate( const char * data
, sal_Int32 len
)
240 isInteger( &(data
[0]),4 ) &&
241 isInteger( &(data
[5]),2) &&
242 isInteger( &(data
[8]),2 );
245 static bool isTime( const char * data
, sal_Int32 len
)
250 isInteger( &(data
[0]),2 ) &&
251 isInteger( &(data
[3]),2) &&
252 isInteger( &(data
[6]),2 );
256 static bool isTimestamp( const char * data
, sal_Int32 len
)
258 return len
== 19 && isDate( data
, 10) && isTime( &(data
[11]),8 );
261 sal_Int32
ResultSet::guessDataType( sal_Int32 column
)
263 // we don't look into more than 100 rows ...
264 sal_Int32 ret
= com::sun::star::sdbc::DataType::INTEGER
;
266 int maxRows
= ( m_rowCount
> 100 ? 100 : m_rowCount
);
267 for( int i
= 0 ; i
< maxRows
; i
++ )
269 if( ! PQgetisnull( m_result
, i
, column
-1 ) )
271 const char * p
= PQgetvalue( m_result
, i
, column
-1 );
272 int len
= PQgetlength( m_result
, i
, column
-1 );
274 if( com::sun::star::sdbc::DataType::INTEGER
== ret
)
276 if( ! isInteger( p
,len
) )
277 ret
= com::sun::star::sdbc::DataType::NUMERIC
;
279 if( com::sun::star::sdbc::DataType::NUMERIC
== ret
)
281 if( ! isNumber( p
,len
) )
283 ret
= com::sun::star::sdbc::DataType::DATE
;
286 if( com::sun::star::sdbc::DataType::DATE
== ret
)
288 if( ! isDate( p
,len
) )
290 ret
= com::sun::star::sdbc::DataType::TIME
;
293 if( com::sun::star::sdbc::DataType::TIME
== ret
)
295 if( ! isTime( p
,len
) )
297 ret
= com::sun::star::sdbc::DataType::TIMESTAMP
;
300 if( com::sun::star::sdbc::DataType::TIMESTAMP
== ret
)
302 if( ! isTimestamp( p
,len
) )
304 ret
= com::sun::star::sdbc::DataType::LONGVARCHAR
;
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */