Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / file / FStatement.cxx
blob754630e5d93fd1f21e15f363b949d3940592ccd8
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"
45 #include <algorithm>
46 #include <tools/debug.hxx>
47 #include <rtl/logfile.hxx>
49 namespace connectivity
51 namespace file
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)
72 ,m_pParseTree(NULL)
73 ,m_pSQLAnalyzer(NULL)
74 ,m_pEvaluationKeySet(NULL)
75 ,m_pTable(NULL)
76 ,m_nMaxFieldSize(0)
77 ,m_nMaxRows(0)
78 ,m_nQueryTimeOut(0)
79 ,m_nFetchSize(0)
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 );
108 disposing();
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);
119 if (xComp.is())
120 xComp->dispose();
121 m_xResultSet = Reference< XResultSet>();
123 //------------------------------------------------------------------------------
124 void OStatement_BASE2::disposing()
126 ::osl::MutexGuard aGuard(m_aMutex);
128 disposeResultSet();
130 if(m_pSQLAnalyzer)
131 m_pSQLAnalyzer->dispose();
133 if(m_aRow.is())
135 m_aRow->get().clear();
136 m_aRow = NULL;
139 m_aSQLIterator.dispose();
141 if(m_pTable)
143 m_pTable->release();
144 m_pTable = NULL;
147 if (m_pConnection)
149 m_pConnection->release();
150 m_pConnection = NULL;
153 dispose_ChildImpl();
155 if ( m_pParseTree )
157 delete m_pParseTree;
158 m_pParseTree = 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()
171 relase_ChildImpl();
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);
205 dispose();
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);
216 clearWarnings ();
218 if (m_xResultSet.get().is())
219 clearMyResultSet();
221 //--------------------------------------------------------------------
222 // clearMyResultSet
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 ) )
236 xCloseable->close();
238 catch( const DisposedException& ) { }
240 m_xResultSet = Reference< XResultSet>();
242 //--------------------------------------------------------------------
243 // setWarning
244 // Sets the warning
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);
254 m_aLastWarning = ex;
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 );
313 executeQuery(sql);
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);
325 construct(sql);
326 Reference< XResultSet > xRS;
327 OResultSet* pResult = createResultSet();
328 xRS = pResult;
329 initializeResultSet(pResult);
330 m_xResultSet = Reference<XResultSet>(pResult);
332 pResult->OpenImpl();
334 return xRS;
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);
348 construct(sql);
349 OResultSet* pResult = createResultSet();
350 Reference< XResultSet > xRS = pResult;
351 initializeResultSet(pResult);
352 pResult->OpenImpl();
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();
397 if(pOrderbyClause)
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 );
430 else
432 throw SQLException();
435 Reference<XColumnLocate> xColLocate(m_xColNames,UNO_QUERY);
436 if(!xColLocate.is())
437 return;
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);
457 if(m_pParseTree)
459 m_aSQLIterator.setParseTree(m_pParseTree);
460 m_aSQLIterator.traverseAll();
461 const OSQLTables& xTabs = m_aSQLIterator.getTables();
463 // sanity checks
464 if ( xTabs.empty() )
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);
482 break;
483 default:
484 break;
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);
489 if(xTunnel.is())
491 if(m_pTable)
492 m_pTable->release();
493 m_pTable = reinterpret_cast<OFileTable*>(xTunnel->getSomething(OFileTable::getUnoTunnelImplementationId()));
494 if(m_pTable)
495 m_pTable->acquire();
497 OSL_ENSURE(m_pTable,"No table!");
498 if ( m_pTable )
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);
522 if(xIndexSup.is())
523 m_pSQLAnalyzer->setIndexes(xIndexSup->getIndexes());
525 anylizeSQL();
527 else
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" );
548 GetAssignValues();
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);
570 return;
573 if (SQL_ISRULE(m_pParseTree,select_statement))
574 // no values have to be set for SELECT
575 return;
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);
583 // unbound all
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);
604 else
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);
624 // Values ...
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");
636 // List of values
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");
641 String aColumnName;
642 OSQLParseNode * pRow_Value_Const;
643 xub_StrLen nIndex=0;
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));
654 else
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++);
661 else
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);
672 // unbound all
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)
725 // set value:
726 SetAssignValue(aColumnName, pRow_Value_Constructor_Elem->getTokenValue());
728 else if (SQL_ISTOKEN(pRow_Value_Constructor_Elem,NULL))
730 // set NULL
731 SetAssignValue(aColumnName, String(), sal_True);
733 else if (SQL_ISRULE(pRow_Value_Constructor_Elem,parameter))
734 parseParamterElem(aColumnName,pRow_Value_Constructor_Elem);
735 else
737 throwFunctionSequenceException(*this);
740 //------------------------------------------------------------------
741 void OStatement_Base::SetAssignValue(const String& aColumnName,
742 const String& aValue,
743 sal_Bool bSetNull,
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?
752 if (!xCol.is())
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.
761 if (bSetNull)
762 (m_aAssignValues->get())[nId]->setNull();
763 else
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
768 case DataType::CHAR:
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
773 break;
775 case DataType::BIT:
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;
781 else
783 throwFunctionSequenceException(*this);
786 break;
787 case DataType::TINYINT:
788 case DataType::SMALLINT:
789 case DataType::INTEGER:
790 case DataType::DECIMAL:
791 case DataType::NUMERIC:
792 case DataType::REAL:
793 case DataType::DOUBLE:
794 case DataType::DATE:
795 case DataType::TIME:
796 case DataType::TIMESTAMP:
798 *(m_aAssignValues->get())[nId] = ORowSetValue(aValue);
799 } break;
800 default:
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" );
815 // do nothing here
817 // =============================================================================
818 } // namespace file
819 // =============================================================================
820 }// namespace connectivity
821 // -----------------------------------------------------------------------------
823 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */