1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <stdio.h>
#include <osl/diagnose.h>
#include <comphelper/property.hxx>
#include <comphelper/uno3.hxx>
#include <osl/thread.h>
#include <tools/diagnose_ex.h>
#include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
#include <com/sun/star/sdbc/ResultSetType.hpp>
#include <com/sun/star/sdbc/FetchDirection.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <comphelper/sequence.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <comphelper/extract.hxx>
#include <comphelper/types.hxx>
#include <comphelper/processfactory.hxx>
#include <connectivity/dbexception.hxx>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <algorithm>
#include "diagnose_ex.h"
#include "MDriver.hxx"
#include "MStatement.hxx"
#include "MConnection.hxx"
#include "MResultSet.hxx"
#include "MDatabaseMetaData.hxx"
#include "resource/mozab_res.hrc"
#include "resource/common_res.hrc"
#if OSL_DEBUG_LEVEL > 0
# define OUtoCStr( x ) ( OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
#else /* OSL_DEBUG_LEVEL */
# define OUtoCStr( x ) ("dummy")
#endif /* OSL_DEBUG_LEVEL */
static ::osl::Mutex m_ThreadMutex;
using namespace ::comphelper;
using namespace connectivity::mozab;
using namespace connectivity;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::container;
using namespace com::sun::star::io;
using namespace com::sun::star::util;
OCommonStatement::OCommonStatement(OConnection* _pConnection )
:OCommonStatement_IBASE(m_aMutex)
,OPropertySetHelper(OCommonStatement_IBASE::rBHelper)
,OCommonStatement_SBASE((::cppu::OWeakObject*)_pConnection, this)
,m_xDBMetaData(_pConnection->getMetaData())
,m_pTable(NULL)
,m_pConnection(_pConnection)
,m_aParser(::comphelper::getComponentContext(
_pConnection->getDriver()->getMSFactory()))
,m_pSQLIterator( new OSQLParseTreeIterator( _pConnection, _pConnection->createCatalog()->getTables(), m_aParser, NULL ) )
,m_pParseTree(NULL)
,rBHelper(OCommonStatement_IBASE::rBHelper)
{
m_pConnection->acquire();
OSL_TRACE("In/Out: OCommonStatement::OCommonStatement" );
}
OCommonStatement::~OCommonStatement()
{
}
void OCommonStatement::disposing()
{
::osl::MutexGuard aGuard(m_aMutex);
clearWarnings();
clearCachedResultSet();
if (m_pConnection)
m_pConnection->release();
m_pConnection = NULL;
m_pSQLIterator->dispose();
dispose_ChildImpl();
OCommonStatement_IBASE::disposing();
}
Any SAL_CALL OCommonStatement::queryInterface( const Type & rType ) throw(RuntimeException)
{
Any aRet = OCommonStatement_IBASE::queryInterface(rType);
if(!aRet.hasValue())
aRet = OPropertySetHelper::queryInterface(rType);
return aRet;
}
Sequence< Type > SAL_CALL OCommonStatement::getTypes( ) throw(RuntimeException)
{
::cppu::OTypeCollection aTypes( cppu::UnoType<XMultiPropertySet>::get(),
cppu::UnoType<XFastPropertySet>::get(),
cppu::UnoType<XPropertySet>::get());
return ::comphelper::concatSequences(aTypes.getTypes(),OCommonStatement_IBASE::getTypes());
}
void SAL_CALL OCommonStatement::close( ) throw(SQLException, RuntimeException)
{
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
}
dispose();
}
void OCommonStatement::createTable( ) throw ( SQLException, RuntimeException )
{
if(m_pParseTree)
{
::rtl::Reference<connectivity::OSQLColumns> xCreateColumn;
if (m_pSQLIterator->getStatementType() == SQL_STATEMENT_CREATE_TABLE)
{
const OSQLTables& xTabs = m_pSQLIterator->getTables();
OSL_ENSURE( !xTabs.empty(), "Need a Table");
OUString ouTableName=xTabs.begin()->first;
xCreateColumn = m_pSQLIterator->getCreateColumns();
OSL_ENSURE(xCreateColumn.is(), "Need the Columns!!");
const OColumnAlias& aColumnAlias = m_pConnection->getColumnAlias();
OSQLColumns::Vector::const_iterator aIter = xCreateColumn->get().begin();
const OUString sProprtyName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME);
OUString sName;
for (sal_Int32 i = 1; aIter != xCreateColumn->get().end();++aIter, i++)
{
(*aIter)->getPropertyValue(sProprtyName) >>= sName;
if ( !aColumnAlias.hasAlias( sName ) )
{
const OUString sError( getOwnConnection()->getResources().getResourceStringWithSubstitution(
STR_INVALID_COLUMNNAME,
"$columnname$", sName
) );
::dbtools::throwGenericSQLException(sError,*this);
}
}
MDatabaseMetaDataHelper _aDbHelper;
if (!_aDbHelper.NewAddressBook(m_pConnection,ouTableName))
{
getOwnConnection()->throwSQLException( _aDbHelper.getError(), *this );
}
m_pSQLIterator.reset( new ::connectivity::OSQLParseTreeIterator(
m_pConnection, m_pConnection->createCatalog()->getTables(), m_aParser, NULL ) );
}
}
else
getOwnConnection()->throwSQLException( STR_QUERY_TOO_COMPLEX, *this );
}
OCommonStatement::StatementType OCommonStatement::parseSql( const OUString& sql , sal_Bool bAdjusted)
throw ( SQLException, RuntimeException )
{
OUString aErr;
m_pParseTree = m_aParser.parseTree(aErr,sql);
#if OSL_DEBUG_LEVEL > 0
{
const char* str = OUtoCStr(sql);
OSL_UNUSED( str );
OSL_TRACE("ParseSQL: %s", OUtoCStr( sql ) );
}
#endif // OSL_DEBUG_LEVEL
if(m_pParseTree)
{
m_pSQLIterator->setParseTree(m_pParseTree);
m_pSQLIterator->traverseAll();
const OSQLTables& xTabs = m_pSQLIterator->getTables();
if(xTabs.empty())
getOwnConnection()->throwSQLException( STR_QUERY_AT_LEAST_ONE_TABLES, *this );
#if OSL_DEBUG_LEVEL > 0
OSQLTables::const_iterator citer;
for( citer = xTabs.begin(); citer != xTabs.end(); ++citer ) {
OSL_TRACE("SELECT Table : %s", OUtoCStr(citer->first) );
}
#endif
Reference<XIndexAccess> xNames;
switch(m_pSQLIterator->getStatementType())
{
case SQL_STATEMENT_SELECT:
// at this moment we support only one table per select statement
OSL_ENSURE( xTabs.begin() != xTabs.end(), "Need a Table");
m_pTable = static_cast< OTable* > (xTabs.begin()->second.get());
m_xColNames = m_pTable->getColumns();
xNames = Reference<XIndexAccess>(m_xColNames,UNO_QUERY);
// set the binding of the resultrow
m_aRow = new OValueVector(xNames->getCount());
(m_aRow->get())[0].setBound(sal_True);
::std::for_each(m_aRow->get().begin()+1,m_aRow->get().end(),TSetBound(sal_False));
// create the column mapping
createColumnMapping();
analyseSQL();
return eSelect;
case SQL_STATEMENT_CREATE_TABLE:
createTable();
return eCreateTable;
default:
break;
}
}
else if(!bAdjusted) //Our sql parser does not support a statement like "create table foo"
// So we append ("E-mail" varchar) to the last of it to make it work
{
return parseSql(sql + "(""E-mail"" character)",sal_True);
}
getOwnConnection()->throwSQLException( STR_QUERY_TOO_COMPLEX, *this );
OSL_FAIL( "OCommonStatement::parseSql: unreachable!" );
return eSelect;
}
Reference< XResultSet > OCommonStatement::impl_executeCurrentQuery()
{
clearCachedResultSet();
::rtl::Reference< OResultSet > pResult( new OResultSet( this, m_pSQLIterator ) );
initializeResultSet( pResult.get() );
pResult->executeQuery();
cacheResultSet( pResult ); // only cache if we survived the execution
return pResult.get();
}
void OCommonStatement::initializeResultSet( OResultSet* _pResult )
{
ENSURE_OR_THROW( _pResult, "invalid result set" );
_pResult->setColumnMapping(m_aColMapping);
_pResult->setOrderByColumns(m_aOrderbyColumnNumber);
_pResult->setOrderByAscending(m_aOrderbyAscending);
_pResult->setBindingRow(m_aRow);
_pResult->setTable(m_pTable);
}
void OCommonStatement::clearCachedResultSet()
{
Reference< XResultSet > xResultSet( m_xResultSet.get(), UNO_QUERY );
if ( !xResultSet.is() )
return;
try
{
Reference< XCloseable > xCloseable( xResultSet, UNO_QUERY_THROW );
xCloseable->close();
}
catch( const DisposedException& )
{
DBG_UNHANDLED_EXCEPTION();
}
m_xResultSet.clear();
}
void OCommonStatement::cacheResultSet( const ::rtl::Reference< OResultSet >& _pResult )
{
ENSURE_OR_THROW( _pResult.is(), "invalid result set" );
m_xResultSet = Reference< XResultSet >( _pResult.get() );
}
sal_Bool SAL_CALL OCommonStatement::execute( const OUString& sql ) throw(SQLException, RuntimeException)
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
OSL_TRACE("Statement::execute( %s )", OUtoCStr( sql ) );
Reference< XResultSet > xRS = executeQuery( sql );
// returns true when a resultset is available
return xRS.is();
}
Reference< XResultSet > SAL_CALL OCommonStatement::executeQuery( const OUString& sql ) throw(SQLException, RuntimeException)
{
::osl::MutexGuard aGuard( m_ThreadMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
OSL_TRACE("Statement::executeQuery( %s )", OUtoCStr( sql ) );
// parse the statement
StatementType eStatementType = parseSql( sql );
if ( eStatementType != eSelect )
return NULL;
return impl_executeCurrentQuery();
}
Reference< XConnection > SAL_CALL OCommonStatement::getConnection( ) throw(SQLException, RuntimeException)
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
// just return our connection here
return (Reference< XConnection >)m_pConnection;
}
Any SAL_CALL OStatement::queryInterface( const Type & rType ) throw(RuntimeException)
{
Any aRet = ::cppu::queryInterface(rType,static_cast< XServiceInfo*> (this));
if(!aRet.hasValue())
aRet = OCommonStatement::queryInterface(rType);
return aRet;
}
sal_Int32 SAL_CALL OCommonStatement::executeUpdate( const OUString& /*sql*/ ) throw(SQLException, RuntimeException)
{
::dbtools::throwFeatureNotImplementedException( "XStatement::executeUpdate", *this );
return 0;
}
Any SAL_CALL OCommonStatement::getWarnings( ) throw(SQLException, RuntimeException)
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
return makeAny(m_aLastWarning);
}
void SAL_CALL OCommonStatement::clearWarnings( ) throw(SQLException, RuntimeException)
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OCommonStatement_IBASE::rBHelper.bDisposed);
m_aLastWarning = SQLWarning();
}
::cppu::IPropertyArrayHelper* OCommonStatement::createArrayHelper( ) const
{
// this properties are define by the service resultset
// they must in alphabetic order
Sequence< Property > aProps(9);
Property* pProperties = aProps.getArray();
sal_Int32 nPos = 0;
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME),
PROPERTY_ID_CURSORNAME, cppu::UnoType<OUString>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING),
PROPERTY_ID_ESCAPEPROCESSING, ::getBooleanCppuType(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION),
PROPERTY_ID_FETCHDIRECTION, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE),
PROPERTY_ID_FETCHSIZE, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE),
PROPERTY_ID_MAXFIELDSIZE, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS),
PROPERTY_ID_MAXROWS, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT),
PROPERTY_ID_QUERYTIMEOUT, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY),
PROPERTY_ID_RESULTSETCONCURRENCY, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE),
PROPERTY_ID_RESULTSETTYPE, cppu::UnoType<sal_Int32>::get(), 0);
pProperties[nPos++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_USEBOOKMARKS),
PROPERTY_ID_USEBOOKMARKS, ::getBooleanCppuType(), 0);
return new ::cppu::OPropertyArrayHelper(aProps);
}
::cppu::IPropertyArrayHelper & OCommonStatement::getInfoHelper()
{
return *const_cast<OCommonStatement*>(this)->getArrayHelper();
}
sal_Bool OCommonStatement::convertFastPropertyValue(
Any & /*rConvertedValue*/,
Any & /*rOldValue*/,
sal_Int32 /*nHandle*/,
const Any& /*rValue*/ )
throw (::com::sun::star::lang::IllegalArgumentException)
{
sal_Bool bConverted = sal_False;
// here we have to try to convert
return bConverted;
}
void OCommonStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& /*rValue*/) throw (Exception)
{
// set the value to whatever is necessary
switch(nHandle)
{
case PROPERTY_ID_QUERYTIMEOUT:
case PROPERTY_ID_MAXFIELDSIZE:
case PROPERTY_ID_MAXROWS:
case PROPERTY_ID_RESULTSETCONCURRENCY:
case PROPERTY_ID_RESULTSETTYPE:
case PROPERTY_ID_FETCHDIRECTION:
case PROPERTY_ID_FETCHSIZE:
case PROPERTY_ID_ESCAPEPROCESSING:
default:
;
}
}
void OCommonStatement::getFastPropertyValue(Any& /*rValue*/,sal_Int32 nHandle) const
{
switch(nHandle)
{
case PROPERTY_ID_QUERYTIMEOUT:
case PROPERTY_ID_MAXFIELDSIZE:
case PROPERTY_ID_MAXROWS:
case PROPERTY_ID_RESULTSETCONCURRENCY:
case PROPERTY_ID_RESULTSETTYPE:
case PROPERTY_ID_FETCHDIRECTION:
case PROPERTY_ID_FETCHSIZE:
case PROPERTY_ID_ESCAPEPROCESSING:
default:
;
}
}
IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbcx.OStatement","com.sun.star.sdbc.Statement");
void SAL_CALL OCommonStatement::acquire() throw()
{
OCommonStatement_IBASE::acquire();
}
void SAL_CALL OCommonStatement::release() throw()
{
relase_ChildImpl();
}
void SAL_CALL OStatement::acquire() throw()
{
OCommonStatement::acquire();
}
void SAL_CALL OStatement::release() throw()
{
OCommonStatement::release();
}
Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OCommonStatement::getPropertySetInfo( ) throw(RuntimeException)
{
return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
}
void OCommonStatement::createColumnMapping()
{
size_t i;
// initialize the column index map (mapping select columns to table columns)
::rtl::Reference<connectivity::OSQLColumns> xColumns = m_pSQLIterator->getSelectColumns();
m_aColMapping.resize(xColumns->get().size() + 1);
for (i=0; i<m_aColMapping.size(); ++i)
m_aColMapping[i] = static_cast<sal_Int32>(i);
Reference<XIndexAccess> xNames(m_xColNames,UNO_QUERY);
// now check which columns are bound
#if OSL_DEBUG_LEVEL > 0
for ( i = 0; i < m_aColMapping.size(); i++ )
OSL_TRACE("BEFORE Mapped: %d -> %d", i, m_aColMapping[i] );
#endif
OResultSet::setBoundedColumns(m_aRow,xColumns,xNames,sal_True,m_xDBMetaData,m_aColMapping);
#if OSL_DEBUG_LEVEL > 0
for ( i = 0; i < m_aColMapping.size(); i++ )
OSL_TRACE("AFTER Mapped: %d -> %d", i, m_aColMapping[i] );
#endif
}
void OCommonStatement::analyseSQL()
{
const OSQLParseNode* pOrderbyClause = m_pSQLIterator->getOrderTree();
if(pOrderbyClause)
{
OSQLParseNode * pOrderingSpecCommalist = pOrderbyClause->getChild(2);
OSL_ENSURE(SQL_ISRULE(pOrderingSpecCommalist,ordering_spec_commalist),"OResultSet: Fehler im Parse Tree");
for (sal_uInt32 m = 0; m < pOrderingSpecCommalist->count(); m++)
{
OSQLParseNode * pOrderingSpec = pOrderingSpecCommalist->getChild(m);
OSL_ENSURE(SQL_ISRULE(pOrderingSpec,ordering_spec),"OResultSet: Fehler im Parse Tree");
OSL_ENSURE(pOrderingSpec->count() == 2,"OResultSet: Fehler im Parse Tree");
OSQLParseNode * pColumnRef = pOrderingSpec->getChild(0);
if(!SQL_ISRULE(pColumnRef,column_ref))
{
throw SQLException();
}
OSQLParseNode * pAscendingDescending = pOrderingSpec->getChild(1);
setOrderbyColumn(pColumnRef,pAscendingDescending);
}
}
}
void OCommonStatement::setOrderbyColumn( OSQLParseNode* pColumnRef,
OSQLParseNode* pAscendingDescending)
{
OUString aColumnName;
if (pColumnRef->count() == 1)
aColumnName = pColumnRef->getChild(0)->getTokenValue();
else if (pColumnRef->count() == 3)
{
pColumnRef->getChild(2)->parseNodeToStr( aColumnName, getOwnConnection(), NULL, sal_False, sal_False );
}
else
{
throw SQLException();
}
Reference<XColumnLocate> xColLocate(m_xColNames,UNO_QUERY);
if(!xColLocate.is())
return;
m_aOrderbyColumnNumber.push_back(xColLocate->findColumn(aColumnName));
// Ascending or Descending?
m_aOrderbyAscending.push_back((SQL_ISTOKEN(pAscendingDescending,DESC)) ? SQL_DESC : SQL_ASC);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|