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                                         throw(::com::sun::star::sdbc::SQLException);
112 
113 			static void GetInfo(OConnection* _pConnection,
114 								SQLHANDLE _aConnectionHandle,
115 								SQLUSMALLINT _nInfo,
116 								::rtl::OUString &_rValue,
117                                 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
118 								rtl_TextEncoding _nTextEncoding)
119                                 throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
120 
121             static void GetInfo(OConnection* _pConnection,
122 								SQLHANDLE _aConnectionHandle,
123 								SQLUSMALLINT _nInfo,
124 								sal_Int32 &_rValue,
125 								const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
126 
127 			static void GetInfo(OConnection* _pConnection,
128 								SQLHANDLE _aConnectionHandle,
129 								SQLUSMALLINT _nInfo,
130 								SQLUSMALLINT &_rValue,
131 								const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
132 
133 			static void GetInfo(OConnection* _pConnection,
134 								SQLHANDLE _aConnectionHandle,
135 								SQLUSMALLINT _nInfo,
136 								SQLUINTEGER &_rValue,
137 								const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
138 
139 			static void GetInfo(OConnection* _pConnection,
140 								SQLHANDLE _aConnectionHandle,
141 								SQLUSMALLINT _nInfo,
142 								sal_Bool &_rValue,
143 								const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
144 
145 			static sal_Int32 MapOdbcType2Jdbc(sal_Int32 _nType);
146 			static sal_Int32 jdbcTypeToOdbc(sal_Int32 jdbcType);
147 
DateToOdbcDate(const::com::sun::star::util::Date & x)148 			static DATE_STRUCT DateToOdbcDate(const ::com::sun::star::util::Date& x)
149 			{
150 				DATE_STRUCT aVal;
151 				aVal.year	= x.Year;
152 				aVal.month	= x.Month;
153 				aVal.day	= x.Day;
154 				return aVal;
155 			}
TimeToOdbcTime(const::com::sun::star::util::Time & x)156 			static TIME_STRUCT TimeToOdbcTime(const ::com::sun::star::util::Time& x)
157 			{
158 				TIME_STRUCT aVal;
159 				aVal.hour	= x.Hours;
160 				aVal.minute	= x.Minutes;
161 				aVal.second	= x.Seconds;
162 				return aVal;
163 			}
DateTimeToTimestamp(const::com::sun::star::util::DateTime & x)164 			static TIMESTAMP_STRUCT DateTimeToTimestamp(const ::com::sun::star::util::DateTime& x)
165 			{
166 				TIMESTAMP_STRUCT aVal;
167 				aVal.year		= x.Year;
168 				aVal.month		= x.Month;
169 				aVal.day		= x.Day;
170 				aVal.hour		= x.Hours;
171 				aVal.minute		= x.Minutes;
172 				aVal.second		= x.Seconds;
173 				aVal.fraction	= x.HundredthSeconds * ODBC_FRACTION_UNITS_PER_HSECOND;
174 				return aVal;
175 			}
176 			/**
177 				getBindTypes set the ODBC type for C
178 				@param	_bUseWChar			true when Unicode should be used
179 				@param	_bUseOldTimeDate	true when the old datetime format should be used
180 				@param	_nOdbcType			the ODBC sql type
181 				@param	fCType				the C type for the ODBC type
182 				@param	fSqlType			the SQL type for the ODBC type
183 			*/
184 			static void getBindTypes(sal_Bool _bUseWChar,
185 									 sal_Bool _bUseOldTimeDate,
186 									 SQLSMALLINT _nOdbcType,
187 									 SQLSMALLINT& fCType,
188 									 SQLSMALLINT& fSqlType);
189 
190 			static ::rtl::OUString getStringValue(	OConnection* _pConnection,
191 													SQLHANDLE _aStatementHandle,
192 													sal_Int32 columnIndex,
193 													SQLSMALLINT _fSqlType,
194 													sal_Bool &_bWasNull,
195 													const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
196 													rtl_TextEncoding _nTextEncoding) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
197 
198 			static  ::com::sun::star::uno::Sequence<sal_Int8> getBytesValue(OConnection* _pConnection,
199 																			SQLHANDLE _aStatementHandle,
200 																			sal_Int32 columnIndex,
201 																			SQLSMALLINT _fSqlType,
202 																			sal_Bool &_bWasNull,
203 																			const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
204 			static void getValue(	OConnection* _pConnection,
205 									SQLHANDLE _aStatementHandle,
206 									sal_Int32 columnIndex,
207 									SQLSMALLINT _nType,
208 									sal_Bool &_bWasNull,
209 									const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
210 									void* _pValue,
211 									SQLLEN _nSize) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
212 
213 			/**
214 				bindData copies the from pValue to pData
215 				@param	_nOdbcType			the ODBC sql type
216 				@param	_bUseWChar			true when Unicode should be used
217 				@param	_pData				contains a copy of the data to be set
218 				@param	_pValue				contains the data to be copied
219 				@param	_nTextEncoding		the text encoding
220 				@param	_nColumnSize		the columnsize which is a out param
221 			*/
222 			static void bindData(	SQLSMALLINT _nOdbcType,
223 									sal_Bool _bUseWChar,
224 									sal_Int8 *&_pData,
225 									SQLLEN*& pLen,
226 									const void* _pValue,
227 									rtl_TextEncoding _nTextEncoding,
228 									SQLULEN& _nColumnSize);
229 
230 			static void bindParameter(	OConnection* _pConnection,
231 										SQLHANDLE _hStmt,
232 										sal_Int32 nPos,
233 										sal_Int8*& pDataBuffer,
234 										sal_Int8* pLenBuffer,
235 										SQLSMALLINT _nJDBCtype,
236 										sal_Bool _bUseWChar,
237 										sal_Bool _bUseOldTimeDate,
238 										const void* _pValue,
239 										const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
240 										rtl_TextEncoding _nTextEncoding)
241 										 throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
242 
243 			static void bindValue(	OConnection* _pConnection,
244 									SQLHANDLE _aStatementHandle,
245 									sal_Int32 columnIndex,
246 									SQLSMALLINT _nType,
247 									SQLSMALLINT _nMaxLen,
248 									const void* _pValue,
249 									void*		_pData,
250 									SQLLEN *pLen,
251 									const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
252 									rtl_TextEncoding _nTextEncoding,
253 									sal_Bool _bUseOldTimeDate) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
254 		};
255 
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)256 		template <class T> void getValue(	OConnection* _pConnection,
257 											SQLHANDLE _aStatementHandle,
258 											sal_Int32 columnIndex,
259 											SQLSMALLINT _nType,
260 											sal_Bool &_bWasNull,
261 											const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
262 											T& _rValue) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
263 		{
264 			OTools::getValue(_pConnection,_aStatementHandle,columnIndex,_nType,_bWasNull,_xInterface,&_rValue,sizeof _rValue);
265 		}
266 		//-----------------------------------------------------------------------------
267 
268 
269 	}
270 }
271 #endif // _CONNECTIVITY_OTOOLS_HXX_
272 
273