1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FStatement.cxx,v $
10 * $Revision: 1.44.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <osl/diagnose.h>
34 #include "file/FStatement.hxx"
35 #include "file/FConnection.hxx"
36 #include "file/FDriver.hxx"
37 #include "file/FResultSet.hxx"
38 #include <comphelper/property.hxx>
39 #include <comphelper/uno3.hxx>
40 #include <osl/thread.h>
41 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
42 #include <com/sun/star/sdbc/ResultSetType.hpp>
43 #include <com/sun/star/sdbc/FetchDirection.hpp>
44 #include <com/sun/star/lang/DisposedException.hpp>
45 #include <comphelper/sequence.hxx>
46 #include <cppuhelper/typeprovider.hxx>
47 #include "connectivity/dbexception.hxx"
48 #include "resource/file_res.hrc"
50 #include <tools/debug.hxx>
51 #include <rtl/logfile.hxx>
53 #define THROW_SQL(x) \
54 OTools::ThrowException(x,m_aStatementHandle,SQL_HANDLE_STMT,*this)
56 namespace connectivity
61 //------------------------------------------------------------------------------
62 using namespace dbtools
;
63 using namespace com::sun::star::uno
;
64 using namespace com::sun::star::lang
;
65 using namespace com::sun::star::beans
;
66 using namespace com::sun::star::sdbc
;
67 using namespace com::sun::star::sdbcx
;
68 using namespace com::sun::star::container
;
69 DBG_NAME( file_OStatement_Base
)
71 //------------------------------------------------------------------------------
72 OStatement_Base::OStatement_Base(OConnection
* _pConnection
)
73 :OStatement_BASE(m_aMutex
)
74 ,::comphelper::OPropertyContainer(OStatement_BASE::rBHelper
)
75 ,m_xDBMetaData(_pConnection
->getMetaData())
76 ,m_aParser(_pConnection
->getDriver()->getFactory())
77 ,m_aSQLIterator( _pConnection
, _pConnection
->createCatalog()->getTables(), m_aParser
, NULL
)
78 ,m_pConnection(_pConnection
)
81 ,m_pEvaluationKeySet(NULL
)
87 ,m_nResultSetType(ResultSetType::FORWARD_ONLY
)
88 ,m_nFetchDirection(FetchDirection::FORWARD
)
89 ,m_nResultSetConcurrency(ResultSetConcurrency::UPDATABLE
)
90 ,m_bEscapeProcessing(sal_True
)
91 ,rBHelper(OStatement_BASE::rBHelper
)
93 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::OStatement_Base" );
94 DBG_CTOR( file_OStatement_Base
, NULL
);
96 m_pConnection
->acquire();
98 sal_Int32 nAttrib
= 0;
100 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME
), PROPERTY_ID_CURSORNAME
, nAttrib
,&m_aCursorName
, ::getCppuType(reinterpret_cast< ::rtl::OUString
*>(NULL
)));
101 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE
), PROPERTY_ID_MAXFIELDSIZE
, nAttrib
,&m_nMaxFieldSize
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
102 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS
), PROPERTY_ID_MAXROWS
, nAttrib
,&m_nMaxRows
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
103 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT
), PROPERTY_ID_QUERYTIMEOUT
, nAttrib
,&m_nQueryTimeOut
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
104 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE
), PROPERTY_ID_FETCHSIZE
, nAttrib
,&m_nFetchSize
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
105 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE
), PROPERTY_ID_RESULTSETTYPE
, nAttrib
,&m_nResultSetType
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
106 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION
), PROPERTY_ID_FETCHDIRECTION
, nAttrib
,&m_nFetchDirection
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
107 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING
),PROPERTY_ID_ESCAPEPROCESSING
, nAttrib
,&m_bEscapeProcessing
,::getCppuBooleanType());
109 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY
), PROPERTY_ID_RESULTSETCONCURRENCY
, nAttrib
,&m_nResultSetConcurrency
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
111 // -----------------------------------------------------------------------------
112 OStatement_Base::~OStatement_Base()
114 osl_incrementInterlockedCount( &m_refCount
);
116 delete m_pSQLAnalyzer
;
118 DBG_DTOR( file_OStatement_Base
, NULL
);
120 //------------------------------------------------------------------------------
121 void OStatement_Base::disposeResultSet()
123 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposeResultSet" );
124 // free the cursor if alive
125 Reference
< XComponent
> xComp(m_xResultSet
.get(), UNO_QUERY
);
128 m_xResultSet
= Reference
< XResultSet
>();
130 //------------------------------------------------------------------------------
131 void OStatement_BASE2::disposing()
133 ::osl::MutexGuard
aGuard(m_aMutex
);
138 m_pSQLAnalyzer
->dispose();
142 m_aRow
->get().clear();
146 m_aSQLIterator
.dispose();
156 m_pConnection
->release();
157 m_pConnection
= NULL
;
168 OStatement_Base::disposing();
170 // -----------------------------------------------------------------------------
171 void SAL_CALL
OStatement_Base::acquire() throw()
173 OStatement_BASE::acquire();
175 //-----------------------------------------------------------------------------
176 void SAL_CALL
OStatement_BASE2::release() throw()
180 //-----------------------------------------------------------------------------
181 Any SAL_CALL
OStatement_Base::queryInterface( const Type
& rType
) throw(RuntimeException
)
183 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::queryInterface" );
184 const Any aRet
= OStatement_BASE::queryInterface(rType
);
185 return aRet
.hasValue() ? aRet
: OPropertySetHelper::queryInterface(rType
);
187 // -------------------------------------------------------------------------
188 Sequence
< Type
> SAL_CALL
OStatement_Base::getTypes( ) throw(RuntimeException
)
190 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getTypes" );
191 ::cppu::OTypeCollection
aTypes( ::getCppuType( (const Reference
< ::com::sun::star::beans::XMultiPropertySet
> *)0 ),
192 ::getCppuType( (const Reference
< ::com::sun::star::beans::XFastPropertySet
> *)0 ),
193 ::getCppuType( (const Reference
< ::com::sun::star::beans::XPropertySet
> *)0 ));
195 return ::comphelper::concatSequences(aTypes
.getTypes(),OStatement_BASE::getTypes());
197 // -------------------------------------------------------------------------
199 void SAL_CALL
OStatement_Base::cancel( ) throw(RuntimeException
)
201 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::cancel" );
203 // -------------------------------------------------------------------------
205 void SAL_CALL
OStatement_Base::close( ) throw(SQLException
, RuntimeException
)
207 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::close" );
209 ::osl::MutexGuard
aGuard( m_aMutex
);
210 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
214 // -------------------------------------------------------------------------
216 void OStatement_Base::reset() throw (SQLException
)
218 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::reset" );
219 ::osl::MutexGuard
aGuard( m_aMutex
);
220 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
225 if (m_xResultSet
.get().is())
228 //--------------------------------------------------------------------
230 // If a ResultSet was created for this Statement, close it
231 //--------------------------------------------------------------------
233 void OStatement_Base::clearMyResultSet () throw (SQLException
)
235 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearMyResultSet " );
236 ::osl::MutexGuard
aGuard( m_aMutex
);
237 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
241 Reference
<XCloseable
> xCloseable
;
242 if ( ::comphelper::query_interface( m_xResultSet
.get(), xCloseable
) )
245 catch( const DisposedException
& ) { }
247 m_xResultSet
= Reference
< XResultSet
>();
249 //--------------------------------------------------------------------
252 //--------------------------------------------------------------------
254 void OStatement_Base::setWarning (const SQLWarning
&ex
) throw( SQLException
)
256 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setWarning " );
257 ::osl::MutexGuard
aGuard( m_aMutex
);
258 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
264 // -------------------------------------------------------------------------
265 Any SAL_CALL
OStatement_Base::getWarnings( ) throw(SQLException
, RuntimeException
)
267 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getWarnings" );
268 ::osl::MutexGuard
aGuard( m_aMutex
);
269 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
271 return makeAny(m_aLastWarning
);
273 // -------------------------------------------------------------------------
274 void SAL_CALL
OStatement_Base::clearWarnings( ) throw(SQLException
, RuntimeException
)
276 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearWarnings" );
277 ::osl::MutexGuard
aGuard( m_aMutex
);
278 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
280 m_aLastWarning
= SQLWarning();
282 // -------------------------------------------------------------------------
283 ::cppu::IPropertyArrayHelper
* OStatement_Base::createArrayHelper( ) const
285 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createArrayHelper" );
286 Sequence
< Property
> aProps
;
287 describeProperties(aProps
);
288 return new ::cppu::OPropertyArrayHelper(aProps
);
291 // -------------------------------------------------------------------------
292 ::cppu::IPropertyArrayHelper
& OStatement_Base::getInfoHelper()
294 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getInfoHelper" );
295 return *const_cast<OStatement_Base
*>(this)->getArrayHelper();
297 // -------------------------------------------------------------------------
298 OResultSet
* OStatement::createResultSet()
300 return new OResultSet(this,m_aSQLIterator
);
302 // -------------------------------------------------------------------------
303 IMPLEMENT_SERVICE_INFO(OStatement
,"com.sun.star.sdbc.driver.file.Statement","com.sun.star.sdbc.Statement");
304 // -----------------------------------------------------------------------------
305 void SAL_CALL
OStatement::acquire() throw()
307 OStatement_BASE2::acquire();
309 // -----------------------------------------------------------------------------
310 void SAL_CALL
OStatement::release() throw()
312 OStatement_BASE2::release();
314 // -----------------------------------------------------------------------------
315 // -------------------------------------------------------------------------
316 sal_Bool SAL_CALL
OStatement::execute( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
318 ::osl::MutexGuard
aGuard( m_aMutex
);
322 return m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
;
325 // -------------------------------------------------------------------------
327 Reference
< XResultSet
> SAL_CALL
OStatement::executeQuery( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
329 ::osl::MutexGuard
aGuard( m_aMutex
);
330 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
333 Reference
< XResultSet
> xRS
;
334 OResultSet
* pResult
= createResultSet();
336 initializeResultSet(pResult
);
337 m_xResultSet
= Reference
<XResultSet
>(pResult
);
343 // -------------------------------------------------------------------------
344 Reference
< XConnection
> SAL_CALL
OStatement::getConnection( ) throw(SQLException
, RuntimeException
)
346 return (Reference
< XConnection
>)m_pConnection
;
348 // -------------------------------------------------------------------------
349 sal_Int32 SAL_CALL
OStatement::executeUpdate( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
351 ::osl::MutexGuard
aGuard( m_aMutex
);
352 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
356 OResultSet
* pResult
= createResultSet();
357 Reference
< XResultSet
> xRS
= pResult
;
358 initializeResultSet(pResult
);
361 return pResult
->getRowCountResult();
364 // -----------------------------------------------------------------------------
365 void SAL_CALL
OStatement_Base::disposing(void)
367 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposing" );
368 if(m_aEvaluateRow
.isValid())
370 m_aEvaluateRow
->get().clear();
371 m_aEvaluateRow
= NULL
;
373 delete m_pEvaluationKeySet
;
374 OStatement_BASE::disposing();
376 // -----------------------------------------------------------------------------
377 Reference
< ::com::sun::star::beans::XPropertySetInfo
> SAL_CALL
OStatement_Base::getPropertySetInfo( ) throw(RuntimeException
)
379 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getPropertySetInfo" );
380 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
382 // -----------------------------------------------------------------------------
383 Any SAL_CALL
OStatement::queryInterface( const Type
& rType
) throw(RuntimeException
)
385 Any aRet
= OStatement_XStatement::queryInterface( rType
);
386 return aRet
.hasValue() ? aRet
: OStatement_BASE2::queryInterface( rType
);
388 // -----------------------------------------------------------------------------
389 OSQLAnalyzer
* OStatement_Base::createAnalyzer()
391 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createAnalyzer" );
392 return new OSQLAnalyzer(m_pConnection
);
394 // -----------------------------------------------------------------------------
395 void OStatement_Base::anylizeSQL()
397 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::anylizeSQL" );
398 OSL_ENSURE(m_pSQLAnalyzer
,"OResultSet::anylizeSQL: Analyzer isn't set!");
399 // start analysing the statement
400 m_pSQLAnalyzer
->setOrigColumns(m_xColNames
);
401 m_pSQLAnalyzer
->start(m_pParseTree
);
403 const OSQLParseNode
* pOrderbyClause
= m_aSQLIterator
.getOrderTree();
406 OSQLParseNode
* pOrderingSpecCommalist
= pOrderbyClause
->getChild(2);
407 OSL_ENSURE(SQL_ISRULE(pOrderingSpecCommalist
,ordering_spec_commalist
),"OResultSet: Fehler im Parse Tree");
409 for (sal_uInt32 m
= 0; m
< pOrderingSpecCommalist
->count(); m
++)
411 OSQLParseNode
* pOrderingSpec
= pOrderingSpecCommalist
->getChild(m
);
412 OSL_ENSURE(SQL_ISRULE(pOrderingSpec
,ordering_spec
),"OResultSet: Fehler im Parse Tree");
413 OSL_ENSURE(pOrderingSpec
->count() == 2,"OResultSet: Fehler im Parse Tree");
415 OSQLParseNode
* pColumnRef
= pOrderingSpec
->getChild(0);
416 if(!SQL_ISRULE(pColumnRef
,column_ref
))
418 throw SQLException();
420 OSQLParseNode
* pAscendingDescending
= pOrderingSpec
->getChild(1);
421 setOrderbyColumn(pColumnRef
,pAscendingDescending
);
425 //------------------------------------------------------------------
426 void OStatement_Base::setOrderbyColumn( OSQLParseNode
* pColumnRef
,
427 OSQLParseNode
* pAscendingDescending
)
429 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setOrderbyColumn" );
430 ::rtl::OUString aColumnName
;
431 if (pColumnRef
->count() == 1)
432 aColumnName
= pColumnRef
->getChild(0)->getTokenValue();
433 else if (pColumnRef
->count() == 3)
435 // Nur die Table Range-Variable darf hier vorkommen:
436 // if (!(pColumnRef->getChild(0)->getTokenValue() == aTableRange))
438 // aStatus.Set(SQL_STAT_ERROR,
439 // String::CreateFromAscii("S1000"),
440 // aStatus.CreateErrorMessage(String(SdbResId(STR_STAT_INVALID_RANGE_VAR))),
444 pColumnRef
->getChild(2)->parseNodeToStr( aColumnName
, getOwnConnection(), NULL
, sal_False
, sal_False
);
448 // aStatus.SetStatementTooComplex();
449 throw SQLException();
452 Reference
<XColumnLocate
> xColLocate(m_xColNames
,UNO_QUERY
);
455 // Alles geprueft und wir haben den Namen der Column.
456 // Die wievielte Column ist das?
459 m_aOrderbyColumnNumber
.push_back(xColLocate
->findColumn(aColumnName
));
463 ::vos::ORef
<OSQLColumns
> aSelectColumns
= m_aSQLIterator
.getSelectColumns();
464 ::comphelper::UStringMixEqual aCase
;
465 OSQLColumns::Vector::const_iterator aFind
= ::connectivity::find(aSelectColumns
->get().begin(),aSelectColumns
->get().end(),aColumnName
,aCase
);
466 if ( aFind
== aSelectColumns
->get().end() )
467 throw SQLException();
468 m_aOrderbyColumnNumber
.push_back((aFind
- aSelectColumns
->get().begin()) + 1);
471 // Ascending or Descending?
472 m_aOrderbyAscending
.push_back((SQL_ISTOKEN(pAscendingDescending
,DESC
)) ? SQL_DESC
: SQL_ASC
);
475 // -----------------------------------------------------------------------------
476 void OStatement_Base::construct(const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
478 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::construct" );
479 ::rtl::OUString aErr
;
480 m_pParseTree
= m_aParser
.parseTree(aErr
,sql
);
483 m_aSQLIterator
.setParseTree(m_pParseTree
);
484 m_aSQLIterator
.traverseAll();
485 const OSQLTables
& xTabs
= m_aSQLIterator
.getTables();
489 // no tables -> nothing to operate on -> error
490 m_pConnection
->throwGenericSQLException(STR_QUERY_NO_TABLE
,*this);
492 if ( xTabs
.size() > 1 || m_aSQLIterator
.hasErrors() )
493 // more than one table -> can't operate on them -> error
494 m_pConnection
->throwGenericSQLException(STR_QUERY_MORE_TABLES
,*this);
496 if ( (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
) && m_aSQLIterator
.getSelectColumns()->get().empty() )
497 // SELECT statement without columns -> error
498 m_pConnection
->throwGenericSQLException(STR_QUERY_NO_COLUMN
,*this);
500 if ( m_aSQLIterator
.getStatementType() == SQL_STATEMENT_CREATE_TABLE
)
501 // CREATE TABLE is not supported at all
502 m_pConnection
->throwGenericSQLException(STR_QUERY_TOO_COMPLEX
,*this);
504 if ( ( m_aSQLIterator
.getStatementType() == SQL_STATEMENT_ODBC_CALL
) || ( m_aSQLIterator
.getStatementType() == SQL_STATEMENT_UNKNOWN
) )
505 // ODBC call or unknown statement type -> error
506 m_pConnection
->throwGenericSQLException(STR_QUERY_TOO_COMPLEX
,*this);
508 // at this moment we support only one table per select statement
509 Reference
< ::com::sun::star::lang::XUnoTunnel
> xTunnel(xTabs
.begin()->second
,UNO_QUERY
);
514 m_pTable
= reinterpret_cast<OFileTable
*>(xTunnel
->getSomething(OFileTable::getUnoTunnelImplementationId()));
518 OSL_ENSURE(m_pTable
,"No table!");
520 m_xColNames
= m_pTable
->getColumns();
521 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
522 // set the binding of the resultrow
523 m_aRow
= new OValueRefVector(xNames
->getCount());
524 (m_aRow
->get())[0]->setBound(sal_True
);
525 ::std::for_each(m_aRow
->get().begin()+1,m_aRow
->get().end(),TSetRefBound(sal_False
));
527 // set the binding of the resultrow
528 m_aEvaluateRow
= new OValueRefVector(xNames
->getCount());
530 (m_aEvaluateRow
->get())[0]->setBound(sal_True
);
531 ::std::for_each(m_aEvaluateRow
->get().begin()+1,m_aEvaluateRow
->get().end(),TSetRefBound(sal_False
));
533 // set the select row
534 m_aSelectRow
= new OValueRefVector(m_aSQLIterator
.getSelectColumns()->get().size());
535 ::std::for_each(m_aSelectRow
->get().begin(),m_aSelectRow
->get().end(),TSetRefBound(sal_True
));
537 // create the column mapping
538 createColumnMapping();
540 m_pSQLAnalyzer
= createAnalyzer();
542 Reference
<XIndexesSupplier
> xIndexSup(xTunnel
,UNO_QUERY
);
544 m_pSQLAnalyzer
->setIndexes(xIndexSup
->getIndexes());
549 throw SQLException(aErr
,*this,::rtl::OUString(),0,Any());
551 // -----------------------------------------------------------------------------
552 void OStatement_Base::createColumnMapping()
554 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createColumnMapping" );
555 // initialize the column index map (mapping select columns to table columns)
556 ::vos::ORef
<connectivity::OSQLColumns
> xColumns
= m_aSQLIterator
.getSelectColumns();
557 m_aColMapping
.resize(xColumns
->get().size() + 1);
558 for (sal_Int32 i
=0; i
<(sal_Int32
)m_aColMapping
.size(); ++i
)
559 m_aColMapping
[i
] = i
;
561 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
562 // now check which columns are bound
563 OResultSet::setBoundedColumns(m_aRow
,m_aSelectRow
,xColumns
,xNames
,sal_True
,m_xDBMetaData
,m_aColMapping
);
565 // -----------------------------------------------------------------------------
566 void OStatement_Base::initializeResultSet(OResultSet
* _pResult
)
568 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::initializeResultSet" );
571 _pResult
->setSqlAnalyzer(m_pSQLAnalyzer
);
572 _pResult
->setOrderByColumns(m_aOrderbyColumnNumber
);
573 _pResult
->setOrderByAscending(m_aOrderbyAscending
);
574 _pResult
->setBindingRow(m_aRow
);
575 _pResult
->setColumnMapping(m_aColMapping
);
576 _pResult
->setEvaluationRow(m_aEvaluateRow
);
577 _pResult
->setAssignValues(m_aAssignValues
);
578 _pResult
->setSelectRow(m_aSelectRow
);
580 m_pSQLAnalyzer
->bindSelectRow(m_aRow
);
581 m_pEvaluationKeySet
= m_pSQLAnalyzer
->bindEvaluationRow(m_aEvaluateRow
); // Werte im Code des Compilers setzen
582 _pResult
->setEvaluationKeySet(m_pEvaluationKeySet
);
584 // -----------------------------------------------------------------------------
585 void OStatement_Base::GetAssignValues()
587 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::GetAssignValues" );
588 if (m_pParseTree
== NULL
)
590 ::dbtools::throwFunctionSequenceException(*this);
594 if (SQL_ISRULE(m_pParseTree
,select_statement
))
595 // Keine zu setzenden Werte bei SELECT
597 else if (SQL_ISRULE(m_pParseTree
,insert_statement
))
599 // Row fuer die zu setzenden Werte anlegen (Referenz durch new)
600 if(m_aAssignValues
.isValid())
601 m_aAssignValues
->get().clear();
602 sal_Int32 nCount
= Reference
<XIndexAccess
>(m_xColNames
,UNO_QUERY
)->getCount();
603 m_aAssignValues
= new OAssignValues(nCount
);
605 ::std::for_each(m_aAssignValues
->get().begin()+1,m_aAssignValues
->get().end(),TSetRefBound(sal_False
));
607 m_aParameterIndexes
.resize(nCount
+1,SQL_NO_PARAMETER
);
609 // Liste der Columns-Namen, die in der column_commalist vorkommen (mit ; getrennt):
610 ::std::vector
<String
> aColumnNameList
;
612 OSL_ENSURE(m_pParseTree
->count() >= 4,"OResultSet: Fehler im Parse Tree");
614 OSQLParseNode
* pOptColumnCommalist
= m_pParseTree
->getChild(3);
615 OSL_ENSURE(pOptColumnCommalist
!= NULL
,"OResultSet: Fehler im Parse Tree");
616 OSL_ENSURE(SQL_ISRULE(pOptColumnCommalist
,opt_column_commalist
),"OResultSet: Fehler im Parse Tree");
617 if (pOptColumnCommalist
->count() == 0)
619 const Sequence
< ::rtl::OUString
>& aNames
= m_xColNames
->getElementNames();
620 const ::rtl::OUString
* pBegin
= aNames
.getConstArray();
621 const ::rtl::OUString
* pEnd
= pBegin
+ aNames
.getLength();
622 for (; pBegin
!= pEnd
; ++pBegin
)
623 aColumnNameList
.push_back(*pBegin
);
627 OSL_ENSURE(pOptColumnCommalist
->count() == 3,"OResultSet: Fehler im Parse Tree");
629 OSQLParseNode
* pColumnCommalist
= pOptColumnCommalist
->getChild(1);
630 OSL_ENSURE(pColumnCommalist
!= NULL
,"OResultSet: Fehler im Parse Tree");
631 OSL_ENSURE(SQL_ISRULE(pColumnCommalist
,column_commalist
),"OResultSet: Fehler im Parse Tree");
632 OSL_ENSURE(pColumnCommalist
->count() > 0,"OResultSet: Fehler im Parse Tree");
634 // Alle Columns in der column_commalist ...
635 for (sal_uInt32 i
= 0; i
< pColumnCommalist
->count(); i
++)
637 OSQLParseNode
* pCol
= pColumnCommalist
->getChild(i
);
638 OSL_ENSURE(pCol
!= NULL
,"OResultSet: Fehler im Parse Tree");
639 aColumnNameList
.push_back(pCol
->getTokenValue());
642 if ( aColumnNameList
.empty() )
643 throwFunctionSequenceException(*this);
646 OSQLParseNode
* pValuesOrQuerySpec
= m_pParseTree
->getChild(4);
647 OSL_ENSURE(pValuesOrQuerySpec
!= NULL
,"OResultSet: pValuesOrQuerySpec darf nicht NULL sein!");
648 OSL_ENSURE(SQL_ISRULE(pValuesOrQuerySpec
,values_or_query_spec
),"OResultSet: ! SQL_ISRULE(pValuesOrQuerySpec,values_or_query_spec)");
649 OSL_ENSURE(pValuesOrQuerySpec
->count() > 0,"OResultSet: pValuesOrQuerySpec->count() <= 0");
651 // nur "VALUES" ist erlaubt ...
652 if (! SQL_ISTOKEN(pValuesOrQuerySpec
->getChild(0),VALUES
))
653 throwFunctionSequenceException(*this);
655 OSL_ENSURE(pValuesOrQuerySpec
->count() == 4,"OResultSet: pValuesOrQuerySpec->count() != 4");
658 OSQLParseNode
* pInsertAtomCommalist
= pValuesOrQuerySpec
->getChild(2);
659 OSL_ENSURE(pInsertAtomCommalist
!= NULL
,"OResultSet: pInsertAtomCommalist darf nicht NULL sein!");
660 OSL_ENSURE(pInsertAtomCommalist
->count() > 0,"OResultSet: pInsertAtomCommalist <= 0");
663 OSQLParseNode
* pRow_Value_Const
;
665 for (sal_uInt32 i
= 0; i
< pInsertAtomCommalist
->count(); i
++)
667 pRow_Value_Const
= pInsertAtomCommalist
->getChild(i
); // row_value_constructor
668 OSL_ENSURE(pRow_Value_Const
!= NULL
,"OResultSet: pRow_Value_Const darf nicht NULL sein!");
669 if(SQL_ISRULE(pRow_Value_Const
,parameter
))
671 ParseAssignValues(aColumnNameList
,pRow_Value_Const
,nIndex
++); // kann nur ein Columnname vorhanden sein pro Schleife
673 else if(pRow_Value_Const
->isToken())
674 ParseAssignValues(aColumnNameList
,pRow_Value_Const
,static_cast<xub_StrLen
>(i
));
677 if(pRow_Value_Const
->count() == aColumnNameList
.size())
679 for (sal_uInt32 j
= 0; j
< pRow_Value_Const
->count(); ++j
)
680 ParseAssignValues(aColumnNameList
,pRow_Value_Const
->getChild(j
),nIndex
++);
683 throwFunctionSequenceException(*this);
687 else if (SQL_ISRULE(m_pParseTree
,update_statement_searched
))
689 if(m_aAssignValues
.isValid())
690 m_aAssignValues
->get().clear();
691 sal_Int32 nCount
= Reference
<XIndexAccess
>(m_xColNames
,UNO_QUERY
)->getCount();
692 m_aAssignValues
= new OAssignValues(nCount
);
694 ::std::for_each(m_aAssignValues
->get().begin()+1,m_aAssignValues
->get().end(),TSetRefBound(sal_False
));
696 m_aParameterIndexes
.resize(nCount
+1,SQL_NO_PARAMETER
);
698 OSL_ENSURE(m_pParseTree
->count() >= 4,"OResultSet: Fehler im Parse Tree");
700 OSQLParseNode
* pAssignmentCommalist
= m_pParseTree
->getChild(3);
701 OSL_ENSURE(pAssignmentCommalist
!= NULL
,"OResultSet: pAssignmentCommalist == NULL");
702 OSL_ENSURE(SQL_ISRULE(pAssignmentCommalist
,assignment_commalist
),"OResultSet: Fehler im Parse Tree");
703 OSL_ENSURE(pAssignmentCommalist
->count() > 0,"OResultSet: pAssignmentCommalist->count() <= 0");
705 // Alle Zuweisungen (Kommaliste) bearbeiten ...
706 ::std::vector
< String
> aList(1);
707 for (sal_uInt32 i
= 0; i
< pAssignmentCommalist
->count(); i
++)
709 OSQLParseNode
* pAssignment
= pAssignmentCommalist
->getChild(i
);
710 OSL_ENSURE(pAssignment
!= NULL
,"OResultSet: pAssignment == NULL");
711 OSL_ENSURE(SQL_ISRULE(pAssignment
,assignment
),"OResultSet: Fehler im Parse Tree");
712 OSL_ENSURE(pAssignment
->count() == 3,"OResultSet: pAssignment->count() != 3");
714 OSQLParseNode
* pCol
= pAssignment
->getChild(0);
715 OSL_ENSURE(pCol
!= NULL
,"OResultSet: pCol == NULL");
717 OSQLParseNode
* pComp
= pAssignment
->getChild(1);
718 OSL_ENSURE(pComp
!= NULL
,"OResultSet: pComp == NULL");
719 OSL_ENSURE(pComp
->getNodeType() == SQL_NODE_EQUAL
,"OResultSet: pComp->getNodeType() != SQL_NODE_COMPARISON");
720 if (pComp
->getTokenValue().toChar() != '=')
722 // aStatus.SetInvalidStatement();
723 throwFunctionSequenceException(*this);
726 OSQLParseNode
* pVal
= pAssignment
->getChild(2);
727 OSL_ENSURE(pVal
!= NULL
,"OResultSet: pVal == NULL");
728 aList
[0] = pCol
->getTokenValue();
729 ParseAssignValues(aList
,pVal
,0);
734 // -------------------------------------------------------------------------
735 void OStatement_Base::ParseAssignValues(const ::std::vector
< String
>& aColumnNameList
,OSQLParseNode
* pRow_Value_Constructor_Elem
,xub_StrLen nIndex
)
737 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::ParseAssignValues" );
738 OSL_ENSURE(nIndex
<= aColumnNameList
.size(),"SdbFileCursor::ParseAssignValues: nIndex > aColumnNameList.GetTokenCount()");
739 String
aColumnName(aColumnNameList
[nIndex
]);
740 OSL_ENSURE(aColumnName
.Len() > 0,"OResultSet: Column-Name nicht gefunden");
741 OSL_ENSURE(pRow_Value_Constructor_Elem
!= NULL
,"OResultSet: pRow_Value_Constructor_Elem darf nicht NULL sein!");
743 if (pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_STRING
||
744 pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_INTNUM
||
745 pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_APPROXNUM
)
748 SetAssignValue(aColumnName
, pRow_Value_Constructor_Elem
->getTokenValue());
750 else if (SQL_ISTOKEN(pRow_Value_Constructor_Elem
,NULL
))
753 SetAssignValue(aColumnName
, String(), TRUE
);
755 else if (SQL_ISRULE(pRow_Value_Constructor_Elem
,parameter
))
756 parseParamterElem(aColumnName
,pRow_Value_Constructor_Elem
);
759 // aStatus.SetStatementTooComplex();
760 throwFunctionSequenceException(*this);
763 //------------------------------------------------------------------
764 void OStatement_Base::SetAssignValue(const String
& aColumnName
,
765 const String
& aValue
,
769 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::SetAssignValue" );
770 Reference
<XPropertySet
> xCol
;
771 m_xColNames
->getByName(aColumnName
) >>= xCol
;
772 sal_Int32 nId
= Reference
<XColumnLocate
>(m_xColNames
,UNO_QUERY
)->findColumn(aColumnName
);
773 // Kommt diese Column ueberhaupt in der Datei vor?
777 // Diese Column gibt es nicht!
778 // aStatus.Set(SQL_STAT_ERROR,
779 // String::CreateFromAscii("S0022"),
780 // aStatus.CreateErrorMessage(String(SdbResId(STR_STAT_COLUMN_NOT_FOUND))),
782 throwFunctionSequenceException(*this);
785 // Value an die Row mit den zuzuweisenden Werten binden:
786 // const ODbVariantRef& xValue = (*aAssignValues)[pFileColumn->GetId()];
788 // Alles geprueft und wir haben den Namen der Column.
789 // Jetzt eine Value allozieren, den Wert setzen und die Value an die Row binden.
791 (m_aAssignValues
->get())[nId
]->setNull();
794 switch (::comphelper::getINT32(xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))))
796 // Kriterium je nach Typ als String oder double in die Variable packen ...
798 case DataType::VARCHAR
:
799 case DataType::LONGVARCHAR
:
800 *(m_aAssignValues
->get())[nId
] = ORowSetValue(aValue
);
801 // Zeichensatz ist bereits konvertiert, da ja das gesamte Statement konvertiert wurde
806 if (aValue
.EqualsIgnoreCaseAscii("TRUE") || aValue
.GetChar(0) == '1')
807 *(m_aAssignValues
->get())[nId
] = sal_True
;
808 else if (aValue
.EqualsIgnoreCaseAscii("FALSE") || aValue
.GetChar(0) == '0')
809 *(m_aAssignValues
->get())[nId
] = sal_False
;
812 // aStatus.Set(SQL_STAT_ERROR); // nyi: genauer!
813 throwFunctionSequenceException(*this);
817 case DataType::TINYINT
:
818 case DataType::SMALLINT
:
819 case DataType::INTEGER
:
820 case DataType::DECIMAL
:
821 case DataType::NUMERIC
:
823 case DataType::DOUBLE
:
826 case DataType::TIMESTAMP
:
828 *(m_aAssignValues
->get())[nId
] = ORowSetValue(aValue
); // .ToDouble
831 // double n = xValue->toDouble();
832 // xValue->setDouble(n);
836 // aStatus.SetDriverNotCapableError();
840 throwFunctionSequenceException(*this);
844 // Parameter-Nr. merken (als User Data)
845 // SQL_NO_PARAMETER = kein Parameter.
846 m_aAssignValues
->setParameterIndex(nId
,nParameter
);
847 if(nParameter
!= SQL_NO_PARAMETER
)
848 m_aParameterIndexes
[nParameter
] = nId
;
850 // -----------------------------------------------------------------------------
851 void OStatement_Base::parseParamterElem(const String
& /*_sColumnName*/,OSQLParseNode
* /*pRow_Value_Constructor_Elem*/)
853 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::parseParamterElem" );
856 // =============================================================================
858 // =============================================================================
859 }// namespace connectivity
860 // -----------------------------------------------------------------------------