1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
27cdf0e10cSrcweir #include "connectivity/dbconversion.hxx"
28cdf0e10cSrcweir #include "connectivity/dbcharset.hxx"
29cdf0e10cSrcweir #include "connectivity/SQLStatementHelper.hxx"
30cdf0e10cSrcweir #include <unotools/confignode.hxx>
31cdf0e10cSrcweir #include "resource/sharedresources.hxx"
32cdf0e10cSrcweir #include "resource/common_res.hrc"
33cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
34cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
36cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
37cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
38cdf0e10cSrcweir #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
39cdf0e10cSrcweir #include <com/sun/star/sdbc/XDriverAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/sdbcx/Privilege.hpp>
42cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
43cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
44cdf0e10cSrcweir #include <com/sun/star/sdbc/KeyRule.hpp>
45cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
46cdf0e10cSrcweir #include "TConnection.hxx"
47cdf0e10cSrcweir #include "connectivity/sdbcx/VColumn.hxx"
48cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
49cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <tools/diagnose_ex.h>
52cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
53cdf0e10cSrcweir #include <comphelper/configurationhelper.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //.........................................................................
56cdf0e10cSrcweir namespace dbtools
57cdf0e10cSrcweir {
58cdf0e10cSrcweir //.........................................................................
59cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
60cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
61cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbc;
62cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbcx;
63cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
64cdf0e10cSrcweir 	using namespace ::com::sun::star::container;
65cdf0e10cSrcweir 	using namespace ::com::sun::star::frame;
66cdf0e10cSrcweir 	using namespace connectivity;
67cdf0e10cSrcweir 	using namespace comphelper;
68cdf0e10cSrcweir 
createStandardColumnPart(const Reference<XPropertySet> & xColProp,const Reference<XConnection> & _xConnection,ISQLStatementHelper * _pHelper,const::rtl::OUString & _sCreatePattern)69cdf0e10cSrcweir ::rtl::OUString createStandardColumnPart(const Reference< XPropertySet >& xColProp,const Reference< XConnection>& _xConnection,ISQLStatementHelper* _pHelper,const ::rtl::OUString& _sCreatePattern)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	::rtl::OUString sTypeName;
77cdf0e10cSrcweir 	sal_Int32		nDataType	= 0;
78cdf0e10cSrcweir 	sal_Int32		nPrecision	= 0;
79cdf0e10cSrcweir 	sal_Int32		nScale		= 0;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	const ::rtl::OUString sQuoteString = xMetaData->getIdentifierQuoteString();
82cdf0e10cSrcweir 	::rtl::OUStringBuffer aSql = ::dbtools::quoteName(sQuoteString,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))));
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	aSql.appendAscii(" ");
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	nDataType = nPrecision = nScale = 0;
87cdf0e10cSrcweir 	sal_Bool bIsAutoIncrement = sal_False;
88cdf0e10cSrcweir 	xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPENAME))			>>= sTypeName;
89cdf0e10cSrcweir 	xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE))				>>= nDataType;
90cdf0e10cSrcweir 	xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_PRECISION))			>>= nPrecision;
91cdf0e10cSrcweir 	xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCALE))				>>= nScale;
92cdf0e10cSrcweir 	xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT))	>>= bIsAutoIncrement;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	// check if the user enter a specific string to create autoincrement values
95cdf0e10cSrcweir 	::rtl::OUString sAutoIncrementValue;
96cdf0e10cSrcweir 	Reference<XPropertySetInfo> xPropInfo = xColProp->getPropertySetInfo();
97cdf0e10cSrcweir 	if ( xPropInfo.is() && xPropInfo->hasPropertyByName(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) )
98cdf0e10cSrcweir 		xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) >>= sAutoIncrementValue;
99cdf0e10cSrcweir 	// look if we have to use precisions
100cdf0e10cSrcweir 	sal_Bool bUseLiteral = sal_False;
101cdf0e10cSrcweir 	::rtl::OUString sPreFix,sPostFix,sCreateParams;
102cdf0e10cSrcweir 	{
103cdf0e10cSrcweir 		Reference<XResultSet> xRes = xMetaData->getTypeInfo();
104cdf0e10cSrcweir 		if(xRes.is())
105cdf0e10cSrcweir 		{
106cdf0e10cSrcweir 			Reference<XRow> xRow(xRes,UNO_QUERY);
107cdf0e10cSrcweir 			while(xRes->next())
108cdf0e10cSrcweir 			{
109cdf0e10cSrcweir 				::rtl::OUString sTypeName2Cmp = xRow->getString(1);
110cdf0e10cSrcweir 				sal_Int32 nType = xRow->getShort(2);
111cdf0e10cSrcweir 				sPreFix = xRow->getString (4);
112cdf0e10cSrcweir 				sPostFix = xRow->getString (5);
113cdf0e10cSrcweir 				sCreateParams = xRow->getString(6);
114cdf0e10cSrcweir 				// first identical type will be used if typename is empty
115cdf0e10cSrcweir 				if ( !sTypeName.getLength() && nType == nDataType )
116cdf0e10cSrcweir 					sTypeName = sTypeName2Cmp;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 				if( sTypeName.equalsIgnoreAsciiCase(sTypeName2Cmp) && nType == nDataType && sCreateParams.getLength() && !xRow->wasNull())
119cdf0e10cSrcweir 				{
120cdf0e10cSrcweir 					bUseLiteral = sal_True;
121cdf0e10cSrcweir 					break;
122cdf0e10cSrcweir 				}
123cdf0e10cSrcweir 			}
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
128cdf0e10cSrcweir 	if ( sAutoIncrementValue.getLength() && (nIndex = sTypeName.indexOf(sAutoIncrementValue)) != -1 )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		sTypeName = sTypeName.replaceAt(nIndex,sTypeName.getLength() - nIndex,::rtl::OUString());
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	if ( (nPrecision > 0 || nScale > 0) && bUseLiteral )
134cdf0e10cSrcweir 	{
135cdf0e10cSrcweir 		sal_Int32 nParenPos = sTypeName.indexOf('(');
136cdf0e10cSrcweir 		if ( nParenPos == -1 )
137cdf0e10cSrcweir 		{
138cdf0e10cSrcweir 			aSql.append(sTypeName);
139cdf0e10cSrcweir 			aSql.appendAscii("(");
140cdf0e10cSrcweir 		}
141cdf0e10cSrcweir 		else
142cdf0e10cSrcweir 		{
143cdf0e10cSrcweir 			aSql.append(sTypeName.copy(0,++nParenPos));
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         if ( nPrecision > 0 && nDataType != DataType::TIMESTAMP )
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir 		    aSql.append(nPrecision);
149cdf0e10cSrcweir             if ( (nScale > 0) || (_sCreatePattern.getLength() && sCreateParams.indexOf(_sCreatePattern) != -1) )
150cdf0e10cSrcweir                 aSql.appendAscii(",");
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 		if ( (nScale > 0) || (_sCreatePattern.getLength() && sCreateParams.indexOf(_sCreatePattern) != -1 ) || nDataType == DataType::TIMESTAMP )
153cdf0e10cSrcweir 			aSql.append(nScale);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         if ( nParenPos == -1 )
156cdf0e10cSrcweir 			aSql.appendAscii(")");
157cdf0e10cSrcweir 		else
158cdf0e10cSrcweir 		{
159cdf0e10cSrcweir 			nParenPos = sTypeName.indexOf(')',nParenPos);
160cdf0e10cSrcweir 			aSql.append(sTypeName.copy(nParenPos));
161cdf0e10cSrcweir 		}
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir 	else
164cdf0e10cSrcweir 		aSql.append(sTypeName); // simply add the type name
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	::rtl::OUString aDefault = ::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)));
167cdf0e10cSrcweir 	if ( aDefault.getLength() )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir 		aSql.append(::rtl::OUString::createFromAscii(" DEFAULT "));
170cdf0e10cSrcweir         aSql.append(sPreFix);
171cdf0e10cSrcweir         aSql.append(aDefault);
172cdf0e10cSrcweir         aSql.append(sPostFix);
173cdf0e10cSrcweir     } // if ( aDefault.getLength() )
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	if(::comphelper::getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISNULLABLE))) == ColumnValue::NO_NULLS)
176cdf0e10cSrcweir 		aSql.append(::rtl::OUString::createFromAscii(" NOT NULL"));
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	if ( bIsAutoIncrement && sAutoIncrementValue.getLength())
179cdf0e10cSrcweir 	{
180cdf0e10cSrcweir 		aSql.appendAscii(" ");
181cdf0e10cSrcweir 		aSql.append(sAutoIncrementValue);
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     if ( _pHelper )
185cdf0e10cSrcweir         _pHelper->addComment(xColProp,aSql);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	return aSql.makeStringAndClear();
188cdf0e10cSrcweir }
189cdf0e10cSrcweir // -----------------------------------------------------------------------------
190cdf0e10cSrcweir 
createStandardCreateStatement(const Reference<XPropertySet> & descriptor,const Reference<XConnection> & _xConnection,ISQLStatementHelper * _pHelper,const::rtl::OUString & _sCreatePattern)191cdf0e10cSrcweir ::rtl::OUString createStandardCreateStatement(const Reference< XPropertySet >& descriptor,const Reference< XConnection>& _xConnection,ISQLStatementHelper* _pHelper,const ::rtl::OUString& _sCreatePattern)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	::rtl::OUStringBuffer aSql	= ::rtl::OUString::createFromAscii("CREATE TABLE ");
194cdf0e10cSrcweir 	::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
197cdf0e10cSrcweir 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))	>>= sCatalog;
200cdf0e10cSrcweir 	descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME))	>>= sSchema;
201cdf0e10cSrcweir 	descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))			>>= sTable;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	sComposedName = ::dbtools::composeTableName( xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInTableDefinitions );
204cdf0e10cSrcweir 	if ( !sComposedName.getLength() )
205cdf0e10cSrcweir 		::dbtools::throwFunctionSequenceException(_xConnection);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	aSql.append(sComposedName);
208cdf0e10cSrcweir     aSql.append(::rtl::OUString::createFromAscii(" ("));
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	// columns
211cdf0e10cSrcweir 	Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
212cdf0e10cSrcweir 	Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
213cdf0e10cSrcweir 	// check if there are columns
214cdf0e10cSrcweir 	if(!xColumns.is() || !xColumns->getCount())
215cdf0e10cSrcweir 		::dbtools::throwFunctionSequenceException(_xConnection);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	Reference< XPropertySet > xColProp;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	sal_Int32 nCount = xColumns->getCount();
220cdf0e10cSrcweir 	for(sal_Int32 i=0;i<nCount;++i)
221cdf0e10cSrcweir 	{
222cdf0e10cSrcweir 		if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
223cdf0e10cSrcweir 		{
224cdf0e10cSrcweir 			aSql.append(createStandardColumnPart(xColProp,_xConnection,_pHelper,_sCreatePattern));
225cdf0e10cSrcweir 			aSql.appendAscii(",");
226cdf0e10cSrcweir 		}
227cdf0e10cSrcweir 	}
228cdf0e10cSrcweir 	return aSql.makeStringAndClear();
229cdf0e10cSrcweir }
230cdf0e10cSrcweir namespace
231cdf0e10cSrcweir {
generateColumnNames(const Reference<XIndexAccess> & _xColumns,const Reference<XDatabaseMetaData> & _xMetaData)232cdf0e10cSrcweir 	::rtl::OUString generateColumnNames(const Reference<XIndexAccess>& _xColumns,const Reference<XDatabaseMetaData>& _xMetaData)
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 		::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
235cdf0e10cSrcweir 		static const ::rtl::OUString sComma(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(",")));
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		const ::rtl::OUString sQuote(_xMetaData->getIdentifierQuoteString());
238cdf0e10cSrcweir 		::rtl::OUString sSql = ::rtl::OUString::createFromAscii(" (");
239cdf0e10cSrcweir 		Reference< XPropertySet > xColProp;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 		sal_Int32 nColCount  = _xColumns->getCount();
242cdf0e10cSrcweir 		for(sal_Int32 i=0;i<nColCount;++i)
243cdf0e10cSrcweir 		{
244cdf0e10cSrcweir 			if ( (_xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
245cdf0e10cSrcweir 				sSql += ::dbtools::quoteName(sQuote,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))))
246cdf0e10cSrcweir 						+ sComma;
247cdf0e10cSrcweir 		}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 		if ( nColCount )
250cdf0e10cSrcweir 			sSql = sSql.replaceAt(sSql.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
251cdf0e10cSrcweir 		return sSql;
252cdf0e10cSrcweir 	}
253cdf0e10cSrcweir }
254cdf0e10cSrcweir // -----------------------------------------------------------------------------
createStandardKeyStatement(const Reference<XPropertySet> & descriptor,const Reference<XConnection> & _xConnection)255cdf0e10cSrcweir ::rtl::OUString createStandardKeyStatement(const Reference< XPropertySet >& descriptor,const Reference< XConnection>& _xConnection)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
258cdf0e10cSrcweir 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	::rtl::OUStringBuffer aSql;
261cdf0e10cSrcweir 	// keys
262cdf0e10cSrcweir 	Reference<XKeysSupplier> xKeySup(descriptor,UNO_QUERY);
263cdf0e10cSrcweir 	Reference<XIndexAccess> xKeys = xKeySup->getKeys();
264cdf0e10cSrcweir 	if ( xKeys.is() )
265cdf0e10cSrcweir 	{
266cdf0e10cSrcweir 		Reference< XPropertySet > xColProp;
267cdf0e10cSrcweir 		Reference<XIndexAccess> xColumns;
268cdf0e10cSrcweir 		Reference<XColumnsSupplier> xColumnSup;
269cdf0e10cSrcweir 		::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
270cdf0e10cSrcweir 		sal_Bool bPKey = sal_False;
271cdf0e10cSrcweir 		for(sal_Int32 i=0;i<xKeys->getCount();++i)
272cdf0e10cSrcweir 		{
273cdf0e10cSrcweir 			if ( (xKeys->getByIndex(i) >>= xColProp) && xColProp.is() )
274cdf0e10cSrcweir 			{
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 				sal_Int32 nKeyType		= ::comphelper::getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)));
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 				if ( nKeyType == KeyType::PRIMARY )
279cdf0e10cSrcweir 				{
280cdf0e10cSrcweir 					if(bPKey)
281cdf0e10cSrcweir 						::dbtools::throwFunctionSequenceException(_xConnection);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 					bPKey = sal_True;
284cdf0e10cSrcweir 					xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
285cdf0e10cSrcweir 					xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
286cdf0e10cSrcweir 					if(!xColumns.is() || !xColumns->getCount())
287cdf0e10cSrcweir 						::dbtools::throwFunctionSequenceException(_xConnection);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 					const ::rtl::OUString sQuote	 = xMetaData->getIdentifierQuoteString();
290cdf0e10cSrcweir 					aSql.append(::rtl::OUString::createFromAscii(" PRIMARY KEY "));
291cdf0e10cSrcweir 					aSql.append(generateColumnNames(xColumns,xMetaData));
292cdf0e10cSrcweir 				}
293cdf0e10cSrcweir 				else if(nKeyType == KeyType::UNIQUE)
294cdf0e10cSrcweir 				{
295cdf0e10cSrcweir 					xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
296cdf0e10cSrcweir 					xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
297cdf0e10cSrcweir 					if(!xColumns.is() || !xColumns->getCount())
298cdf0e10cSrcweir 						::dbtools::throwFunctionSequenceException(_xConnection);
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 					const ::rtl::OUString sQuote	 = xMetaData->getIdentifierQuoteString();
301cdf0e10cSrcweir 					aSql.append(::rtl::OUString::createFromAscii(" UNIQUE "));
302cdf0e10cSrcweir 					aSql.append(generateColumnNames(xColumns,xMetaData));
303cdf0e10cSrcweir 				}
304cdf0e10cSrcweir 				else if(nKeyType == KeyType::FOREIGN)
305cdf0e10cSrcweir 				{
306cdf0e10cSrcweir 					sal_Int32 nDeleteRule	= getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DELETERULE)));
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 					xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
309cdf0e10cSrcweir 					xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
310cdf0e10cSrcweir 					if(!xColumns.is() || !xColumns->getCount())
311cdf0e10cSrcweir 						::dbtools::throwFunctionSequenceException(_xConnection);
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 					aSql.append(::rtl::OUString::createFromAscii(" FOREIGN KEY "));
314cdf0e10cSrcweir 					::rtl::OUString sRefTable = getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)));
315cdf0e10cSrcweir 					::dbtools::qualifiedNameComponents(xMetaData,
316cdf0e10cSrcweir 														sRefTable,
317cdf0e10cSrcweir 														sCatalog,
318cdf0e10cSrcweir 														sSchema,
319cdf0e10cSrcweir 														sTable,
320cdf0e10cSrcweir 														::dbtools::eInDataManipulation);
321cdf0e10cSrcweir 					sComposedName = ::dbtools::composeTableName( xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInTableDefinitions );
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 					if ( !sComposedName.getLength() )
325cdf0e10cSrcweir 						::dbtools::throwFunctionSequenceException(_xConnection);
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 					aSql.append(generateColumnNames(xColumns,xMetaData));
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 					switch(nDeleteRule)
330cdf0e10cSrcweir 					{
331cdf0e10cSrcweir 						case KeyRule::CASCADE:
332cdf0e10cSrcweir 							aSql.append(::rtl::OUString::createFromAscii(" ON DELETE CASCADE "));
333cdf0e10cSrcweir 							break;
334cdf0e10cSrcweir 						case KeyRule::RESTRICT:
335cdf0e10cSrcweir 							aSql.append(::rtl::OUString::createFromAscii(" ON DELETE RESTRICT "));
336cdf0e10cSrcweir 							break;
337cdf0e10cSrcweir 						case KeyRule::SET_NULL:
338cdf0e10cSrcweir 							aSql.append(::rtl::OUString::createFromAscii(" ON DELETE SET NULL "));
339cdf0e10cSrcweir 							break;
340cdf0e10cSrcweir 						case KeyRule::SET_DEFAULT:
341cdf0e10cSrcweir 							aSql.append(::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT "));
342cdf0e10cSrcweir 							break;
343cdf0e10cSrcweir 						default:
344cdf0e10cSrcweir 							;
345cdf0e10cSrcweir 					}
346cdf0e10cSrcweir 				}
347cdf0e10cSrcweir 			}
348cdf0e10cSrcweir 		}
349cdf0e10cSrcweir 	}
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 	if ( aSql.getLength() )
352cdf0e10cSrcweir 	{
353cdf0e10cSrcweir 		if ( aSql.charAt(aSql.getLength()-1) == ',' )
354cdf0e10cSrcweir 			aSql.setCharAt(aSql.getLength()-1,')');
355cdf0e10cSrcweir 		else
356cdf0e10cSrcweir 			aSql.appendAscii(")");
357cdf0e10cSrcweir 	}
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 	return aSql.makeStringAndClear();
360cdf0e10cSrcweir 
361cdf0e10cSrcweir }
362cdf0e10cSrcweir // -----------------------------------------------------------------------------
createSqlCreateTableStatement(const Reference<XPropertySet> & descriptor,const Reference<XConnection> & _xConnection,ISQLStatementHelper * _pHelper,const::rtl::OUString & _sCreatePattern)363cdf0e10cSrcweir ::rtl::OUString createSqlCreateTableStatement(	const Reference< XPropertySet >& descriptor,
364cdf0e10cSrcweir 												const Reference< XConnection>& _xConnection,
365cdf0e10cSrcweir                                                 ISQLStatementHelper* _pHelper,
366cdf0e10cSrcweir                                                 const ::rtl::OUString& _sCreatePattern)
367cdf0e10cSrcweir {
368cdf0e10cSrcweir 	::rtl::OUString aSql = ::dbtools::createStandardCreateStatement(descriptor,_xConnection,_pHelper,_sCreatePattern);
369cdf0e10cSrcweir 	const ::rtl::OUString sKeyStmt = ::dbtools::createStandardKeyStatement(descriptor,_xConnection);
370cdf0e10cSrcweir 	if ( sKeyStmt.getLength() )
371cdf0e10cSrcweir 		aSql += sKeyStmt;
372cdf0e10cSrcweir 	else
373cdf0e10cSrcweir 	{
374cdf0e10cSrcweir 		if ( aSql.lastIndexOf(',') == (aSql.getLength()-1) )
375cdf0e10cSrcweir 			aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
376cdf0e10cSrcweir 		else
377cdf0e10cSrcweir 			aSql += ::rtl::OUString::createFromAscii(")");
378cdf0e10cSrcweir 	}
379cdf0e10cSrcweir 	return aSql;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir namespace
382cdf0e10cSrcweir {
lcl_createSDBCXColumn(const Reference<XNameAccess> & _xPrimaryKeyColumns,const Reference<XConnection> & _xConnection,const Any & _aCatalog,const::rtl::OUString & _aSchema,const::rtl::OUString & _aTable,const::rtl::OUString & _rQueryName,const::rtl::OUString & _rName,sal_Bool _bCase,sal_Bool _bQueryForInfo,sal_Bool _bIsAutoIncrement,sal_Bool _bIsCurrency,sal_Int32 _nDataType)383cdf0e10cSrcweir 	Reference<XPropertySet> lcl_createSDBCXColumn(const Reference<XNameAccess>& _xPrimaryKeyColumns,
384cdf0e10cSrcweir 										  const Reference<XConnection>& _xConnection,
385cdf0e10cSrcweir 										  const Any& _aCatalog,
386cdf0e10cSrcweir 										  const ::rtl::OUString& _aSchema,
387cdf0e10cSrcweir 										  const ::rtl::OUString& _aTable,
388cdf0e10cSrcweir 										  const ::rtl::OUString& _rQueryName,
389cdf0e10cSrcweir 										  const ::rtl::OUString& _rName,
390cdf0e10cSrcweir 										  sal_Bool _bCase,
391cdf0e10cSrcweir 										  sal_Bool _bQueryForInfo,
392cdf0e10cSrcweir 										  sal_Bool _bIsAutoIncrement,
393cdf0e10cSrcweir 										  sal_Bool _bIsCurrency,
394cdf0e10cSrcweir 										  sal_Int32 _nDataType)
395cdf0e10cSrcweir 	{
396cdf0e10cSrcweir 		Reference<XPropertySet> xProp;
397cdf0e10cSrcweir 		Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
398cdf0e10cSrcweir 		Reference< XResultSet > xResult = xMetaData->getColumns(_aCatalog, _aSchema, _aTable, _rQueryName);
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 		if ( xResult.is() )
401cdf0e10cSrcweir 		{
402cdf0e10cSrcweir 			UStringMixEqual aMixCompare(_bCase);
403cdf0e10cSrcweir 			Reference< XRow > xRow(xResult,UNO_QUERY);
404cdf0e10cSrcweir 			while( xResult->next() )
405cdf0e10cSrcweir 			{
406cdf0e10cSrcweir 				if ( aMixCompare(xRow->getString(4),_rName) )
407cdf0e10cSrcweir 				{
408cdf0e10cSrcweir 					sal_Int32		nField5	= xRow->getInt(5);
409cdf0e10cSrcweir 					::rtl::OUString aField6 = xRow->getString(6);
410cdf0e10cSrcweir 					sal_Int32		nField7 = xRow->getInt(7)
411cdf0e10cSrcweir 								,	nField9 = xRow->getInt(9)
412cdf0e10cSrcweir 								,	nField11= xRow->getInt(11);
413cdf0e10cSrcweir 					::rtl::OUString sField12 = xRow->getString(12),
414cdf0e10cSrcweir                                     sField13 = xRow->getString(13);
415cdf0e10cSrcweir 					::comphelper::disposeComponent(xRow);
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 					sal_Bool bAutoIncrement = _bIsAutoIncrement
418cdf0e10cSrcweir 							,bIsCurrency	= _bIsCurrency;
419cdf0e10cSrcweir 					if ( _bQueryForInfo )
420cdf0e10cSrcweir 					{
421cdf0e10cSrcweir 						const ::rtl::OUString sQuote = xMetaData->getIdentifierQuoteString();
422cdf0e10cSrcweir 						::rtl::OUString sQuotedName  = ::dbtools::quoteName(sQuote,_rName);
423cdf0e10cSrcweir 						::rtl::OUString sComposedName;
424cdf0e10cSrcweir 						sComposedName = composeTableNameForSelect(_xConnection, getString( _aCatalog ), _aSchema, _aTable );
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 						ColumnInformationMap aInfo(_bCase);
427cdf0e10cSrcweir 						collectColumnInformation(_xConnection,sComposedName,sQuotedName,aInfo);
428cdf0e10cSrcweir 						ColumnInformationMap::iterator aIter = aInfo.begin();
429cdf0e10cSrcweir 						if ( aIter != aInfo.end() )
430cdf0e10cSrcweir 						{
431cdf0e10cSrcweir 							bAutoIncrement	= aIter->second.first.first;
432cdf0e10cSrcweir 							bIsCurrency		= aIter->second.first.second;
433cdf0e10cSrcweir 							if ( DataType::OTHER == nField5 )
434cdf0e10cSrcweir 								nField5		= aIter->second.second;
435cdf0e10cSrcweir 						}
436cdf0e10cSrcweir 					}
437cdf0e10cSrcweir 					else if ( DataType::OTHER == nField5 )
438cdf0e10cSrcweir 						nField5 = _nDataType;
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 					if ( nField11 != ColumnValue::NO_NULLS )
441cdf0e10cSrcweir 					{
442cdf0e10cSrcweir 						try
443cdf0e10cSrcweir 						{
444cdf0e10cSrcweir                             if ( _xPrimaryKeyColumns.is() )
445cdf0e10cSrcweir                             {
446cdf0e10cSrcweir                                 if ( _xPrimaryKeyColumns->hasByName(_rName) )
447cdf0e10cSrcweir                                     nField11 = ColumnValue::NO_NULLS;
448cdf0e10cSrcweir 
449cdf0e10cSrcweir                             }
450cdf0e10cSrcweir                             else
451cdf0e10cSrcweir                             {
452cdf0e10cSrcweir 							    Reference< XResultSet > xPKeys = xMetaData->getPrimaryKeys( _aCatalog, _aSchema, _aTable );
453cdf0e10cSrcweir                                 Reference< XRow > xPKeyRow( xPKeys, UNO_QUERY_THROW );
454cdf0e10cSrcweir                                 while( xPKeys->next() ) // there can be only one primary key
455cdf0e10cSrcweir 							    {
456cdf0e10cSrcweir                                     ::rtl::OUString sKeyColumn = xPKeyRow->getString(4);
457cdf0e10cSrcweir 								    if ( aMixCompare(_rName,sKeyColumn) )
458cdf0e10cSrcweir 								    {
459cdf0e10cSrcweir 									    nField11 = ColumnValue::NO_NULLS;
460cdf0e10cSrcweir 									    break;
461cdf0e10cSrcweir 								    }
462cdf0e10cSrcweir 							    }
463cdf0e10cSrcweir 							}
464cdf0e10cSrcweir                         }
465cdf0e10cSrcweir 						catch(SQLException&)
466cdf0e10cSrcweir 						{
467cdf0e10cSrcweir                             OSL_ENSURE( false, "lcl_createSDBCXColumn: caught an exception!" );
468cdf0e10cSrcweir 						}
469cdf0e10cSrcweir 					}
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 					connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
472cdf0e10cSrcweir 												aField6,
473cdf0e10cSrcweir 												sField13,
474cdf0e10cSrcweir                                                 sField12,
475cdf0e10cSrcweir 												nField11,
476cdf0e10cSrcweir 												nField7,
477cdf0e10cSrcweir 												nField9,
478cdf0e10cSrcweir 												nField5,
479cdf0e10cSrcweir 												bAutoIncrement,
480cdf0e10cSrcweir 												sal_False,
481cdf0e10cSrcweir 												bIsCurrency,
482cdf0e10cSrcweir 												_bCase);
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 					xProp = pRet;
485cdf0e10cSrcweir 					break;
486cdf0e10cSrcweir 				}
487cdf0e10cSrcweir 			}
488cdf0e10cSrcweir 		}
489cdf0e10cSrcweir 
490cdf0e10cSrcweir 		return xProp;
491cdf0e10cSrcweir 	}
492cdf0e10cSrcweir 	//------------------------------------------------------------------
lcl_getXModel(const Reference<XInterface> & _xIface)493cdf0e10cSrcweir 	Reference< XModel> lcl_getXModel(const Reference< XInterface>& _xIface)
494cdf0e10cSrcweir 	{
495cdf0e10cSrcweir 		Reference< XInterface > xParent = _xIface;
496cdf0e10cSrcweir 		Reference< XModel > xModel(xParent,UNO_QUERY);;
497cdf0e10cSrcweir 		while( xParent.is() && !xModel.is() )
498cdf0e10cSrcweir 		{
499cdf0e10cSrcweir 			Reference<XChild> xChild(xParent,UNO_QUERY);
500cdf0e10cSrcweir 			xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
501cdf0e10cSrcweir 			xModel.set(xParent,UNO_QUERY);
502cdf0e10cSrcweir 		}
503cdf0e10cSrcweir 		return xModel;
504cdf0e10cSrcweir 	}
505cdf0e10cSrcweir }
506cdf0e10cSrcweir // -----------------------------------------------------------------------------
createSDBCXColumn(const Reference<XPropertySet> & _xTable,const Reference<XConnection> & _xConnection,const::rtl::OUString & _rName,sal_Bool _bCase,sal_Bool _bQueryForInfo,sal_Bool _bIsAutoIncrement,sal_Bool _bIsCurrency,sal_Int32 _nDataType)507cdf0e10cSrcweir Reference<XPropertySet> createSDBCXColumn(const Reference<XPropertySet>& _xTable,
508cdf0e10cSrcweir 										  const Reference<XConnection>& _xConnection,
509cdf0e10cSrcweir 										  const ::rtl::OUString& _rName,
510cdf0e10cSrcweir 										  sal_Bool _bCase,
511cdf0e10cSrcweir 										  sal_Bool _bQueryForInfo,
512cdf0e10cSrcweir 										  sal_Bool _bIsAutoIncrement,
513cdf0e10cSrcweir 										  sal_Bool _bIsCurrency,
514cdf0e10cSrcweir 										  sal_Int32 _nDataType)
515cdf0e10cSrcweir {
516cdf0e10cSrcweir 	Reference<XPropertySet> xProp;
517cdf0e10cSrcweir 	OSL_ENSURE(_xTable.is(),"Table is NULL!");
518cdf0e10cSrcweir 	if ( !_xTable.is() )
519cdf0e10cSrcweir 		return xProp;
520cdf0e10cSrcweir 
521cdf0e10cSrcweir 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
522cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
523cdf0e10cSrcweir 	Any aCatalog;
524cdf0e10cSrcweir 	aCatalog = _xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 	::rtl::OUString aSchema, aTable;
527cdf0e10cSrcweir 	_xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME))	>>= aSchema;
528cdf0e10cSrcweir 	_xTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))		>>= aTable;
529cdf0e10cSrcweir 
530cdf0e10cSrcweir     Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(_xTable);
531cdf0e10cSrcweir 
532cdf0e10cSrcweir 	xProp = lcl_createSDBCXColumn(xPrimaryKeyColumns,_xConnection,aCatalog, aSchema, aTable, _rName,_rName,_bCase,_bQueryForInfo,_bIsAutoIncrement,_bIsCurrency,_nDataType);
533cdf0e10cSrcweir 	if ( !xProp.is() )
534cdf0e10cSrcweir 	{
535cdf0e10cSrcweir 		xProp = lcl_createSDBCXColumn(xPrimaryKeyColumns,_xConnection,aCatalog, aSchema, aTable, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")),_rName,_bCase,_bQueryForInfo,_bIsAutoIncrement,_bIsCurrency,_nDataType);
536cdf0e10cSrcweir 		if ( !xProp.is() )
537cdf0e10cSrcweir 			xProp = new connectivity::sdbcx::OColumn(_rName,
538cdf0e10cSrcweir 												::rtl::OUString(),::rtl::OUString(),::rtl::OUString(),
539cdf0e10cSrcweir 												ColumnValue::NULLABLE_UNKNOWN,
540cdf0e10cSrcweir 												0,
541cdf0e10cSrcweir 												0,
542cdf0e10cSrcweir 												DataType::VARCHAR,
543cdf0e10cSrcweir 												_bIsAutoIncrement,
544cdf0e10cSrcweir 												sal_False,
545cdf0e10cSrcweir 												_bIsCurrency,
546cdf0e10cSrcweir 												_bCase);
547cdf0e10cSrcweir 
548cdf0e10cSrcweir 	}
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 	return xProp;
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir // -----------------------------------------------------------------------------
getBooleanDataSourceSetting(const Reference<XConnection> & _rxConnection,const sal_Char * _pAsciiSettingName)554cdf0e10cSrcweir bool getBooleanDataSourceSetting( const Reference< XConnection >& _rxConnection, const sal_Char* _pAsciiSettingName )
555cdf0e10cSrcweir {
556cdf0e10cSrcweir     bool bValue( false );
557cdf0e10cSrcweir     try
558cdf0e10cSrcweir     {
559cdf0e10cSrcweir 		Reference< XPropertySet> xDataSourceProperties( findDataSource( _rxConnection ), UNO_QUERY );
560cdf0e10cSrcweir         OSL_ENSURE( xDataSourceProperties.is(), "::dbtools::getBooleanDataSourceSetting: somebody is using this with a non-SDB-level connection!" );
561cdf0e10cSrcweir 		if ( xDataSourceProperties.is() )
562cdf0e10cSrcweir 		{
563cdf0e10cSrcweir 			Reference< XPropertySet > xSettings(
564cdf0e10cSrcweir 			    xDataSourceProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Settings") ) ),
565cdf0e10cSrcweir                 UNO_QUERY_THROW
566cdf0e10cSrcweir             );
567cdf0e10cSrcweir             OSL_VERIFY( xSettings->getPropertyValue( ::rtl::OUString::createFromAscii( _pAsciiSettingName ) ) >>= bValue );
568cdf0e10cSrcweir 		}
569cdf0e10cSrcweir     }
570cdf0e10cSrcweir     catch( const Exception& )
571cdf0e10cSrcweir     {
572cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
573cdf0e10cSrcweir     }
574cdf0e10cSrcweir     return bValue;
575cdf0e10cSrcweir }
576cdf0e10cSrcweir // -------------------------------------------------------------------------
getDataSourceSetting(const Reference<XInterface> & _xChild,const::rtl::OUString & _sAsciiSettingsName,Any & _rSettingsValue)577cdf0e10cSrcweir bool getDataSourceSetting( const Reference< XInterface >& _xChild, const ::rtl::OUString& _sAsciiSettingsName,
578cdf0e10cSrcweir     Any& /* [out] */ _rSettingsValue )
579cdf0e10cSrcweir {
580cdf0e10cSrcweir     bool bIsPresent = false;
581cdf0e10cSrcweir     try
582cdf0e10cSrcweir     {
583cdf0e10cSrcweir         const Reference< XPropertySet> xDataSourceProperties( findDataSource( _xChild ), UNO_QUERY );
584cdf0e10cSrcweir         if ( !xDataSourceProperties.is() )
585cdf0e10cSrcweir             return false;
586cdf0e10cSrcweir 
587cdf0e10cSrcweir         const Reference< XPropertySet > xSettings(
588cdf0e10cSrcweir 			    xDataSourceProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Settings") ) ),
589cdf0e10cSrcweir                 UNO_QUERY_THROW
590cdf0e10cSrcweir             );
591cdf0e10cSrcweir 
592cdf0e10cSrcweir         _rSettingsValue = xSettings->getPropertyValue( _sAsciiSettingsName );
593cdf0e10cSrcweir         bIsPresent = true;
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir     catch( const Exception& )
596cdf0e10cSrcweir     {
597cdf0e10cSrcweir 	    bIsPresent = false;
598cdf0e10cSrcweir     }
599cdf0e10cSrcweir     return bIsPresent;
600cdf0e10cSrcweir }
601cdf0e10cSrcweir // -------------------------------------------------------------------------
getDataSourceSetting(const Reference<XInterface> & _rxDataSource,const sal_Char * _pAsciiSettingsName,Any & _rSettingsValue)602cdf0e10cSrcweir bool getDataSourceSetting( const Reference< XInterface >& _rxDataSource, const sal_Char* _pAsciiSettingsName,
603cdf0e10cSrcweir     Any& /* [out] */ _rSettingsValue )
604cdf0e10cSrcweir {
605cdf0e10cSrcweir     ::rtl::OUString sAsciiSettingsName = ::rtl::OUString::createFromAscii(_pAsciiSettingsName);
606cdf0e10cSrcweir     return getDataSourceSetting( _rxDataSource, sAsciiSettingsName,_rSettingsValue );
607cdf0e10cSrcweir }
608cdf0e10cSrcweir // -----------------------------------------------------------------------------
isDataSourcePropertyEnabled(const Reference<XInterface> & _xProp,const::rtl::OUString & _sProperty,sal_Bool _bDefault)609cdf0e10cSrcweir sal_Bool isDataSourcePropertyEnabled(const Reference<XInterface>& _xProp,const ::rtl::OUString& _sProperty,sal_Bool _bDefault)
610cdf0e10cSrcweir {
611cdf0e10cSrcweir 	sal_Bool bEnabled = _bDefault;
612cdf0e10cSrcweir 	try
613cdf0e10cSrcweir 	{
614cdf0e10cSrcweir 		Reference< XPropertySet> xProp(findDataSource(_xProp),UNO_QUERY);
615cdf0e10cSrcweir 		if ( xProp.is() )
616cdf0e10cSrcweir 		{
617cdf0e10cSrcweir 			Sequence< PropertyValue > aInfo;
618cdf0e10cSrcweir 			xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Info"))) >>= aInfo;
619cdf0e10cSrcweir 			const PropertyValue* pValue =::std::find_if(aInfo.getConstArray(),
620cdf0e10cSrcweir 												aInfo.getConstArray() + aInfo.getLength(),
621cdf0e10cSrcweir 												::std::bind2nd(TPropertyValueEqualFunctor(),_sProperty));
622cdf0e10cSrcweir 			if ( pValue && pValue != (aInfo.getConstArray() + aInfo.getLength()) )
623cdf0e10cSrcweir 				pValue->Value >>= bEnabled;
624cdf0e10cSrcweir 		}
625cdf0e10cSrcweir 	}
626cdf0e10cSrcweir 	catch(SQLException&)
627cdf0e10cSrcweir 	{
628cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
629cdf0e10cSrcweir 	}
630cdf0e10cSrcweir 	return bEnabled;
631cdf0e10cSrcweir }
632cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDataDefinitionByURLAndConnection(const::rtl::OUString & _rsUrl,const Reference<XConnection> & _xConnection,const Reference<XMultiServiceFactory> & _rxFactory)633cdf0e10cSrcweir Reference< XTablesSupplier> getDataDefinitionByURLAndConnection(
634cdf0e10cSrcweir 			const ::rtl::OUString& _rsUrl,
635cdf0e10cSrcweir 			const Reference< XConnection>& _xConnection,
636cdf0e10cSrcweir 			const Reference< XMultiServiceFactory>& _rxFactory)
637cdf0e10cSrcweir {
638cdf0e10cSrcweir 	Reference< XTablesSupplier> xTablesSup;
639cdf0e10cSrcweir     try
640cdf0e10cSrcweir     {
641cdf0e10cSrcweir 	    Reference< XDriverAccess> xManager(
642cdf0e10cSrcweir             _rxFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.sdbc.DriverManager") ),
643cdf0e10cSrcweir             UNO_QUERY_THROW );
644cdf0e10cSrcweir 	    Reference< XDataDefinitionSupplier > xSupp( xManager->getDriverByURL( _rsUrl ), UNO_QUERY );
645cdf0e10cSrcweir 
646cdf0e10cSrcweir 	    if ( xSupp.is() )
647cdf0e10cSrcweir         {
648cdf0e10cSrcweir 		    xTablesSup = xSupp->getDataDefinitionByConnection( _xConnection );
649cdf0e10cSrcweir             OSL_ENSURE(xTablesSup.is(),"No table supplier!");
650cdf0e10cSrcweir         }
651cdf0e10cSrcweir     }
652cdf0e10cSrcweir     catch( const Exception& )
653cdf0e10cSrcweir     {
654cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
655cdf0e10cSrcweir     }
656cdf0e10cSrcweir 	return xTablesSup;
657cdf0e10cSrcweir }
658cdf0e10cSrcweir 
659cdf0e10cSrcweir // -----------------------------------------------------------------------------
getTablePrivileges(const Reference<XDatabaseMetaData> & _xMetaData,const::rtl::OUString & _sCatalog,const::rtl::OUString & _sSchema,const::rtl::OUString & _sTable)660cdf0e10cSrcweir sal_Int32 getTablePrivileges(const Reference< XDatabaseMetaData>& _xMetaData,
661cdf0e10cSrcweir 							 const ::rtl::OUString& _sCatalog,
662cdf0e10cSrcweir 							 const ::rtl::OUString& _sSchema,
663cdf0e10cSrcweir 							 const ::rtl::OUString& _sTable)
664cdf0e10cSrcweir {
665cdf0e10cSrcweir 	OSL_ENSURE(_xMetaData.is(),"Invalid metadata!");
666cdf0e10cSrcweir 	sal_Int32 nPrivileges = 0;
667cdf0e10cSrcweir 	try
668cdf0e10cSrcweir 	{
669cdf0e10cSrcweir 		Any aVal;
670cdf0e10cSrcweir 		if(_sCatalog.getLength())
671cdf0e10cSrcweir 			aVal <<= _sCatalog;
672cdf0e10cSrcweir 		Reference< XResultSet > xPrivileges = _xMetaData->getTablePrivileges(aVal, _sSchema, _sTable);
673cdf0e10cSrcweir 		Reference< XRow > xCurrentRow(xPrivileges, UNO_QUERY);
674cdf0e10cSrcweir 
675cdf0e10cSrcweir 		if ( xCurrentRow.is() )
676cdf0e10cSrcweir 		{
677cdf0e10cSrcweir 			::rtl::OUString sUserWorkingFor = _xMetaData->getUserName();
678cdf0e10cSrcweir 			static const ::rtl::OUString sSELECT	= ::rtl::OUString::createFromAscii("SELECT");
679cdf0e10cSrcweir 			static const ::rtl::OUString sINSERT	= ::rtl::OUString::createFromAscii("INSERT");
680cdf0e10cSrcweir 			static const ::rtl::OUString sUPDATE	= ::rtl::OUString::createFromAscii("UPDATE");
681cdf0e10cSrcweir 			static const ::rtl::OUString sDELETE	= ::rtl::OUString::createFromAscii("DELETE");
682cdf0e10cSrcweir 			static const ::rtl::OUString sREAD		= ::rtl::OUString::createFromAscii("READ");
683cdf0e10cSrcweir 			static const ::rtl::OUString sCREATE	= ::rtl::OUString::createFromAscii("CREATE");
684cdf0e10cSrcweir 			static const ::rtl::OUString sALTER		= ::rtl::OUString::createFromAscii("ALTER");
685cdf0e10cSrcweir 			static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
686cdf0e10cSrcweir 			static const ::rtl::OUString sDROP		= ::rtl::OUString::createFromAscii("DROP");
687cdf0e10cSrcweir 			// after creation the set is positioned before the first record, per definitionem
688cdf0e10cSrcweir #ifdef DBG_UTIL
689cdf0e10cSrcweir 			Reference< XResultSetMetaDataSupplier > xSup(xPrivileges,UNO_QUERY);
690cdf0e10cSrcweir 			if ( xSup.is() )
691cdf0e10cSrcweir 			{
692cdf0e10cSrcweir 				Reference< XResultSetMetaData > xRsMetaData = xSup->getMetaData();
693cdf0e10cSrcweir 				if ( xRsMetaData.is() )
694cdf0e10cSrcweir 				{
695cdf0e10cSrcweir 					sal_Int32 nCount = xRsMetaData->getColumnCount();
696cdf0e10cSrcweir 					for (sal_Int32 i=1; i<=nCount; ++i)
697cdf0e10cSrcweir 					{
698cdf0e10cSrcweir 						::rtl::OUString sColumnName = xRsMetaData->getColumnName(i);
699cdf0e10cSrcweir 					}
700cdf0e10cSrcweir 				}
701cdf0e10cSrcweir 			}
702cdf0e10cSrcweir #endif
703cdf0e10cSrcweir 
704cdf0e10cSrcweir 			::rtl::OUString sPrivilege, sGrantee;
705cdf0e10cSrcweir 			while ( xPrivileges->next() )
706cdf0e10cSrcweir 			{
707cdf0e10cSrcweir #ifdef DBG_UTIL
708cdf0e10cSrcweir 				::rtl::OUString sCat, sSchema, sName, sGrantor, sGrantable;
709cdf0e10cSrcweir 				sCat		= xCurrentRow->getString(1);
710cdf0e10cSrcweir 				sSchema		= xCurrentRow->getString(2);
711cdf0e10cSrcweir 				sName		= xCurrentRow->getString(3);
712cdf0e10cSrcweir 				sGrantor	= xCurrentRow->getString(4);
713cdf0e10cSrcweir #endif
714cdf0e10cSrcweir 				sGrantee	= xCurrentRow->getString(5);
715cdf0e10cSrcweir 				sPrivilege	= xCurrentRow->getString(6);
716cdf0e10cSrcweir #ifdef DBG_UTIL
717cdf0e10cSrcweir 				sGrantable	= xCurrentRow->getString(7);
718cdf0e10cSrcweir #endif
719cdf0e10cSrcweir 
720cdf0e10cSrcweir 				if (!sUserWorkingFor.equalsIgnoreAsciiCase(sGrantee))
721cdf0e10cSrcweir 					continue;
722cdf0e10cSrcweir 
723cdf0e10cSrcweir 				if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
724cdf0e10cSrcweir 					nPrivileges |= Privilege::SELECT;
725cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
726cdf0e10cSrcweir 					nPrivileges |= Privilege::INSERT;
727cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
728cdf0e10cSrcweir 					nPrivileges |= Privilege::UPDATE;
729cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
730cdf0e10cSrcweir 					nPrivileges |= Privilege::DELETE;
731cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
732cdf0e10cSrcweir 					nPrivileges |= Privilege::READ;
733cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
734cdf0e10cSrcweir 					nPrivileges |= Privilege::CREATE;
735cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
736cdf0e10cSrcweir 					nPrivileges |= Privilege::ALTER;
737cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
738cdf0e10cSrcweir 					nPrivileges |= Privilege::REFERENCE;
739cdf0e10cSrcweir 				else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
740cdf0e10cSrcweir 					nPrivileges |= Privilege::DROP;
741cdf0e10cSrcweir 			}
742cdf0e10cSrcweir 		}
743cdf0e10cSrcweir 		disposeComponent(xPrivileges);
744cdf0e10cSrcweir 	}
745cdf0e10cSrcweir 	catch(const SQLException& e)
746cdf0e10cSrcweir 	{
747cdf0e10cSrcweir 		static ::rtl::OUString sNotSupportedState = ::rtl::OUString::createFromAscii("IM001");
748cdf0e10cSrcweir 		// some drivers don't support any privileges so we assume that we are allowed to do all we want :-)
749cdf0e10cSrcweir 		if(e.SQLState == sNotSupportedState)
750cdf0e10cSrcweir 			nPrivileges |=	Privilege::DROP			|
751cdf0e10cSrcweir 							Privilege::REFERENCE	|
752cdf0e10cSrcweir 							Privilege::ALTER		|
753cdf0e10cSrcweir 							Privilege::CREATE		|
754cdf0e10cSrcweir 							Privilege::READ			|
755cdf0e10cSrcweir 							Privilege::DELETE		|
756cdf0e10cSrcweir 							Privilege::UPDATE		|
757cdf0e10cSrcweir 							Privilege::INSERT		|
758cdf0e10cSrcweir 							Privilege::SELECT;
759cdf0e10cSrcweir 		else
760cdf0e10cSrcweir 			OSL_ENSURE(0,"Could not collect the privileges !");
761cdf0e10cSrcweir 	}
762cdf0e10cSrcweir 	return nPrivileges;
763cdf0e10cSrcweir }
764cdf0e10cSrcweir // -----------------------------------------------------------------------------
765cdf0e10cSrcweir // we need some more information about the column
collectColumnInformation(const Reference<XConnection> & _xConnection,const::rtl::OUString & _sComposedName,const::rtl::OUString & _rName,ColumnInformationMap & _rInfo)766cdf0e10cSrcweir void collectColumnInformation(const Reference< XConnection>& _xConnection,
767cdf0e10cSrcweir 							  const ::rtl::OUString& _sComposedName,
768cdf0e10cSrcweir 							  const ::rtl::OUString& _rName,
769cdf0e10cSrcweir 							  ColumnInformationMap& _rInfo)
770cdf0e10cSrcweir {
771cdf0e10cSrcweir 	static ::rtl::OUString STR_WHERE = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" WHERE "));
772cdf0e10cSrcweir 
773cdf0e10cSrcweir 	::rtl::OUString sSelect = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SELECT "));
774cdf0e10cSrcweir 	sSelect += _rName;
775cdf0e10cSrcweir 	sSelect += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" FROM "));
776cdf0e10cSrcweir 	sSelect += _sComposedName;
777cdf0e10cSrcweir 	sSelect += STR_WHERE;
778cdf0e10cSrcweir 	sSelect += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0 = 1"));
779cdf0e10cSrcweir 
780cdf0e10cSrcweir 	try
781cdf0e10cSrcweir 	{
782cdf0e10cSrcweir         ::utl::SharedUNOComponent< XStatement > xStmt( _xConnection->createStatement() );
783cdf0e10cSrcweir         Reference< XPropertySet > xStatementProps( xStmt, UNO_QUERY_THROW );
784cdf0e10cSrcweir         xStatementProps->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ), makeAny( (sal_Bool)sal_False ) );
785cdf0e10cSrcweir 		Reference< XResultSet > xResult( xStmt->executeQuery( sSelect ), UNO_QUERY_THROW );
786cdf0e10cSrcweir         Reference< XResultSetMetaDataSupplier > xSuppMeta( xResult, UNO_QUERY_THROW );
787cdf0e10cSrcweir 		Reference< XResultSetMetaData > xMeta( xSuppMeta->getMetaData(), UNO_QUERY_THROW );
788cdf0e10cSrcweir 
789cdf0e10cSrcweir         sal_Int32 nCount = xMeta->getColumnCount();
790cdf0e10cSrcweir         OSL_ENSURE( nCount != 0, "::dbtools::collectColumnInformation: result set has empty (column-less) meta data!" );
791cdf0e10cSrcweir 		for (sal_Int32 i=1; i <= nCount ; ++i)
792cdf0e10cSrcweir 		{
793cdf0e10cSrcweir 			_rInfo.insert(ColumnInformationMap::value_type(xMeta->getColumnName(i),
794cdf0e10cSrcweir 				ColumnInformation(TBoolPair(xMeta->isAutoIncrement(i),xMeta->isCurrency(i)),xMeta->getColumnType(i))));
795cdf0e10cSrcweir 		}
796cdf0e10cSrcweir 	}
797cdf0e10cSrcweir     catch( const Exception& )
798cdf0e10cSrcweir     {
799cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
800cdf0e10cSrcweir     }
801cdf0e10cSrcweir }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir // -----------------------------------------------------------------------------
isEmbeddedInDatabase(const Reference<XInterface> & _rxComponent,Reference<XConnection> & _rxActualConnection)804cdf0e10cSrcweir bool isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent, Reference< XConnection >& _rxActualConnection )
805cdf0e10cSrcweir {
806cdf0e10cSrcweir     bool bIsEmbedded = false;
807cdf0e10cSrcweir 	try
808cdf0e10cSrcweir 	{
809cdf0e10cSrcweir 		Reference< XModel > xModel = lcl_getXModel( _rxComponent );
810cdf0e10cSrcweir 
811cdf0e10cSrcweir 		if ( xModel.is() )
812cdf0e10cSrcweir 		{
813cdf0e10cSrcweir 			Sequence< PropertyValue > aArgs = xModel->getArgs();
814cdf0e10cSrcweir 			const PropertyValue* pIter = aArgs.getConstArray();
815cdf0e10cSrcweir 			const PropertyValue* pEnd  = pIter + aArgs.getLength();
816cdf0e10cSrcweir 			for(;pIter != pEnd;++pIter)
817cdf0e10cSrcweir 			{
818cdf0e10cSrcweir 				if ( pIter->Name.equalsAscii("ComponentData") )
819cdf0e10cSrcweir 				{
820cdf0e10cSrcweir 					Sequence<PropertyValue> aDocumentContext;
821cdf0e10cSrcweir 					pIter->Value >>= aDocumentContext;
822cdf0e10cSrcweir 					const PropertyValue* pContextIter = aDocumentContext.getConstArray();
823cdf0e10cSrcweir 					const PropertyValue* pContextEnd  = pContextIter + aDocumentContext.getLength();
824cdf0e10cSrcweir 					for(;pContextIter != pContextEnd;++pContextIter)
825cdf0e10cSrcweir 					{
826cdf0e10cSrcweir 						if (  pContextIter->Name.equalsAscii( "ActiveConnection" )
827cdf0e10cSrcweir                         && ( pContextIter->Value >>= _rxActualConnection )
828cdf0e10cSrcweir                         )
829cdf0e10cSrcweir 						{
830cdf0e10cSrcweir                             bIsEmbedded = true;
831cdf0e10cSrcweir 							break;
832cdf0e10cSrcweir 						}
833cdf0e10cSrcweir 					}
834cdf0e10cSrcweir 					break;
835cdf0e10cSrcweir 				}
836cdf0e10cSrcweir 			}
837cdf0e10cSrcweir 		}
838cdf0e10cSrcweir 	}
839cdf0e10cSrcweir 	catch(Exception&)
840cdf0e10cSrcweir 	{
841cdf0e10cSrcweir 		// not intereseted in
842cdf0e10cSrcweir 	}
843cdf0e10cSrcweir     return bIsEmbedded;
844cdf0e10cSrcweir }
845cdf0e10cSrcweir // -----------------------------------------------------------------------------
846cdf0e10cSrcweir namespace
847cdf0e10cSrcweir {
lcl_getEncodingName(rtl_TextEncoding _eEncoding)848cdf0e10cSrcweir     ::rtl::OUString lcl_getEncodingName( rtl_TextEncoding _eEncoding )
849cdf0e10cSrcweir     {
850cdf0e10cSrcweir         ::rtl::OUString sEncodingName;
851cdf0e10cSrcweir 
852cdf0e10cSrcweir         OCharsetMap aCharsets;
853cdf0e10cSrcweir         OCharsetMap::CharsetIterator aEncodingPos = aCharsets.find( _eEncoding );
854cdf0e10cSrcweir         OSL_ENSURE( aEncodingPos != aCharsets.end(), "lcl_getEncodingName: *which* encoding?" );
855cdf0e10cSrcweir         if ( aEncodingPos != aCharsets.end() )
856cdf0e10cSrcweir             sEncodingName = (*aEncodingPos).getIanaName();
857cdf0e10cSrcweir 
858cdf0e10cSrcweir         return sEncodingName;
859cdf0e10cSrcweir     }
860cdf0e10cSrcweir }
861cdf0e10cSrcweir 
862cdf0e10cSrcweir // -----------------------------------------------------------------------------
convertUnicodeString(const::rtl::OUString & _rSource,::rtl::OString & _rDest,rtl_TextEncoding _eEncoding)863cdf0e10cSrcweir sal_Int32 DBTypeConversion::convertUnicodeString( const ::rtl::OUString& _rSource, ::rtl::OString& _rDest, rtl_TextEncoding _eEncoding ) SAL_THROW((com::sun::star::sdbc::SQLException))
864cdf0e10cSrcweir {
865cdf0e10cSrcweir     if ( !rtl_convertUStringToString( &_rDest.pData, _rSource.getStr(), _rSource.getLength(),
866cdf0e10cSrcweir             _eEncoding,
867cdf0e10cSrcweir             RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
868cdf0e10cSrcweir             RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE |
869cdf0e10cSrcweir             RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 |
870cdf0e10cSrcweir             RTL_UNICODETOTEXT_FLAGS_NOCOMPOSITE )
871cdf0e10cSrcweir         )
872cdf0e10cSrcweir     {
873cdf0e10cSrcweir         SharedResources aResources;
874cdf0e10cSrcweir         ::rtl::OUString sMessage = aResources.getResourceStringWithSubstitution( STR_CANNOT_CONVERT_STRING,
875cdf0e10cSrcweir             "$string$", _rSource,
876cdf0e10cSrcweir             "$charset$",  lcl_getEncodingName( _eEncoding )
877cdf0e10cSrcweir         );
878cdf0e10cSrcweir 
879cdf0e10cSrcweir         throw SQLException(
880cdf0e10cSrcweir             sMessage,
881cdf0e10cSrcweir             NULL,
882cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "22018" ) ),
883cdf0e10cSrcweir             22018,
884cdf0e10cSrcweir             Any()
885cdf0e10cSrcweir         );
886cdf0e10cSrcweir     }
887cdf0e10cSrcweir 
888cdf0e10cSrcweir     return _rDest.getLength();
889cdf0e10cSrcweir }
890cdf0e10cSrcweir 
891cdf0e10cSrcweir // -----------------------------------------------------------------------------
convertUnicodeStringToLength(const::rtl::OUString & _rSource,::rtl::OString & _rDest,sal_Int32 _nMaxLen,rtl_TextEncoding _eEncoding)892cdf0e10cSrcweir sal_Int32 DBTypeConversion::convertUnicodeStringToLength( const ::rtl::OUString& _rSource, ::rtl::OString&  _rDest,
893cdf0e10cSrcweir    sal_Int32 _nMaxLen, rtl_TextEncoding _eEncoding ) SAL_THROW((SQLException))
894cdf0e10cSrcweir {
895cdf0e10cSrcweir     sal_Int32 nLen = convertUnicodeString( _rSource, _rDest, _eEncoding );
896cdf0e10cSrcweir     if ( nLen > _nMaxLen )
897cdf0e10cSrcweir     {
898cdf0e10cSrcweir         SharedResources aResources;
899cdf0e10cSrcweir         ::rtl::OUString sMessage = aResources.getResourceStringWithSubstitution( STR_STRING_LENGTH_EXCEEDED,
900cdf0e10cSrcweir             "$string$", _rSource,
901cdf0e10cSrcweir             "$maxlen$", ::rtl::OUString::valueOf( _nMaxLen ),
902cdf0e10cSrcweir             "$charset$", lcl_getEncodingName( _eEncoding )
903cdf0e10cSrcweir         );
904cdf0e10cSrcweir 
905cdf0e10cSrcweir         throw SQLException(
906cdf0e10cSrcweir             sMessage,
907cdf0e10cSrcweir             NULL,
908cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "22001" ) ),
909cdf0e10cSrcweir             22001,
910cdf0e10cSrcweir             Any()
911cdf0e10cSrcweir         );
912cdf0e10cSrcweir     }
913cdf0e10cSrcweir 
914cdf0e10cSrcweir    return nLen;
915cdf0e10cSrcweir }
lcl_getReportEngines()916cdf0e10cSrcweir ::rtl::OUString lcl_getReportEngines()
917cdf0e10cSrcweir {
918cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.DataAccess/ReportEngines"));
919cdf0e10cSrcweir 	return s_sNodeName;
920cdf0e10cSrcweir }
921cdf0e10cSrcweir // -----------------------------------------------------------------------------
lcl_getDefaultReportEngine()922cdf0e10cSrcweir ::rtl::OUString lcl_getDefaultReportEngine()
923cdf0e10cSrcweir {
924cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("DefaultReportEngine"));
925cdf0e10cSrcweir 	return s_sNodeName;
926cdf0e10cSrcweir }
927cdf0e10cSrcweir // -----------------------------------------------------------------------------
lcl_getReportEngineNames()928cdf0e10cSrcweir ::rtl::OUString lcl_getReportEngineNames()
929cdf0e10cSrcweir {
930cdf0e10cSrcweir 	static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("ReportEngineNames"));
931cdf0e10cSrcweir 	return s_sNodeName;
932cdf0e10cSrcweir }
933cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDefaultReportEngineServiceName(const Reference<XMultiServiceFactory> & _rxORB)934cdf0e10cSrcweir ::rtl::OUString getDefaultReportEngineServiceName(const Reference< XMultiServiceFactory >& _rxORB)
935cdf0e10cSrcweir {
936cdf0e10cSrcweir 	::utl::OConfigurationTreeRoot aReportEngines = ::utl::OConfigurationTreeRoot::createWithServiceFactory(
937cdf0e10cSrcweir 		_rxORB, lcl_getReportEngines(), -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
938cdf0e10cSrcweir 
939cdf0e10cSrcweir 	if ( aReportEngines.isValid() )
940cdf0e10cSrcweir 	{
941cdf0e10cSrcweir 		::rtl::OUString sDefaultReportEngineName;
942cdf0e10cSrcweir 		aReportEngines.getNodeValue(lcl_getDefaultReportEngine()) >>= sDefaultReportEngineName;
943cdf0e10cSrcweir         if ( sDefaultReportEngineName.getLength() )
944cdf0e10cSrcweir         {
945cdf0e10cSrcweir 		    ::utl::OConfigurationNode aReportEngineNames = aReportEngines.openNode(lcl_getReportEngineNames());
946cdf0e10cSrcweir 		    if ( aReportEngineNames.isValid() )
947cdf0e10cSrcweir 		    {
948cdf0e10cSrcweir 			    ::utl::OConfigurationNode aReportEngine = aReportEngineNames.openNode(sDefaultReportEngineName);
949cdf0e10cSrcweir 			    if ( aReportEngine.isValid() )
950cdf0e10cSrcweir 			    {
951cdf0e10cSrcweir                     ::rtl::OUString sRet;
952cdf0e10cSrcweir 		            const static ::rtl::OUString s_sService(RTL_CONSTASCII_USTRINGPARAM("ServiceName"));
953cdf0e10cSrcweir 		            aReportEngine.getNodeValue(s_sService) >>= sRet;
954cdf0e10cSrcweir 				    return sRet;
955cdf0e10cSrcweir 			    }
956cdf0e10cSrcweir 		    }
957cdf0e10cSrcweir         }
958cdf0e10cSrcweir     	else
959cdf0e10cSrcweir         	return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.report.pentaho.SOReportJobFactory"));
960cdf0e10cSrcweir 	}
961cdf0e10cSrcweir     else
962cdf0e10cSrcweir         return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.report.pentaho.SOReportJobFactory"));
963cdf0e10cSrcweir 	return ::rtl::OUString();
964cdf0e10cSrcweir }
965cdf0e10cSrcweir // -----------------------------------------------------------------------------
966cdf0e10cSrcweir //.........................................................................
967cdf0e10cSrcweir }	// namespace dbtools
968cdf0e10cSrcweir //.........................................................................
969cdf0e10cSrcweir 
970