Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "file/fanalyzer.hxx"
21 : : #include "connectivity/sqlparse.hxx"
22 : : #include <osl/diagnose.h>
23 : : #include <tools/debug.hxx>
24 : : #include <comphelper/extract.hxx>
25 : : #include "connectivity/sqlnode.hxx"
26 : : #include "connectivity/dbexception.hxx"
27 : : #include "file/FConnection.hxx"
28 : : #include "resource/file_res.hrc"
29 : :
30 : : using namespace ::connectivity;
31 : : using namespace ::connectivity::file;
32 : : using namespace ::com::sun::star::uno;
33 : : using namespace ::com::sun::star::beans;
34 : : using namespace ::com::sun::star::sdbc;
35 : : using namespace ::com::sun::star::container;
36 : :
37 : : DBG_NAME( file_OSQLAnalyzer )
38 : : //------------------------------------------------------------------
39 : 88 : OSQLAnalyzer::OSQLAnalyzer(OConnection* _pConnection)
40 : : :m_pConnection(_pConnection)
41 : : ,m_bHasSelectionCode(sal_False)
42 : 88 : ,m_bSelectionFirstTime(sal_True)
43 : : {
44 : : DBG_CTOR( file_OSQLAnalyzer, NULL );
45 [ + - ][ + - ]: 88 : m_aCompiler = new OPredicateCompiler(this);
46 [ + - ][ + - ]: 88 : m_aInterpreter = new OPredicateInterpreter(m_aCompiler);
[ + - ]
47 : 88 : }
48 : :
49 : : // -----------------------------------------------------------------------------
50 [ + - ][ + - ]: 88 : OSQLAnalyzer::~OSQLAnalyzer()
51 : : {
52 : : DBG_DTOR( file_OSQLAnalyzer, NULL );
53 [ - + ]: 176 : }
54 : :
55 : : // -----------------------------------------------------------------------------
56 : 88 : void OSQLAnalyzer::setIndexes(const Reference< XNameAccess>& _xIndexes)
57 : : {
58 : 88 : m_aCompiler->m_xIndexes = _xIndexes;
59 : 88 : }
60 : : //------------------------------------------------------------------
61 : 88 : void OSQLAnalyzer::start(OSQLParseNode* pSQLParseNode)
62 : : {
63 [ + - ][ + - ]: 88 : if (SQL_ISRULE(pSQLParseNode,select_statement))
[ + - ]
64 : : {
65 : : DBG_ASSERT(pSQLParseNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
66 : :
67 : : // check that we don't use anything other than count(*) as function
68 : 88 : OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
69 [ + + ][ + + ]: 88 : if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
[ + - ]
70 : : {
71 [ + + ]: 80 : for (sal_uInt32 i = 0; i < pSelection->count(); i++)
72 : : {
73 : 44 : OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
74 [ - + ][ # # : 836 : if ( ( SQL_ISRULE(pColumnRef,set_fct_spec) && pColumnRef->count() == 4 )
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ ][ - + ]
[ + - ]
75 : 88 : || SQL_ISRULE(pColumnRef,char_value_fct)
76 : 88 : || SQL_ISRULE(pColumnRef,char_substring_fct)
77 : 88 : || SQL_ISRULE(pColumnRef,position_exp)
78 : 88 : || SQL_ISRULE(pColumnRef,fold)
79 : 88 : || SQL_ISRULE(pColumnRef,length_exp)
80 : 88 : || SQL_ISRULE(pColumnRef,num_value_exp)
81 : 88 : || SQL_ISRULE(pColumnRef,term)
82 : 88 : || SQL_ISRULE(pColumnRef,factor)
83 : 88 : || SQL_ISRULE(pColumnRef,set_fct_spec) )
84 : : {
85 [ # # ][ # # ]: 0 : ::rtl::Reference<OPredicateCompiler> pCompiler = new OPredicateCompiler(this);
86 [ # # ][ # # ]: 0 : pCompiler->setOrigColumns(m_aCompiler->getOrigColumns());
87 [ # # ][ # # ]: 0 : ::rtl::Reference<OPredicateInterpreter> pInterpreter = new OPredicateInterpreter(pCompiler);
[ # # ]
88 [ # # ]: 0 : pCompiler->execute( pColumnRef );
89 [ # # ][ # # ]: 0 : m_aSelectionEvaluations.push_back( TPredicates(pCompiler,pInterpreter) );
[ # # ][ # # ]
[ # # ]
90 : : }
91 [ + - ][ - + ]: 44 : else if ( ( SQL_ISRULE(pColumnRef,general_set_fct) && pColumnRef->count() != 4 ) )
[ # # ][ - + ]
92 : : {
93 [ # # ]: 0 : m_pConnection->throwGenericSQLException(STR_QUERY_COMPLEX_COUNT,NULL);
94 : : }
95 : : else
96 : : {
97 [ - + ][ # # : 176 : if ( SQL_ISPUNCTUATION( pColumnRef, "*" )
+ - + - -
+ # # # #
# # # # #
# # # #
# ][ - + ]
98 : 88 : || ( SQL_ISRULE( pColumnRef, column_ref )
99 : 44 : && ( pColumnRef->count() == 3 )
100 : 0 : && ( pColumnRef->getChild(0)->getNodeType() == SQL_NODE_NAME )
101 : 0 : && SQL_ISPUNCTUATION( pColumnRef->getChild(1), "." )
102 : 0 : && SQL_ISRULE( pColumnRef->getChild(2), column_val )
103 : 0 : && SQL_ISPUNCTUATION( pColumnRef->getChild(2)->getChild(0), "*" )
104 : : )
105 : : )
106 : : {
107 : : // push one element for each column of our table
108 [ # # ]: 0 : const Reference< XNameAccess > xColumnNames( m_aCompiler->getOrigColumns() );
109 [ # # ][ # # ]: 0 : const Sequence< ::rtl::OUString > aColumnNames( xColumnNames->getElementNames() );
110 [ # # ]: 0 : for ( sal_Int32 j=0; j<aColumnNames.getLength(); ++j )
111 [ # # ][ # # ]: 0 : m_aSelectionEvaluations.push_back( TPredicates() );
[ # # ][ # # ]
112 : : }
113 : : else
114 [ + - ]: 44 : m_aSelectionEvaluations.push_back( TPredicates() );
115 : : }
116 : : }
117 : : }
118 : : }
119 : :
120 : 88 : m_aCompiler->start(pSQLParseNode);
121 : 88 : }
122 : :
123 : : //------------------------------------------------------------------
124 : 88 : void OSQLAnalyzer::bindRow(OCodeList& rCodeList,const OValueRefRow& _pRow,OEvaluateSetList& _rEvaluateSetList)
125 : : {
126 : : // count criteria
127 : : // if only one criterion, and the corresponding field is indexed
128 : : // then the index will be used
129 : 88 : OEvaluateSet* pEvaluateSet = NULL;
130 : :
131 [ + - ][ + - ]: 316 : for (OCodeList::iterator aIter = rCodeList.begin(); aIter != rCodeList.end(); ++aIter)
[ + + ]
132 : : {
133 [ + - ][ + - ]: 228 : OOperandAttr* pAttr = PTR_CAST(OOperandAttr,(*aIter));
[ + - ][ + - ]
[ + - ][ + + ]
[ + - ]
134 [ + + ]: 228 : if (pAttr)
135 : : {
136 [ + - ][ - + ]: 24 : if (pAttr->isIndexed() && !m_aCompiler->hasORCondition())
[ # # ][ - + ]
137 : : {
138 [ # # ][ # # ]: 0 : OCode* pCode1 = *(aIter + 1);
139 [ # # ][ # # ]: 0 : OCode* pCode2 = *(aIter + 2);
140 : :
141 [ # # ][ # # ]: 0 : if (PTR_CAST(OOperand,pCode1))
[ # # ][ # # ]
[ # # ][ # # ]
142 [ # # ][ # # ]: 0 : pEvaluateSet = pAttr->preProcess(PTR_CAST(OBoolOperator,pCode2), PTR_CAST(OOperand,pCode1));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
143 : : else
144 [ # # ][ # # ]: 0 : pEvaluateSet = pAttr->preProcess(PTR_CAST(OBoolOperator,pCode1));
[ # # ][ # # ]
[ # # ]
145 : : }
146 : :
147 [ - + ]: 24 : if (pEvaluateSet)
148 : : {
149 [ # # ]: 0 : _rEvaluateSetList.push_back(pEvaluateSet);
150 : 0 : pEvaluateSet = NULL;
151 : : }
152 [ + - ]: 24 : pAttr->bindValue(_pRow);
153 : : }
154 : : }
155 : 88 : }
156 : : //------------------------------------------------------------------
157 : 88 : void OSQLAnalyzer::bindSelectRow(const OValueRefRow& _pRow)
158 : : {
159 : : // first the select part
160 [ + - ]: 88 : OEvaluateSetList aEvaluateSetList;
161 [ + - ][ + + ]: 132 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
162 : : {
163 [ - + ]: 44 : if ( aIter->first.is() )
164 [ # # ]: 0 : bindRow( aIter->first->m_aCodeList,_pRow,aEvaluateSetList);
165 : 88 : }
166 : 88 : }
167 : : //------------------------------------------------------------------
168 : 88 : ::std::vector<sal_Int32>* OSQLAnalyzer::bindEvaluationRow(OValueRefRow& _pRow)
169 : : {
170 [ + - ]: 88 : OEvaluateSetList aEvaluateSetList;
171 [ + - ]: 88 : bindRow( m_aCompiler->m_aCodeList,_pRow,aEvaluateSetList);
172 : :
173 : 88 : ::std::vector<sal_Int32>* pKeySet = NULL;
174 : 88 : OEvaluateSet* pEvaluateSet = NULL;
175 : :
176 : : // create Keyset with smallest list
177 [ - + ]: 88 : if(!aEvaluateSetList.empty())
178 : : {
179 : : // which list has the smallest count?
180 : 0 : OEvaluateSetList::iterator i = aEvaluateSetList.begin();
181 [ # # ]: 0 : pEvaluateSet = *(i);
182 [ # # ][ # # ]: 0 : for(++i; i != aEvaluateSetList.end();++i)
[ # # ][ # # ]
183 : : {
184 [ # # ]: 0 : OEvaluateSet* pEvaluateSetComp = (*i);
185 [ # # ][ # # ]: 0 : for(OEvaluateSet::reverse_iterator j = pEvaluateSet->rbegin(); j != pEvaluateSet->rend(); ++j)
[ # # ]
186 : : {
187 [ # # ][ # # ]: 0 : if (pEvaluateSetComp->find(j->second) != pEvaluateSetComp->end())
[ # # ][ # # ]
188 [ # # ][ # # ]: 0 : pEvaluateSet->erase(j->second);
189 : : }
190 : : }
191 [ # # ][ # # ]: 0 : pKeySet = new ::std::vector<sal_Int32>(pEvaluateSet->size());
192 : 0 : sal_Int32 k=0;
193 [ # # ][ # # ]: 0 : for(OEvaluateSet::iterator j = pEvaluateSet->begin(); j != pEvaluateSet->end(); ++j,++k)
[ # # ]
194 : : {
195 [ # # ][ # # ]: 0 : (*pKeySet)[k] = j->second;
196 : : }
197 : :
198 : : // delete all
199 [ # # ][ # # ]: 0 : for(i = aEvaluateSetList.begin(); i != aEvaluateSetList.end();++i)
[ # # ]
200 [ # # ][ # # ]: 0 : delete (*i);
201 : : }
202 : :
203 : 88 : return pKeySet;
204 : : }
205 : :
206 : : // -----------------------------------------------------------------------------
207 : 24 : OOperandAttr* OSQLAnalyzer::createOperandAttr(sal_Int32 _nPos,
208 : : const Reference< XPropertySet>& _xCol,
209 : : const Reference< XNameAccess>& /*_xIndexes*/)
210 : : {
211 [ + - ]: 24 : return new OOperandAttr(static_cast<sal_uInt16>(_nPos),_xCol);
212 : : }
213 : : // -----------------------------------------------------------------------------
214 : 3170 : sal_Bool OSQLAnalyzer::hasRestriction() const
215 : : {
216 : 3170 : return m_aCompiler->hasCode();
217 : : }
218 : : // -----------------------------------------------------------------------------
219 : 2858 : sal_Bool OSQLAnalyzer::hasFunctions() const
220 : : {
221 [ + + ]: 2858 : if ( m_bSelectionFirstTime )
222 : : {
223 : 34 : m_bSelectionFirstTime = sal_False;
224 [ + - ][ + + ]: 44 : for ( ::std::vector< TPredicates >::const_iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end() && !m_bHasSelectionCode ;++aIter)
[ + - ][ + - ]
[ + + ][ # # ]
225 : : {
226 [ - + ]: 10 : if ( aIter->first.is() )
227 : 0 : m_bHasSelectionCode = aIter->first->hasCode();
228 : : }
229 : : }
230 : 2858 : return m_bHasSelectionCode;
231 : : }
232 : : // -----------------------------------------------------------------------------
233 : 0 : void OSQLAnalyzer::setSelectionEvaluationResult(OValueRefRow& _pRow,const ::std::vector<sal_Int32>& _rColumnMapping)
234 : : {
235 : 0 : sal_Int32 nPos = 1;
236 [ # # ][ # # ]: 0 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter,++nPos)
237 : : {
238 [ # # ]: 0 : if ( aIter->second.is() )
239 : : {
240 : : // the first column (index 0) is for convenience only. The first real select column is no 1.
241 : 0 : sal_Int32 map = nPos;
242 [ # # ]: 0 : if ( nPos < static_cast< sal_Int32 >( _rColumnMapping.size() ) )
243 [ # # ]: 0 : map = _rColumnMapping[nPos];
244 [ # # ]: 0 : if ( map > 0 )
245 [ # # ]: 0 : aIter->second->startSelection( (_pRow->get())[map] );
246 : : }
247 : : }
248 : 0 : }
249 : : // -----------------------------------------------------------------------------
250 : 88 : void OSQLAnalyzer::dispose()
251 : : {
252 : 88 : m_aCompiler->dispose();
253 [ + - ][ + + ]: 132 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
254 : : {
255 [ - + ]: 44 : if ( aIter->first.is() )
256 [ # # ]: 0 : aIter->first->dispose();
257 : : }
258 : 88 : }
259 : : // -----------------------------------------------------------------------------
260 : 88 : void OSQLAnalyzer::setOrigColumns(const OFileColumns& rCols)
261 : : {
262 : 88 : m_aCompiler->setOrigColumns(rCols);
263 [ + - ][ - + ]: 88 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
264 : : {
265 [ # # ]: 0 : if ( aIter->first.is() )
266 [ # # ]: 0 : aIter->first->setOrigColumns(rCols);
267 : : }
268 : 88 : }
269 : : // -----------------------------------------------------------------------------
270 : :
271 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|