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 <connectivity/dbtools.hxx>
21 #include <connectivity/dbconversion.hxx>
22 #include <connectivity/dbcharset.hxx>
23 #include <SQLStatementHelper.hxx>
24 #include <unotools/confignode.hxx>
25 #include <resource/sharedresources.hxx>
26 #include <strings.hrc>
27 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
28 #include <com/sun/star/sdbc/SQLException.hpp>
29 #include <com/sun/star/sdbc/XConnection.hpp>
30 #include <com/sun/star/sdbc/XDataSource.hpp>
31 #include <com/sun/star/sdbc/ColumnValue.hpp>
32 #include <com/sun/star/sdbc/DataType.hpp>
33 #include <com/sun/star/sdbc/DriverManager.hpp>
34 #include <com/sun/star/sdbc/XRow.hpp>
35 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
36 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
37 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
38 #include <com/sun/star/sdbcx/Privilege.hpp>
39 #include <com/sun/star/container/XIndexAccess.hpp>
40 #include <com/sun/star/sdbc/KeyRule.hpp>
41 #include <com/sun/star/sdbcx/KeyType.hpp>
42 #include <TConnection.hxx>
43 #include <connectivity/sdbcx/VColumn.hxx>
44 #include <com/sun/star/frame/XModel.hpp>
45 #include <com/sun/star/container/XChild.hpp>
47 #include <comphelper/types.hxx>
48 #include <tools/diagnose_ex.h>
49 #include <unotools/sharedunocomponent.hxx>
51 #include <string_view>
56 using namespace ::com::sun::star::uno
;
57 using namespace ::com::sun::star::beans
;
58 using namespace ::com::sun::star::sdb
;
59 using namespace ::com::sun::star::sdbc
;
60 using namespace ::com::sun::star::sdbcx
;
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::container
;
63 using namespace ::com::sun::star::frame
;
64 using namespace connectivity
;
65 using namespace comphelper
;
67 OUString
createStandardTypePart(const Reference
< XPropertySet
>& xColProp
,const Reference
< XConnection
>& _xConnection
,std::u16string_view _sCreatePattern
)
70 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
72 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
75 sal_Int32 nDataType
= 0;
76 sal_Int32 nPrecision
= 0;
79 nDataType
= nPrecision
= nScale
= 0;
81 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_TYPENAME
)) >>= sTypeName
;
82 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_TYPE
)) >>= nDataType
;
83 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_PRECISION
)) >>= nPrecision
;
84 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCALE
)) >>= nScale
;
88 // check if the user enter a specific string to create autoincrement values
89 OUString sAutoIncrementValue
;
90 Reference
<XPropertySetInfo
> xPropInfo
= xColProp
->getPropertySetInfo();
91 if ( xPropInfo
.is() && xPropInfo
->hasPropertyByName(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) )
92 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) >>= sAutoIncrementValue
;
93 // look if we have to use precisions
94 bool bUseLiteral
= false;
95 OUString sPrefix
,sPostfix
,sCreateParams
;
97 Reference
<XResultSet
> xRes
= xMetaData
->getTypeInfo();
100 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
103 OUString sTypeName2Cmp
= xRow
->getString(1);
104 sal_Int32 nType
= xRow
->getShort(2);
105 sPrefix
= xRow
->getString (4);
106 sPostfix
= xRow
->getString (5);
107 sCreateParams
= xRow
->getString(6);
108 // first identical type will be used if typename is empty
109 if ( sTypeName
.isEmpty() && nType
== nDataType
)
110 sTypeName
= sTypeName2Cmp
;
112 if( sTypeName
.equalsIgnoreAsciiCase(sTypeName2Cmp
) && nType
== nDataType
&& !sCreateParams
.isEmpty() && !xRow
->wasNull())
121 if ( !sAutoIncrementValue
.isEmpty() )
123 sal_Int32 nIndex
= sTypeName
.indexOf(sAutoIncrementValue
);
125 sTypeName
= sTypeName
.replaceAt(nIndex
,sTypeName
.getLength() - nIndex
,OUString());
128 if ( (nPrecision
> 0 || nScale
> 0) && bUseLiteral
)
130 sal_Int32 nParenPos
= sTypeName
.indexOf('(');
131 if ( nParenPos
== -1 )
133 aSql
.append(sTypeName
);
138 aSql
.append(std::u16string_view(sTypeName
).substr(0, ++nParenPos
));
141 if ( nPrecision
> 0 && nDataType
!= DataType::TIMESTAMP
)
143 aSql
.append(nPrecision
);
144 if ( (nScale
> 0) || (!_sCreatePattern
.empty() && sCreateParams
.indexOf(_sCreatePattern
) != -1) )
147 if ( (nScale
> 0) || ( !_sCreatePattern
.empty() && sCreateParams
.indexOf(_sCreatePattern
) != -1 ) || nDataType
== DataType::TIMESTAMP
)
150 if ( nParenPos
== -1 )
154 nParenPos
= sTypeName
.indexOf(')',nParenPos
);
155 aSql
.append(std::u16string_view(sTypeName
).substr(nParenPos
));
159 aSql
.append(sTypeName
); // simply add the type name
161 OUString aDefault
= ::comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_DEFAULTVALUE
)));
162 if ( !aDefault
.isEmpty() )
164 aSql
.append(" DEFAULT ");
165 aSql
.append(sPrefix
);
166 aSql
.append(aDefault
);
167 aSql
.append(sPostfix
);
168 } // if ( aDefault.getLength() )
170 return aSql
.makeStringAndClear();
173 OUString
createStandardColumnPart(const Reference
< XPropertySet
>& xColProp
,const Reference
< XConnection
>& _xConnection
,ISQLStatementHelper
* _pHelper
,std::u16string_view _sCreatePattern
)
175 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
177 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
179 bool bIsAutoIncrement
= false;
180 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT
)) >>= bIsAutoIncrement
;
182 const OUString sQuoteString
= xMetaData
->getIdentifierQuoteString();
183 OUStringBuffer aSql
= ::dbtools::quoteName(sQuoteString
,::comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
))));
185 // check if the user enter a specific string to create autoincrement values
186 OUString sAutoIncrementValue
;
187 Reference
<XPropertySetInfo
> xPropInfo
= xColProp
->getPropertySetInfo();
188 if ( xPropInfo
.is() && xPropInfo
->hasPropertyByName(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) )
189 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) >>= sAutoIncrementValue
;
193 aSql
.append(createStandardTypePart(xColProp
, _xConnection
, _sCreatePattern
));
195 if(::comphelper::getINT32(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISNULLABLE
))) == ColumnValue::NO_NULLS
)
196 aSql
.append(" NOT NULL");
198 if ( bIsAutoIncrement
&& !sAutoIncrementValue
.isEmpty())
201 aSql
.append(sAutoIncrementValue
);
205 _pHelper
->addComment(xColProp
,aSql
);
207 return aSql
.makeStringAndClear();
211 OUString
createStandardCreateStatement(const Reference
< XPropertySet
>& descriptor
,const Reference
< XConnection
>& _xConnection
,ISQLStatementHelper
* _pHelper
,std::u16string_view _sCreatePattern
)
213 OUStringBuffer
aSql("CREATE TABLE ");
214 OUString sCatalog
,sSchema
,sTable
,sComposedName
;
216 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
217 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
219 descriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
)) >>= sCatalog
;
220 descriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= sSchema
;
221 descriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= sTable
;
223 sComposedName
= ::dbtools::composeTableName( xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
224 if ( sComposedName
.isEmpty() )
225 ::dbtools::throwFunctionSequenceException(_xConnection
);
227 aSql
.append(sComposedName
);
231 Reference
<XColumnsSupplier
> xColumnSup(descriptor
,UNO_QUERY
);
232 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
233 // check if there are columns
234 if(!xColumns
.is() || !xColumns
->getCount())
235 ::dbtools::throwFunctionSequenceException(_xConnection
);
237 Reference
< XPropertySet
> xColProp
;
239 sal_Int32 nCount
= xColumns
->getCount();
240 for(sal_Int32 i
=0;i
<nCount
;++i
)
242 if ( (xColumns
->getByIndex(i
) >>= xColProp
) && xColProp
.is() )
244 aSql
.append(createStandardColumnPart(xColProp
,_xConnection
,_pHelper
,_sCreatePattern
));
248 return aSql
.makeStringAndClear();
252 OUString
generateColumnNames(const Reference
<XIndexAccess
>& _xColumns
,const Reference
<XDatabaseMetaData
>& _xMetaData
)
254 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
256 const OUString
sQuote(_xMetaData
->getIdentifierQuoteString());
257 OUStringBuffer
sSql( " (" );
258 Reference
< XPropertySet
> xColProp
;
260 sal_Int32 nColCount
= _xColumns
->getCount();
261 for(sal_Int32 i
=0;i
<nColCount
;++i
)
263 if ( (_xColumns
->getByIndex(i
) >>= xColProp
) && xColProp
.is() )
264 sSql
.append( ::dbtools::quoteName(sQuote
,::comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)))) )
269 sSql
[sSql
.getLength()-1] = ')';
270 return sSql
.makeStringAndClear();
274 OUString
createStandardKeyStatement(const Reference
< XPropertySet
>& descriptor
,const Reference
< XConnection
>& _xConnection
)
276 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
277 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
281 Reference
<XKeysSupplier
> xKeySup(descriptor
,UNO_QUERY
);
282 Reference
<XIndexAccess
> xKeys
= xKeySup
->getKeys();
285 Reference
< XPropertySet
> xColProp
;
286 Reference
<XIndexAccess
> xColumns
;
287 Reference
<XColumnsSupplier
> xColumnSup
;
288 OUString sCatalog
,sSchema
,sTable
,sComposedName
;
290 for(sal_Int32 i
=0;i
<xKeys
->getCount();++i
)
292 if ( (xKeys
->getByIndex(i
) >>= xColProp
) && xColProp
.is() )
295 sal_Int32 nKeyType
= ::comphelper::getINT32(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_TYPE
)));
297 if ( nKeyType
== KeyType::PRIMARY
)
300 ::dbtools::throwFunctionSequenceException(_xConnection
);
303 xColumnSup
.set(xColProp
,UNO_QUERY
);
304 xColumns
.set(xColumnSup
->getColumns(),UNO_QUERY
);
305 if(!xColumns
.is() || !xColumns
->getCount())
306 ::dbtools::throwFunctionSequenceException(_xConnection
);
308 aSql
.append(" PRIMARY KEY ");
309 aSql
.append(generateColumnNames(xColumns
,xMetaData
));
311 else if(nKeyType
== KeyType::UNIQUE
)
313 xColumnSup
.set(xColProp
,UNO_QUERY
);
314 xColumns
.set(xColumnSup
->getColumns(),UNO_QUERY
);
315 if(!xColumns
.is() || !xColumns
->getCount())
316 ::dbtools::throwFunctionSequenceException(_xConnection
);
318 aSql
.append(" UNIQUE ");
319 aSql
.append(generateColumnNames(xColumns
,xMetaData
));
321 else if(nKeyType
== KeyType::FOREIGN
)
323 sal_Int32 nDeleteRule
= getINT32(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_DELETERULE
)));
325 xColumnSup
.set(xColProp
,UNO_QUERY
);
326 xColumns
.set(xColumnSup
->getColumns(),UNO_QUERY
);
327 if(!xColumns
.is() || !xColumns
->getCount())
328 ::dbtools::throwFunctionSequenceException(_xConnection
);
330 aSql
.append(" FOREIGN KEY ");
331 OUString sRefTable
= getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE
)));
332 ::dbtools::qualifiedNameComponents(xMetaData
,
337 ::dbtools::EComposeRule::InDataManipulation
);
338 sComposedName
= ::dbtools::composeTableName( xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
341 if ( sComposedName
.isEmpty() )
342 ::dbtools::throwFunctionSequenceException(_xConnection
);
344 aSql
.append(generateColumnNames(xColumns
,xMetaData
));
348 case KeyRule::CASCADE
:
349 aSql
.append(" ON DELETE CASCADE ");
351 case KeyRule::RESTRICT
:
352 aSql
.append(" ON DELETE RESTRICT ");
354 case KeyRule::SET_NULL
:
355 aSql
.append(" ON DELETE SET NULL ");
357 case KeyRule::SET_DEFAULT
:
358 aSql
.append(" ON DELETE SET DEFAULT ");
368 if ( !aSql
.isEmpty() )
370 if ( aSql
[aSql
.getLength() - 1] == ',' )
371 aSql
[aSql
.getLength() - 1] = ')';
376 return aSql
.makeStringAndClear();
380 OUString
createSqlCreateTableStatement( const Reference
< XPropertySet
>& descriptor
,
381 const Reference
< XConnection
>& _xConnection
)
383 OUString aSql
= ::dbtools::createStandardCreateStatement(descriptor
,_xConnection
,nullptr,{});
384 const OUString sKeyStmt
= ::dbtools::createStandardKeyStatement(descriptor
,_xConnection
);
385 if ( !sKeyStmt
.isEmpty() )
389 if ( aSql
.endsWith(",") )
390 aSql
= aSql
.replaceAt(aSql
.getLength()-1, 1, ")");
398 Reference
<XPropertySet
> lcl_createSDBCXColumn(const Reference
<XNameAccess
>& _xPrimaryKeyColumns
,
399 const Reference
<XConnection
>& _xConnection
,
400 const Any
& _aCatalog
,
401 const OUString
& _aSchema
,
402 const OUString
& _aTable
,
403 const OUString
& _rQueryName
,
404 const OUString
& _rName
,
407 bool _bIsAutoIncrement
,
409 sal_Int32 _nDataType
)
411 Reference
<XPropertySet
> xProp
;
412 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
413 Reference
< XResultSet
> xResult
= xMetaData
->getColumns(_aCatalog
, _aSchema
, _aTable
, _rQueryName
);
415 _aCatalog
>>= sCatalog
;
419 UStringMixEqual
aMixCompare(_bCase
);
420 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
421 while( xResult
->next() )
423 if ( aMixCompare(xRow
->getString(4),_rName
) )
425 sal_Int32 nField5
= xRow
->getInt(5);
426 OUString aField6
= xRow
->getString(6);
427 sal_Int32 nField7
= xRow
->getInt(7)
428 , nField9
= xRow
->getInt(9)
429 , nField11
= xRow
->getInt(11);
430 OUString sField12
= xRow
->getString(12),
431 sField13
= xRow
->getString(13);
432 ::comphelper::disposeComponent(xRow
);
434 bool bAutoIncrement
= _bIsAutoIncrement
435 ,bIsCurrency
= _bIsCurrency
;
436 if ( _bQueryForInfo
)
438 const OUString sQuote
= xMetaData
->getIdentifierQuoteString();
439 OUString sQuotedName
= ::dbtools::quoteName(sQuote
,_rName
);
440 OUString sComposedName
= composeTableNameForSelect(_xConnection
, getString( _aCatalog
), _aSchema
, _aTable
);
442 ColumnInformationMap
aInfo(_bCase
);
443 collectColumnInformation(_xConnection
,sComposedName
,sQuotedName
,aInfo
);
444 ColumnInformationMap::const_iterator aIter
= aInfo
.begin();
445 if ( aIter
!= aInfo
.end() )
447 bAutoIncrement
= aIter
->second
.first
.first
;
448 bIsCurrency
= aIter
->second
.first
.second
;
449 if ( DataType::OTHER
== nField5
)
450 nField5
= aIter
->second
.second
;
453 else if ( DataType::OTHER
== nField5
)
454 nField5
= _nDataType
;
456 if ( nField11
!= ColumnValue::NO_NULLS
)
460 if ( _xPrimaryKeyColumns
.is() )
462 if ( _xPrimaryKeyColumns
->hasByName(_rName
) )
463 nField11
= ColumnValue::NO_NULLS
;
468 Reference
< XResultSet
> xPKeys
= xMetaData
->getPrimaryKeys( _aCatalog
, _aSchema
, _aTable
);
469 Reference
< XRow
> xPKeyRow( xPKeys
, UNO_QUERY_THROW
);
470 while( xPKeys
->next() ) // there can be only one primary key
472 OUString sKeyColumn
= xPKeyRow
->getString(4);
473 if ( aMixCompare(_rName
,sKeyColumn
) )
475 nField11
= ColumnValue::NO_NULLS
;
483 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "lcl_createSDBCXColumn" );
487 connectivity::sdbcx::OColumn
* pRet
= new connectivity::sdbcx::OColumn(_rName
,
512 Reference
< XModel
> lcl_getXModel(const Reference
< XInterface
>& _xIface
)
514 Reference
< XInterface
> xParent
= _xIface
;
515 Reference
< XModel
> xModel(xParent
,UNO_QUERY
);
516 while( xParent
.is() && !xModel
.is() )
518 Reference
<XChild
> xChild(xParent
,UNO_QUERY
);
519 xParent
.set(xChild
.is() ? xChild
->getParent() : Reference
< XInterface
>(),UNO_QUERY
);
520 xModel
.set(xParent
,UNO_QUERY
);
526 Reference
<XPropertySet
> createSDBCXColumn(const Reference
<XPropertySet
>& _xTable
,
527 const Reference
<XConnection
>& _xConnection
,
528 const OUString
& _rName
,
531 bool _bIsAutoIncrement
,
533 sal_Int32 _nDataType
)
535 Reference
<XPropertySet
> xProp
;
536 OSL_ENSURE(_xTable
.is(),"Table is NULL!");
540 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
541 Any aCatalog
= _xTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
));
543 aCatalog
>>= sCatalog
;
545 OUString aSchema
, aTable
;
546 _xTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= aSchema
;
547 _xTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= aTable
;
549 Reference
<XNameAccess
> xPrimaryKeyColumns
= getPrimaryKeyColumns_throw(_xTable
);
551 xProp
= lcl_createSDBCXColumn(xPrimaryKeyColumns
,_xConnection
,aCatalog
, aSchema
, aTable
, _rName
,_rName
,_bCase
,_bQueryForInfo
,_bIsAutoIncrement
,_bIsCurrency
,_nDataType
);
554 xProp
= lcl_createSDBCXColumn(xPrimaryKeyColumns
,_xConnection
,aCatalog
, aSchema
, aTable
, "%",_rName
,_bCase
,_bQueryForInfo
,_bIsAutoIncrement
,_bIsCurrency
,_nDataType
);
556 xProp
= new connectivity::sdbcx::OColumn(_rName
,
557 OUString(),OUString(),OUString(),
558 ColumnValue::NULLABLE_UNKNOWN
,
576 bool getBooleanDataSourceSetting( const Reference
< XConnection
>& _rxConnection
, const char* _pAsciiSettingName
)
578 bool bValue( false );
581 Reference
< XPropertySet
> xDataSourceProperties( findDataSource( _rxConnection
), UNO_QUERY
);
582 OSL_ENSURE( xDataSourceProperties
.is(), "::dbtools::getBooleanDataSourceSetting: somebody is using this with a non-SDB-level connection!" );
583 if ( xDataSourceProperties
.is() )
585 Reference
< XPropertySet
> xSettings(
586 xDataSourceProperties
->getPropertyValue("Settings"),
589 OSL_VERIFY( xSettings
->getPropertyValue( OUString::createFromAscii( _pAsciiSettingName
) ) >>= bValue
);
592 catch( const Exception
& )
594 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
599 bool getDataSourceSetting( const Reference
< XInterface
>& _xChild
, const OUString
& _sAsciiSettingsName
,
600 Any
& /* [out] */ _rSettingsValue
)
602 bool bIsPresent
= false;
605 const Reference
< XPropertySet
> xDataSourceProperties( findDataSource( _xChild
), UNO_QUERY
);
606 if ( !xDataSourceProperties
.is() )
609 const Reference
< XPropertySet
> xSettings(
610 xDataSourceProperties
->getPropertyValue("Settings"),
614 _rSettingsValue
= xSettings
->getPropertyValue( _sAsciiSettingsName
);
617 catch( const Exception
& )
624 bool getDataSourceSetting( const Reference
< XInterface
>& _rxDataSource
, const char* _pAsciiSettingsName
,
625 Any
& /* [out] */ _rSettingsValue
)
627 OUString sAsciiSettingsName
= OUString::createFromAscii(_pAsciiSettingsName
);
628 return getDataSourceSetting( _rxDataSource
, sAsciiSettingsName
,_rSettingsValue
);
631 bool isDataSourcePropertyEnabled(const Reference
<XInterface
>& _xProp
, const OUString
& _sProperty
, bool _bDefault
)
633 bool bEnabled
= _bDefault
;
636 Reference
< XPropertySet
> xProp(findDataSource(_xProp
),UNO_QUERY
);
639 Sequence
< PropertyValue
> aInfo
;
640 xProp
->getPropertyValue("Info") >>= aInfo
;
641 const PropertyValue
* pValue
=std::find_if(aInfo
.begin(),
643 [&_sProperty
](const PropertyValue
& lhs
)
644 { return lhs
.Name
== _sProperty
; });
645 if ( pValue
!= aInfo
.end() )
646 pValue
->Value
>>= bEnabled
;
651 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
656 Reference
< XTablesSupplier
> getDataDefinitionByURLAndConnection(
657 const OUString
& _rsUrl
,
658 const Reference
< XConnection
>& _xConnection
,
659 const Reference
< XComponentContext
>& _rxContext
)
661 Reference
< XTablesSupplier
> xTablesSup
;
664 Reference
< XDriverManager2
> xManager
= DriverManager::create( _rxContext
);
665 Reference
< XDataDefinitionSupplier
> xSupp( xManager
->getDriverByURL( _rsUrl
), UNO_QUERY
);
669 xTablesSup
= xSupp
->getDataDefinitionByConnection( _xConnection
);
670 OSL_ENSURE(xTablesSup
.is(),"No table supplier!");
673 catch( const Exception
& )
675 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
681 sal_Int32
getTablePrivileges(const Reference
< XDatabaseMetaData
>& _xMetaData
,
682 const OUString
& _sCatalog
,
683 const OUString
& _sSchema
,
684 const OUString
& _sTable
)
686 OSL_ENSURE(_xMetaData
.is(),"Invalid metadata!");
687 sal_Int32 nPrivileges
= 0;
691 if(!_sCatalog
.isEmpty())
693 Reference
< XResultSet
> xPrivileges
= _xMetaData
->getTablePrivileges(aVal
, _sSchema
, _sTable
);
694 Reference
< XRow
> xCurrentRow(xPrivileges
, UNO_QUERY
);
696 const OUString sUserWorkingFor
= _xMetaData
->getUserName();
697 static const char sSELECT
[] = "SELECT";
698 static const char sINSERT
[] = "INSERT";
699 static const char sUPDATE
[] = "UPDATE";
700 static const char sDELETE
[] = "DELETE";
701 static const char sREAD
[] = "READ";
702 static const char sCREATE
[] = "CREATE";
703 static const char sALTER
[] = "ALTER";
704 static const char sREFERENCE
[] = "REFERENCE";
705 static const char sDROP
[] = "DROP";
707 if ( xCurrentRow
.is() )
709 // after creation the set is positioned before the first record, per definition
710 OUString sPrivilege
, sGrantee
;
711 while ( xPrivileges
->next() )
713 sGrantee
= xCurrentRow
->getString(5);
714 sPrivilege
= xCurrentRow
->getString(6);
716 if (!sUserWorkingFor
.equalsIgnoreAsciiCase(sGrantee
))
719 if (sPrivilege
.equalsIgnoreAsciiCase(sSELECT
))
720 nPrivileges
|= Privilege::SELECT
;
721 else if (sPrivilege
.equalsIgnoreAsciiCase(sINSERT
))
722 nPrivileges
|= Privilege::INSERT
;
723 else if (sPrivilege
.equalsIgnoreAsciiCase(sUPDATE
))
724 nPrivileges
|= Privilege::UPDATE
;
725 else if (sPrivilege
.equalsIgnoreAsciiCase(sDELETE
))
726 nPrivileges
|= Privilege::DELETE
;
727 else if (sPrivilege
.equalsIgnoreAsciiCase(sREAD
))
728 nPrivileges
|= Privilege::READ
;
729 else if (sPrivilege
.equalsIgnoreAsciiCase(sCREATE
))
730 nPrivileges
|= Privilege::CREATE
;
731 else if (sPrivilege
.equalsIgnoreAsciiCase(sALTER
))
732 nPrivileges
|= Privilege::ALTER
;
733 else if (sPrivilege
.equalsIgnoreAsciiCase(sREFERENCE
))
734 nPrivileges
|= Privilege::REFERENCE
;
735 else if (sPrivilege
.equalsIgnoreAsciiCase(sDROP
))
736 nPrivileges
|= Privilege::DROP
;
739 disposeComponent(xPrivileges
);
741 // Some drivers put a table privilege as soon as any column has the privilege,
742 // some drivers only if all columns have the privilege.
743 // To unify the situation, collect column privileges here, too.
744 Reference
< XResultSet
> xColumnPrivileges
= _xMetaData
->getColumnPrivileges(aVal
, _sSchema
, _sTable
, "%");
745 Reference
< XRow
> xColumnCurrentRow(xColumnPrivileges
, UNO_QUERY
);
746 if ( xColumnCurrentRow
.is() )
748 // after creation the set is positioned before the first record, per definition
749 OUString sPrivilege
, sGrantee
;
750 while ( xColumnPrivileges
->next() )
752 sGrantee
= xColumnCurrentRow
->getString(6);
753 sPrivilege
= xColumnCurrentRow
->getString(7);
755 if (!sUserWorkingFor
.equalsIgnoreAsciiCase(sGrantee
))
758 if (sPrivilege
.equalsIgnoreAsciiCase(sSELECT
))
759 nPrivileges
|= Privilege::SELECT
;
760 else if (sPrivilege
.equalsIgnoreAsciiCase(sINSERT
))
761 nPrivileges
|= Privilege::INSERT
;
762 else if (sPrivilege
.equalsIgnoreAsciiCase(sUPDATE
))
763 nPrivileges
|= Privilege::UPDATE
;
764 else if (sPrivilege
.equalsIgnoreAsciiCase(sDELETE
))
765 nPrivileges
|= Privilege::DELETE
;
766 else if (sPrivilege
.equalsIgnoreAsciiCase(sREAD
))
767 nPrivileges
|= Privilege::READ
;
768 else if (sPrivilege
.equalsIgnoreAsciiCase(sCREATE
))
769 nPrivileges
|= Privilege::CREATE
;
770 else if (sPrivilege
.equalsIgnoreAsciiCase(sALTER
))
771 nPrivileges
|= Privilege::ALTER
;
772 else if (sPrivilege
.equalsIgnoreAsciiCase(sREFERENCE
))
773 nPrivileges
|= Privilege::REFERENCE
;
774 else if (sPrivilege
.equalsIgnoreAsciiCase(sDROP
))
775 nPrivileges
|= Privilege::DROP
;
778 disposeComponent(xColumnPrivileges
);
780 catch(const SQLException
& e
)
782 // some drivers don't support any privileges so we assume that we are allowed to do all we want :-)
783 if(e
.SQLState
== "IM001")
784 nPrivileges
|= Privilege::DROP
|
785 Privilege::REFERENCE
|
794 OSL_FAIL("Could not collect the privileges !");
799 // we need some more information about the column
800 void collectColumnInformation(const Reference
< XConnection
>& _xConnection
,
801 const OUString
& _sComposedName
,
802 const OUString
& _rName
,
803 ColumnInformationMap
& _rInfo
)
805 OUString sSelect
= "SELECT " + _rName
+
806 " FROM " + _sComposedName
+
811 ::utl::SharedUNOComponent
< XStatement
> xStmt( _xConnection
->createStatement() );
812 Reference
< XPropertySet
> xStatementProps( xStmt
, UNO_QUERY_THROW
);
813 xStatementProps
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING
), makeAny( false ) );
814 Reference
< XResultSet
> xResult( xStmt
->executeQuery( sSelect
), UNO_SET_THROW
);
815 Reference
< XResultSetMetaDataSupplier
> xSuppMeta( xResult
, UNO_QUERY_THROW
);
816 Reference
< XResultSetMetaData
> xMeta( xSuppMeta
->getMetaData(), UNO_SET_THROW
);
818 sal_Int32 nCount
= xMeta
->getColumnCount();
819 OSL_ENSURE( nCount
!= 0, "::dbtools::collectColumnInformation: result set has empty (column-less) meta data!" );
820 for (sal_Int32 i
=1; i
<= nCount
; ++i
)
822 _rInfo
.emplace( xMeta
->getColumnName(i
),
823 ColumnInformation(TBoolPair(xMeta
->isAutoIncrement(i
),xMeta
->isCurrency(i
)),xMeta
->getColumnType(i
)));
826 catch( const Exception
& )
828 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
833 bool isEmbeddedInDatabase( const Reference
< XInterface
>& _rxComponent
, Reference
< XConnection
>& _rxActualConnection
)
835 bool bIsEmbedded
= false;
838 Reference
< XModel
> xModel
= lcl_getXModel( _rxComponent
);
842 Sequence
< PropertyValue
> aArgs
= xModel
->getArgs();
843 const PropertyValue
* pIter
= aArgs
.getConstArray();
844 const PropertyValue
* pEnd
= pIter
+ aArgs
.getLength();
845 for(;pIter
!= pEnd
;++pIter
)
847 if ( pIter
->Name
== "ComponentData" )
849 Sequence
<PropertyValue
> aDocumentContext
;
850 pIter
->Value
>>= aDocumentContext
;
851 const PropertyValue
* pContextIter
= aDocumentContext
.getConstArray();
852 const PropertyValue
* pContextEnd
= pContextIter
+ aDocumentContext
.getLength();
853 for(;pContextIter
!= pContextEnd
;++pContextIter
)
855 if ( pContextIter
->Name
== "ActiveConnection"
856 && ( pContextIter
->Value
>>= _rxActualConnection
)
877 OUString
lcl_getEncodingName( rtl_TextEncoding _eEncoding
)
879 OUString sEncodingName
;
881 OCharsetMap aCharsets
;
882 OCharsetMap::CharsetIterator aEncodingPos
= aCharsets
.find( _eEncoding
);
883 OSL_ENSURE( aEncodingPos
!= aCharsets
.end(), "lcl_getEncodingName: *which* encoding?" );
884 if ( aEncodingPos
!= aCharsets
.end() )
885 sEncodingName
= (*aEncodingPos
).getIanaName();
887 return sEncodingName
;
892 sal_Int32
DBTypeConversion::convertUnicodeString( const OUString
& _rSource
, OString
& _rDest
, rtl_TextEncoding _eEncoding
)
894 if ( !rtl_convertUStringToString( &_rDest
.pData
, _rSource
.getStr(), _rSource
.getLength(),
896 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
897 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE
|
898 RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0
)
901 SharedResources aResources
;
902 OUString sMessage
= aResources
.getResourceStringWithSubstitution( STR_CANNOT_CONVERT_STRING
,
903 "$string$", _rSource
,
904 "$charset$", lcl_getEncodingName( _eEncoding
)
916 return _rDest
.getLength();
920 sal_Int32
DBTypeConversion::convertUnicodeStringToLength( const OUString
& _rSource
, OString
& _rDest
,
921 sal_Int32 _nMaxLen
, rtl_TextEncoding _eEncoding
)
923 sal_Int32 nLen
= convertUnicodeString( _rSource
, _rDest
, _eEncoding
);
924 if ( nLen
> _nMaxLen
)
926 SharedResources aResources
;
927 OUString sMessage
= aResources
.getResourceStringWithSubstitution( STR_STRING_LENGTH_EXCEEDED
,
928 "$string$", _rSource
,
929 "$maxlen$", OUString::number( _nMaxLen
),
930 "$charset$", lcl_getEncodingName( _eEncoding
)
944 static OUString
lcl_getReportEngines()
946 return "org.openoffice.Office.DataAccess/ReportEngines";
949 static OUString
lcl_getDefaultReportEngine()
951 return "DefaultReportEngine";
954 static OUString
lcl_getReportEngineNames()
956 return "ReportEngineNames";
959 OUString
getDefaultReportEngineServiceName(const Reference
< XComponentContext
>& _rxORB
)
961 ::utl::OConfigurationTreeRoot aReportEngines
= ::utl::OConfigurationTreeRoot::createWithComponentContext(
962 _rxORB
, lcl_getReportEngines(), -1, ::utl::OConfigurationTreeRoot::CM_READONLY
);
964 if ( aReportEngines
.isValid() )
966 OUString sDefaultReportEngineName
;
967 aReportEngines
.getNodeValue(lcl_getDefaultReportEngine()) >>= sDefaultReportEngineName
;
968 if ( !sDefaultReportEngineName
.isEmpty() )
970 ::utl::OConfigurationNode aReportEngineNames
= aReportEngines
.openNode(lcl_getReportEngineNames());
971 if ( aReportEngineNames
.isValid() )
973 ::utl::OConfigurationNode aReportEngine
= aReportEngineNames
.openNode(sDefaultReportEngineName
);
974 if ( aReportEngine
.isValid() )
977 aReportEngine
.getNodeValue("ServiceName") >>= sRet
;
983 return "org.libreoffice.report.pentaho.SOReportJobFactory";
986 return "org.libreoffice.report.pentaho.SOReportJobFactory";
990 bool isAggregateColumn(const Reference
< XSingleSelectQueryComposer
> &_xParser
, const Reference
< XPropertySet
> &_xField
)
993 _xField
->getPropertyValue("Name") >>= sName
;
994 Reference
< XColumnsSupplier
> xColumnsSupplier(_xParser
, UNO_QUERY
);
995 Reference
< css::container::XNameAccess
> xCols
;
996 if (xColumnsSupplier
.is())
997 xCols
= xColumnsSupplier
->getColumns();
999 return isAggregateColumn(xCols
, sName
);
1002 bool isAggregateColumn(const Reference
< XNameAccess
> &_xColumns
, const OUString
&_sName
)
1004 if ( _xColumns
.is() && _xColumns
->hasByName(_sName
) )
1006 Reference
<XPropertySet
> xProp(_xColumns
->getByName(_sName
),UNO_QUERY
);
1008 return isAggregateColumn( xProp
);
1013 bool isAggregateColumn( const Reference
< XPropertySet
> &_xColumn
)
1017 static const char sAgg
[] = "AggregateFunction";
1018 if ( _xColumn
->getPropertySetInfo()->hasPropertyByName(sAgg
) )
1019 _xColumn
->getPropertyValue(sAgg
) >>= bAgg
;
1025 } // namespace dbtools
1028 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */