xref: /trunk/main/connectivity/source/drivers/macab/MacabStatement.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "MacabStatement.hxx"
32*cdf0e10cSrcweir #include "MacabConnection.hxx"
33*cdf0e10cSrcweir #include "MacabAddressBook.hxx"
34*cdf0e10cSrcweir #include "MacabDriver.hxx"
35*cdf0e10cSrcweir #include "MacabResultSet.hxx"
36*cdf0e10cSrcweir #include "MacabResultSetMetaData.hxx"
37*cdf0e10cSrcweir #include "macabcondition.hxx"
38*cdf0e10cSrcweir #include "macaborder.hxx"
39*cdf0e10cSrcweir #include "TConnection.hxx"
40*cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
41*cdf0e10cSrcweir #include "resource/sharedresources.hxx"
42*cdf0e10cSrcweir #include "resource/macab_res.hrc"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
45*cdf0e10cSrcweir # define OUtoCStr( x ) ( ::rtl::OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
46*cdf0e10cSrcweir #else /* OSL_DEBUG_LEVEL */
47*cdf0e10cSrcweir # define OUtoCStr( x ) ("dummy")
48*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace connectivity::macab;
51*cdf0e10cSrcweir using namespace com::sun::star::uno;
52*cdf0e10cSrcweir using namespace com::sun::star::lang;
53*cdf0e10cSrcweir using namespace com::sun::star::beans;
54*cdf0e10cSrcweir using namespace com::sun::star::sdbc;
55*cdf0e10cSrcweir using namespace com::sun::star::sdbcx;
56*cdf0e10cSrcweir using namespace com::sun::star::container;
57*cdf0e10cSrcweir using namespace com::sun::star::io;
58*cdf0e10cSrcweir using namespace com::sun::star::util;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir namespace connectivity
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     namespace macab
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir     void impl_throwError(sal_uInt16 _nErrorId)
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         ::connectivity::SharedResources aResources;
67*cdf0e10cSrcweir         const ::rtl::OUString sError( aResources.getResourceString(_nErrorId) );
68*cdf0e10cSrcweir         ::dbtools::throwGenericSQLException(sError,NULL);
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(MacabStatement, "com.sun.star.sdbc.drivers.MacabStatement", "com.sun.star.sdbc.Statement");
74*cdf0e10cSrcweir //------------------------------------------------------------------------------
75*cdf0e10cSrcweir MacabCommonStatement::MacabCommonStatement(MacabConnection* _pConnection )
76*cdf0e10cSrcweir     : MacabCommonStatement_BASE(m_aMutex),
77*cdf0e10cSrcweir     OPropertySetHelper(MacabCommonStatement_BASE::rBHelper),
78*cdf0e10cSrcweir     m_aParser(_pConnection->getDriver()->getMSFactory()),
79*cdf0e10cSrcweir     m_aSQLIterator(_pConnection, _pConnection->createCatalog()->getTables(), m_aParser, NULL ),
80*cdf0e10cSrcweir     m_pParseTree(NULL),
81*cdf0e10cSrcweir     m_pConnection(_pConnection),
82*cdf0e10cSrcweir     rBHelper(MacabCommonStatement_BASE::rBHelper)
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir     m_pConnection->acquire();
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir // -----------------------------------------------------------------------------
87*cdf0e10cSrcweir MacabCommonStatement::~MacabCommonStatement()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir // -----------------------------------------------------------------------------
91*cdf0e10cSrcweir void MacabCommonStatement::disposing()
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     MacabCommonStatement_BASE::disposing();
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir // -----------------------------------------------------------------------------
96*cdf0e10cSrcweir void MacabCommonStatement::resetParameters() const throw(::com::sun::star::sdbc::SQLException)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     impl_throwError(STR_PARA_ONLY_PREPARED);
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir // -----------------------------------------------------------------------------
101*cdf0e10cSrcweir void MacabCommonStatement::getNextParameter(::rtl::OUString &) const throw(::com::sun::star::sdbc::SQLException)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     impl_throwError(STR_PARA_ONLY_PREPARED);
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir // -----------------------------------------------------------------------------
106*cdf0e10cSrcweir MacabCondition *MacabCommonStatement::analyseWhereClause(const OSQLParseNode *pParseNode) const throw(SQLException)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     if (pParseNode->count() == 3)
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         const OSQLParseNode *pLeft = pParseNode->getChild(0),
111*cdf0e10cSrcweir                             *pMiddle = pParseNode->getChild(1),
112*cdf0e10cSrcweir                             *pRight = pParseNode->getChild(2);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         // WHERE ( ... ) ?
115*cdf0e10cSrcweir         if (SQL_ISPUNCTUATION(pLeft, "(") && SQL_ISPUNCTUATION(pRight, ")"))
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir             return analyseWhereClause(pMiddle);
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         else if (SQL_ISRULE(pParseNode, comparison_predicate))
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             if (pLeft->isToken() && pRight->isToken())
122*cdf0e10cSrcweir             {
123*cdf0e10cSrcweir                 switch (pMiddle->getNodeType())
124*cdf0e10cSrcweir                 {
125*cdf0e10cSrcweir                     case SQL_NODE_EQUAL:
126*cdf0e10cSrcweir                         // WHERE 0 = 1
127*cdf0e10cSrcweir                         return new MacabConditionConstant(pLeft->getTokenValue() == pRight->getTokenValue());
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir                     case SQL_NODE_NOTEQUAL:
130*cdf0e10cSrcweir                         // WHERE 0 <> 1
131*cdf0e10cSrcweir                         // (might not be correct SQL... don't care, handling anyway)
132*cdf0e10cSrcweir                         return new MacabConditionConstant(pLeft->getTokenValue() != pRight->getTokenValue());
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                     default:
135*cdf0e10cSrcweir                         break;
136*cdf0e10cSrcweir                 }
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             else if (SQL_ISRULE(pLeft, column_ref))
139*cdf0e10cSrcweir             {
140*cdf0e10cSrcweir                 ::rtl::OUString sColumnName,
141*cdf0e10cSrcweir                                 sTableRange;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir                 m_aSQLIterator.getColumnRange(pLeft, sColumnName, sTableRange);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir                 if (pRight->isToken() || SQL_ISRULE(pRight, parameter))
146*cdf0e10cSrcweir                 {
147*cdf0e10cSrcweir                     ::rtl::OUString sMatchString;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir                     if (pRight->isToken())                      // WHERE Name = 'Doe'
150*cdf0e10cSrcweir                         sMatchString = pRight->getTokenValue();
151*cdf0e10cSrcweir                     else if (SQL_ISRULE(pRight, parameter))     // WHERE Name = ?
152*cdf0e10cSrcweir                         getNextParameter(sMatchString);
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir                     switch (pMiddle->getNodeType())
155*cdf0e10cSrcweir                     {
156*cdf0e10cSrcweir                         case SQL_NODE_EQUAL:
157*cdf0e10cSrcweir                             // WHERE Name = 'Smith'
158*cdf0e10cSrcweir                             return new MacabConditionEqual(m_pHeader, sColumnName, sMatchString);
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir                         case SQL_NODE_NOTEQUAL:
161*cdf0e10cSrcweir                             // WHERE Name <> 'Jones'
162*cdf0e10cSrcweir                             return new MacabConditionDifferent(m_pHeader, sColumnName, sMatchString);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir                         default:
165*cdf0e10cSrcweir                             break;
166*cdf0e10cSrcweir                     }
167*cdf0e10cSrcweir                 }
168*cdf0e10cSrcweir             }
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         else if (SQL_ISRULE(pParseNode, search_condition))
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             if (SQL_ISTOKEN(pMiddle, OR))
173*cdf0e10cSrcweir             {
174*cdf0e10cSrcweir                 // WHERE Name = 'Smith' OR Name = 'Jones'
175*cdf0e10cSrcweir                 return new MacabConditionOr(
176*cdf0e10cSrcweir                     analyseWhereClause(pLeft),
177*cdf0e10cSrcweir                     analyseWhereClause(pRight));
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir         else if (SQL_ISRULE(pParseNode, boolean_term))
181*cdf0e10cSrcweir         {
182*cdf0e10cSrcweir             if (SQL_ISTOKEN(pMiddle, AND))
183*cdf0e10cSrcweir             {
184*cdf0e10cSrcweir                 // WHERE Name = 'Smith' AND "Given Name" = 'Peter'
185*cdf0e10cSrcweir                 return new MacabConditionAnd(
186*cdf0e10cSrcweir                     analyseWhereClause(pLeft),
187*cdf0e10cSrcweir                     analyseWhereClause(pRight));
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir     else if (SQL_ISRULE(pParseNode, test_for_null) || SQL_ISRULE(pParseNode, like_predicate))
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         const OSQLParseNode *pLeft = pParseNode->getChild(0);
194*cdf0e10cSrcweir         const OSQLParseNode* pPart2 = pParseNode->getChild(1);
195*cdf0e10cSrcweir         const OSQLParseNode *pMiddleLeft = pPart2->getChild(0),
196*cdf0e10cSrcweir                             *pMiddleRight = pPart2->getChild(1),
197*cdf0e10cSrcweir                             *pRight = pPart2->getChild(2);
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         if (SQL_ISRULE(pParseNode, test_for_null))
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             if (SQL_ISRULE(pLeft, column_ref) &&
202*cdf0e10cSrcweir                             SQL_ISTOKEN(pMiddleLeft, IS) &&
203*cdf0e10cSrcweir                             SQL_ISTOKEN(pRight, NULL))
204*cdf0e10cSrcweir             {
205*cdf0e10cSrcweir                 ::rtl::OUString sColumnName,
206*cdf0e10cSrcweir                                 sTableRange;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir                 m_aSQLIterator.getColumnRange(pLeft, sColumnName, sTableRange);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir                 if (SQL_ISTOKEN(pMiddleRight, NOT))
211*cdf0e10cSrcweir                 {
212*cdf0e10cSrcweir                     // WHERE "Mobile Phone" IS NOT NULL
213*cdf0e10cSrcweir                     return new MacabConditionNotNull(m_pHeader, sColumnName);
214*cdf0e10cSrcweir                 }
215*cdf0e10cSrcweir                 else
216*cdf0e10cSrcweir                 {
217*cdf0e10cSrcweir                     // WHERE "Mobile Phone" IS NULL
218*cdf0e10cSrcweir                     return new MacabConditionNull(m_pHeader, sColumnName);
219*cdf0e10cSrcweir                 }
220*cdf0e10cSrcweir             }
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir         else if (SQL_ISRULE(pParseNode, like_predicate))
223*cdf0e10cSrcweir         {
224*cdf0e10cSrcweir             if (SQL_ISRULE(pLeft, column_ref))
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir                 ::rtl::OUString sColumnName,
227*cdf0e10cSrcweir                                 sTableRange;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                 m_aSQLIterator.getColumnRange(pLeft, sColumnName, sTableRange);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                 if (pMiddleRight->isToken() || SQL_ISRULE(pMiddleRight, parameter))
232*cdf0e10cSrcweir                 {
233*cdf0e10cSrcweir                     ::rtl::OUString sMatchString;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir                     if (pMiddleRight->isToken())                    // WHERE Name LIKE 'Sm%'
236*cdf0e10cSrcweir                         sMatchString = pMiddleRight->getTokenValue();
237*cdf0e10cSrcweir                     else if (SQL_ISRULE(pMiddleRight, parameter))   // WHERE Name LIKE ?
238*cdf0e10cSrcweir                         getNextParameter(sMatchString);
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir                     return new MacabConditionSimilar(m_pHeader, sColumnName, sMatchString);
241*cdf0e10cSrcweir                 }
242*cdf0e10cSrcweir             }
243*cdf0e10cSrcweir         }
244*cdf0e10cSrcweir     }
245*cdf0e10cSrcweir     impl_throwError(STR_QUERY_TOO_COMPLEX);
246*cdf0e10cSrcweir     // Unreachable:
247*cdf0e10cSrcweir     OSL_ASSERT(false);
248*cdf0e10cSrcweir     return 0;
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir // -----------------------------------------------------------------------------
251*cdf0e10cSrcweir MacabOrder *MacabCommonStatement::analyseOrderByClause(const OSQLParseNode *pParseNode) const throw(SQLException)
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir     if (SQL_ISRULE(pParseNode, ordering_spec_commalist))
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         MacabComplexOrder *list = new MacabComplexOrder();
256*cdf0e10cSrcweir         sal_uInt32 n = pParseNode->count();
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         // Iterate through the ordering columns
259*cdf0e10cSrcweir         for (sal_uInt32 i = 0; i < n; i++)
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             list->addOrder
262*cdf0e10cSrcweir                 (analyseOrderByClause(pParseNode->getChild(i)));
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         return list;
266*cdf0e10cSrcweir     }
267*cdf0e10cSrcweir     else if (SQL_ISRULE(pParseNode, ordering_spec))
268*cdf0e10cSrcweir     {
269*cdf0e10cSrcweir         if (pParseNode->count() == 2)
270*cdf0e10cSrcweir         {
271*cdf0e10cSrcweir             OSQLParseNode* pColumnRef = pParseNode->getChild(0);
272*cdf0e10cSrcweir             OSQLParseNode* pAscendingDescending = pParseNode->getChild(1);
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir             if (SQL_ISRULE(pColumnRef, column_ref))
275*cdf0e10cSrcweir             {
276*cdf0e10cSrcweir                 if (pColumnRef->count() == 3)
277*cdf0e10cSrcweir                     pColumnRef = pColumnRef->getChild(2);
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir                 if (pColumnRef->count() == 1)
280*cdf0e10cSrcweir                 {
281*cdf0e10cSrcweir                     ::rtl::OUString sColumnName =
282*cdf0e10cSrcweir                         pColumnRef->getChild(0)->getTokenValue();
283*cdf0e10cSrcweir                     sal_Bool bAscending =
284*cdf0e10cSrcweir                         SQL_ISTOKEN(pAscendingDescending, DESC)?
285*cdf0e10cSrcweir                         sal_False:
286*cdf0e10cSrcweir                         sal_True;
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir                     return new MacabSimpleOrder(m_pHeader, sColumnName, bAscending);
289*cdf0e10cSrcweir                 }
290*cdf0e10cSrcweir             }
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir     impl_throwError(STR_QUERY_TOO_COMPLEX);
294*cdf0e10cSrcweir     // Unreachable:
295*cdf0e10cSrcweir     OSL_ASSERT(false);
296*cdf0e10cSrcweir     return 0;
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir //------------------------------------------------------------------------------
299*cdf0e10cSrcweir ::rtl::OUString MacabCommonStatement::getTableName() const
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     const OSQLTables& xTabs = m_aSQLIterator.getTables();
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir     if( xTabs.empty() )
304*cdf0e10cSrcweir         return ::rtl::OUString();
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     // can only deal with one table at a time
307*cdf0e10cSrcweir     if(xTabs.size() > 1 || m_aSQLIterator.hasErrors() )
308*cdf0e10cSrcweir         return ::rtl::OUString();
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir     return xTabs.begin()->first;
311*cdf0e10cSrcweir }
312*cdf0e10cSrcweir //------------------------------------------------------------------------------
313*cdf0e10cSrcweir void MacabCommonStatement::setMacabFields(MacabResultSet *pResult) const throw(SQLException)
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir     ::vos::ORef<connectivity::OSQLColumns> xColumns;    // selected columns
316*cdf0e10cSrcweir     MacabResultSetMetaData *pMeta;              // meta information - holds the list of AddressBook fields
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir     xColumns = m_aSQLIterator.getSelectColumns();
319*cdf0e10cSrcweir     if (!xColumns.isValid())
320*cdf0e10cSrcweir     {
321*cdf0e10cSrcweir         ::connectivity::SharedResources aResources;
322*cdf0e10cSrcweir         const ::rtl::OUString sError( aResources.getResourceString(
323*cdf0e10cSrcweir                 STR_INVALID_COLUMN_SELECTION
324*cdf0e10cSrcweir              ) );
325*cdf0e10cSrcweir         ::dbtools::throwGenericSQLException(sError,NULL);
326*cdf0e10cSrcweir     }
327*cdf0e10cSrcweir     pMeta = static_cast<MacabResultSetMetaData *>(pResult->getMetaData().get());
328*cdf0e10cSrcweir     pMeta->setMacabFields(xColumns);
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir // -------------------------------------------------------------------------
331*cdf0e10cSrcweir void MacabCommonStatement::selectRecords(MacabResultSet *pResult) const throw(SQLException)
332*cdf0e10cSrcweir {
333*cdf0e10cSrcweir     const OSQLParseNode *pParseNode;
334*cdf0e10cSrcweir     MacabCondition *pCondition;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir     pParseNode = m_aSQLIterator.getWhereTree();
337*cdf0e10cSrcweir     if (pParseNode != NULL)
338*cdf0e10cSrcweir     {
339*cdf0e10cSrcweir         if (SQL_ISRULE(pParseNode, where_clause))
340*cdf0e10cSrcweir         {
341*cdf0e10cSrcweir             // Since we don't support parameters, don't reset them. If we ever
342*cdf0e10cSrcweir             // support them, uncomment this line and fix resetParameters.
343*cdf0e10cSrcweir             //resetParameters();
344*cdf0e10cSrcweir             pParseNode = pParseNode->getChild(1);
345*cdf0e10cSrcweir             pCondition = analyseWhereClause(pParseNode);
346*cdf0e10cSrcweir             if (pCondition->isAlwaysTrue())
347*cdf0e10cSrcweir                 pResult->allMacabRecords();
348*cdf0e10cSrcweir             else if (!pCondition->isAlwaysFalse())
349*cdf0e10cSrcweir                 pResult->someMacabRecords(pCondition);
350*cdf0e10cSrcweir             delete pCondition;
351*cdf0e10cSrcweir             return;
352*cdf0e10cSrcweir         }
353*cdf0e10cSrcweir     }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir     // no WHERE clause: get all rows
356*cdf0e10cSrcweir     pResult->allMacabRecords();
357*cdf0e10cSrcweir }
358*cdf0e10cSrcweir // -------------------------------------------------------------------------
359*cdf0e10cSrcweir void MacabCommonStatement::sortRecords(MacabResultSet *pResult) const throw(SQLException)
360*cdf0e10cSrcweir {
361*cdf0e10cSrcweir     const OSQLParseNode *pParseNode;
362*cdf0e10cSrcweir     MacabOrder *pOrder;
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir     pParseNode = m_aSQLIterator.getOrderTree();
365*cdf0e10cSrcweir     if (pParseNode != NULL)
366*cdf0e10cSrcweir     {
367*cdf0e10cSrcweir         if (SQL_ISRULE(pParseNode, opt_order_by_clause))
368*cdf0e10cSrcweir         {
369*cdf0e10cSrcweir             pParseNode = pParseNode->getChild(2);
370*cdf0e10cSrcweir             pOrder = analyseOrderByClause(pParseNode);
371*cdf0e10cSrcweir             pResult->sortMacabRecords(pOrder);
372*cdf0e10cSrcweir             delete pOrder;
373*cdf0e10cSrcweir         }
374*cdf0e10cSrcweir     }
375*cdf0e10cSrcweir }
376*cdf0e10cSrcweir //-----------------------------------------------------------------------------
377*cdf0e10cSrcweir Any SAL_CALL MacabCommonStatement::queryInterface( const Type & rType ) throw(RuntimeException)
378*cdf0e10cSrcweir {
379*cdf0e10cSrcweir     Any aRet = MacabCommonStatement_BASE::queryInterface(rType);
380*cdf0e10cSrcweir     if (!aRet.hasValue())
381*cdf0e10cSrcweir         aRet = OPropertySetHelper::queryInterface(rType);
382*cdf0e10cSrcweir     return aRet;
383*cdf0e10cSrcweir }
384*cdf0e10cSrcweir // -------------------------------------------------------------------------
385*cdf0e10cSrcweir Sequence< Type > SAL_CALL MacabCommonStatement::getTypes(  ) throw(RuntimeException)
386*cdf0e10cSrcweir {
387*cdf0e10cSrcweir     ::cppu::OTypeCollection aTypes( ::getCppuType( (const Reference< XMultiPropertySet > *)0 ),
388*cdf0e10cSrcweir                                     ::getCppuType( (const Reference< XFastPropertySet > *)0 ),
389*cdf0e10cSrcweir                                     ::getCppuType( (const Reference< XPropertySet > *)0 ));
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     return comphelper::concatSequences(aTypes.getTypes(),MacabCommonStatement_BASE::getTypes());
392*cdf0e10cSrcweir }
393*cdf0e10cSrcweir // -------------------------------------------------------------------------
394*cdf0e10cSrcweir void SAL_CALL MacabCommonStatement::cancel(  ) throw(RuntimeException)
395*cdf0e10cSrcweir {
396*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
399*cdf0e10cSrcweir     // cancel the current sql statement
400*cdf0e10cSrcweir }
401*cdf0e10cSrcweir // -------------------------------------------------------------------------
402*cdf0e10cSrcweir void SAL_CALL MacabCommonStatement::close(  ) throw(SQLException, RuntimeException)
403*cdf0e10cSrcweir {
404*cdf0e10cSrcweir     {
405*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
406*cdf0e10cSrcweir         checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     }
409*cdf0e10cSrcweir     dispose();
410*cdf0e10cSrcweir }
411*cdf0e10cSrcweir // -------------------------------------------------------------------------
412*cdf0e10cSrcweir sal_Bool SAL_CALL MacabCommonStatement::execute(
413*cdf0e10cSrcweir         const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
414*cdf0e10cSrcweir {
415*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
416*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir     Reference< XResultSet > xRS = executeQuery(sql);
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir     return xRS.is();
421*cdf0e10cSrcweir }
422*cdf0e10cSrcweir // -------------------------------------------------------------------------
423*cdf0e10cSrcweir Reference< XResultSet > SAL_CALL MacabCommonStatement::executeQuery(
424*cdf0e10cSrcweir         const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
425*cdf0e10cSrcweir {
426*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
427*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir OSL_TRACE("Mac OS Address book - SQL Request: %s", OUtoCStr(sql));
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir     MacabResultSet* pResult = new MacabResultSet(this);
432*cdf0e10cSrcweir     Reference< XResultSet > xRS = pResult;
433*cdf0e10cSrcweir     ::rtl::OUString aErr;
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir     m_pParseTree = m_aParser.parseTree(aErr, sql);
436*cdf0e10cSrcweir     if (m_pParseTree == NULL)
437*cdf0e10cSrcweir         throw SQLException(aErr, *this, aErr, 0, Any());
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir     m_aSQLIterator.setParseTree(m_pParseTree);
440*cdf0e10cSrcweir     m_aSQLIterator.traverseAll();
441*cdf0e10cSrcweir     switch (m_aSQLIterator.getStatementType())
442*cdf0e10cSrcweir     {
443*cdf0e10cSrcweir         case SQL_STATEMENT_SELECT:
444*cdf0e10cSrcweir             {
445*cdf0e10cSrcweir             ::rtl::OUString sTableName = getTableName(); // FROM which table ?
446*cdf0e10cSrcweir             if (sTableName.getLength() != 0) // a match
447*cdf0e10cSrcweir             {
448*cdf0e10cSrcweir                 MacabRecords *aRecords;
449*cdf0e10cSrcweir                 aRecords = m_pConnection->getAddressBook()->getMacabRecords(sTableName);
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir                 // In case, somehow, we don't have anything with the name m_sTableName
452*cdf0e10cSrcweir                 if(aRecords == NULL)
453*cdf0e10cSrcweir                 {
454*cdf0e10cSrcweir                     impl_throwError(STR_NO_TABLE);
455*cdf0e10cSrcweir                 }
456*cdf0e10cSrcweir                 else
457*cdf0e10cSrcweir                 {
458*cdf0e10cSrcweir                     m_pHeader = aRecords->getHeader();
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir                     pResult->setTableName(sTableName);
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir                     setMacabFields(pResult);        // SELECT which columns ?
463*cdf0e10cSrcweir                     selectRecords(pResult); // WHERE which condition ?
464*cdf0e10cSrcweir                     sortRecords(pResult);   // ORDER BY which columns ?
465*cdf0e10cSrcweir                 }
466*cdf0e10cSrcweir // To be continued: DISTINCT
467*cdf0e10cSrcweir //                  etc...
468*cdf0e10cSrcweir             }
469*cdf0e10cSrcweir             }
470*cdf0e10cSrcweir             break;
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir         default:
473*cdf0e10cSrcweir // To be continued: UPDATE
474*cdf0e10cSrcweir //                  DELETE
475*cdf0e10cSrcweir //                  etc...
476*cdf0e10cSrcweir             impl_throwError(STR_QUERY_TOO_COMPLEX);
477*cdf0e10cSrcweir     }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir     m_xResultSet = Reference<XResultSet>(pResult);
480*cdf0e10cSrcweir     return xRS;
481*cdf0e10cSrcweir }
482*cdf0e10cSrcweir // -------------------------------------------------------------------------
483*cdf0e10cSrcweir Reference< XConnection > SAL_CALL MacabCommonStatement::getConnection(  ) throw(SQLException, RuntimeException)
484*cdf0e10cSrcweir {
485*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
486*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir     // just return our connection here
489*cdf0e10cSrcweir     return (Reference< XConnection >) m_pConnection;
490*cdf0e10cSrcweir }
491*cdf0e10cSrcweir // -------------------------------------------------------------------------
492*cdf0e10cSrcweir sal_Int32 SAL_CALL MacabCommonStatement::executeUpdate( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
493*cdf0e10cSrcweir {
494*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
495*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir     // the return values gives information about how many rows are affected by executing the sql statement
498*cdf0e10cSrcweir     return 0;
499*cdf0e10cSrcweir }
500*cdf0e10cSrcweir // -------------------------------------------------------------------------
501*cdf0e10cSrcweir Any SAL_CALL MacabCommonStatement::getWarnings(  ) throw(SQLException, RuntimeException)
502*cdf0e10cSrcweir {
503*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
504*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir     return makeAny(m_aLastWarning);
507*cdf0e10cSrcweir }
508*cdf0e10cSrcweir // -------------------------------------------------------------------------
509*cdf0e10cSrcweir void SAL_CALL MacabCommonStatement::clearWarnings(  ) throw(SQLException, RuntimeException)
510*cdf0e10cSrcweir {
511*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
512*cdf0e10cSrcweir     checkDisposed(MacabCommonStatement_BASE::rBHelper.bDisposed);
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir     m_aLastWarning = SQLWarning();
515*cdf0e10cSrcweir }
516*cdf0e10cSrcweir // -------------------------------------------------------------------------
517*cdf0e10cSrcweir ::cppu::IPropertyArrayHelper* MacabCommonStatement::createArrayHelper( ) const
518*cdf0e10cSrcweir {
519*cdf0e10cSrcweir     // this properties are defined by the service statement
520*cdf0e10cSrcweir     // they must be in alphabetic order
521*cdf0e10cSrcweir     Sequence< Property > aProps(10);
522*cdf0e10cSrcweir     Property* pProperties = aProps.getArray();
523*cdf0e10cSrcweir     sal_Int32 nPos = 0;
524*cdf0e10cSrcweir     DECL_PROP0(CURSORNAME,  ::rtl::OUString);
525*cdf0e10cSrcweir     DECL_BOOL_PROP0(ESCAPEPROCESSING);
526*cdf0e10cSrcweir     DECL_PROP0(FETCHDIRECTION,sal_Int32);
527*cdf0e10cSrcweir     DECL_PROP0(FETCHSIZE,   sal_Int32);
528*cdf0e10cSrcweir     DECL_PROP0(MAXFIELDSIZE,sal_Int32);
529*cdf0e10cSrcweir     DECL_PROP0(MAXROWS,     sal_Int32);
530*cdf0e10cSrcweir     DECL_PROP0(QUERYTIMEOUT,sal_Int32);
531*cdf0e10cSrcweir     DECL_PROP0(RESULTSETCONCURRENCY,sal_Int32);
532*cdf0e10cSrcweir     DECL_PROP0(RESULTSETTYPE,sal_Int32);
533*cdf0e10cSrcweir     DECL_BOOL_PROP0(USEBOOKMARKS);
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir     return new ::cppu::OPropertyArrayHelper(aProps);
536*cdf0e10cSrcweir }
537*cdf0e10cSrcweir // -------------------------------------------------------------------------
538*cdf0e10cSrcweir ::cppu::IPropertyArrayHelper & MacabCommonStatement::getInfoHelper()
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir     return *const_cast<MacabCommonStatement*>(this)->getArrayHelper();
541*cdf0e10cSrcweir }
542*cdf0e10cSrcweir // -------------------------------------------------------------------------
543*cdf0e10cSrcweir sal_Bool MacabCommonStatement::convertFastPropertyValue(
544*cdf0e10cSrcweir         Any &,
545*cdf0e10cSrcweir         Any &,
546*cdf0e10cSrcweir         sal_Int32,
547*cdf0e10cSrcweir         const Any&) throw (::com::sun::star::lang::IllegalArgumentException)
548*cdf0e10cSrcweir {
549*cdf0e10cSrcweir     sal_Bool bConverted = sal_False;
550*cdf0e10cSrcweir     // here we have to try to convert
551*cdf0e10cSrcweir     return bConverted;
552*cdf0e10cSrcweir }
553*cdf0e10cSrcweir // -------------------------------------------------------------------------
554*cdf0e10cSrcweir void MacabCommonStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any&) throw (Exception)
555*cdf0e10cSrcweir {
556*cdf0e10cSrcweir     // set the value to whatever is nescessary
557*cdf0e10cSrcweir     switch (nHandle)
558*cdf0e10cSrcweir     {
559*cdf0e10cSrcweir         case PROPERTY_ID_QUERYTIMEOUT:
560*cdf0e10cSrcweir         case PROPERTY_ID_MAXFIELDSIZE:
561*cdf0e10cSrcweir         case PROPERTY_ID_MAXROWS:
562*cdf0e10cSrcweir         case PROPERTY_ID_CURSORNAME:
563*cdf0e10cSrcweir         case PROPERTY_ID_RESULTSETCONCURRENCY:
564*cdf0e10cSrcweir         case PROPERTY_ID_RESULTSETTYPE:
565*cdf0e10cSrcweir         case PROPERTY_ID_FETCHDIRECTION:
566*cdf0e10cSrcweir         case PROPERTY_ID_FETCHSIZE:
567*cdf0e10cSrcweir         case PROPERTY_ID_ESCAPEPROCESSING:
568*cdf0e10cSrcweir         case PROPERTY_ID_USEBOOKMARKS:
569*cdf0e10cSrcweir         default:
570*cdf0e10cSrcweir             ;
571*cdf0e10cSrcweir     }
572*cdf0e10cSrcweir }
573*cdf0e10cSrcweir // -------------------------------------------------------------------------
574*cdf0e10cSrcweir void MacabCommonStatement::getFastPropertyValue(Any&,sal_Int32 nHandle) const
575*cdf0e10cSrcweir {
576*cdf0e10cSrcweir     switch (nHandle)
577*cdf0e10cSrcweir     {
578*cdf0e10cSrcweir         case PROPERTY_ID_QUERYTIMEOUT:
579*cdf0e10cSrcweir         case PROPERTY_ID_MAXFIELDSIZE:
580*cdf0e10cSrcweir         case PROPERTY_ID_MAXROWS:
581*cdf0e10cSrcweir         case PROPERTY_ID_CURSORNAME:
582*cdf0e10cSrcweir         case PROPERTY_ID_RESULTSETCONCURRENCY:
583*cdf0e10cSrcweir         case PROPERTY_ID_RESULTSETTYPE:
584*cdf0e10cSrcweir         case PROPERTY_ID_FETCHDIRECTION:
585*cdf0e10cSrcweir         case PROPERTY_ID_FETCHSIZE:
586*cdf0e10cSrcweir         case PROPERTY_ID_ESCAPEPROCESSING:
587*cdf0e10cSrcweir         case PROPERTY_ID_USEBOOKMARKS:
588*cdf0e10cSrcweir         default:
589*cdf0e10cSrcweir             ;
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir }
592*cdf0e10cSrcweir // -----------------------------------------------------------------------------
593*cdf0e10cSrcweir void SAL_CALL MacabCommonStatement::acquire() throw()
594*cdf0e10cSrcweir {
595*cdf0e10cSrcweir     MacabCommonStatement_BASE::acquire();
596*cdf0e10cSrcweir }
597*cdf0e10cSrcweir // -----------------------------------------------------------------------------
598*cdf0e10cSrcweir void SAL_CALL MacabCommonStatement::release() throw()
599*cdf0e10cSrcweir {
600*cdf0e10cSrcweir     MacabCommonStatement_BASE::release();
601*cdf0e10cSrcweir }
602*cdf0e10cSrcweir // -----------------------------------------------------------------------------
603*cdf0e10cSrcweir Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL MacabCommonStatement::getPropertySetInfo(  ) throw(RuntimeException)
604*cdf0e10cSrcweir {
605*cdf0e10cSrcweir     return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
606*cdf0e10cSrcweir }
607*cdf0e10cSrcweir // -----------------------------------------------------------------------------
608*cdf0e10cSrcweir MacabStatement::MacabStatement(MacabConnection* _pConnection)
609*cdf0e10cSrcweir     : MacabStatement_BASE(_pConnection)
610*cdf0e10cSrcweir {
611*cdf0e10cSrcweir }
612