1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/diagnose.h>
30 #include "file/FStatement.hxx"
31 #include "file/FConnection.hxx"
32 #include "file/FDriver.hxx"
33 #include "file/FResultSet.hxx"
34 #include <comphelper/property.hxx>
35 #include <comphelper/uno3.hxx>
36 #include <osl/thread.h>
37 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
38 #include <com/sun/star/sdbc/ResultSetType.hpp>
39 #include <com/sun/star/sdbc/FetchDirection.hpp>
40 #include <com/sun/star/lang/DisposedException.hpp>
41 #include <comphelper/sequence.hxx>
42 #include <cppuhelper/typeprovider.hxx>
43 #include "connectivity/dbexception.hxx"
44 #include "resource/file_res.hrc"
46 #include <tools/debug.hxx>
47 #include <rtl/logfile.hxx>
49 namespace connectivity
54 //------------------------------------------------------------------------------
55 using namespace dbtools
;
56 using namespace com::sun::star::uno
;
57 using namespace com::sun::star::lang
;
58 using namespace com::sun::star::beans
;
59 using namespace com::sun::star::sdbc
;
60 using namespace com::sun::star::sdbcx
;
61 using namespace com::sun::star::container
;
62 DBG_NAME( file_OStatement_Base
)
64 //------------------------------------------------------------------------------
65 OStatement_Base::OStatement_Base(OConnection
* _pConnection
)
66 :OStatement_BASE(m_aMutex
)
67 ,::comphelper::OPropertyContainer(OStatement_BASE::rBHelper
)
68 ,m_xDBMetaData(_pConnection
->getMetaData())
69 ,m_aParser(_pConnection
->getDriver()->getFactory())
70 ,m_aSQLIterator( _pConnection
, _pConnection
->createCatalog()->getTables(), m_aParser
, NULL
)
71 ,m_pConnection(_pConnection
)
74 ,m_pEvaluationKeySet(NULL
)
80 ,m_nResultSetType(ResultSetType::FORWARD_ONLY
)
81 ,m_nFetchDirection(FetchDirection::FORWARD
)
82 ,m_nResultSetConcurrency(ResultSetConcurrency::UPDATABLE
)
83 ,m_bEscapeProcessing(sal_True
)
84 ,rBHelper(OStatement_BASE::rBHelper
)
86 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::OStatement_Base" );
87 DBG_CTOR( file_OStatement_Base
, NULL
);
89 m_pConnection
->acquire();
91 sal_Int32 nAttrib
= 0;
93 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME
), PROPERTY_ID_CURSORNAME
, nAttrib
,&m_aCursorName
, ::getCppuType(reinterpret_cast< ::rtl::OUString
*>(NULL
)));
94 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE
), PROPERTY_ID_MAXFIELDSIZE
, nAttrib
,&m_nMaxFieldSize
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
95 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS
), PROPERTY_ID_MAXROWS
, nAttrib
,&m_nMaxRows
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
96 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT
), PROPERTY_ID_QUERYTIMEOUT
, nAttrib
,&m_nQueryTimeOut
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
97 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE
), PROPERTY_ID_FETCHSIZE
, nAttrib
,&m_nFetchSize
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
98 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE
), PROPERTY_ID_RESULTSETTYPE
, nAttrib
,&m_nResultSetType
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
99 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION
), PROPERTY_ID_FETCHDIRECTION
, nAttrib
,&m_nFetchDirection
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
100 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING
),PROPERTY_ID_ESCAPEPROCESSING
, nAttrib
,&m_bEscapeProcessing
,::getCppuBooleanType());
102 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY
), PROPERTY_ID_RESULTSETCONCURRENCY
, nAttrib
,&m_nResultSetConcurrency
, ::getCppuType(reinterpret_cast<sal_Int32
*>(NULL
)));
104 // -----------------------------------------------------------------------------
105 OStatement_Base::~OStatement_Base()
107 osl_incrementInterlockedCount( &m_refCount
);
109 delete m_pSQLAnalyzer
;
111 DBG_DTOR( file_OStatement_Base
, NULL
);
113 //------------------------------------------------------------------------------
114 void OStatement_Base::disposeResultSet()
116 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposeResultSet" );
117 // free the cursor if alive
118 Reference
< XComponent
> xComp(m_xResultSet
.get(), UNO_QUERY
);
121 m_xResultSet
= Reference
< XResultSet
>();
123 //------------------------------------------------------------------------------
124 void OStatement_BASE2::disposing()
126 ::osl::MutexGuard
aGuard(m_aMutex
);
131 m_pSQLAnalyzer
->dispose();
135 m_aRow
->get().clear();
139 m_aSQLIterator
.dispose();
149 m_pConnection
->release();
150 m_pConnection
= NULL
;
161 OStatement_Base::disposing();
163 // -----------------------------------------------------------------------------
164 void SAL_CALL
OStatement_Base::acquire() throw()
166 OStatement_BASE::acquire();
168 //-----------------------------------------------------------------------------
169 void SAL_CALL
OStatement_BASE2::release() throw()
173 //-----------------------------------------------------------------------------
174 Any SAL_CALL
OStatement_Base::queryInterface( const Type
& rType
) throw(RuntimeException
)
176 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::queryInterface" );
177 const Any aRet
= OStatement_BASE::queryInterface(rType
);
178 return aRet
.hasValue() ? aRet
: OPropertySetHelper::queryInterface(rType
);
180 // -------------------------------------------------------------------------
181 Sequence
< Type
> SAL_CALL
OStatement_Base::getTypes( ) throw(RuntimeException
)
183 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getTypes" );
184 ::cppu::OTypeCollection
aTypes( ::getCppuType( (const Reference
< ::com::sun::star::beans::XMultiPropertySet
> *)0 ),
185 ::getCppuType( (const Reference
< ::com::sun::star::beans::XFastPropertySet
> *)0 ),
186 ::getCppuType( (const Reference
< ::com::sun::star::beans::XPropertySet
> *)0 ));
188 return ::comphelper::concatSequences(aTypes
.getTypes(),OStatement_BASE::getTypes());
190 // -------------------------------------------------------------------------
192 void SAL_CALL
OStatement_Base::cancel( ) throw(RuntimeException
)
194 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::cancel" );
196 // -------------------------------------------------------------------------
198 void SAL_CALL
OStatement_Base::close( ) throw(SQLException
, RuntimeException
)
200 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::close" );
202 ::osl::MutexGuard
aGuard( m_aMutex
);
203 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
207 // -------------------------------------------------------------------------
209 void OStatement_Base::reset() throw (SQLException
)
211 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::reset" );
212 ::osl::MutexGuard
aGuard( m_aMutex
);
213 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
218 if (m_xResultSet
.get().is())
221 //--------------------------------------------------------------------
223 // If a ResultSet was created for this Statement, close it
224 //--------------------------------------------------------------------
226 void OStatement_Base::clearMyResultSet () throw (SQLException
)
228 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearMyResultSet " );
229 ::osl::MutexGuard
aGuard( m_aMutex
);
230 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
234 Reference
<XCloseable
> xCloseable
;
235 if ( ::comphelper::query_interface( m_xResultSet
.get(), xCloseable
) )
238 catch( const DisposedException
& ) { }
240 m_xResultSet
= Reference
< XResultSet
>();
242 //--------------------------------------------------------------------
245 //--------------------------------------------------------------------
247 void OStatement_Base::setWarning (const SQLWarning
&ex
) throw( SQLException
)
249 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setWarning " );
250 ::osl::MutexGuard
aGuard( m_aMutex
);
251 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
257 // -------------------------------------------------------------------------
258 Any SAL_CALL
OStatement_Base::getWarnings( ) throw(SQLException
, RuntimeException
)
260 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getWarnings" );
261 ::osl::MutexGuard
aGuard( m_aMutex
);
262 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
264 return makeAny(m_aLastWarning
);
266 // -------------------------------------------------------------------------
267 void SAL_CALL
OStatement_Base::clearWarnings( ) throw(SQLException
, RuntimeException
)
269 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearWarnings" );
270 ::osl::MutexGuard
aGuard( m_aMutex
);
271 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
273 m_aLastWarning
= SQLWarning();
275 // -------------------------------------------------------------------------
276 ::cppu::IPropertyArrayHelper
* OStatement_Base::createArrayHelper( ) const
278 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createArrayHelper" );
279 Sequence
< Property
> aProps
;
280 describeProperties(aProps
);
281 return new ::cppu::OPropertyArrayHelper(aProps
);
284 // -------------------------------------------------------------------------
285 ::cppu::IPropertyArrayHelper
& OStatement_Base::getInfoHelper()
287 //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getInfoHelper" );
288 return *const_cast<OStatement_Base
*>(this)->getArrayHelper();
290 // -------------------------------------------------------------------------
291 OResultSet
* OStatement::createResultSet()
293 return new OResultSet(this,m_aSQLIterator
);
295 // -------------------------------------------------------------------------
296 IMPLEMENT_SERVICE_INFO(OStatement
,"com.sun.star.sdbc.driver.file.Statement","com.sun.star.sdbc.Statement");
297 // -----------------------------------------------------------------------------
298 void SAL_CALL
OStatement::acquire() throw()
300 OStatement_BASE2::acquire();
302 // -----------------------------------------------------------------------------
303 void SAL_CALL
OStatement::release() throw()
305 OStatement_BASE2::release();
307 // -----------------------------------------------------------------------------
308 // -------------------------------------------------------------------------
309 sal_Bool SAL_CALL
OStatement::execute( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
311 ::osl::MutexGuard
aGuard( m_aMutex
);
315 return m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
;
318 // -------------------------------------------------------------------------
320 Reference
< XResultSet
> SAL_CALL
OStatement::executeQuery( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
322 ::osl::MutexGuard
aGuard( m_aMutex
);
323 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
326 Reference
< XResultSet
> xRS
;
327 OResultSet
* pResult
= createResultSet();
329 initializeResultSet(pResult
);
330 m_xResultSet
= Reference
<XResultSet
>(pResult
);
336 // -------------------------------------------------------------------------
337 Reference
< XConnection
> SAL_CALL
OStatement::getConnection( ) throw(SQLException
, RuntimeException
)
339 return (Reference
< XConnection
>)m_pConnection
;
341 // -------------------------------------------------------------------------
342 sal_Int32 SAL_CALL
OStatement::executeUpdate( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
344 ::osl::MutexGuard
aGuard( m_aMutex
);
345 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
349 OResultSet
* pResult
= createResultSet();
350 Reference
< XResultSet
> xRS
= pResult
;
351 initializeResultSet(pResult
);
354 return pResult
->getRowCountResult();
357 // -----------------------------------------------------------------------------
358 void SAL_CALL
OStatement_Base::disposing(void)
360 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposing" );
361 if(m_aEvaluateRow
.is())
363 m_aEvaluateRow
->get().clear();
364 m_aEvaluateRow
= NULL
;
366 delete m_pEvaluationKeySet
;
367 OStatement_BASE::disposing();
369 // -----------------------------------------------------------------------------
370 Reference
< ::com::sun::star::beans::XPropertySetInfo
> SAL_CALL
OStatement_Base::getPropertySetInfo( ) throw(RuntimeException
)
372 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getPropertySetInfo" );
373 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
375 // -----------------------------------------------------------------------------
376 Any SAL_CALL
OStatement::queryInterface( const Type
& rType
) throw(RuntimeException
)
378 Any aRet
= OStatement_XStatement::queryInterface( rType
);
379 return aRet
.hasValue() ? aRet
: OStatement_BASE2::queryInterface( rType
);
381 // -----------------------------------------------------------------------------
382 OSQLAnalyzer
* OStatement_Base::createAnalyzer()
384 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createAnalyzer" );
385 return new OSQLAnalyzer(m_pConnection
);
387 // -----------------------------------------------------------------------------
388 void OStatement_Base::anylizeSQL()
390 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::anylizeSQL" );
391 OSL_ENSURE(m_pSQLAnalyzer
,"OResultSet::anylizeSQL: Analyzer isn't set!");
392 // start analysing the statement
393 m_pSQLAnalyzer
->setOrigColumns(m_xColNames
);
394 m_pSQLAnalyzer
->start(m_pParseTree
);
396 const OSQLParseNode
* pOrderbyClause
= m_aSQLIterator
.getOrderTree();
399 OSQLParseNode
* pOrderingSpecCommalist
= pOrderbyClause
->getChild(2);
400 OSL_ENSURE(SQL_ISRULE(pOrderingSpecCommalist
,ordering_spec_commalist
),"OResultSet: Fehler im Parse Tree");
402 for (sal_uInt32 m
= 0; m
< pOrderingSpecCommalist
->count(); m
++)
404 OSQLParseNode
* pOrderingSpec
= pOrderingSpecCommalist
->getChild(m
);
405 OSL_ENSURE(SQL_ISRULE(pOrderingSpec
,ordering_spec
),"OResultSet: Fehler im Parse Tree");
406 OSL_ENSURE(pOrderingSpec
->count() == 2,"OResultSet: Fehler im Parse Tree");
408 OSQLParseNode
* pColumnRef
= pOrderingSpec
->getChild(0);
409 if(!SQL_ISRULE(pColumnRef
,column_ref
))
411 throw SQLException();
413 OSQLParseNode
* pAscendingDescending
= pOrderingSpec
->getChild(1);
414 setOrderbyColumn(pColumnRef
,pAscendingDescending
);
418 //------------------------------------------------------------------
419 void OStatement_Base::setOrderbyColumn( OSQLParseNode
* pColumnRef
,
420 OSQLParseNode
* pAscendingDescending
)
422 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setOrderbyColumn" );
423 ::rtl::OUString aColumnName
;
424 if (pColumnRef
->count() == 1)
425 aColumnName
= pColumnRef
->getChild(0)->getTokenValue();
426 else if (pColumnRef
->count() == 3)
428 pColumnRef
->getChild(2)->parseNodeToStr( aColumnName
, getOwnConnection(), NULL
, sal_False
, sal_False
);
432 throw SQLException();
435 Reference
<XColumnLocate
> xColLocate(m_xColNames
,UNO_QUERY
);
438 // Everything tested and we have the name of the Column.
439 // What number is the Column?
440 ::rtl::Reference
<OSQLColumns
> aSelectColumns
= m_aSQLIterator
.getSelectColumns();
441 ::comphelper::UStringMixEqual aCase
;
442 OSQLColumns::Vector::const_iterator aFind
= ::connectivity::find(aSelectColumns
->get().begin(),aSelectColumns
->get().end(),aColumnName
,aCase
);
443 if ( aFind
== aSelectColumns
->get().end() )
444 throw SQLException();
445 m_aOrderbyColumnNumber
.push_back((aFind
- aSelectColumns
->get().begin()) + 1);
447 // Ascending or Descending?
448 m_aOrderbyAscending
.push_back((SQL_ISTOKEN(pAscendingDescending
,DESC
)) ? SQL_DESC
: SQL_ASC
);
451 // -----------------------------------------------------------------------------
452 void OStatement_Base::construct(const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
454 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::construct" );
455 ::rtl::OUString aErr
;
456 m_pParseTree
= m_aParser
.parseTree(aErr
,sql
);
459 m_aSQLIterator
.setParseTree(m_pParseTree
);
460 m_aSQLIterator
.traverseAll();
461 const OSQLTables
& xTabs
= m_aSQLIterator
.getTables();
465 // no tables -> nothing to operate on -> error
466 m_pConnection
->throwGenericSQLException(STR_QUERY_NO_TABLE
,*this);
468 if ( xTabs
.size() > 1 || m_aSQLIterator
.hasErrors() )
469 // more than one table -> can't operate on them -> error
470 m_pConnection
->throwGenericSQLException(STR_QUERY_MORE_TABLES
,*this);
472 if ( (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
) && m_aSQLIterator
.getSelectColumns()->get().empty() )
473 // SELECT statement without columns -> error
474 m_pConnection
->throwGenericSQLException(STR_QUERY_NO_COLUMN
,*this);
476 switch(m_aSQLIterator
.getStatementType())
478 case SQL_STATEMENT_CREATE_TABLE
:
479 case SQL_STATEMENT_ODBC_CALL
:
480 case SQL_STATEMENT_UNKNOWN
:
481 m_pConnection
->throwGenericSQLException(STR_QUERY_TOO_COMPLEX
,*this);
487 // at this moment we support only one table per select statement
488 Reference
< ::com::sun::star::lang::XUnoTunnel
> xTunnel(xTabs
.begin()->second
,UNO_QUERY
);
493 m_pTable
= reinterpret_cast<OFileTable
*>(xTunnel
->getSomething(OFileTable::getUnoTunnelImplementationId()));
497 OSL_ENSURE(m_pTable
,"No table!");
499 m_xColNames
= m_pTable
->getColumns();
500 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
501 // set the binding of the resultrow
502 m_aRow
= new OValueRefVector(xNames
->getCount());
503 (m_aRow
->get())[0]->setBound(sal_True
);
504 ::std::for_each(m_aRow
->get().begin()+1,m_aRow
->get().end(),TSetRefBound(sal_False
));
506 // set the binding of the resultrow
507 m_aEvaluateRow
= new OValueRefVector(xNames
->getCount());
509 (m_aEvaluateRow
->get())[0]->setBound(sal_True
);
510 ::std::for_each(m_aEvaluateRow
->get().begin()+1,m_aEvaluateRow
->get().end(),TSetRefBound(sal_False
));
512 // set the select row
513 m_aSelectRow
= new OValueRefVector(m_aSQLIterator
.getSelectColumns()->get().size());
514 ::std::for_each(m_aSelectRow
->get().begin(),m_aSelectRow
->get().end(),TSetRefBound(sal_True
));
516 // create the column mapping
517 createColumnMapping();
519 m_pSQLAnalyzer
= createAnalyzer();
521 Reference
<XIndexesSupplier
> xIndexSup(xTunnel
,UNO_QUERY
);
523 m_pSQLAnalyzer
->setIndexes(xIndexSup
->getIndexes());
528 throw SQLException(aErr
,*this,::rtl::OUString(),0,Any());
530 // -----------------------------------------------------------------------------
531 void OStatement_Base::createColumnMapping()
533 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createColumnMapping" );
534 // initialize the column index map (mapping select columns to table columns)
535 ::rtl::Reference
<connectivity::OSQLColumns
> xColumns
= m_aSQLIterator
.getSelectColumns();
536 m_aColMapping
.resize(xColumns
->get().size() + 1);
537 for (sal_Int32 i
=0; i
<(sal_Int32
)m_aColMapping
.size(); ++i
)
538 m_aColMapping
[i
] = i
;
540 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
541 // now check which columns are bound
542 OResultSet::setBoundedColumns(m_aRow
,m_aSelectRow
,xColumns
,xNames
,sal_True
,m_xDBMetaData
,m_aColMapping
);
544 // -----------------------------------------------------------------------------
545 void OStatement_Base::initializeResultSet(OResultSet
* _pResult
)
547 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::initializeResultSet" );
550 _pResult
->setSqlAnalyzer(m_pSQLAnalyzer
);
551 _pResult
->setOrderByColumns(m_aOrderbyColumnNumber
);
552 _pResult
->setOrderByAscending(m_aOrderbyAscending
);
553 _pResult
->setBindingRow(m_aRow
);
554 _pResult
->setColumnMapping(m_aColMapping
);
555 _pResult
->setEvaluationRow(m_aEvaluateRow
);
556 _pResult
->setAssignValues(m_aAssignValues
);
557 _pResult
->setSelectRow(m_aSelectRow
);
559 m_pSQLAnalyzer
->bindSelectRow(m_aRow
);
560 m_pEvaluationKeySet
= m_pSQLAnalyzer
->bindEvaluationRow(m_aEvaluateRow
); // Set values in the code of the Compiler
561 _pResult
->setEvaluationKeySet(m_pEvaluationKeySet
);
563 // -----------------------------------------------------------------------------
564 void OStatement_Base::GetAssignValues()
566 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::GetAssignValues" );
567 if (m_pParseTree
== NULL
)
569 ::dbtools::throwFunctionSequenceException(*this);
573 if (SQL_ISRULE(m_pParseTree
,select_statement
))
574 // no values have to be set for SELECT
576 else if (SQL_ISRULE(m_pParseTree
,insert_statement
))
578 // Create Row for the values to be set (Reference trough new)
579 if(m_aAssignValues
.is())
580 m_aAssignValues
->get().clear();
581 sal_Int32 nCount
= Reference
<XIndexAccess
>(m_xColNames
,UNO_QUERY
)->getCount();
582 m_aAssignValues
= new OAssignValues(nCount
);
584 ::std::for_each(m_aAssignValues
->get().begin()+1,m_aAssignValues
->get().end(),TSetRefBound(sal_False
));
586 m_aParameterIndexes
.resize(nCount
+1,SQL_NO_PARAMETER
);
588 // List of Column-Names, that exist in the column_commalist (seperated by ;):
589 ::std::vector
<String
> aColumnNameList
;
591 OSL_ENSURE(m_pParseTree
->count() >= 4,"OResultSet: Fehler im Parse Tree");
593 OSQLParseNode
* pOptColumnCommalist
= m_pParseTree
->getChild(3);
594 OSL_ENSURE(pOptColumnCommalist
!= NULL
,"OResultSet: Fehler im Parse Tree");
595 OSL_ENSURE(SQL_ISRULE(pOptColumnCommalist
,opt_column_commalist
),"OResultSet: Fehler im Parse Tree");
596 if (pOptColumnCommalist
->count() == 0)
598 const Sequence
< ::rtl::OUString
>& aNames
= m_xColNames
->getElementNames();
599 const ::rtl::OUString
* pBegin
= aNames
.getConstArray();
600 const ::rtl::OUString
* pEnd
= pBegin
+ aNames
.getLength();
601 for (; pBegin
!= pEnd
; ++pBegin
)
602 aColumnNameList
.push_back(*pBegin
);
606 OSL_ENSURE(pOptColumnCommalist
->count() == 3,"OResultSet: Fehler im Parse Tree");
608 OSQLParseNode
* pColumnCommalist
= pOptColumnCommalist
->getChild(1);
609 OSL_ENSURE(pColumnCommalist
!= NULL
,"OResultSet: Fehler im Parse Tree");
610 OSL_ENSURE(SQL_ISRULE(pColumnCommalist
,column_commalist
),"OResultSet: Fehler im Parse Tree");
611 OSL_ENSURE(pColumnCommalist
->count() > 0,"OResultSet: Fehler im Parse Tree");
613 // All Columns in the column_commalist ...
614 for (sal_uInt32 i
= 0; i
< pColumnCommalist
->count(); i
++)
616 OSQLParseNode
* pCol
= pColumnCommalist
->getChild(i
);
617 OSL_ENSURE(pCol
!= NULL
,"OResultSet: Fehler im Parse Tree");
618 aColumnNameList
.push_back(pCol
->getTokenValue());
621 if ( aColumnNameList
.empty() )
622 throwFunctionSequenceException(*this);
625 OSQLParseNode
* pValuesOrQuerySpec
= m_pParseTree
->getChild(4);
626 OSL_ENSURE(pValuesOrQuerySpec
!= NULL
,"OResultSet: pValuesOrQuerySpec darf nicht NULL sein!");
627 OSL_ENSURE(SQL_ISRULE(pValuesOrQuerySpec
,values_or_query_spec
),"OResultSet: ! SQL_ISRULE(pValuesOrQuerySpec,values_or_query_spec)");
628 OSL_ENSURE(pValuesOrQuerySpec
->count() > 0,"OResultSet: pValuesOrQuerySpec->count() <= 0");
630 // just "VALUES" is allowed ...
631 if (! SQL_ISTOKEN(pValuesOrQuerySpec
->getChild(0),VALUES
))
632 throwFunctionSequenceException(*this);
634 OSL_ENSURE(pValuesOrQuerySpec
->count() == 4,"OResultSet: pValuesOrQuerySpec->count() != 4");
637 OSQLParseNode
* pInsertAtomCommalist
= pValuesOrQuerySpec
->getChild(2);
638 OSL_ENSURE(pInsertAtomCommalist
!= NULL
,"OResultSet: pInsertAtomCommalist darf nicht NULL sein!");
639 OSL_ENSURE(pInsertAtomCommalist
->count() > 0,"OResultSet: pInsertAtomCommalist <= 0");
642 OSQLParseNode
* pRow_Value_Const
;
644 for (sal_uInt32 i
= 0; i
< pInsertAtomCommalist
->count(); i
++)
646 pRow_Value_Const
= pInsertAtomCommalist
->getChild(i
); // row_value_constructor
647 OSL_ENSURE(pRow_Value_Const
!= NULL
,"OResultSet: pRow_Value_Const darf nicht NULL sein!");
648 if(SQL_ISRULE(pRow_Value_Const
,parameter
))
650 ParseAssignValues(aColumnNameList
,pRow_Value_Const
,nIndex
++); // only one Columnname allowed per loop
652 else if(pRow_Value_Const
->isToken())
653 ParseAssignValues(aColumnNameList
,pRow_Value_Const
,static_cast<xub_StrLen
>(i
));
656 if(pRow_Value_Const
->count() == aColumnNameList
.size())
658 for (sal_uInt32 j
= 0; j
< pRow_Value_Const
->count(); ++j
)
659 ParseAssignValues(aColumnNameList
,pRow_Value_Const
->getChild(j
),nIndex
++);
662 throwFunctionSequenceException(*this);
666 else if (SQL_ISRULE(m_pParseTree
,update_statement_searched
))
668 if(m_aAssignValues
.is())
669 m_aAssignValues
->get().clear();
670 sal_Int32 nCount
= Reference
<XIndexAccess
>(m_xColNames
,UNO_QUERY
)->getCount();
671 m_aAssignValues
= new OAssignValues(nCount
);
673 ::std::for_each(m_aAssignValues
->get().begin()+1,m_aAssignValues
->get().end(),TSetRefBound(sal_False
));
675 m_aParameterIndexes
.resize(nCount
+1,SQL_NO_PARAMETER
);
677 OSL_ENSURE(m_pParseTree
->count() >= 4,"OResultSet: Fehler im Parse Tree");
679 OSQLParseNode
* pAssignmentCommalist
= m_pParseTree
->getChild(3);
680 OSL_ENSURE(pAssignmentCommalist
!= NULL
,"OResultSet: pAssignmentCommalist == NULL");
681 OSL_ENSURE(SQL_ISRULE(pAssignmentCommalist
,assignment_commalist
),"OResultSet: Fehler im Parse Tree");
682 OSL_ENSURE(pAssignmentCommalist
->count() > 0,"OResultSet: pAssignmentCommalist->count() <= 0");
684 // work on all assignments (commalist) ...
685 ::std::vector
< String
> aList(1);
686 for (sal_uInt32 i
= 0; i
< pAssignmentCommalist
->count(); i
++)
688 OSQLParseNode
* pAssignment
= pAssignmentCommalist
->getChild(i
);
689 OSL_ENSURE(pAssignment
!= NULL
,"OResultSet: pAssignment == NULL");
690 OSL_ENSURE(SQL_ISRULE(pAssignment
,assignment
),"OResultSet: Fehler im Parse Tree");
691 OSL_ENSURE(pAssignment
->count() == 3,"OResultSet: pAssignment->count() != 3");
693 OSQLParseNode
* pCol
= pAssignment
->getChild(0);
694 OSL_ENSURE(pCol
!= NULL
,"OResultSet: pCol == NULL");
696 OSQLParseNode
* pComp
= pAssignment
->getChild(1);
697 OSL_ENSURE(pComp
!= NULL
,"OResultSet: pComp == NULL");
698 OSL_ENSURE(pComp
->getNodeType() == SQL_NODE_EQUAL
,"OResultSet: pComp->getNodeType() != SQL_NODE_COMPARISON");
699 if (pComp
->getTokenValue().toChar() != '=')
701 throwFunctionSequenceException(*this);
704 OSQLParseNode
* pVal
= pAssignment
->getChild(2);
705 OSL_ENSURE(pVal
!= NULL
,"OResultSet: pVal == NULL");
706 aList
[0] = pCol
->getTokenValue();
707 ParseAssignValues(aList
,pVal
,0);
712 // -------------------------------------------------------------------------
713 void OStatement_Base::ParseAssignValues(const ::std::vector
< String
>& aColumnNameList
,OSQLParseNode
* pRow_Value_Constructor_Elem
,xub_StrLen nIndex
)
715 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::ParseAssignValues" );
716 OSL_ENSURE(nIndex
<= aColumnNameList
.size(),"SdbFileCursor::ParseAssignValues: nIndex > aColumnNameList.GetTokenCount()");
717 String
aColumnName(aColumnNameList
[nIndex
]);
718 OSL_ENSURE(aColumnName
.Len() > 0,"OResultSet: Column-Name nicht gefunden");
719 OSL_ENSURE(pRow_Value_Constructor_Elem
!= NULL
,"OResultSet: pRow_Value_Constructor_Elem darf nicht NULL sein!");
721 if (pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_STRING
||
722 pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_INTNUM
||
723 pRow_Value_Constructor_Elem
->getNodeType() == SQL_NODE_APPROXNUM
)
726 SetAssignValue(aColumnName
, pRow_Value_Constructor_Elem
->getTokenValue());
728 else if (SQL_ISTOKEN(pRow_Value_Constructor_Elem
,NULL
))
731 SetAssignValue(aColumnName
, String(), sal_True
);
733 else if (SQL_ISRULE(pRow_Value_Constructor_Elem
,parameter
))
734 parseParamterElem(aColumnName
,pRow_Value_Constructor_Elem
);
737 throwFunctionSequenceException(*this);
740 //------------------------------------------------------------------
741 void OStatement_Base::SetAssignValue(const String
& aColumnName
,
742 const String
& aValue
,
744 sal_uInt32 nParameter
)
746 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::SetAssignValue" );
747 Reference
<XPropertySet
> xCol
;
748 m_xColNames
->getByName(aColumnName
) >>= xCol
;
749 sal_Int32 nId
= Reference
<XColumnLocate
>(m_xColNames
,UNO_QUERY
)->findColumn(aColumnName
);
750 // does this column actually exist in the file?
754 // This Column doesn't exist!
755 throwFunctionSequenceException(*this);
759 // Everything tested and we have the names of the Column.
760 // Now allocate one Value, set the value and tie the value to the Row.
762 (m_aAssignValues
->get())[nId
]->setNull();
765 switch (::comphelper::getINT32(xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))))
767 // put criteria depending on the Type as String or double in the variable
769 case DataType::VARCHAR
:
770 case DataType::LONGVARCHAR
:
771 *(m_aAssignValues
->get())[nId
] = ORowSetValue(aValue
);
772 //Characterset is already converted, since the entire statement was converted
777 if (aValue
.EqualsIgnoreCaseAscii("TRUE") || aValue
.GetChar(0) == '1')
778 *(m_aAssignValues
->get())[nId
] = sal_True
;
779 else if (aValue
.EqualsIgnoreCaseAscii("FALSE") || aValue
.GetChar(0) == '0')
780 *(m_aAssignValues
->get())[nId
] = sal_False
;
783 throwFunctionSequenceException(*this);
787 case DataType::TINYINT
:
788 case DataType::SMALLINT
:
789 case DataType::INTEGER
:
790 case DataType::DECIMAL
:
791 case DataType::NUMERIC
:
793 case DataType::DOUBLE
:
796 case DataType::TIMESTAMP
:
798 *(m_aAssignValues
->get())[nId
] = ORowSetValue(aValue
);
801 throwFunctionSequenceException(*this);
805 // save Parameter-No. (as User Data)
806 // SQL_NO_PARAMETER = no Parameter.
807 m_aAssignValues
->setParameterIndex(nId
,nParameter
);
808 if(nParameter
!= SQL_NO_PARAMETER
)
809 m_aParameterIndexes
[nParameter
] = nId
;
811 // -----------------------------------------------------------------------------
812 void OStatement_Base::parseParamterElem(const String
& /*_sColumnName*/,OSQLParseNode
* /*pRow_Value_Constructor_Elem*/)
814 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "file", "Ocke.Janssen@sun.com", "OStatement_Base::parseParamterElem" );
817 // =============================================================================
819 // =============================================================================
820 }// namespace connectivity
821 // -----------------------------------------------------------------------------
823 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */