1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ADatabaseMetaDataImpl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "ado/ADatabaseMetaData.hxx"
34 #include "ado/ADatabaseMetaDataResultSetMetaData.hxx"
35 #include "ado/Awrapado.hxx"
36 #include "ado/AGroup.hxx"
37 #include "ado/adoimp.hxx"
38 #include "ado/AIndex.hxx"
39 #include "ado/AKey.hxx"
40 #include "ado/ATable.hxx"
41 #include <com/sun/star/sdbc/DataType.hpp>
42 #include <com/sun/star/sdbc/ProcedureResult.hpp>
43 #include <com/sun/star/sdbc/ColumnValue.hpp>
47 #include <com/sun/star/sdbcx/Privilege.hpp>
48 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
49 #include <com/sun/star/sdbc/KeyRule.hpp>
50 #include <com/sun/star/sdbcx/KeyType.hpp>
52 using namespace connectivity::ado
;
53 using namespace ::com::sun::star::sdbc
;
54 using namespace ::com::sun::star::sdbcx
;
55 using namespace ::com::sun::star::uno
;
57 // -------------------------------------------------------------------------
58 void ODatabaseMetaData::fillLiterals()
60 ADORecordset
*pRecordset
= NULL
;
63 m_pADOConnection
->OpenSchema(adSchemaDBInfoLiterals
,vtEmpty
,vtEmpty
,&pRecordset
);
65 ADOS::ThrowException(*m_pADOConnection
,*this);
67 OSL_ENSURE(pRecordset
,"fillLiterals: no resultset!");
70 WpADORecordset
aRecordset(pRecordset
);
72 aRecordset
.MoveFirst();
75 while(!aRecordset
.IsAtEOF())
77 WpOLEAppendCollection
<ADOFields
, ADOField
, WpADOField
> aFields(aRecordset
.GetFields());
78 WpADOField
aField(aFields
.GetItem(1));
79 aInfo
.pwszLiteralValue
= aField
.get_Value();
80 aField
= aFields
.GetItem(5);
81 aInfo
.fSupported
= aField
.get_Value();
82 aField
= aFields
.GetItem(6);
83 aInfo
.cchMaxLen
= aField
.get_Value().getUInt32();
85 aField
= aFields
.GetItem(4);
86 sal_uInt32 nId
= aField
.get_Value().getUInt32();
87 m_aLiteralInfo
[nId
] = aInfo
;
89 aRecordset
.MoveNext();
94 // -------------------------------------------------------------------------
95 sal_Int32
ODatabaseMetaData::getMaxSize(sal_uInt32 _nId
)
97 if(!m_aLiteralInfo
.size())
101 ::std::map
<sal_uInt32
,LiteralInfo
>::const_iterator aIter
= m_aLiteralInfo
.find(_nId
);
102 if(aIter
!= m_aLiteralInfo
.end() && (*aIter
).second
.fSupported
)
103 nSize
= ((*aIter
).second
.cchMaxLen
== (-1)) ? 0 : (*aIter
).second
.cchMaxLen
;
106 // -------------------------------------------------------------------------
107 sal_Bool
ODatabaseMetaData::isCapable(sal_uInt32 _nId
)
109 if(!m_aLiteralInfo
.size())
111 sal_Bool bSupported
= sal_False
;
112 ::std::map
<sal_uInt32
,LiteralInfo
>::const_iterator aIter
= m_aLiteralInfo
.find(_nId
);
113 if(aIter
!= m_aLiteralInfo
.end())
114 bSupported
= (*aIter
).second
.fSupported
;
118 // -------------------------------------------------------------------------
119 ::rtl::OUString
ODatabaseMetaData::getLiteral(sal_uInt32 _nId
)
121 if(!m_aLiteralInfo
.size())
123 ::rtl::OUString sStr
;
124 ::std::map
<sal_uInt32
,LiteralInfo
>::const_iterator aIter
= m_aLiteralInfo
.find(_nId
);
125 if(aIter
!= m_aLiteralInfo
.end() && (*aIter
).second
.fSupported
)
126 sStr
= (*aIter
).second
.pwszLiteralValue
;
129 // -----------------------------------------------------------------------------
130 // -------------------------------------------------------------------------
131 void ODatabaseMetaDataResultSetMetaData::setColumnPrivilegesMap()
133 m_mColumns
[8] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("IS_GRANTABLE"),
134 ColumnValue::NULLABLE
,
138 // -------------------------------------------------------------------------
139 void ODatabaseMetaDataResultSetMetaData::setColumnsMap()
141 m_mColumns
[6] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("TYPE_NAME"),
142 ColumnValue::NO_NULLS
,
145 m_mColumns
[11] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("NULLABLE"),
146 ColumnValue::NO_NULLS
,
149 m_mColumns
[12] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("REMARKS"),
150 ColumnValue::NULLABLE
,
153 m_mColumns
[13] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("COLUMN_DEF"),
154 ColumnValue::NULLABLE
,
157 m_mColumns
[14] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("SQL_DATA_TYPE"),
158 ColumnValue::NO_NULLS
,
161 m_mColumns
[15] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("SQL_DATETIME_SUB"),
162 ColumnValue::NO_NULLS
,
165 m_mColumns
[16] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("CHAR_OCTET_LENGTH"),
166 ColumnValue::NO_NULLS
,
170 // -------------------------------------------------------------------------
171 void ODatabaseMetaDataResultSetMetaData::setTablesMap()
173 m_mColumns
[5] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("REMARKS"),
174 ColumnValue::NULLABLE
,
178 // -------------------------------------------------------------------------
179 void ODatabaseMetaDataResultSetMetaData::setProcedureColumnsMap()
181 m_mColumns
[12] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("NULLABLE"),
182 ColumnValue::NO_NULLS
,
186 // -------------------------------------------------------------------------
187 void ODatabaseMetaDataResultSetMetaData::setPrimaryKeysMap()
189 m_mColumns
[5] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("KEY_SEQ"),
190 ColumnValue::NO_NULLS
,
193 m_mColumns
[6] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("PK_NAME"),
194 ColumnValue::NULLABLE
,
198 // -------------------------------------------------------------------------
199 void ODatabaseMetaDataResultSetMetaData::setIndexInfoMap()
201 m_mColumns
[4] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("NON_UNIQUE"),
202 ColumnValue::NO_NULLS
,
205 m_mColumns
[5] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("INDEX_QUALIFIER"),
206 ColumnValue::NULLABLE
,
209 m_mColumns
[10] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("ASC_OR_DESC"),
210 ColumnValue::NULLABLE
,
214 // -------------------------------------------------------------------------
215 void ODatabaseMetaDataResultSetMetaData::setTablePrivilegesMap()
217 m_mColumns
[6] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("PRIVILEGE"),
218 ColumnValue::NULLABLE
,
221 m_mColumns
[7] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("IS_GRANTABLE"),
222 ColumnValue::NULLABLE
,
226 // -------------------------------------------------------------------------
227 void ODatabaseMetaDataResultSetMetaData::setCrossReferenceMap()
229 m_mColumns
[9] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("KEY_SEQ"),
230 ColumnValue::NO_NULLS
,
234 // -------------------------------------------------------------------------
235 void ODatabaseMetaDataResultSetMetaData::setTypeInfoMap()
237 m_mColumns
[3] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("PRECISION"),
238 ColumnValue::NO_NULLS
,
241 m_mColumns
[7] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("NULLABLE"),
242 ColumnValue::NO_NULLS
,
245 m_mColumns
[12] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("AUTO_INCREMENT"),
246 ColumnValue::NO_NULLS
,
249 m_mColumns
[16] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("SQL_DATA_TYPE"),
250 ColumnValue::NO_NULLS
,
253 m_mColumns
[17] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("SQL_DATETIME_SUB"),
254 ColumnValue::NO_NULLS
,
257 m_mColumns
[18] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("NUM_PREC_RADIX"),
258 ColumnValue::NO_NULLS
,
262 // -------------------------------------------------------------------------
263 void ODatabaseMetaDataResultSetMetaData::setProceduresMap()
265 m_mColumns
[7] = OColumn(::rtl::OUString(),::rtl::OUString::createFromAscii("REMARKS"),
266 ColumnValue::NULLABLE
,
270 // -------------------------------------------------------------------------
271 sal_Bool SAL_CALL
ODatabaseMetaDataResultSetMetaData::isSearchable( sal_Int32 column
) throw(SQLException
, RuntimeException
)
273 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
274 return (*m_mColumnsIter
).second
.isSearchable();
277 // -------------------------------------------------------------------------
278 sal_Bool SAL_CALL
ODatabaseMetaDataResultSetMetaData::isAutoIncrement( sal_Int32 column
) throw(SQLException
, RuntimeException
)
280 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
281 return (*m_mColumnsIter
).second
.isAutoIncrement();
284 // -------------------------------------------------------------------------
285 ::rtl::OUString SAL_CALL
ODatabaseMetaDataResultSetMetaData::getColumnServiceName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
287 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
288 return (*m_mColumnsIter
).second
.getColumnServiceName();
289 return ::rtl::OUString();
291 // -------------------------------------------------------------------------
292 ::rtl::OUString SAL_CALL
ODatabaseMetaDataResultSetMetaData::getTableName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
294 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
295 return (*m_mColumnsIter
).second
.getTableName();
296 return ::rtl::OUString();
298 // -------------------------------------------------------------------------
299 ::rtl::OUString SAL_CALL
ODatabaseMetaDataResultSetMetaData::getCatalogName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
301 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
302 return (*m_mColumnsIter
).second
.getCatalogName();
303 return ::rtl::OUString();
305 // -------------------------------------------------------------------------
306 ::rtl::OUString SAL_CALL
ODatabaseMetaDataResultSetMetaData::getColumnTypeName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
308 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
309 return (*m_mColumnsIter
).second
.getColumnTypeName();
310 return ::rtl::OUString();
312 // -------------------------------------------------------------------------
314 sal_Bool SAL_CALL
ODatabaseMetaDataResultSetMetaData::isCaseSensitive( sal_Int32 column
) throw(SQLException
, RuntimeException
)
316 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
317 return (*m_mColumnsIter
).second
.isCaseSensitive();
320 // -------------------------------------------------------------------------
322 ::rtl::OUString SAL_CALL
ODatabaseMetaDataResultSetMetaData::getSchemaName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
324 if(m_mColumns
.size() && (m_mColumnsIter
= m_mColumns
.find(column
)) != m_mColumns
.end())
325 return (*m_mColumnsIter
).second
.getSchemaName();
326 return ::rtl::OUString();
328 // -----------------------------------------------------------------------------
329 // -------------------------------------------------------------------------
330 ObjectTypeEnum
OAdoGroup::MapObjectType(sal_Int32 _ObjType
)
332 ObjectTypeEnum eNumType
= adPermObjTable
;
335 case PrivilegeObject::TABLE
:
337 case PrivilegeObject::VIEW
:
338 eNumType
= adPermObjView
;
340 case PrivilegeObject::COLUMN
:
341 eNumType
= adPermObjColumn
;
346 // -------------------------------------------------------------------------
347 sal_Int32
OAdoGroup::MapRight(RightsEnum _eNum
)
349 sal_Int32 nRight
= 0;
350 if(_eNum
& adRightRead
)
351 nRight
|= Privilege::SELECT
;
352 if(_eNum
& adRightInsert
)
353 nRight
|= Privilege::INSERT
;
354 if(_eNum
& adRightUpdate
)
355 nRight
|= Privilege::UPDATE
;
356 if(_eNum
& adRightDelete
)
357 nRight
|= Privilege::DELETE
;
358 if(_eNum
& adRightReadDesign
)
359 nRight
|= Privilege::READ
;
360 if(_eNum
& adRightCreate
)
361 nRight
|= Privilege::CREATE
;
362 if(_eNum
& adRightWriteDesign
)
363 nRight
|= Privilege::ALTER
;
364 if(_eNum
& adRightReference
)
365 nRight
|= Privilege::REFERENCE
;
366 if(_eNum
& adRightDrop
)
367 nRight
|= Privilege::DROP
;
371 // -------------------------------------------------------------------------
372 RightsEnum
OAdoGroup::Map2Right(sal_Int32 _eNum
)
374 sal_Int32 nRight
= adRightNone
;
375 if(_eNum
& Privilege::SELECT
)
376 nRight
|= adRightRead
;
378 if(_eNum
& Privilege::INSERT
)
379 nRight
|= adRightInsert
;
381 if(_eNum
& Privilege::UPDATE
)
382 nRight
|= adRightUpdate
;
384 if(_eNum
& Privilege::DELETE
)
385 nRight
|= adRightDelete
;
387 if(_eNum
& Privilege::READ
)
388 nRight
|= adRightReadDesign
;
390 if(_eNum
& Privilege::CREATE
)
391 nRight
|= adRightCreate
;
393 if(_eNum
& Privilege::ALTER
)
394 nRight
|= adRightWriteDesign
;
396 if(_eNum
& Privilege::REFERENCE
)
397 nRight
|= adRightReference
;
399 if(_eNum
& Privilege::DROP
)
400 nRight
|= adRightDrop
;
402 return (RightsEnum
)nRight
;
404 // -------------------------------------------------------------------------
405 void WpADOIndex::Create()
409 _ADOIndex
* pIndex
= NULL
;
410 hr
= CoCreateInstance(ADOS::CLSID_ADOINDEX_25
,
412 CLSCTX_INPROC_SERVER
,
413 ADOS::IID_ADOINDEX_25
,
423 // -------------------------------------------------------------------------
424 void OAdoIndex::fillPropertyValues()
426 if(m_aIndex
.IsValid())
428 m_Name
= m_aIndex
.get_Name();
429 m_IsUnique
= m_aIndex
.get_Unique();
430 m_IsPrimaryKeyIndex
= m_aIndex
.get_PrimaryKey();
431 m_IsClustered
= m_aIndex
.get_Clustered();
434 // -----------------------------------------------------------------------------
435 void WpADOKey::Create()
438 _ADOKey
* pKey
= NULL
;
439 hr
= CoCreateInstance(ADOS::CLSID_ADOKEY_25
,
441 CLSCTX_INPROC_SERVER
,
452 // -------------------------------------------------------------------------
453 void OAdoKey::fillPropertyValues()
457 m_aProps
->m_Type
= MapKeyRule(m_aKey
.get_Type());
458 m_Name
= m_aKey
.get_Name();
459 m_aProps
->m_ReferencedTable
= m_aKey
.get_RelatedTable();
460 m_aProps
->m_UpdateRule
= MapRule(m_aKey
.get_UpdateRule());
461 m_aProps
->m_DeleteRule
= MapRule(m_aKey
.get_DeleteRule());
464 // -------------------------------------------------------------------------
465 sal_Int32
OAdoKey::MapRule(const RuleEnum
& _eNum
)
467 sal_Int32 eNum
= KeyRule::NO_ACTION
;
471 eNum
= KeyRule::CASCADE
;
474 eNum
= KeyRule::SET_NULL
;
477 eNum
= KeyRule::NO_ACTION
;
480 eNum
= KeyRule::SET_DEFAULT
;
485 // -------------------------------------------------------------------------
486 RuleEnum
OAdoKey::Map2Rule(const sal_Int32
& _eNum
)
488 RuleEnum eNum
= adRINone
;
491 case KeyRule::CASCADE
:
494 case KeyRule::SET_NULL
:
497 case KeyRule::NO_ACTION
:
500 case KeyRule::SET_DEFAULT
:
501 eNum
= adRISetDefault
;
506 // -------------------------------------------------------------------------
507 sal_Int32
OAdoKey::MapKeyRule(const KeyTypeEnum
& _eNum
)
509 sal_Int32 nKeyType
= KeyType::PRIMARY
;
513 nKeyType
= KeyType::PRIMARY
;
516 nKeyType
= KeyType::FOREIGN
;
519 nKeyType
= KeyType::UNIQUE
;
524 // -------------------------------------------------------------------------
525 KeyTypeEnum
OAdoKey::Map2KeyRule(const sal_Int32
& _eNum
)
527 KeyTypeEnum
eNum( adKeyPrimary
);
530 case KeyType::PRIMARY
:
533 case KeyType::FOREIGN
:
536 case KeyType::UNIQUE
:
540 OSL_ENSURE( false, "OAdoKey::Map2KeyRule: invalid key type!" );
544 // -----------------------------------------------------------------------------
545 void WpADOTable::Create()
548 _ADOTable
* pTable
= NULL
;
549 hr
= CoCreateInstance(ADOS::CLSID_ADOTABLE_25
,
551 CLSCTX_INPROC_SERVER
,
552 ADOS::IID_ADOTABLE_25
,
562 // -------------------------------------------------------------------------
563 ::rtl::OUString
WpADOCatalog::GetObjectOwner(const ::rtl::OUString
& _rName
, ObjectTypeEnum _eNum
)
568 OLEString
sStr1(_rName
);
569 pInterface
->GetObjectOwner(sStr1
,_eNum
,_rVar
,&aBSTR
);
572 // -----------------------------------------------------------------------------
573 void OAdoTable::fillPropertyValues()
575 if(m_aTable
.IsValid())
577 m_Name
= m_aTable
.get_Name();
578 m_Type
= m_aTable
.get_Type();
580 WpADOCatalog
aCat(m_aTable
.get_ParentCatalog());
582 m_CatalogName
= aCat
.GetObjectOwner(m_aTable
.get_Name(),adPermObjTable
);
585 WpADOProperties aProps
= m_aTable
.get_Properties();
587 m_Description
= OTools::getValue(aProps
,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Description")));
591 // -----------------------------------------------------------------------------
592 void WpADOUser::Create()
595 _ADOUser
* pUser
= NULL
;
596 hr
= CoCreateInstance(ADOS::CLSID_ADOUSER_25
,
598 CLSCTX_INPROC_SERVER
,
599 ADOS::IID_ADOUSER_25
,
609 // -------------------------------------------------------------------------