1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
50 m_pADOConnection
->OpenSchema(adSchemaDBInfoLiterals
,vtEmpty
,vtEmpty
,&pRecordset
);
52 ADOS::ThrowException(*m_pADOConnection
,*this);
54 OSL_ENSURE(pRecordset
,"fillLiterals: no resultset!");
57 WpADORecordset
aRecordset(pRecordset
);
59 aRecordset
.MoveFirst();
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();
82 sal_Int32
ODatabaseMetaData::getMaxSize(sal_uInt32 _nId
)
84 if(!m_aLiteralInfo
.size())
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
;
94 sal_Bool
ODatabaseMetaData::isCapable(sal_uInt32 _nId
)
96 if(!m_aLiteralInfo
.size())
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
;
106 OUString
ODatabaseMetaData::getLiteral(sal_uInt32 _nId
)
108 if(!m_aLiteralInfo
.size())
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
;
118 void ODatabaseMetaDataResultSetMetaData::setColumnPrivilegesMap()
120 m_mColumns
[8] = OColumn(OUString(),OUString("IS_GRANTABLE"),
121 ColumnValue::NULLABLE
,
126 void ODatabaseMetaDataResultSetMetaData::setColumnsMap()
128 m_mColumns
[6] = OColumn(OUString(),OUString("TYPE_NAME"),
129 ColumnValue::NO_NULLS
,
132 m_mColumns
[11] = OColumn(OUString(),OUString("NULLABLE"),
133 ColumnValue::NO_NULLS
,
136 m_mColumns
[12] = OColumn(OUString(),OUString("REMARKS"),
137 ColumnValue::NULLABLE
,
140 m_mColumns
[13] = OColumn(OUString(),OUString("COLUMN_DEF"),
141 ColumnValue::NULLABLE
,
144 m_mColumns
[14] = OColumn(OUString(),OUString("SQL_DATA_TYPE"),
145 ColumnValue::NO_NULLS
,
148 m_mColumns
[15] = OColumn(OUString(),OUString("SQL_DATETIME_SUB"),
149 ColumnValue::NO_NULLS
,
152 m_mColumns
[16] = OColumn(OUString(),OUString("CHAR_OCTET_LENGTH"),
153 ColumnValue::NO_NULLS
,
158 void ODatabaseMetaDataResultSetMetaData::setTablesMap()
160 m_mColumns
[5] = OColumn(OUString(),OUString("REMARKS"),
161 ColumnValue::NULLABLE
,
166 void ODatabaseMetaDataResultSetMetaData::setProcedureColumnsMap()
168 m_mColumns
[12] = OColumn(OUString(),OUString("NULLABLE"),
169 ColumnValue::NO_NULLS
,
174 void ODatabaseMetaDataResultSetMetaData::setPrimaryKeysMap()
176 m_mColumns
[5] = OColumn(OUString(),OUString("KEY_SEQ"),
177 ColumnValue::NO_NULLS
,
180 m_mColumns
[6] = OColumn(OUString(),OUString("PK_NAME"),
181 ColumnValue::NULLABLE
,
186 void ODatabaseMetaDataResultSetMetaData::setIndexInfoMap()
188 m_mColumns
[4] = OColumn(OUString(),OUString("NON_UNIQUE"),
189 ColumnValue::NO_NULLS
,
192 m_mColumns
[5] = OColumn(OUString(),OUString("INDEX_QUALIFIER"),
193 ColumnValue::NULLABLE
,
196 m_mColumns
[10] = OColumn(OUString(),OUString("ASC_OR_DESC"),
197 ColumnValue::NULLABLE
,
202 void ODatabaseMetaDataResultSetMetaData::setTablePrivilegesMap()
204 m_mColumns
[6] = OColumn(OUString(),OUString("PRIVILEGE"),
205 ColumnValue::NULLABLE
,
208 m_mColumns
[7] = OColumn(OUString(),OUString("IS_GRANTABLE"),
209 ColumnValue::NULLABLE
,
214 void ODatabaseMetaDataResultSetMetaData::setCrossReferenceMap()
216 m_mColumns
[9] = OColumn(OUString(),OUString("KEY_SEQ"),
217 ColumnValue::NO_NULLS
,
222 void ODatabaseMetaDataResultSetMetaData::setTypeInfoMap()
224 m_mColumns
[3] = OColumn(OUString(),OUString("PRECISION"),
225 ColumnValue::NO_NULLS
,
228 m_mColumns
[7] = OColumn(OUString(),OUString("NULLABLE"),
229 ColumnValue::NO_NULLS
,
232 m_mColumns
[12] = OColumn(OUString(),OUString("AUTO_INCREMENT"),
233 ColumnValue::NO_NULLS
,
236 m_mColumns
[16] = OColumn(OUString(),OUString("SQL_DATA_TYPE"),
237 ColumnValue::NO_NULLS
,
240 m_mColumns
[17] = OColumn(OUString(),OUString("SQL_DATETIME_SUB"),
241 ColumnValue::NO_NULLS
,
244 m_mColumns
[18] = OColumn(OUString(),OUString("NUM_PREC_RADIX"),
245 ColumnValue::NO_NULLS
,
250 void ODatabaseMetaDataResultSetMetaData::setProceduresMap()
252 m_mColumns
[7] = OColumn(OUString(),OUString("REMARKS"),
253 ColumnValue::NULLABLE
,
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();
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();
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();
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();
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();
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();
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();
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();
317 ObjectTypeEnum
OAdoGroup::MapObjectType(sal_Int32 _ObjType
)
319 ObjectTypeEnum eNumType
= adPermObjTable
;
322 case PrivilegeObject::TABLE
:
324 case PrivilegeObject::VIEW
:
325 eNumType
= adPermObjView
;
327 case PrivilegeObject::COLUMN
:
328 eNumType
= adPermObjColumn
;
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
;
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
,
397 CLSCTX_INPROC_SERVER
,
398 ADOS::IID_ADOINDEX_25
,
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
,
425 CLSCTX_INPROC_SERVER
,
437 void OAdoKey::fillPropertyValues()
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
;
455 eNum
= KeyRule::CASCADE
;
458 eNum
= KeyRule::SET_NULL
;
461 eNum
= KeyRule::NO_ACTION
;
464 eNum
= KeyRule::SET_DEFAULT
;
470 RuleEnum
OAdoKey::Map2Rule(const sal_Int32
& _eNum
)
472 RuleEnum eNum
= adRINone
;
475 case KeyRule::CASCADE
:
478 case KeyRule::SET_NULL
:
481 case KeyRule::NO_ACTION
:
484 case KeyRule::SET_DEFAULT
:
485 eNum
= adRISetDefault
;
491 sal_Int32
OAdoKey::MapKeyRule(const KeyTypeEnum
& _eNum
)
493 sal_Int32 nKeyType
= KeyType::PRIMARY
;
497 nKeyType
= KeyType::PRIMARY
;
500 nKeyType
= KeyType::FOREIGN
;
503 nKeyType
= KeyType::UNIQUE
;
509 KeyTypeEnum
OAdoKey::Map2KeyRule(const sal_Int32
& _eNum
)
511 KeyTypeEnum
eNum( adKeyPrimary
);
514 case KeyType::PRIMARY
:
517 case KeyType::FOREIGN
:
520 case KeyType::UNIQUE
:
524 OSL_FAIL( "OAdoKey::Map2KeyRule: invalid key type!" );
529 void WpADOTable::Create()
531 _ADOTable
* pTable
= NULL
;
532 HRESULT hr
= CoCreateInstance(ADOS::CLSID_ADOTABLE_25
,
534 CLSCTX_INPROC_SERVER
,
535 ADOS::IID_ADOTABLE_25
,
546 OUString
WpADOCatalog::GetObjectOwner(const OUString
& _rName
, ObjectTypeEnum _eNum
)
551 OLEString
sStr1(_rName
);
552 pInterface
->GetObjectOwner(sStr1
,_eNum
,_rVar
,&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());
565 m_CatalogName
= aCat
.GetObjectOwner(m_aTable
.get_Name(),adPermObjTable
);
568 WpADOProperties aProps
= m_aTable
.get_Properties();
570 m_Description
= OTools::getValue(aProps
,OUString("Description"));
575 void WpADOUser::Create()
577 _ADOUser
* pUser
= NULL
;
578 HRESULT hr
= CoCreateInstance(ADOS::CLSID_ADOUSER_25
,
580 CLSCTX_INPROC_SERVER
,
581 ADOS::IID_ADOUSER_25
,
594 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */