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 0 : OSQLAnalyzer::OSQLAnalyzer(OConnection* _pConnection)
38 : :m_pConnection(_pConnection)
39 : ,m_bHasSelectionCode(sal_False)
40 0 : ,m_bSelectionFirstTime(sal_True)
41 : {
42 0 : m_aCompiler = new OPredicateCompiler(this);
43 0 : m_aInterpreter = new OPredicateInterpreter(m_aCompiler);
44 0 : }
45 :
46 :
47 0 : OSQLAnalyzer::~OSQLAnalyzer()
48 : {
49 0 : }
50 :
51 :
52 0 : void OSQLAnalyzer::setIndexes(const Reference< XNameAccess>& _xIndexes)
53 : {
54 0 : m_aCompiler->m_xIndexes = _xIndexes;
55 0 : }
56 :
57 0 : void OSQLAnalyzer::start(OSQLParseNode* pSQLParseNode)
58 : {
59 0 : 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 0 : OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
65 0 : if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
66 : {
67 0 : for (sal_uInt32 i = 0; i < pSelection->count(); i++)
68 : {
69 0 : OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
70 0 : if ( ( SQL_ISRULE(pColumnRef,set_fct_spec) && pColumnRef->count() == 4 )
71 0 : || SQL_ISRULE(pColumnRef,char_value_fct)
72 0 : || SQL_ISRULE(pColumnRef,char_substring_fct)
73 0 : || SQL_ISRULE(pColumnRef,position_exp)
74 0 : || SQL_ISRULE(pColumnRef,fold)
75 0 : || SQL_ISRULE(pColumnRef,length_exp)
76 0 : || SQL_ISRULE(pColumnRef,num_value_exp)
77 0 : || SQL_ISRULE(pColumnRef,term)
78 0 : || SQL_ISRULE(pColumnRef,factor)
79 0 : || 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 0 : 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 0 : if ( SQL_ISPUNCTUATION( pColumnRef, "*" )
94 0 : || ( SQL_ISRULE( pColumnRef, column_ref )
95 0 : && ( 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 0 : m_aSelectionEvaluations.push_back( TPredicates() );
111 : }
112 : }
113 : }
114 : }
115 :
116 0 : m_aCompiler->start(pSQLParseNode);
117 0 : }
118 :
119 :
120 0 : 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 0 : OEvaluateSet* pEvaluateSet = NULL;
126 :
127 0 : for (OCodeList::iterator aIter = rCodeList.begin(); aIter != rCodeList.end(); ++aIter)
128 : {
129 0 : OOperandAttr* pAttr = PTR_CAST(OOperandAttr,(*aIter));
130 0 : if (pAttr)
131 : {
132 0 : 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 0 : if (pEvaluateSet)
144 : {
145 0 : _rEvaluateSetList.push_back(pEvaluateSet);
146 0 : pEvaluateSet = NULL;
147 : }
148 0 : pAttr->bindValue(_pRow);
149 : }
150 : }
151 0 : }
152 :
153 0 : void OSQLAnalyzer::bindSelectRow(const OValueRefRow& _pRow)
154 : {
155 : // first the select part
156 0 : OEvaluateSetList aEvaluateSetList;
157 0 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
158 : {
159 0 : if ( aIter->first.is() )
160 0 : bindRow( aIter->first->m_aCodeList,_pRow,aEvaluateSetList);
161 0 : }
162 0 : }
163 :
164 0 : ::std::vector<sal_Int32>* OSQLAnalyzer::bindEvaluationRow(OValueRefRow& _pRow)
165 : {
166 0 : OEvaluateSetList aEvaluateSetList;
167 0 : bindRow( m_aCompiler->m_aCodeList,_pRow,aEvaluateSetList);
168 :
169 0 : ::std::vector<sal_Int32>* pKeySet = NULL;
170 0 : OEvaluateSet* pEvaluateSet = NULL;
171 :
172 : // create Keyset with smallest list
173 0 : 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 0 : return pKeySet;
200 : }
201 :
202 :
203 0 : OOperandAttr* OSQLAnalyzer::createOperandAttr(sal_Int32 _nPos,
204 : const Reference< XPropertySet>& _xCol,
205 : const Reference< XNameAccess>& /*_xIndexes*/)
206 : {
207 0 : return new OOperandAttr(static_cast<sal_uInt16>(_nPos),_xCol);
208 : }
209 :
210 0 : sal_Bool OSQLAnalyzer::hasRestriction() const
211 : {
212 0 : return m_aCompiler->hasCode();
213 : }
214 :
215 0 : sal_Bool OSQLAnalyzer::hasFunctions() const
216 : {
217 0 : if ( m_bSelectionFirstTime )
218 : {
219 0 : m_bSelectionFirstTime = sal_False;
220 0 : for ( ::std::vector< TPredicates >::const_iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end() && !m_bHasSelectionCode ;++aIter)
221 : {
222 0 : if ( aIter->first.is() )
223 0 : m_bHasSelectionCode = aIter->first->hasCode();
224 : }
225 : }
226 0 : 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 0 : void OSQLAnalyzer::dispose()
247 : {
248 0 : m_aCompiler->dispose();
249 0 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
250 : {
251 0 : if ( aIter->first.is() )
252 0 : aIter->first->dispose();
253 : }
254 0 : }
255 :
256 0 : void OSQLAnalyzer::setOrigColumns(const OFileColumns& rCols)
257 : {
258 0 : m_aCompiler->setOrigColumns(rCols);
259 0 : 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 0 : }
265 :
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|