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