1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "MacabStatement.hxx"
22 #include "sqlbison.hxx"
23 #include "MacabConnection.hxx"
24 #include "MacabAddressBook.hxx"
25 #include "MacabDriver.hxx"
26 #include "MacabResultSet.hxx"
27 #include "MacabResultSetMetaData.hxx"
28 #include "macabcondition.hxx"
29 #include "macaborder.hxx"
30 #include "TConnection.hxx"
31 #include <connectivity/dbexception.hxx>
32 #include "resource/sharedresources.hxx"
33 #include "resource/macab_res.hrc"
35 #if OSL_DEBUG_LEVEL > 0
36 # define OUtoCStr( x ) ( OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
37 #else /* OSL_DEBUG_LEVEL */
38 # define OUtoCStr( x ) ("dummy")
39 #endif /* OSL_DEBUG_LEVEL */
41 using namespace connectivity::macab
;
42 using namespace com::sun::star::uno
;
43 using namespace com::sun::star::lang
;
44 using namespace com::sun::star::beans
;
45 using namespace com::sun::star::sdbc
;
46 using namespace com::sun::star::sdbcx
;
47 using namespace com::sun::star::container
;
48 using namespace com::sun::star::io
;
49 using namespace com::sun::star::util
;
51 namespace connectivity
55 void impl_throwError(sal_uInt16 _nErrorId
)
57 ::connectivity::SharedResources aResources
;
58 const OUString
sError( aResources
.getResourceString(_nErrorId
) );
59 ::dbtools::throwGenericSQLException(sError
,NULL
);
64 IMPLEMENT_SERVICE_INFO(MacabStatement
, "com.sun.star.sdbc.drivers.MacabStatement", "com.sun.star.sdbc.Statement");
66 MacabCommonStatement::MacabCommonStatement(MacabConnection
* _pConnection
)
67 : MacabCommonStatement_BASE(m_aMutex
),
68 OPropertySetHelper(MacabCommonStatement_BASE::rBHelper
),
69 m_aParser(_pConnection
->getDriver()->getComponentContext()),
70 m_aSQLIterator(_pConnection
, _pConnection
->createCatalog()->getTables(), m_aParser
, NULL
),
72 m_pConnection(_pConnection
),
73 rBHelper(MacabCommonStatement_BASE::rBHelper
)
75 m_pConnection
->acquire();
78 MacabCommonStatement::~MacabCommonStatement()
82 void MacabCommonStatement::disposing()
84 MacabCommonStatement_BASE::disposing();
87 void MacabCommonStatement::resetParameters() const throw(::com::sun::star::sdbc::SQLException
)
91 void MacabCommonStatement::getNextParameter(OUString
&) const throw(::com::sun::star::sdbc::SQLException
)
93 impl_throwError(STR_PARA_ONLY_PREPARED
);
96 MacabCondition
*MacabCommonStatement::analyseWhereClause(const OSQLParseNode
*pParseNode
) const throw(SQLException
)
98 if (pParseNode
->count() == 3)
100 const OSQLParseNode
*pLeft
= pParseNode
->getChild(0),
101 *pMiddle
= pParseNode
->getChild(1),
102 *pRight
= pParseNode
->getChild(2);
105 if (SQL_ISPUNCTUATION(pLeft
, "(") && SQL_ISPUNCTUATION(pRight
, ")"))
107 return analyseWhereClause(pMiddle
);
109 else if (SQL_ISRULE(pParseNode
, comparison_predicate
))
111 if (pLeft
->isToken() && pRight
->isToken())
113 switch (pMiddle
->getNodeType())
117 return new MacabConditionConstant(pLeft
->getTokenValue() == pRight
->getTokenValue());
119 case SQL_NODE_NOTEQUAL
:
121 // (might not be correct SQL... don't care, handling anyway)
122 return new MacabConditionConstant(pLeft
->getTokenValue() != pRight
->getTokenValue());
128 else if (SQL_ISRULE(pLeft
, column_ref
))
130 OUString sColumnName
,
133 m_aSQLIterator
.getColumnRange(pLeft
, sColumnName
, sTableRange
);
135 if (pRight
->isToken() || SQL_ISRULE(pRight
, parameter
))
137 OUString sMatchString
;
139 if (pRight
->isToken()) // WHERE Name = 'Doe'
140 sMatchString
= pRight
->getTokenValue();
141 else if (SQL_ISRULE(pRight
, parameter
)) // WHERE Name = ?
142 getNextParameter(sMatchString
);
144 switch (pMiddle
->getNodeType())
147 // WHERE Name = 'Smith'
148 return new MacabConditionEqual(m_pHeader
, sColumnName
, sMatchString
);
150 case SQL_NODE_NOTEQUAL
:
151 // WHERE Name <> 'Jones'
152 return new MacabConditionDifferent(m_pHeader
, sColumnName
, sMatchString
);
160 else if (SQL_ISRULE(pParseNode
, search_condition
))
162 if (SQL_ISTOKEN(pMiddle
, OR
))
164 // WHERE Name = 'Smith' OR Name = 'Jones'
165 return new MacabConditionOr(
166 analyseWhereClause(pLeft
),
167 analyseWhereClause(pRight
));
170 else if (SQL_ISRULE(pParseNode
, boolean_term
))
172 if (SQL_ISTOKEN(pMiddle
, AND
))
174 // WHERE Name = 'Smith' AND "Given Name" = 'Peter'
175 return new MacabConditionAnd(
176 analyseWhereClause(pLeft
),
177 analyseWhereClause(pRight
));
181 else if (SQL_ISRULE(pParseNode
, test_for_null
) || SQL_ISRULE(pParseNode
, like_predicate
))
183 const OSQLParseNode
*pLeft
= pParseNode
->getChild(0);
184 const OSQLParseNode
* pPart2
= pParseNode
->getChild(1);
185 const OSQLParseNode
*pMiddleLeft
= pPart2
->getChild(0),
186 *pMiddleRight
= pPart2
->getChild(1),
187 *pRight
= pPart2
->getChild(2);
189 if (SQL_ISRULE(pParseNode
, test_for_null
))
191 if (SQL_ISRULE(pLeft
, column_ref
) &&
192 SQL_ISTOKEN(pMiddleLeft
, IS
) &&
193 SQL_ISTOKEN(pRight
, NULL
))
195 OUString sColumnName
,
198 m_aSQLIterator
.getColumnRange(pLeft
, sColumnName
, sTableRange
);
200 if (SQL_ISTOKEN(pMiddleRight
, NOT
))
202 // WHERE "Mobile Phone" IS NOT NULL
203 return new MacabConditionNotNull(m_pHeader
, sColumnName
);
207 // WHERE "Mobile Phone" IS NULL
208 return new MacabConditionNull(m_pHeader
, sColumnName
);
212 else if (SQL_ISRULE(pParseNode
, like_predicate
))
214 if (SQL_ISRULE(pLeft
, column_ref
))
216 OUString sColumnName
,
219 m_aSQLIterator
.getColumnRange(pLeft
, sColumnName
, sTableRange
);
221 if (pMiddleRight
->isToken() || SQL_ISRULE(pMiddleRight
, parameter
))
223 OUString sMatchString
;
225 if (pMiddleRight
->isToken()) // WHERE Name LIKE 'Sm%'
226 sMatchString
= pMiddleRight
->getTokenValue();
227 else if (SQL_ISRULE(pMiddleRight
, parameter
)) // WHERE Name LIKE ?
228 getNextParameter(sMatchString
);
230 return new MacabConditionSimilar(m_pHeader
, sColumnName
, sMatchString
);
235 impl_throwError(STR_QUERY_TOO_COMPLEX
);
241 MacabOrder
*MacabCommonStatement::analyseOrderByClause(const OSQLParseNode
*pParseNode
) const throw(SQLException
)
243 if (SQL_ISRULE(pParseNode
, ordering_spec_commalist
))
245 MacabComplexOrder
*list
= new MacabComplexOrder();
246 sal_uInt32 n
= pParseNode
->count();
248 // Iterate through the ordering columns
249 for (sal_uInt32 i
= 0; i
< n
; i
++)
252 (analyseOrderByClause(pParseNode
->getChild(i
)));
257 else if (SQL_ISRULE(pParseNode
, ordering_spec
))
259 if (pParseNode
->count() == 2)
261 OSQLParseNode
* pColumnRef
= pParseNode
->getChild(0);
262 OSQLParseNode
* pAscendingDescending
= pParseNode
->getChild(1);
264 if (SQL_ISRULE(pColumnRef
, column_ref
))
266 if (pColumnRef
->count() == 3)
267 pColumnRef
= pColumnRef
->getChild(2);
269 if (pColumnRef
->count() == 1)
271 OUString sColumnName
=
272 pColumnRef
->getChild(0)->getTokenValue();
274 !SQL_ISTOKEN(pAscendingDescending
, DESC
);
276 return new MacabSimpleOrder(m_pHeader
, sColumnName
, bAscending
);
281 impl_throwError(STR_QUERY_TOO_COMPLEX
);
287 OUString
MacabCommonStatement::getTableName() const
289 const OSQLTables
& xTabs
= m_aSQLIterator
.getTables();
294 // can only deal with one table at a time
295 if(xTabs
.size() > 1 || m_aSQLIterator
.hasErrors() )
298 return xTabs
.begin()->first
;
301 void MacabCommonStatement::setMacabFields(MacabResultSet
*pResult
) const throw(SQLException
)
303 ::rtl::Reference
<connectivity::OSQLColumns
> xColumns
; // selected columns
304 MacabResultSetMetaData
*pMeta
; // meta information - holds the list of AddressBook fields
306 xColumns
= m_aSQLIterator
.getSelectColumns();
309 ::connectivity::SharedResources aResources
;
310 const OUString
sError( aResources
.getResourceString(
311 STR_INVALID_COLUMN_SELECTION
313 ::dbtools::throwGenericSQLException(sError
,NULL
);
315 pMeta
= static_cast<MacabResultSetMetaData
*>(pResult
->getMetaData().get());
316 pMeta
->setMacabFields(xColumns
);
319 void MacabCommonStatement::selectRecords(MacabResultSet
*pResult
) const throw(SQLException
)
321 const OSQLParseNode
*pParseNode
;
323 pParseNode
= m_aSQLIterator
.getWhereTree();
324 if (pParseNode
!= NULL
)
326 if (SQL_ISRULE(pParseNode
, where_clause
))
329 pParseNode
= pParseNode
->getChild(1);
330 MacabCondition
*pCondition
= analyseWhereClause(pParseNode
);
331 if (pCondition
->isAlwaysTrue())
332 pResult
->allMacabRecords();
334 pResult
->someMacabRecords(pCondition
);
340 // no WHERE clause: get all rows
341 pResult
->allMacabRecords();
344 void MacabCommonStatement::sortRecords(MacabResultSet
*pResult
) const throw(SQLException
)
346 const OSQLParseNode
*pParseNode
;
348 pParseNode
= m_aSQLIterator
.getOrderTree();
349 if (pParseNode
!= NULL
)
351 if (SQL_ISRULE(pParseNode
, opt_order_by_clause
))
353 pParseNode
= pParseNode
->getChild(2);
354 MacabOrder
*pOrder
= analyseOrderByClause(pParseNode
);
355 pResult
->sortMacabRecords(pOrder
);
361 Any SAL_CALL
MacabCommonStatement::queryInterface( const Type
& rType
) throw(RuntimeException
, std::exception
)
363 Any aRet
= MacabCommonStatement_BASE::queryInterface(rType
);
364 if (!aRet
.hasValue())
365 aRet
= OPropertySetHelper::queryInterface(rType
);
369 Sequence
< Type
> SAL_CALL
MacabCommonStatement::getTypes( ) throw(RuntimeException
, std::exception
)
371 ::cppu::OTypeCollection
aTypes( cppu::UnoType
<XMultiPropertySet
>::get(),
372 cppu::UnoType
<XFastPropertySet
>::get(),
373 cppu::UnoType
<XPropertySet
>::get());
375 return comphelper::concatSequences(aTypes
.getTypes(),MacabCommonStatement_BASE::getTypes());
378 void SAL_CALL
MacabCommonStatement::cancel( ) throw(RuntimeException
)
380 ::osl::MutexGuard
aGuard( m_aMutex
);
382 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
383 // cancel the current sql statement
386 void SAL_CALL
MacabCommonStatement::close( ) throw(SQLException
, RuntimeException
)
389 ::osl::MutexGuard
aGuard( m_aMutex
);
390 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
396 sal_Bool SAL_CALL
MacabCommonStatement::execute(
397 const OUString
& sql
) throw(SQLException
, RuntimeException
)
399 ::osl::MutexGuard
aGuard( m_aMutex
);
400 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
402 Reference
< XResultSet
> xRS
= executeQuery(sql
);
407 Reference
< XResultSet
> SAL_CALL
MacabCommonStatement::executeQuery(
408 const OUString
& sql
) throw(SQLException
, RuntimeException
)
410 ::osl::MutexGuard
aGuard( m_aMutex
);
411 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
413 OSL_TRACE("Mac OS Address book - SQL Request: %s", OUtoCStr(sql
));
415 MacabResultSet
* pResult
= new MacabResultSet(this);
416 Reference
< XResultSet
> xRS
= pResult
;
419 m_pParseTree
= m_aParser
.parseTree(aErr
, sql
);
420 if (m_pParseTree
== NULL
)
421 throw SQLException(aErr
, *this, aErr
, 0, Any());
423 m_aSQLIterator
.setParseTree(m_pParseTree
);
424 m_aSQLIterator
.traverseAll();
425 switch (m_aSQLIterator
.getStatementType())
427 case SQL_STATEMENT_SELECT
:
429 OUString sTableName
= getTableName(); // FROM which table ?
430 if (sTableName
.getLength() != 0) // a match
432 MacabRecords
*aRecords
;
433 aRecords
= m_pConnection
->getAddressBook()->getMacabRecords(sTableName
);
435 // In case, somehow, we don't have anything with the name m_sTableName
438 impl_throwError(STR_NO_TABLE
);
442 m_pHeader
= aRecords
->getHeader();
444 pResult
->setTableName(sTableName
);
446 setMacabFields(pResult
); // SELECT which columns ?
447 selectRecords(pResult
); // WHERE which condition ?
448 sortRecords(pResult
); // ORDER BY which columns ?
450 // To be continued: DISTINCT
457 // To be continued: UPDATE
460 impl_throwError(STR_QUERY_TOO_COMPLEX
);
463 m_xResultSet
= Reference
<XResultSet
>(pResult
);
467 Reference
< XConnection
> SAL_CALL
MacabCommonStatement::getConnection( ) throw(SQLException
, RuntimeException
)
469 ::osl::MutexGuard
aGuard( m_aMutex
);
470 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
472 // just return our connection here
473 return m_pConnection
;
476 sal_Int32 SAL_CALL
MacabCommonStatement::executeUpdate( const OUString
& ) throw(SQLException
, RuntimeException
)
478 ::osl::MutexGuard
aGuard( m_aMutex
);
479 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
481 // the return values gives information about how many rows are affected by executing the sql statement
485 Any SAL_CALL
MacabCommonStatement::getWarnings( ) throw(SQLException
, RuntimeException
)
487 ::osl::MutexGuard
aGuard( m_aMutex
);
488 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
490 return makeAny(m_aLastWarning
);
493 void SAL_CALL
MacabCommonStatement::clearWarnings( ) throw(SQLException
, RuntimeException
)
495 ::osl::MutexGuard
aGuard( m_aMutex
);
496 checkDisposed(MacabCommonStatement_BASE::rBHelper
.bDisposed
);
498 m_aLastWarning
= SQLWarning();
501 ::cppu::IPropertyArrayHelper
* MacabCommonStatement::createArrayHelper( ) const
503 // this properties are defined by the service statement
504 // they must be in alphabetic order
505 Sequence
< Property
> aProps(10);
506 Property
* pProperties
= aProps
.getArray();
508 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME
),
509 PROPERTY_ID_CURSORNAME
, cppu::UnoType
<OUString
>::get(), 0);
510 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING
),
511 PROPERTY_ID_ESCAPEPROCESSING
, cppu::UnoType
<bool>::get(), 0);
512 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION
),
513 PROPERTY_ID_FETCHDIRECTION
, cppu::UnoType
<sal_Int32
>::get(), 0);
514 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE
),
515 PROPERTY_ID_FETCHSIZE
, cppu::UnoType
<sal_Int32
>::get(), 0);
516 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE
),
517 PROPERTY_ID_MAXFIELDSIZE
, cppu::UnoType
<sal_Int32
>::get(), 0);
518 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS
),
519 PROPERTY_ID_MAXROWS
, cppu::UnoType
<sal_Int32
>::get(), 0);
520 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT
),
521 PROPERTY_ID_QUERYTIMEOUT
, cppu::UnoType
<sal_Int32
>::get(), 0);
522 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY
),
523 PROPERTY_ID_RESULTSETCONCURRENCY
, cppu::UnoType
<sal_Int32
>::get(), 0);
524 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE
),
525 PROPERTY_ID_RESULTSETTYPE
, cppu::UnoType
<sal_Int32
>::get(), 0);
526 pProperties
[nPos
++] = ::com::sun::star::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_USEBOOKMARKS
),
527 PROPERTY_ID_USEBOOKMARKS
, cppu::UnoType
<bool>::get(), 0);
529 return new ::cppu::OPropertyArrayHelper(aProps
);
532 ::cppu::IPropertyArrayHelper
& MacabCommonStatement::getInfoHelper()
534 return *getArrayHelper();
537 sal_Bool
MacabCommonStatement::convertFastPropertyValue(
541 const Any
&) throw (::com::sun::star::lang::IllegalArgumentException
)
543 bool bConverted
= false;
544 // here we have to try to convert
548 void MacabCommonStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle
,const Any
&) throw (Exception
)
550 // set the value to whatever is necessary
553 case PROPERTY_ID_QUERYTIMEOUT
:
554 case PROPERTY_ID_MAXFIELDSIZE
:
555 case PROPERTY_ID_MAXROWS
:
556 case PROPERTY_ID_CURSORNAME
:
557 case PROPERTY_ID_RESULTSETCONCURRENCY
:
558 case PROPERTY_ID_RESULTSETTYPE
:
559 case PROPERTY_ID_FETCHDIRECTION
:
560 case PROPERTY_ID_FETCHSIZE
:
561 case PROPERTY_ID_ESCAPEPROCESSING
:
562 case PROPERTY_ID_USEBOOKMARKS
:
568 void MacabCommonStatement::getFastPropertyValue(Any
&,sal_Int32 nHandle
) const
572 case PROPERTY_ID_QUERYTIMEOUT
:
573 case PROPERTY_ID_MAXFIELDSIZE
:
574 case PROPERTY_ID_MAXROWS
:
575 case PROPERTY_ID_CURSORNAME
:
576 case PROPERTY_ID_RESULTSETCONCURRENCY
:
577 case PROPERTY_ID_RESULTSETTYPE
:
578 case PROPERTY_ID_FETCHDIRECTION
:
579 case PROPERTY_ID_FETCHSIZE
:
580 case PROPERTY_ID_ESCAPEPROCESSING
:
581 case PROPERTY_ID_USEBOOKMARKS
:
587 void SAL_CALL
MacabCommonStatement::acquire() throw()
589 MacabCommonStatement_BASE::acquire();
592 void SAL_CALL
MacabCommonStatement::release() throw()
594 MacabCommonStatement_BASE::release();
597 Reference
< ::com::sun::star::beans::XPropertySetInfo
> SAL_CALL
MacabCommonStatement::getPropertySetInfo( ) throw(RuntimeException
)
599 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
602 MacabStatement::MacabStatement(MacabConnection
* _pConnection
)
603 : MacabStatement_BASE(_pConnection
)
607 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */