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