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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include <com/sun/star/sdbcx/Privilege.hpp>
27 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
28 #include "connectivity/dbexception.hxx"
29 #include "ado/Awrapado.hxx"
30 #include "ado/adoimp.hxx"
31 #include <osl/diagnose.h>
32 #include <com/sun/star/sdbc/DataType.hpp>
33 
34 
35 using namespace connectivity::ado;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::sdbc;
38 using namespace com::sun::star::sdbcx;
39 
40 
41 #define MYADOID(l) {l, 0,0x10,0x80,0,0,0xAA,0,0x6D,0x2E,0xA4};
42 
43 const CLSID ADOS::CLSID_ADOCONNECTION_21	=	MYADOID(0x00000514);
44 const IID	ADOS::IID_ADOCONNECTION_21		=	MYADOID(0x00000550);
45 
46 const CLSID ADOS::CLSID_ADOCOMMAND_21		=	MYADOID(0x00000507);
47 const IID	ADOS::IID_ADOCOMMAND_21			=	MYADOID(0x0000054E);
48 
49 const CLSID ADOS::CLSID_ADORECORDSET_21		=	MYADOID(0x00000535);
50 const IID	ADOS::IID_ADORECORDSET_21		=	MYADOID(0x0000054F);
51 
52 const CLSID ADOS::CLSID_ADOCATALOG_25		=	MYADOID(0x00000602);
53 const IID ADOS::IID_ADOCATALOG_25			=	MYADOID(0x00000603);
54 
55 const CLSID ADOS::CLSID_ADOINDEX_25			=	MYADOID(0x0000061E);
56 const IID ADOS::IID_ADOINDEX_25				=	MYADOID(0x0000061F);
57 
58 const CLSID ADOS::CLSID_ADOTABLE_25			=	MYADOID(0x00000609);
59 const IID ADOS::IID_ADOTABLE_25				=	MYADOID(0x00000610);
60 
61 const CLSID ADOS::CLSID_ADOKEY_25			=	MYADOID(0x00000621);
62 const IID ADOS::IID_ADOKEY_25				=	MYADOID(0x00000622);
63 
64 const CLSID ADOS::CLSID_ADOCOLUMN_25		=	MYADOID(0x0000061B);
65 const IID ADOS::IID_ADOCOLUMN_25			=	MYADOID(0x0000061C);
66 
67 const CLSID ADOS::CLSID_ADOGROUP_25			=	MYADOID(0x00000615);
68 const IID ADOS::IID_ADOGROUP_25				=	MYADOID(0x00000616);
69 
70 const CLSID ADOS::CLSID_ADOUSER_25			=	MYADOID(0x00000618);
71 const IID ADOS::IID_ADOUSER_25				=	MYADOID(0x00000619);
72 
73 const CLSID ADOS::CLSID_ADOVIEW_25			=	MYADOID(0x00000612);
74 const IID ADOS::IID_ADOVIEW_25				=	MYADOID(0x00000613);
75 
GetKeyStr()76 OLEString& ADOS::GetKeyStr()
77 {
78 	static OLEString sKeyStr(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("gxwaezucfyqpwjgqbcmtsncuhwsnyhiohwxz")));
79 	return sKeyStr;
80 }
81 
82 // -------------------------------------------------------------------------
MapADOType2Jdbc(DataTypeEnum eType)83 sal_Int32 ADOS::MapADOType2Jdbc(DataTypeEnum eType)
84 {
85 	sal_Int32 nType = DataType::VARCHAR;;
86 	switch (eType)
87 	{
88 		case adUnsignedSmallInt:
89 		case adSmallInt:			nType = DataType::SMALLINT;		break;
90 		case adUnsignedInt:
91 		case adInteger:				nType = DataType::INTEGER;		break;
92 		case adUnsignedBigInt:
93 		case adBigInt:				nType = DataType::BIGINT;		break;
94 		case adSingle:				nType = DataType::FLOAT;		break;
95 		case adDouble:				nType = DataType::DOUBLE;		break;
96 		case adCurrency:			nType = DataType::DOUBLE;		break;
97 		case adVarNumeric:
98 		case adNumeric:				nType = DataType::NUMERIC;		break;
99 		case adDecimal:				nType = DataType::DECIMAL;		break;
100 		case adDBDate:				nType = DataType::DATE;			break;
101 		case adDBTime:				nType = DataType::TIME;			break;
102 		case adDate:
103 		case adDBTimeStamp:			nType = DataType::TIMESTAMP;	break;
104 		case adBoolean:				nType = DataType::BOOLEAN;		break;
105 //		case adArray:				nType = DataType::ARRAY;		break;
106 		case adBinary:				nType = DataType::BINARY;		break;
107 		case adGUID:				nType = DataType::OBJECT;		break;
108 		case adBSTR:
109 		case adVarWChar:
110 		case adWChar:
111 		case adVarChar:				nType = DataType::VARCHAR;		break;
112 		case adLongVarWChar:
113 		case adLongVarChar:			nType = DataType::LONGVARCHAR;	break;
114 		case adVarBinary:			nType = DataType::VARBINARY;	break;
115 		case adLongVarBinary:		nType = DataType::LONGVARBINARY;break;
116 		case adChar:				nType = DataType::CHAR;			break;
117 		case adUnsignedTinyInt:
118 		case adTinyInt:				nType = DataType::TINYINT;		break;
119 		case adEmpty:				nType = DataType::SQLNULL;		break;
120 		case adUserDefined:
121 		case adPropVariant:
122 		case adFileTime:
123 		case adChapter:
124 		case adIDispatch:
125 		case adIUnknown:
126 		case adError:
127 		case adVariant:
128 									nType = DataType::OTHER;		break;
129 		default:
130 			OSL_ENSURE(0,"MapADOType2Jdbc: Unknown Type!");
131 			;
132 	}
133 	return nType;
134 }
135 // -------------------------------------------------------------------------
MapJdbc2ADOType(sal_Int32 _nType,sal_Int32 _nJetEngine)136 DataTypeEnum ADOS::MapJdbc2ADOType(sal_Int32 _nType,sal_Int32 _nJetEngine)
137 {
138 	switch (_nType)
139 	{
140 		case DataType::SMALLINT:		return adSmallInt;			break;
141 		case DataType::INTEGER:			return adInteger;			break;
142 		case DataType::BIGINT:			return adBigInt;			break;
143 		case DataType::FLOAT:			return adSingle;			break;
144 		case DataType::DOUBLE:			return adDouble;			break;
145 		case DataType::NUMERIC:			return adNumeric;			break;
146 		case DataType::DECIMAL:			return adDecimal;			break;
147 		case DataType::DATE:			return isJetEngine(_nJetEngine) ? adDate : adDBDate;			break;
148 		case DataType::TIME:			return adDBTime;			break;
149 		case DataType::TIMESTAMP:		return isJetEngine(_nJetEngine) ? adDate : adDBTimeStamp;		break;
150         case DataType::BOOLEAN:
151 		case DataType::BIT:				return adBoolean;			break;
152 		case DataType::BINARY:			return adBinary;			break;
153 		case DataType::VARCHAR:			return adVarWChar;			break;
154 		case DataType::CLOB:
155 		case DataType::LONGVARCHAR:		return adLongVarWChar;		break;
156 		case DataType::VARBINARY:		return adVarBinary;			break;
157 		case DataType::BLOB:
158 		case DataType::LONGVARBINARY:	return adLongVarBinary;		break;
159 		case DataType::CHAR:			return adWChar;				break;
160 		case DataType::TINYINT:			return isJetEngine(_nJetEngine) ? adUnsignedTinyInt : adTinyInt;break;
161 		case DataType::OBJECT:			return adGUID;		break;
162 	default:
163 		OSL_ENSURE(0,"MapJdbc2ADOType: Unknown Type!");
164 			;
165 	}
166 	return adEmpty;
167 }
168 // -----------------------------------------------------------------------------
169 const int JET_ENGINETYPE_UNKNOWN	= 0;
170 const int JET_ENGINETYPE_JET10		= 1;
171 const int JET_ENGINETYPE_JET11		= 2;
172 const int JET_ENGINETYPE_JET20		= 3;
173 const int JET_ENGINETYPE_JET3X		= 4;
174 const int JET_ENGINETYPE_JET4X		= 5;
175 const int JET_ENGINETYPE_DBASE3		= 10;
176 const int JET_ENGINETYPE_DBASE4		= 11;
177 const int JET_ENGINETYPE_DBASE5		= 12;
178 const int JET_ENGINETYPE_EXCEL30	= 20;
179 const int JET_ENGINETYPE_EXCEL40	= 21;
180 const int JET_ENGINETYPE_EXCEL50	= 22;
181 const int JET_ENGINETYPE_EXCEL80	= 23;
182 const int JET_ENGINETYPE_EXCEL90	= 24;
183 const int JET_ENGINETYPE_EXCHANGE4	= 30;
184 const int JET_ENGINETYPE_LOTUSWK1	= 40;
185 const int JET_ENGINETYPE_LOTUSWK3	= 41;
186 const int JET_ENGINETYPE_LOTUSWK4	= 42;
187 const int JET_ENGINETYPE_PARADOX3X	= 50;
188 const int JET_ENGINETYPE_PARADOX4X	= 51;
189 const int JET_ENGINETYPE_PARADOX5X	= 52;
190 const int JET_ENGINETYPE_PARADOX7X	= 53;
191 const int JET_ENGINETYPE_TEXT1X		= 60;
192 const int JET_ENGINETYPE_HTML1X		= 70;
193 
isJetEngine(sal_Int32 _nEngineType)194 sal_Bool ADOS::isJetEngine(sal_Int32 _nEngineType)
195 {
196 	sal_Bool bRet = sal_False;
197 	switch(_nEngineType)
198 	{
199 		case JET_ENGINETYPE_UNKNOWN:
200 		case JET_ENGINETYPE_JET10:
201 		case JET_ENGINETYPE_JET11:
202 		case JET_ENGINETYPE_JET20:
203 		case JET_ENGINETYPE_JET3X:
204 		case JET_ENGINETYPE_JET4X:
205 		case JET_ENGINETYPE_DBASE3:
206 		case JET_ENGINETYPE_DBASE4:
207 		case JET_ENGINETYPE_DBASE5:
208 		case JET_ENGINETYPE_EXCEL30:
209 		case JET_ENGINETYPE_EXCEL40:
210 		case JET_ENGINETYPE_EXCEL50:
211 		case JET_ENGINETYPE_EXCEL80:
212 		case JET_ENGINETYPE_EXCEL90:
213 		case JET_ENGINETYPE_EXCHANGE4:
214 		case JET_ENGINETYPE_LOTUSWK1:
215 		case JET_ENGINETYPE_LOTUSWK3:
216 		case JET_ENGINETYPE_LOTUSWK4:
217 		case JET_ENGINETYPE_PARADOX3X:
218 		case JET_ENGINETYPE_PARADOX4X:
219 		case JET_ENGINETYPE_PARADOX5X:
220 		case JET_ENGINETYPE_PARADOX7X:
221 		case JET_ENGINETYPE_TEXT1X:
222 		case JET_ENGINETYPE_HTML1X:
223 			bRet = sal_True;
224 			break;
225 	}
226 	return bRet;
227 }
228 // -----------------------------------------------------------------------------
mapObjectType2Ado(sal_Int32 objType)229 ObjectTypeEnum ADOS::mapObjectType2Ado(sal_Int32 objType)
230 {
231 	ObjectTypeEnum eType = adPermObjTable;
232 	switch(objType)
233 	{
234 		case PrivilegeObject::TABLE:
235 			eType = adPermObjTable;
236 			break;
237 		case PrivilegeObject::VIEW:
238 			eType = adPermObjView;
239 			break;
240 		case PrivilegeObject::COLUMN:
241 			eType = adPermObjColumn;
242 			break;
243 	}
244 	return eType;
245 }
246 // -----------------------------------------------------------------------------
mapAdoType2Object(ObjectTypeEnum objType)247 sal_Int32 ADOS::mapAdoType2Object(ObjectTypeEnum objType)
248 {
249 	sal_Int32 nType = PrivilegeObject::TABLE;
250 	switch(objType)
251 	{
252 		case adPermObjTable:
253 			nType = PrivilegeObject::TABLE;
254 			break;
255 		case adPermObjView:
256 			nType = PrivilegeObject::VIEW;
257 			break;
258 		case adPermObjColumn:
259 			nType = PrivilegeObject::COLUMN;
260 			break;
261         default:
262             OSL_ENSURE( false, "ADOS::mapAdoType2Object: privilege type cannot be translated!" );
263             break;
264 	}
265 	return nType;
266 }
267 #ifdef DELETE
268 #undef DELETE
269 #endif
270 // -----------------------------------------------------------------------------
mapAdoRights2Sdbc(RightsEnum eRights)271 sal_Int32 ADOS::mapAdoRights2Sdbc(RightsEnum eRights)
272 {
273 	sal_Int32 nRights = 0;
274 	if((eRights & adRightInsert) == adRightInsert)
275 		nRights |= Privilege::INSERT;
276 	if((eRights & adRightDelete) == adRightDelete)
277 		nRights |= ::com::sun::star::sdbcx::Privilege::DELETE;
278 	if((eRights & adRightUpdate) == adRightUpdate)
279 		nRights |= Privilege::UPDATE;
280 	if((eRights & adRightWriteDesign) == adRightWriteDesign)
281 		nRights |= Privilege::ALTER;
282 	if((eRights & adRightRead) == adRightRead)
283 		nRights |= Privilege::SELECT;
284 	if((eRights & adRightReference) == adRightReference)
285 		nRights |= Privilege::REFERENCE;
286 	if((eRights & adRightDrop) == adRightDrop)
287 		nRights |= Privilege::DROP;
288 
289 	return nRights;
290 }
291 // -----------------------------------------------------------------------------
mapRights2Ado(sal_Int32 nRights)292 sal_Int32 ADOS::mapRights2Ado(sal_Int32 nRights)
293 {
294 	sal_Int32 eRights = adRightNone;
295 
296 	if((nRights & Privilege::INSERT) == Privilege::INSERT)
297 		eRights |= adRightInsert;
298 	if((nRights & Privilege::DELETE) == Privilege::DELETE)
299 		eRights |= adRightDelete;
300 	if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
301 		eRights |= adRightUpdate;
302 	if((nRights & Privilege::ALTER) == Privilege::ALTER)
303 		eRights |= adRightWriteDesign;
304 	if((nRights & Privilege::SELECT) == Privilege::SELECT)
305 		eRights |= adRightRead;
306 	if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
307 		eRights |= adRightReference;
308 	if((nRights & Privilege::DROP) == Privilege::DROP)
309 		eRights |= adRightDrop;
310 
311 	return eRights;
312 }
313 // -----------------------------------------------------------------------------
getField(ADORecordset * _pRecordSet,sal_Int32 _nColumnIndex)314 WpADOField ADOS::getField(ADORecordset* _pRecordSet,sal_Int32 _nColumnIndex) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
315 {
316 	if ( !_pRecordSet )
317 		return WpADOField();
318 
319 	ADOFields* pFields	= NULL;
320 	_pRecordSet->get_Fields(&pFields);
321 	WpOLEAppendCollection<ADOFields, ADOField, WpADOField>	aFields(pFields);
322 	if(_nColumnIndex <= 0 || _nColumnIndex > aFields.GetItemCount())
323 		::dbtools::throwInvalidIndexException(NULL);
324 	WpADOField aField(aFields.GetItem(_nColumnIndex-1));
325 	if(!aField.IsValid())
326 		::dbtools::throwInvalidIndexException(NULL);
327 	return aField;
328 }
329 // -----------------------------------------------------------------------------
330 
331 
332 
333 
334