NCBI C++ ToolKit
table_query_exec.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: table_query_exec.cpp 39021 2017-07-20 16:37:55Z falkrb $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Robert Falk
27  *
28  * File Description:
29  * Implementation for tree query execution
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
38 
39 #include <corelib/ncbiobj.hpp>
40 #include <corelib/ncbistd.hpp>
41 
43 
44 
45 /******************************************************************************
46  *
47  * CTableQueryExec Implementation
48  *
49  *****************************************************************************/
51 : m_EvalRow((unsigned int)-1)
52 {
53  m_TableData.Reset(&td);
54 }
55 
57  bool& value)
58 {
59  TFieldID col = (TFieldID)x_GetColumnIdx(identifier);
60 
61  return ResolveIdentifier(col, value);
62 }
63 
65  Int8& value)
66 {
67  TFieldID col = (TFieldID)x_GetColumnIdx(identifier);
68 
69  return ResolveIdentifier(col, value);
70 }
71 
73  double& value)
74 {
75  TFieldID col = (TFieldID)x_GetColumnIdx(identifier);
76 
77  return ResolveIdentifier(col, value);
78 }
79 
82 {
83  TFieldID col = (TFieldID)x_GetColumnIdx(identifier);
84 
85  return ResolveIdentifier(col, value);
86 }
87 
89 {
90  unsigned int col = (unsigned int)id;
91 
92  if (col != (unsigned int)-1) {
93  ITableData::ColumnType ct = m_TableData->GetColumnType(col);
94  string s;
95  Int8 ival;
96  double dval;
97 
98  switch (ct) {
99  case (ITableData::kInt) :
100  ival = (Int8)m_TableData->GetIntValue(m_EvalRow, col);
101  if (ival == 1) {
102  value = true;
103  return true;
104  }
105  else if (ival == 0) {
106  value = false;
107  return true;
108  }
109  return false;
110  break;
111  case (ITableData::kReal) :
112  dval = m_TableData->GetIntValue(m_EvalRow, col);
113  if (dval == 1.0) {
114  value = true;
115  return true;
116  }
117  else if (dval == 0.0) {
118  value = false;
119  return true;
120  }
121  return false;
122  break;
123  case (ITableData::kString) :
125  m_TableData->GetStringValue(m_EvalRow, col, s);
126  try {
128  return true;
129  }
130  catch (CStringException&) {
131  return false;
132  }
133  default:
134  return false;
135  }
136  }
137 
138  return false;
139 }
140 
142 {
143  unsigned int col = (unsigned int)id;
144 
145  if (col != (unsigned int)-1) {
146  ITableData::ColumnType ct = m_TableData->GetColumnType(col);
147  string s;
148 
149  switch (ct) {
150  case (ITableData::kInt) :
151  value = (Int8)m_TableData->GetIntValue(m_EvalRow, col);
152  return true;
153  break;
154  case (ITableData::kReal) :
155  value = (Int8)m_TableData->GetRealValue(m_EvalRow, col);
156  return true;
157  break;
158  case (ITableData::kString) :
160  m_TableData->GetStringValue(m_EvalRow, col, s);
161  try {
163  return true;
164  }
165  catch (CStringException&) {
166  return false;
167  }
168  default:
169  return false;
170  }
171  }
172 
173  return false;
174 }
175 
177 {
178  unsigned int col = (unsigned int)id;
179 
180  if (col != (unsigned int)-1) {
181  ITableData::ColumnType ct = m_TableData->GetColumnType(col);
182  string s;
183 
184  switch (ct) {
185  case (ITableData::kInt) :
186  value = (double)m_TableData->GetIntValue(m_EvalRow, col);
187  return true;
188  break;
189  case (ITableData::kReal) :
190  value = m_TableData->GetRealValue(m_EvalRow, col);
191  return true;
192  break;
193  case (ITableData::kString) :
195  m_TableData->GetStringValue(m_EvalRow, col, s);
196  try {
198  return true;
199  }
200  catch (CStringException&) {
201  return false;
202  }
203  default:
204  return false;
205  }
206  }
207 
208  return false;
209 }
210 
212 {
213  unsigned int col = (unsigned int)id;
214 
215  if (col != (unsigned int)-1) {
216  ITableData::ColumnType ct = m_TableData->GetColumnType(col);
217  string s;
218  long ival;
219  double dval;
220 
221  switch (ct) {
222  case (ITableData::kInt) :
223  ival = m_TableData->GetIntValue(m_EvalRow, col);
224  value = NStr::LongToString(ival);
225  return true;
226  break;
227  case (ITableData::kReal) :
228  dval = m_TableData->GetRealValue(m_EvalRow, col);
229  value = NStr::DoubleToString(dval);
230  return true;
231  break;
232  case (ITableData::kString) :
234  m_TableData->GetStringValue(m_EvalRow, col, value);
235  return true;
236  case (ITableData::kObject) :
237  m_TableData->GetStringValue(m_EvalRow, col, value);
238  return true;
239  default:
240  return false;
241  }
242  }
243 
244  return false;
245 }
246 /// @}
247 
248 /// Get biotree feature ID for identifier or return TFieldID(-1)
250 {
251  return x_GetColumnIdx(identifier);
252 }
253 
254 unsigned int CTableQueryExec::x_GetColumnIdx(const std::string& col_name)
255 {
256  for (unsigned int i=0; i<m_TableData->GetColsCount(); ++i) {
257  int cmp = NStr::CompareNocase(col_name.c_str(),
258  m_TableData->GetColumnLabel(i).c_str());
259  if (cmp == 0)
260  return i;
261  }
262 
263  return (unsigned int)-1;
264 }
265 
266 
268 {
269  for (unsigned int i=0; i<m_TableData->GetColsCount(); ++i) {
270  int cmp = NStr::CompareNocase(identifier.c_str(),
271  m_TableData->GetColumnLabel(i).c_str());
272  if (cmp == 0)
273  return true;
274  }
275 
276  return false;
277 }
278 
280 {
281  for (unsigned int i=0; i<m_TableData->GetColsCount(); ++i) {
282  int cmp = NStr::CompareNocase(identifier.c_str(),
283  m_TableData->GetColumnLabel(i).c_str());
284  if (cmp == 0) {
285  ITableData::ColumnType ct = m_TableData->GetColumnType(i);
286  switch (ct) {
295  default: return CQueryParseNode::eNotSet;
296 
297  }
298  }
299  }
300 
302 }
303 
305 {
306  m_ExceptionCount = 0;
307  m_QueriedCount = 0;
308  m_EvalRow = 0;
309 }
310 
312 {
313  return (m_EvalRow >= m_TableData->GetRowsCount());
314 }
315 
317  bool selected = false;
318 
319  try {
320  ++m_QueriedCount;
321  Evaluate(qtree);
322 
323  CQueryParseTree::TNode* top_node = qtree.GetQueryTree();
324  CQueryNodeValue* v = dynamic_cast<CQueryNodeValue*>(
325  top_node->GetValue().GetUserObject());
326 
327  if (v != NULL) {
328  if (v->GetValue()) {
329  m_Selected.push_back(m_EvalRow);
330  selected = true;
331  }
332  }
333  }
334  catch(CQueryParseException &pe) {
335  // Could avoid flooding log since many may be identical, but
336  // maybe the last one is the one you need to see (for debugging...)
337  LOG_POST("Query parse error during execution: " << pe.GetMsg());
339  }
340  catch(CQueryExecException &ex) {
341  // Could avoid flooding log since many may be identical, but
342  // maybe the last one is the one you need to see (for debugging...)
343  LOG_POST(Info << "Query execution error: " << ex.GetMsg());
345  }
346 
347  ++m_EvalRow;
348 
349  return selected;
350 }
351 
352 
353 
354 
class CQueryExecException
class CQueryNodeValue
bool GetValue() const
Set boolean result value (result of (sub)expression).
Query parser exceptions.
Query tree and associated utility methods.
CStringException –.
Definition: ncbistr.hpp:4500
virtual bool EvalComplete()
virtual bool HasIdentifier(const std::string &identifier)
Search for 'identifier' in dictionary and return true if it exists.
virtual TFieldID GetIdentifier(const std::string &identifier)
Get column number for identifier or return TFieldID(-1)
unsigned int m_EvalRow
current table row for evaluation purposes
CIRef< ITableData > m_TableData
pointer to data including data dictionary (columm names)
std::vector< size_t > m_Selected
currently selected rows from table
virtual bool ResolveIdentifier(const std::string &identifier, bool &value)
If query has an identifier, this will resolve it in an application-specific way.
virtual CQueryParseNode::EType IdentifierType(const std::string &identifier)
Return one of eIntConst, eBoolConst, eFloatConst, eString, or eNotSet.
unsigned int x_GetColumnIdx(const std::string &col_name)
return index of col_name or (unsigned int)-1 if it is not found
virtual void EvalStart()
Move to the first row for eval, return false if table empty.
virtual bool EvalNext(CQueryParseTree &qtree)
Move to the next row for eval, return false if table size < m_EvalRow+1.
CTableQueryExec(ITableData &td)
definition of a Culling tree
Definition: ncbi_tree.hpp:100
Include a standard set of the NCBI C++ Toolkit most basic headers.
string
Definition: cgiapp.hpp:690
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
int m_ExceptionCount
Definition: query_exec.hpp:242
unsigned int TFieldID
Definition: query_exec.hpp:148
const TNode * GetQueryTree() const
EType
Query node type.
Definition: query_parse.hpp:84
int m_QueriedCount
Definition: query_exec.hpp:243
virtual void Evaluate(CQueryParseTree &qtree)
Run query tree evaluation.
Definition: query_exec.cpp:110
@ eNotSet
Produced by the (private) default constructor.
Definition: query_parse.hpp:85
@ eFloatConst
Floating point const.
Definition: query_parse.hpp:88
@ eIntConst
Integer const.
Definition: query_parse.hpp:87
@ eString
String ("free text")
Definition: query_parse.hpp:90
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static bool StringToBool(const CTempString str)
Convert string to bool.
Definition: ncbistr.cpp:2812
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5181
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
static Int8 StringToInt8(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to Int8.
Definition: ncbistr.cpp:793
static string LongToString(long value, TNumToStringFlags flags=0, int base=10)
Convert Int to string.
Definition: ncbistr.hpp:5135
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
Definition: ncbistr.cpp:1381
const TValue & GetValue(void) const
Return node's value.
Definition: ncbi_tree.hpp:184
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n td
int i
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Fri Sep 20 14:57:24 2024 by modify_doxy.py rev. 669887