xref: /trunk/main/connectivity/source/inc/odbc/OTools.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _CONNECTIVITY_OTOOLS_HXX_
28*cdf0e10cSrcweir #define _CONNECTIVITY_OTOOLS_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "odbc/OFunctiondefs.hxx"
31*cdf0e10cSrcweir #include "odbc/odbcbasedllapi.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLException.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
36*cdf0e10cSrcweir #include <osl/thread.h>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
39*cdf0e10cSrcweir #include <rtl/textenc.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define ODBC3SQLAllocHandle         1
42*cdf0e10cSrcweir #define ODBC3SQLConnect             2
43*cdf0e10cSrcweir #define ODBC3SQLDriverConnect       3
44*cdf0e10cSrcweir #define ODBC3SQLBrowseConnect       4
45*cdf0e10cSrcweir #define ODBC3SQLDataSources         5
46*cdf0e10cSrcweir #define ODBC3SQLDrivers             6
47*cdf0e10cSrcweir #define ODBC3SQLGetInfo             7
48*cdf0e10cSrcweir #define ODBC3SQLGetFunctions        8
49*cdf0e10cSrcweir #define ODBC3SQLGetTypeInfo         9
50*cdf0e10cSrcweir #define ODBC3SQLSetConnectAttr      10
51*cdf0e10cSrcweir #define ODBC3SQLGetConnectAttr      11
52*cdf0e10cSrcweir #define ODBC3SQLSetEnvAttr          12
53*cdf0e10cSrcweir #define ODBC3SQLGetEnvAttr          13
54*cdf0e10cSrcweir #define ODBC3SQLSetStmtAttr         14
55*cdf0e10cSrcweir #define ODBC3SQLGetStmtAttr         15
56*cdf0e10cSrcweir #define ODBC3SQLPrepare             16
57*cdf0e10cSrcweir #define ODBC3SQLBindParameter       17
58*cdf0e10cSrcweir #define ODBC3SQLSetCursorName       18
59*cdf0e10cSrcweir #define ODBC3SQLExecute             19
60*cdf0e10cSrcweir #define ODBC3SQLExecDirect          20
61*cdf0e10cSrcweir #define ODBC3SQLDescribeParam       21
62*cdf0e10cSrcweir #define ODBC3SQLNumParams           22
63*cdf0e10cSrcweir #define ODBC3SQLParamData           23
64*cdf0e10cSrcweir #define ODBC3SQLPutData             24
65*cdf0e10cSrcweir #define ODBC3SQLRowCount            25
66*cdf0e10cSrcweir #define ODBC3SQLNumResultCols       26
67*cdf0e10cSrcweir #define ODBC3SQLDescribeCol         27
68*cdf0e10cSrcweir #define ODBC3SQLColAttribute        28
69*cdf0e10cSrcweir #define ODBC3SQLBindCol             29
70*cdf0e10cSrcweir #define ODBC3SQLFetch               30
71*cdf0e10cSrcweir #define ODBC3SQLFetchScroll         31
72*cdf0e10cSrcweir #define ODBC3SQLGetData             32
73*cdf0e10cSrcweir #define ODBC3SQLSetPos              33
74*cdf0e10cSrcweir #define ODBC3SQLBulkOperations      34
75*cdf0e10cSrcweir #define ODBC3SQLMoreResults         35
76*cdf0e10cSrcweir #define ODBC3SQLGetDiagRec          36
77*cdf0e10cSrcweir #define ODBC3SQLColumnPrivileges    37
78*cdf0e10cSrcweir #define ODBC3SQLColumns             38
79*cdf0e10cSrcweir #define ODBC3SQLForeignKeys         39
80*cdf0e10cSrcweir #define ODBC3SQLPrimaryKeys         40
81*cdf0e10cSrcweir #define ODBC3SQLProcedureColumns    41
82*cdf0e10cSrcweir #define ODBC3SQLProcedures          42
83*cdf0e10cSrcweir #define ODBC3SQLSpecialColumns      43
84*cdf0e10cSrcweir #define ODBC3SQLStatistics          44
85*cdf0e10cSrcweir #define ODBC3SQLTablePrivileges     45
86*cdf0e10cSrcweir #define ODBC3SQLTables              46
87*cdf0e10cSrcweir #define ODBC3SQLFreeStmt            47
88*cdf0e10cSrcweir #define ODBC3SQLCloseCursor         48
89*cdf0e10cSrcweir #define ODBC3SQLCancel              49
90*cdf0e10cSrcweir #define ODBC3SQLEndTran             50
91*cdf0e10cSrcweir #define ODBC3SQLDisconnect          51
92*cdf0e10cSrcweir #define ODBC3SQLFreeHandle          52
93*cdf0e10cSrcweir #define ODBC3SQLGetCursorName       53
94*cdf0e10cSrcweir #define ODBC3SQLNativeSql           54
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir namespace connectivity
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     namespace odbc
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         class OConnection;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         const sal_uInt32 ODBC_FRACTION_UNITS_PER_HSECOND = 10000000L;
103*cdf0e10cSrcweir         const sal_Int32 MAX_PUT_DATA_LENGTH = 2000;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         class OOO_DLLPUBLIC_ODBCBASE OTools
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir         public:
108*cdf0e10cSrcweir             static void ThrowException( OConnection* _pConnection,
109*cdf0e10cSrcweir                                         SQLRETURN _rRetCode,
110*cdf0e10cSrcweir                                         SQLHANDLE _pContext,
111*cdf0e10cSrcweir                                         SQLSMALLINT _nHandleType,
112*cdf0e10cSrcweir                                         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
113*cdf0e10cSrcweir                                         sal_Bool _bNoFound=sal_True,
114*cdf0e10cSrcweir                                         rtl_TextEncoding _nTextEncoding = RTL_TEXTENCODING_MS_1252)
115*cdf0e10cSrcweir                                         throw(::com::sun::star::sdbc::SQLException);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir             static void GetInfo(OConnection* _pConnection,
118*cdf0e10cSrcweir                                 SQLHANDLE _aConnectionHandle,
119*cdf0e10cSrcweir                                 SQLUSMALLINT _nInfo,
120*cdf0e10cSrcweir                                 ::rtl::OUString &_rValue,
121*cdf0e10cSrcweir                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
122*cdf0e10cSrcweir                                 rtl_TextEncoding _nTextEncoding)
123*cdf0e10cSrcweir                                 throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             static void GetInfo(OConnection* _pConnection,
126*cdf0e10cSrcweir                                 SQLHANDLE _aConnectionHandle,
127*cdf0e10cSrcweir                                 SQLUSMALLINT _nInfo,
128*cdf0e10cSrcweir                                 sal_Int32 &_rValue,
129*cdf0e10cSrcweir                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir             static void GetInfo(OConnection* _pConnection,
132*cdf0e10cSrcweir                                 SQLHANDLE _aConnectionHandle,
133*cdf0e10cSrcweir                                 SQLUSMALLINT _nInfo,
134*cdf0e10cSrcweir                                 SQLUSMALLINT &_rValue,
135*cdf0e10cSrcweir                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             static void GetInfo(OConnection* _pConnection,
138*cdf0e10cSrcweir                                 SQLHANDLE _aConnectionHandle,
139*cdf0e10cSrcweir                                 SQLUSMALLINT _nInfo,
140*cdf0e10cSrcweir                                 SQLUINTEGER &_rValue,
141*cdf0e10cSrcweir                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir             static void GetInfo(OConnection* _pConnection,
144*cdf0e10cSrcweir                                 SQLHANDLE _aConnectionHandle,
145*cdf0e10cSrcweir                                 SQLUSMALLINT _nInfo,
146*cdf0e10cSrcweir                                 sal_Bool &_rValue,
147*cdf0e10cSrcweir                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             static sal_Int32 MapOdbcType2Jdbc(sal_Int32 _nType);
150*cdf0e10cSrcweir             static sal_Int32 jdbcTypeToOdbc(sal_Int32 jdbcType);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir             static DATE_STRUCT DateToOdbcDate(const ::com::sun::star::util::Date& x)
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 DATE_STRUCT aVal;
155*cdf0e10cSrcweir                 aVal.year   = x.Year;
156*cdf0e10cSrcweir                 aVal.month  = x.Month;
157*cdf0e10cSrcweir                 aVal.day    = x.Day;
158*cdf0e10cSrcweir                 return aVal;
159*cdf0e10cSrcweir             }
160*cdf0e10cSrcweir             static TIME_STRUCT TimeToOdbcTime(const ::com::sun::star::util::Time& x)
161*cdf0e10cSrcweir             {
162*cdf0e10cSrcweir                 TIME_STRUCT aVal;
163*cdf0e10cSrcweir                 aVal.hour   = x.Hours;
164*cdf0e10cSrcweir                 aVal.minute = x.Minutes;
165*cdf0e10cSrcweir                 aVal.second = x.Seconds;
166*cdf0e10cSrcweir                 return aVal;
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir             static TIMESTAMP_STRUCT DateTimeToTimestamp(const ::com::sun::star::util::DateTime& x)
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 TIMESTAMP_STRUCT aVal;
171*cdf0e10cSrcweir                 aVal.year       = x.Year;
172*cdf0e10cSrcweir                 aVal.month      = x.Month;
173*cdf0e10cSrcweir                 aVal.day        = x.Day;
174*cdf0e10cSrcweir                 aVal.hour       = x.Hours;
175*cdf0e10cSrcweir                 aVal.minute     = x.Minutes;
176*cdf0e10cSrcweir                 aVal.second     = x.Seconds;
177*cdf0e10cSrcweir                 aVal.fraction   = x.HundredthSeconds * ODBC_FRACTION_UNITS_PER_HSECOND;
178*cdf0e10cSrcweir                 return aVal;
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir             /**
181*cdf0e10cSrcweir                 getBindTypes set the ODBC type for C
182*cdf0e10cSrcweir                 @param  _bUseWChar          true when Unicode should be used
183*cdf0e10cSrcweir                 @param  _bUseOldTimeDate    true when the old datetime format should be used
184*cdf0e10cSrcweir                 @param  _nOdbcType          the ODBC sql type
185*cdf0e10cSrcweir                 @param  fCType              the C type for the ODBC type
186*cdf0e10cSrcweir                 @param  fSqlType            the SQL type for the ODBC type
187*cdf0e10cSrcweir             */
188*cdf0e10cSrcweir             static void getBindTypes(sal_Bool _bUseWChar,
189*cdf0e10cSrcweir                                      sal_Bool _bUseOldTimeDate,
190*cdf0e10cSrcweir                                      SQLSMALLINT _nOdbcType,
191*cdf0e10cSrcweir                                      SQLSMALLINT& fCType,
192*cdf0e10cSrcweir                                      SQLSMALLINT& fSqlType);
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir             static ::rtl::OUString getStringValue(  OConnection* _pConnection,
195*cdf0e10cSrcweir                                                     SQLHANDLE _aStatementHandle,
196*cdf0e10cSrcweir                                                     sal_Int32 columnIndex,
197*cdf0e10cSrcweir                                                     SQLSMALLINT _fSqlType,
198*cdf0e10cSrcweir                                                     sal_Bool &_bWasNull,
199*cdf0e10cSrcweir                                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
200*cdf0e10cSrcweir                                                     rtl_TextEncoding _nTextEncoding) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir             static  ::com::sun::star::uno::Sequence<sal_Int8> getBytesValue(OConnection* _pConnection,
203*cdf0e10cSrcweir                                                                             SQLHANDLE _aStatementHandle,
204*cdf0e10cSrcweir                                                                             sal_Int32 columnIndex,
205*cdf0e10cSrcweir                                                                             SQLSMALLINT _fSqlType,
206*cdf0e10cSrcweir                                                                             sal_Bool &_bWasNull,
207*cdf0e10cSrcweir                                                                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
208*cdf0e10cSrcweir             static void getValue(   OConnection* _pConnection,
209*cdf0e10cSrcweir                                     SQLHANDLE _aStatementHandle,
210*cdf0e10cSrcweir                                     sal_Int32 columnIndex,
211*cdf0e10cSrcweir                                     SQLSMALLINT _nType,
212*cdf0e10cSrcweir                                     sal_Bool &_bWasNull,
213*cdf0e10cSrcweir                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
214*cdf0e10cSrcweir                                     void* _pValue,
215*cdf0e10cSrcweir                                     SQLLEN _nSize) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             /**
218*cdf0e10cSrcweir                 bindData copies the from pValue to pData
219*cdf0e10cSrcweir                 @param  _nOdbcType          the ODBC sql type
220*cdf0e10cSrcweir                 @param  _bUseWChar          true when Unicode should be used
221*cdf0e10cSrcweir                 @param  _pData              contains a copy of the data to be set
222*cdf0e10cSrcweir                 @param  _pValue             contains the data to be copied
223*cdf0e10cSrcweir                 @param  _nTextEncoding      the text encoding
224*cdf0e10cSrcweir                 @param  _nColumnSize        the columnsize which is a out param
225*cdf0e10cSrcweir             */
226*cdf0e10cSrcweir             static void bindData(   SQLSMALLINT _nOdbcType,
227*cdf0e10cSrcweir                                     sal_Bool _bUseWChar,
228*cdf0e10cSrcweir                                     sal_Int8 *&_pData,
229*cdf0e10cSrcweir                                     SQLLEN*& pLen,
230*cdf0e10cSrcweir                                     const void* _pValue,
231*cdf0e10cSrcweir                                     rtl_TextEncoding _nTextEncoding,
232*cdf0e10cSrcweir                                     SQLULEN& _nColumnSize);
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             static void bindParameter(  OConnection* _pConnection,
235*cdf0e10cSrcweir                                         SQLHANDLE _hStmt,
236*cdf0e10cSrcweir                                         sal_Int32 nPos,
237*cdf0e10cSrcweir                                         sal_Int8*& pDataBuffer,
238*cdf0e10cSrcweir                                         sal_Int8* pLenBuffer,
239*cdf0e10cSrcweir                                         SQLSMALLINT _nJDBCtype,
240*cdf0e10cSrcweir                                         sal_Bool _bUseWChar,
241*cdf0e10cSrcweir                                         sal_Bool _bUseOldTimeDate,
242*cdf0e10cSrcweir                                         const void* _pValue,
243*cdf0e10cSrcweir                                         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
244*cdf0e10cSrcweir                                         rtl_TextEncoding _nTextEncoding)
245*cdf0e10cSrcweir                                          throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir             static void bindValue(  OConnection* _pConnection,
248*cdf0e10cSrcweir                                     SQLHANDLE _aStatementHandle,
249*cdf0e10cSrcweir                                     sal_Int32 columnIndex,
250*cdf0e10cSrcweir                                     SQLSMALLINT _nType,
251*cdf0e10cSrcweir                                     SQLSMALLINT _nMaxLen,
252*cdf0e10cSrcweir                                     const void* _pValue,
253*cdf0e10cSrcweir                                     void*       _pData,
254*cdf0e10cSrcweir                                     SQLLEN *pLen,
255*cdf0e10cSrcweir                                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
256*cdf0e10cSrcweir                                     rtl_TextEncoding _nTextEncoding,
257*cdf0e10cSrcweir                                     sal_Bool _bUseOldTimeDate) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
258*cdf0e10cSrcweir         };
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         template <class T> void getValue(   OConnection* _pConnection,
261*cdf0e10cSrcweir                                             SQLHANDLE _aStatementHandle,
262*cdf0e10cSrcweir                                             sal_Int32 columnIndex,
263*cdf0e10cSrcweir                                             SQLSMALLINT _nType,
264*cdf0e10cSrcweir                                             sal_Bool &_bWasNull,
265*cdf0e10cSrcweir                                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
266*cdf0e10cSrcweir                                             T& _rValue) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
267*cdf0e10cSrcweir         {
268*cdf0e10cSrcweir             OTools::getValue(_pConnection,_aStatementHandle,columnIndex,_nType,_bWasNull,_xInterface,&_rValue,sizeof _rValue);
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir         //-----------------------------------------------------------------------------
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir #endif // _CONNECTIVITY_OTOOLS_HXX_
276*cdf0e10cSrcweir 
277