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