1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #ifndef INCLUDED_CONNECTIVITY_SOURCE_INC_ODBC_OTOOLS_HXX
20 #define INCLUDED_CONNECTIVITY_SOURCE_INC_ODBC_OTOOLS_HXX
21 
22 #include <connectivity/odbc.hxx>
23 #include <odbc/odbcbasedllapi.hxx>
24 #include <com/sun/star/sdbc/SQLException.hpp>
25 #include <com/sun/star/util/Date.hpp>
26 #include <com/sun/star/util/Time.hpp>
27 #include <com/sun/star/util/DateTime.hpp>
28 #include <osl/thread.h>
29 #include <rtl/ustring.hxx>
30 #include <com/sun/star/uno/Sequence.hxx>
31 #include <rtl/textenc.h>
32 
33 enum class ODBC3SQLFunctionId
34 {
35     AllocHandle         = 1,
36     Connect             = 2,
37     DriverConnect       = 3,
38     BrowseConnect       = 4,
39     DataSources         = 5,
40     Drivers             = 6,
41     GetInfo             = 7,
42     GetFunctions        = 8,
43     GetTypeInfo         = 9,
44     SetConnectAttr      = 10,
45     GetConnectAttr      = 11,
46     SetEnvAttr          = 12,
47     GetEnvAttr          = 13,
48     SetStmtAttr         = 14,
49     GetStmtAttr         = 15,
50     Prepare             = 16,
51     BindParameter       = 17,
52     SetCursorName       = 18,
53     Execute             = 19,
54     ExecDirect          = 20,
55     DescribeParam       = 21,
56     NumParams           = 22,
57     ParamData           = 23,
58     PutData             = 24,
59     RowCount            = 25,
60     NumResultCols       = 26,
61     DescribeCol         = 27,
62     ColAttribute        = 28,
63     BindCol             = 29,
64     Fetch               = 30,
65     FetchScroll         = 31,
66     GetData             = 32,
67     SetPos              = 33,
68     BulkOperations      = 34,
69     MoreResults         = 35,
70     GetDiagRec          = 36,
71     ColumnPrivileges    = 37,
72     Columns             = 38,
73     ForeignKeys         = 39,
74     PrimaryKeys         = 40,
75     ProcedureColumns    = 41,
76     Procedures          = 42,
77     SpecialColumns      = 43,
78     Statistics          = 44,
79     TablePrivileges     = 45,
80     Tables              = 46,
81     FreeStmt            = 47,
82     CloseCursor         = 48,
83     Cancel              = 49,
84     EndTran             = 50,
85     Disconnect          = 51,
86     FreeHandle          = 52,
87     GetCursorName       = 53,
88     NativeSql           = 54,
89 };
90 
91 namespace connectivity
92 {
93     namespace odbc
94     {
95         class OConnection;
96 
97         const sal_Int32 MAX_PUT_DATA_LENGTH = 2000;
98 
99         class OOO_DLLPUBLIC_ODBCBASE OTools
100         {
101         public:
102             /// @throws css::sdbc::SQLException
103             static void ThrowException( const OConnection* _pConnection,
104                                         SQLRETURN _rRetCode,
105                                         SQLHANDLE _pContext,
106                                         SQLSMALLINT _nHandleType,
107                                         const css::uno::Reference< css::uno::XInterface >& _xInterface,
108                                         bool _bNoFound=true);
109 
110             /// @throws css::sdbc::SQLException
111             /// @throws css::uno::RuntimeException
112             static void GetInfo(OConnection const * _pConnection,
113                                 SQLHANDLE _aConnectionHandle,
114                                 SQLUSMALLINT _nInfo,
115                                 OUString &_rValue,
116                                 const css::uno::Reference< css::uno::XInterface >& _xInterface,
117                                 rtl_TextEncoding _nTextEncoding);
118 
119             /// @throws css::sdbc::SQLException
120             /// @throws css::uno::RuntimeException
121             static void GetInfo(OConnection const * _pConnection,
122                                 SQLHANDLE _aConnectionHandle,
123                                 SQLUSMALLINT _nInfo,
124                                 sal_Int32 &_rValue,
125                                 const css::uno::Reference< css::uno::XInterface >& _xInterface);
126 
127             /// @throws css::sdbc::SQLException
128             /// @throws css::uno::RuntimeException
129             static void GetInfo(OConnection const * _pConnection,
130                                 SQLHANDLE _aConnectionHandle,
131                                 SQLUSMALLINT _nInfo,
132                                 SQLUSMALLINT &_rValue,
133                                 const css::uno::Reference< css::uno::XInterface >& _xInterface);
134 
135             /// @throws css::sdbc::SQLException
136             /// @throws css::uno::RuntimeException
137             static void GetInfo(OConnection const * _pConnection,
138                                 SQLHANDLE _aConnectionHandle,
139                                 SQLUSMALLINT _nInfo,
140                                 SQLUINTEGER &_rValue,
141                                 const css::uno::Reference< css::uno::XInterface >& _xInterface);
142 
143             static sal_Int32 MapOdbcType2Jdbc(SQLSMALLINT _nType);
144             static SQLSMALLINT jdbcTypeToOdbc(sal_Int32 jdbcType);
145 
DateToOdbcDate(const css::util::Date & x)146             static DATE_STRUCT DateToOdbcDate(const css::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 css::util::Time & x)154             static TIME_STRUCT TimeToOdbcTime(const css::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 css::util::DateTime & x)162             static TIMESTAMP_STRUCT DateTimeToTimestamp(const css::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.NanoSeconds;
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(bool _bUseWChar,
183                                      bool _bUseOldTimeDate,
184                                      SQLSMALLINT _nOdbcType,
185                                      SQLSMALLINT& fCType,
186                                      SQLSMALLINT& fSqlType);
187 
188             /// @throws css::sdbc::SQLException
189             /// @throws css::uno::RuntimeException
190             static OUString getStringValue(  OConnection const * _pConnection,
191                                                     SQLHANDLE _aStatementHandle,
192                                                     sal_Int32 columnIndex,
193                                                     SQLSMALLINT _fSqlType,
194                                                     bool &_bWasNull,
195                                                     const css::uno::Reference< css::uno::XInterface >& _xInterface,
196                                                     rtl_TextEncoding _nTextEncoding);
197 
198             /// @throws css::sdbc::SQLException
199             /// @throws css::uno::RuntimeException
200             static  css::uno::Sequence<sal_Int8> getBytesValue(const OConnection* _pConnection,
201                                                                             SQLHANDLE _aStatementHandle,
202                                                                             sal_Int32 columnIndex,
203                                                                             SQLSMALLINT _fSqlType,
204                                                                             bool &_bWasNull,
205                                                                             const css::uno::Reference< css::uno::XInterface >& _xInterface);
206             /// @throws css::sdbc::SQLException
207             /// @throws css::uno::RuntimeException
208             static void getValue(   OConnection const * _pConnection,
209                                     SQLHANDLE _aStatementHandle,
210                                     sal_Int32 columnIndex,
211                                     SQLSMALLINT _nType,
212                                     bool &_bWasNull,
213                                     const css::uno::Reference< css::uno::XInterface >& _xInterface,
214                                     void* _pValue,
215                                     SQLLEN _nSize);
216 
217             /// @throws css::sdbc::SQLException
218             /// @throws css::uno::RuntimeException
219             static void bindValue(  OConnection const * _pConnection,
220                                     SQLHANDLE _aStatementHandle,
221                                     sal_Int32 columnIndex,
222                                     SQLSMALLINT _nType,
223                                     SQLSMALLINT _nMaxLen,
224                                     const void* _pValue,
225                                     void*       _pData,
226                                     SQLLEN *pLen,
227                                     const css::uno::Reference< css::uno::XInterface >& _xInterface,
228                                     rtl_TextEncoding _nTextEncoding,
229                                     bool _bUseOldTimeDate);
230         };
231 
232             /// @throws css::sdbc::SQLException
233             /// @throws css::uno::RuntimeException
getValue(const OConnection * _pConnection,SQLHANDLE _aStatementHandle,sal_Int32 columnIndex,SQLSMALLINT _nType,bool & _bWasNull,const css::uno::Reference<css::uno::XInterface> & _xInterface,T & _rValue)234         template <class T> void getValue(   const OConnection* _pConnection,
235                                             SQLHANDLE _aStatementHandle,
236                                             sal_Int32 columnIndex,
237                                             SQLSMALLINT _nType,
238                                             bool &_bWasNull,
239                                             const css::uno::Reference< css::uno::XInterface >& _xInterface,
240                                             T& _rValue)
241         {
242             OTools::getValue(_pConnection,_aStatementHandle,columnIndex,_nType,_bWasNull,_xInterface,&_rValue,sizeof _rValue);
243         }
244 
245 
246     }
247 }
248 #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_ODBC_OTOOLS_HXX
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
251