Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / ado / ADatabaseMetaDataImpl.cxx
blob100c1ae27e2ef966076ae2d3591c151b861c02b8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ado/ADatabaseMetaData.hxx"
21 #include "ado/ADatabaseMetaDataResultSetMetaData.hxx"
22 #include "ado/Awrapado.hxx"
23 #include "ado/AGroup.hxx"
24 #include "ado/adoimp.hxx"
25 #include "ado/AIndex.hxx"
26 #include "ado/AKey.hxx"
27 #include "ado/ATable.hxx"
28 #include <com/sun/star/sdbc/DataType.hpp>
29 #include <com/sun/star/sdbc/ProcedureResult.hpp>
30 #include <com/sun/star/sdbc/ColumnValue.hpp>
31 #ifdef DELETE
32 #undef DELETE
33 #endif
34 #include <com/sun/star/sdbcx/Privilege.hpp>
35 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
36 #include <com/sun/star/sdbc/KeyRule.hpp>
37 #include <com/sun/star/sdbcx/KeyType.hpp>
39 using namespace connectivity::ado;
40 using namespace ::com::sun::star::sdbc;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::uno;
45 void ODatabaseMetaData::fillLiterals()
47 ADORecordset *pRecordset = NULL;
48 OLEVariant vtEmpty;
49 vtEmpty.setNoArg();
50 m_pADOConnection->OpenSchema(adSchemaDBInfoLiterals,vtEmpty,vtEmpty,&pRecordset);
52 ADOS::ThrowException(*m_pADOConnection,*this);
54 OSL_ENSURE(pRecordset,"fillLiterals: no resultset!");
55 if ( pRecordset )
57 WpADORecordset aRecordset(pRecordset);
59 aRecordset.MoveFirst();
60 OLEVariant aValue;
61 LiteralInfo aInfo;
62 while(!aRecordset.IsAtEOF())
64 WpOLEAppendCollection<ADOFields, ADOField, WpADOField> aFields(aRecordset.GetFields());
65 WpADOField aField(aFields.GetItem(1));
66 aInfo.pwszLiteralValue = aField.get_Value();
67 aField = aFields.GetItem(5);
68 aInfo.fSupported = aField.get_Value();
69 aField = aFields.GetItem(6);
70 aInfo.cchMaxLen = aField.get_Value().getUInt32();
72 aField = aFields.GetItem(4);
73 sal_uInt32 nId = aField.get_Value().getUInt32();
74 m_aLiteralInfo[nId] = aInfo;
76 aRecordset.MoveNext();
78 aRecordset.Close();
82 sal_Int32 ODatabaseMetaData::getMaxSize(sal_uInt32 _nId)
84 if(!m_aLiteralInfo.size())
85 fillLiterals();
87 sal_Int32 nSize = 0;
88 ::std::map<sal_uInt32,LiteralInfo>::const_iterator aIter = m_aLiteralInfo.find(_nId);
89 if(aIter != m_aLiteralInfo.end() && (*aIter).second.fSupported)
90 nSize = (static_cast<sal_Int32>((*aIter).second.cchMaxLen) == (-1)) ? 0 : (*aIter).second.cchMaxLen;
91 return nSize;
94 sal_Bool ODatabaseMetaData::isCapable(sal_uInt32 _nId)
96 if(!m_aLiteralInfo.size())
97 fillLiterals();
98 sal_Bool bSupported = sal_False;
99 ::std::map<sal_uInt32,LiteralInfo>::const_iterator aIter = m_aLiteralInfo.find(_nId);
100 if(aIter != m_aLiteralInfo.end())
101 bSupported = (*aIter).second.fSupported;
102 return bSupported;
106 OUString ODatabaseMetaData::getLiteral(sal_uInt32 _nId)
108 if(!m_aLiteralInfo.size())
109 fillLiterals();
110 OUString sStr;
111 ::std::map<sal_uInt32,LiteralInfo>::const_iterator aIter = m_aLiteralInfo.find(_nId);
112 if(aIter != m_aLiteralInfo.end() && (*aIter).second.fSupported)
113 sStr = (*aIter).second.pwszLiteralValue;
114 return sStr;
118 void ODatabaseMetaDataResultSetMetaData::setColumnPrivilegesMap()
120 m_mColumns[8] = OColumn(OUString(),OUString("IS_GRANTABLE"),
121 ColumnValue::NULLABLE,
122 3,3,0,
123 DataType::VARCHAR);
126 void ODatabaseMetaDataResultSetMetaData::setColumnsMap()
128 m_mColumns[6] = OColumn(OUString(),OUString("TYPE_NAME"),
129 ColumnValue::NO_NULLS,
130 0,0,0,
131 DataType::VARCHAR);
132 m_mColumns[11] = OColumn(OUString(),OUString("NULLABLE"),
133 ColumnValue::NO_NULLS,
134 1,1,0,
135 DataType::INTEGER);
136 m_mColumns[12] = OColumn(OUString(),OUString("REMARKS"),
137 ColumnValue::NULLABLE,
138 0,0,0,
139 DataType::VARCHAR);
140 m_mColumns[13] = OColumn(OUString(),OUString("COLUMN_DEF"),
141 ColumnValue::NULLABLE,
142 0,0,0,
143 DataType::VARCHAR);
144 m_mColumns[14] = OColumn(OUString(),OUString("SQL_DATA_TYPE"),
145 ColumnValue::NO_NULLS,
146 1,1,0,
147 DataType::INTEGER);
148 m_mColumns[15] = OColumn(OUString(),OUString("SQL_DATETIME_SUB"),
149 ColumnValue::NO_NULLS,
150 1,1,0,
151 DataType::INTEGER);
152 m_mColumns[16] = OColumn(OUString(),OUString("CHAR_OCTET_LENGTH"),
153 ColumnValue::NO_NULLS,
154 1,1,0,
155 DataType::INTEGER);
158 void ODatabaseMetaDataResultSetMetaData::setTablesMap()
160 m_mColumns[5] = OColumn(OUString(),OUString("REMARKS"),
161 ColumnValue::NULLABLE,
162 0,0,0,
163 DataType::VARCHAR);
166 void ODatabaseMetaDataResultSetMetaData::setProcedureColumnsMap()
168 m_mColumns[12] = OColumn(OUString(),OUString("NULLABLE"),
169 ColumnValue::NO_NULLS,
170 1,1,0,
171 DataType::INTEGER);
174 void ODatabaseMetaDataResultSetMetaData::setPrimaryKeysMap()
176 m_mColumns[5] = OColumn(OUString(),OUString("KEY_SEQ"),
177 ColumnValue::NO_NULLS,
178 1,1,0,
179 DataType::INTEGER);
180 m_mColumns[6] = OColumn(OUString(),OUString("PK_NAME"),
181 ColumnValue::NULLABLE,
182 0,0,0,
183 DataType::VARCHAR);
186 void ODatabaseMetaDataResultSetMetaData::setIndexInfoMap()
188 m_mColumns[4] = OColumn(OUString(),OUString("NON_UNIQUE"),
189 ColumnValue::NO_NULLS,
190 1,1,0,
191 DataType::BIT);
192 m_mColumns[5] = OColumn(OUString(),OUString("INDEX_QUALIFIER"),
193 ColumnValue::NULLABLE,
194 0,0,0,
195 DataType::VARCHAR);
196 m_mColumns[10] = OColumn(OUString(),OUString("ASC_OR_DESC"),
197 ColumnValue::NULLABLE,
198 0,0,0,
199 DataType::VARCHAR);
202 void ODatabaseMetaDataResultSetMetaData::setTablePrivilegesMap()
204 m_mColumns[6] = OColumn(OUString(),OUString("PRIVILEGE"),
205 ColumnValue::NULLABLE,
206 0,0,0,
207 DataType::VARCHAR);
208 m_mColumns[7] = OColumn(OUString(),OUString("IS_GRANTABLE"),
209 ColumnValue::NULLABLE,
210 0,0,0,
211 DataType::VARCHAR);
214 void ODatabaseMetaDataResultSetMetaData::setCrossReferenceMap()
216 m_mColumns[9] = OColumn(OUString(),OUString("KEY_SEQ"),
217 ColumnValue::NO_NULLS,
218 1,1,0,
219 DataType::INTEGER);
222 void ODatabaseMetaDataResultSetMetaData::setTypeInfoMap()
224 m_mColumns[3] = OColumn(OUString(),OUString("PRECISION"),
225 ColumnValue::NO_NULLS,
226 1,1,0,
227 DataType::INTEGER);
228 m_mColumns[7] = OColumn(OUString(),OUString("NULLABLE"),
229 ColumnValue::NO_NULLS,
230 1,1,0,
231 DataType::INTEGER);
232 m_mColumns[12] = OColumn(OUString(),OUString("AUTO_INCREMENT"),
233 ColumnValue::NO_NULLS,
234 1,1,0,
235 DataType::BIT);
236 m_mColumns[16] = OColumn(OUString(),OUString("SQL_DATA_TYPE"),
237 ColumnValue::NO_NULLS,
238 1,1,0,
239 DataType::INTEGER);
240 m_mColumns[17] = OColumn(OUString(),OUString("SQL_DATETIME_SUB"),
241 ColumnValue::NO_NULLS,
242 1,1,0,
243 DataType::INTEGER);
244 m_mColumns[18] = OColumn(OUString(),OUString("NUM_PREC_RADIX"),
245 ColumnValue::NO_NULLS,
246 1,1,0,
247 DataType::INTEGER);
250 void ODatabaseMetaDataResultSetMetaData::setProceduresMap()
252 m_mColumns[7] = OColumn(OUString(),OUString("REMARKS"),
253 ColumnValue::NULLABLE,
254 0,0,0,
255 DataType::VARCHAR);
258 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isSearchable( sal_Int32 column ) throw(SQLException, RuntimeException)
260 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
261 return (*m_mColumnsIter).second.isSearchable();
262 return sal_True;
265 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isAutoIncrement( sal_Int32 column ) throw(SQLException, RuntimeException)
267 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
268 return (*m_mColumnsIter).second.isAutoIncrement();
269 return sal_False;
272 OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnServiceName( sal_Int32 column ) throw(SQLException, RuntimeException)
274 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
275 return (*m_mColumnsIter).second.getColumnServiceName();
276 return OUString();
279 OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getTableName( sal_Int32 column ) throw(SQLException, RuntimeException)
281 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
282 return (*m_mColumnsIter).second.getTableName();
283 return OUString();
286 OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getCatalogName( sal_Int32 column ) throw(SQLException, RuntimeException)
288 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
289 return (*m_mColumnsIter).second.getCatalogName();
290 return OUString();
293 OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnTypeName( sal_Int32 column ) throw(SQLException, RuntimeException)
295 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
296 return (*m_mColumnsIter).second.getColumnTypeName();
297 return OUString();
301 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isCaseSensitive( sal_Int32 column ) throw(SQLException, RuntimeException)
303 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
304 return (*m_mColumnsIter).second.isCaseSensitive();
305 return sal_True;
309 OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getSchemaName( sal_Int32 column ) throw(SQLException, RuntimeException)
311 if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
312 return (*m_mColumnsIter).second.getSchemaName();
313 return OUString();
317 ObjectTypeEnum OAdoGroup::MapObjectType(sal_Int32 _ObjType)
319 ObjectTypeEnum eNumType= adPermObjTable;
320 switch(_ObjType)
322 case PrivilegeObject::TABLE:
323 break;
324 case PrivilegeObject::VIEW:
325 eNumType = adPermObjView;
326 break;
327 case PrivilegeObject::COLUMN:
328 eNumType = adPermObjColumn;
329 break;
331 return eNumType;
334 sal_Int32 OAdoGroup::MapRight(RightsEnum _eNum)
336 sal_Int32 nRight = 0;
337 if(_eNum & adRightRead)
338 nRight |= Privilege::SELECT;
339 if(_eNum & adRightInsert)
340 nRight |= Privilege::INSERT;
341 if(_eNum & adRightUpdate)
342 nRight |= Privilege::UPDATE;
343 if(_eNum & adRightDelete)
344 nRight |= Privilege::DELETE;
345 if(_eNum & adRightReadDesign)
346 nRight |= Privilege::READ;
347 if(_eNum & adRightCreate)
348 nRight |= Privilege::CREATE;
349 if(_eNum & adRightWriteDesign)
350 nRight |= Privilege::ALTER;
351 if(_eNum & adRightReference)
352 nRight |= Privilege::REFERENCE;
353 if(_eNum & adRightDrop)
354 nRight |= Privilege::DROP;
356 return nRight;
359 RightsEnum OAdoGroup::Map2Right(sal_Int32 _eNum)
361 sal_Int32 nRight = adRightNone;
362 if(_eNum & Privilege::SELECT)
363 nRight |= adRightRead;
365 if(_eNum & Privilege::INSERT)
366 nRight |= adRightInsert;
368 if(_eNum & Privilege::UPDATE)
369 nRight |= adRightUpdate;
371 if(_eNum & Privilege::DELETE)
372 nRight |= adRightDelete;
374 if(_eNum & Privilege::READ)
375 nRight |= adRightReadDesign;
377 if(_eNum & Privilege::CREATE)
378 nRight |= adRightCreate;
380 if(_eNum & Privilege::ALTER)
381 nRight |= adRightWriteDesign;
383 if(_eNum & Privilege::REFERENCE)
384 nRight |= adRightReference;
386 if(_eNum & Privilege::DROP)
387 nRight |= adRightDrop;
389 return (RightsEnum)nRight;
392 void WpADOIndex::Create()
394 _ADOIndex* pIndex = NULL;
395 HRESULT hr = CoCreateInstance(ADOS::CLSID_ADOINDEX_25,
396 NULL,
397 CLSCTX_INPROC_SERVER,
398 ADOS::IID_ADOINDEX_25,
399 (void**)&pIndex );
402 if( !FAILED( hr ) )
404 operator=( pIndex );
405 pIndex->Release();
409 void OAdoIndex::fillPropertyValues()
411 if(m_aIndex.IsValid())
413 m_Name = m_aIndex.get_Name();
414 m_IsUnique = m_aIndex.get_Unique();
415 m_IsPrimaryKeyIndex = m_aIndex.get_PrimaryKey();
416 m_IsClustered = m_aIndex.get_Clustered();
420 void WpADOKey::Create()
422 _ADOKey* pKey = NULL;
423 HRESULT hr = CoCreateInstance(ADOS::CLSID_ADOKEY_25,
424 NULL,
425 CLSCTX_INPROC_SERVER,
426 ADOS::IID_ADOKEY_25,
427 (void**)&pKey );
430 if( !FAILED( hr ) )
432 operator=( pKey );
433 pKey->Release();
437 void OAdoKey::fillPropertyValues()
439 if(m_aKey.IsValid())
441 m_aProps->m_Type = MapKeyRule(m_aKey.get_Type());
442 m_Name = m_aKey.get_Name();
443 m_aProps->m_ReferencedTable = m_aKey.get_RelatedTable();
444 m_aProps->m_UpdateRule = MapRule(m_aKey.get_UpdateRule());
445 m_aProps->m_DeleteRule = MapRule(m_aKey.get_DeleteRule());
449 sal_Int32 OAdoKey::MapRule(const RuleEnum& _eNum)
451 sal_Int32 eNum = KeyRule::NO_ACTION;
452 switch(_eNum)
454 case adRICascade:
455 eNum = KeyRule::CASCADE;
456 break;
457 case adRISetNull:
458 eNum = KeyRule::SET_NULL;
459 break;
460 case adRINone:
461 eNum = KeyRule::NO_ACTION;
462 break;
463 case adRISetDefault:
464 eNum = KeyRule::SET_DEFAULT;
465 break;
467 return eNum;
470 RuleEnum OAdoKey::Map2Rule(const sal_Int32& _eNum)
472 RuleEnum eNum = adRINone;
473 switch(_eNum)
475 case KeyRule::CASCADE:
476 eNum = adRICascade;
477 break;
478 case KeyRule::SET_NULL:
479 eNum = adRISetNull;
480 break;
481 case KeyRule::NO_ACTION:
482 eNum = adRINone;
483 break;
484 case KeyRule::SET_DEFAULT:
485 eNum = adRISetDefault;
486 break;
488 return eNum;
491 sal_Int32 OAdoKey::MapKeyRule(const KeyTypeEnum& _eNum)
493 sal_Int32 nKeyType = KeyType::PRIMARY;
494 switch(_eNum)
496 case adKeyPrimary:
497 nKeyType = KeyType::PRIMARY;
498 break;
499 case adKeyForeign:
500 nKeyType = KeyType::FOREIGN;
501 break;
502 case adKeyUnique:
503 nKeyType = KeyType::UNIQUE;
504 break;
506 return nKeyType;
509 KeyTypeEnum OAdoKey::Map2KeyRule(const sal_Int32& _eNum)
511 KeyTypeEnum eNum( adKeyPrimary );
512 switch(_eNum)
514 case KeyType::PRIMARY:
515 eNum = adKeyPrimary;
516 break;
517 case KeyType::FOREIGN:
518 eNum = adKeyForeign;
519 break;
520 case KeyType::UNIQUE:
521 eNum = adKeyUnique;
522 break;
523 default:
524 OSL_FAIL( "OAdoKey::Map2KeyRule: invalid key type!" );
526 return eNum;
529 void WpADOTable::Create()
531 _ADOTable* pTable = NULL;
532 HRESULT hr = CoCreateInstance(ADOS::CLSID_ADOTABLE_25,
533 NULL,
534 CLSCTX_INPROC_SERVER,
535 ADOS::IID_ADOTABLE_25,
536 (void**)&pTable );
539 if( !FAILED( hr ) )
541 operator=( pTable );
542 pTable->Release();
546 OUString WpADOCatalog::GetObjectOwner(const OUString& _rName, ObjectTypeEnum _eNum)
548 OLEVariant _rVar;
549 _rVar.setNoArg();
550 OLEString aBSTR;
551 OLEString sStr1(_rName);
552 pInterface->GetObjectOwner(sStr1,_eNum,_rVar,&aBSTR);
553 return aBSTR;
556 void OAdoTable::fillPropertyValues()
558 if(m_aTable.IsValid())
560 m_Name = m_aTable.get_Name();
561 m_Type = m_aTable.get_Type();
563 WpADOCatalog aCat(m_aTable.get_ParentCatalog());
564 if(aCat.IsValid())
565 m_CatalogName = aCat.GetObjectOwner(m_aTable.get_Name(),adPermObjTable);
568 WpADOProperties aProps = m_aTable.get_Properties();
569 if(aProps.IsValid())
570 m_Description = OTools::getValue(aProps,OUString("Description"));
575 void WpADOUser::Create()
577 _ADOUser* pUser = NULL;
578 HRESULT hr = CoCreateInstance(ADOS::CLSID_ADOUSER_25,
579 NULL,
580 CLSCTX_INPROC_SERVER,
581 ADOS::IID_ADOUSER_25,
582 (void**)&pUser );
585 if( !FAILED( hr ) )
587 operator=( pUser );
588 pUser->Release();
594 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */