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