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 58 : OSQLAnalyzer::OSQLAnalyzer(OConnection* _pConnection)
38 : :m_pConnection(_pConnection)
39 : ,m_bHasSelectionCode(false)
40 58 : ,m_bSelectionFirstTime(true)
41 : {
42 58 : m_aCompiler = new OPredicateCompiler(this);
43 58 : m_aInterpreter = new OPredicateInterpreter(m_aCompiler);
44 58 : }
45 :
46 :
47 116 : OSQLAnalyzer::~OSQLAnalyzer()
48 : {
49 116 : }
50 :
51 :
52 42 : void OSQLAnalyzer::setIndexes(const Reference< XNameAccess>& _xIndexes)
53 : {
54 42 : m_aCompiler->m_xIndexes = _xIndexes;
55 42 : }
56 :
57 58 : void OSQLAnalyzer::start(OSQLParseNode* pSQLParseNode)
58 : {
59 58 : 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 58 : OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
65 58 : if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
66 : {
67 36 : for (size_t i = 0; i < pSelection->count(); i++)
68 : {
69 20 : OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
70 60 : if ( ( SQL_ISRULE(pColumnRef,set_fct_spec) && pColumnRef->count() == 4 )
71 20 : || SQL_ISRULE(pColumnRef,char_value_fct)
72 20 : || SQL_ISRULE(pColumnRef,char_substring_fct)
73 20 : || SQL_ISRULE(pColumnRef,position_exp)
74 20 : || SQL_ISRULE(pColumnRef,fold)
75 20 : || SQL_ISRULE(pColumnRef,length_exp)
76 20 : || SQL_ISRULE(pColumnRef,num_value_exp)
77 20 : || SQL_ISRULE(pColumnRef,term)
78 20 : || SQL_ISRULE(pColumnRef,factor)
79 40 : || 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 20 : 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 40 : if ( SQL_ISPUNCTUATION( pColumnRef, "*" )
94 40 : || ( SQL_ISRULE( pColumnRef, column_ref )
95 20 : && ( 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 20 : m_aSelectionEvaluations.push_back( TPredicates() );
111 : }
112 : }
113 : }
114 : }
115 :
116 58 : m_aCompiler->start(pSQLParseNode);
117 58 : }
118 :
119 :
120 29 : 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 29 : OEvaluateSet* pEvaluateSet = NULL;
126 :
127 47 : for (OCodeList::iterator aIter = rCodeList.begin(); aIter != rCodeList.end(); ++aIter)
128 : {
129 18 : OOperandAttr* pAttr = PTR_CAST(OOperandAttr,(*aIter));
130 18 : if (pAttr)
131 : {
132 6 : if (pEvaluateSet)
133 : {
134 0 : _rEvaluateSetList.push_back(pEvaluateSet);
135 0 : pEvaluateSet = NULL;
136 : }
137 6 : pAttr->bindValue(_pRow);
138 : }
139 : }
140 29 : }
141 :
142 29 : void OSQLAnalyzer::bindSelectRow(const OValueRefRow& _pRow)
143 : {
144 : // first the select part
145 29 : OEvaluateSetList aEvaluateSetList;
146 38 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
147 : {
148 9 : if ( aIter->first.is() )
149 0 : bindRow( aIter->first->m_aCodeList,_pRow,aEvaluateSetList);
150 29 : }
151 29 : }
152 :
153 29 : ::std::vector<sal_Int32>* OSQLAnalyzer::bindEvaluationRow(OValueRefRow& _pRow)
154 : {
155 29 : OEvaluateSetList aEvaluateSetList;
156 29 : bindRow( m_aCompiler->m_aCodeList,_pRow,aEvaluateSetList);
157 :
158 29 : ::std::vector<sal_Int32>* pKeySet = NULL;
159 29 : OEvaluateSet* pEvaluateSet = NULL;
160 :
161 : // create Keyset with smallest list
162 29 : if(!aEvaluateSetList.empty())
163 : {
164 : // which list has the smallest count?
165 0 : OEvaluateSetList::iterator i = aEvaluateSetList.begin();
166 0 : pEvaluateSet = *(i);
167 0 : for(++i; i != aEvaluateSetList.end();++i)
168 : {
169 0 : OEvaluateSet* pEvaluateSetComp = (*i);
170 0 : for(OEvaluateSet::reverse_iterator j = pEvaluateSet->rbegin(); j != pEvaluateSet->rend(); ++j)
171 : {
172 0 : if (pEvaluateSetComp->find(j->second) != pEvaluateSetComp->end())
173 0 : pEvaluateSet->erase(j->second);
174 : }
175 : }
176 0 : pKeySet = new ::std::vector<sal_Int32>(pEvaluateSet->size());
177 0 : sal_Int32 k=0;
178 0 : for(OEvaluateSet::iterator j = pEvaluateSet->begin(); j != pEvaluateSet->end(); ++j,++k)
179 : {
180 0 : (*pKeySet)[k] = j->second;
181 : }
182 :
183 : // delete all
184 0 : for(i = aEvaluateSetList.begin(); i != aEvaluateSetList.end();++i)
185 0 : delete (*i);
186 : }
187 :
188 29 : return pKeySet;
189 : }
190 :
191 :
192 8 : OOperandAttr* OSQLAnalyzer::createOperandAttr(sal_Int32 _nPos,
193 : const Reference< XPropertySet>& _xCol,
194 : const Reference< XNameAccess>& /*_xIndexes*/)
195 : {
196 8 : return new OOperandAttr(static_cast<sal_uInt16>(_nPos),_xCol);
197 : }
198 :
199 2378 : bool OSQLAnalyzer::hasRestriction() const
200 : {
201 2378 : return m_aCompiler->hasCode();
202 : }
203 :
204 2204 : bool OSQLAnalyzer::hasFunctions() const
205 : {
206 2204 : if ( m_bSelectionFirstTime )
207 : {
208 24 : m_bSelectionFirstTime = false;
209 29 : for ( ::std::vector< TPredicates >::const_iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end() && !m_bHasSelectionCode ;++aIter)
210 : {
211 5 : if ( aIter->first.is() )
212 0 : m_bHasSelectionCode = aIter->first->hasCode();
213 : }
214 : }
215 2204 : return m_bHasSelectionCode;
216 : }
217 :
218 0 : void OSQLAnalyzer::setSelectionEvaluationResult(OValueRefRow& _pRow,const ::std::vector<sal_Int32>& _rColumnMapping)
219 : {
220 0 : sal_Int32 nPos = 1;
221 0 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter,++nPos)
222 : {
223 0 : if ( aIter->second.is() )
224 : {
225 : // the first column (index 0) is for convenience only. The first real select column is no 1.
226 0 : sal_Int32 map = nPos;
227 0 : if ( nPos < static_cast< sal_Int32 >( _rColumnMapping.size() ) )
228 0 : map = _rColumnMapping[nPos];
229 0 : if ( map > 0 )
230 0 : aIter->second->startSelection( (_pRow->get())[map] );
231 : }
232 : }
233 0 : }
234 :
235 58 : void OSQLAnalyzer::dispose()
236 : {
237 58 : m_aCompiler->dispose();
238 78 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
239 : {
240 20 : if ( aIter->first.is() )
241 0 : aIter->first->dispose();
242 : }
243 58 : }
244 :
245 58 : void OSQLAnalyzer::setOrigColumns(const OFileColumns& rCols)
246 : {
247 58 : m_aCompiler->setOrigColumns(rCols);
248 58 : for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
249 : {
250 0 : if ( aIter->first.is() )
251 0 : aIter->first->setOrigColumns(rCols);
252 : }
253 58 : }
254 :
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|