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

Go to the SVN repository for this file.

1 /* $Id: query_node_value.cpp 39115 2017-08-01 18:58:09Z asztalos $
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: Bob Falk
27  *
28  * File Description:
29  * Implementation for query execution using a type-promotion approach
30  * for comparisons.
31  */
32 
33 #include <ncbi_pch.hpp>
34 
36 
37 #include <corelib/ncbiobj.hpp>
38 #include <corelib/ncbistd.hpp>
39 
41 #include <gui/objutils/utils.hpp>
42 
43 #include <cmath>
44 
46 
47 
48 /******************************************************************************
49  *
50  * QueryValueType namespace Implementation
51  *
52  *****************************************************************************/
53 
54  namespace QueryValueType {
55 
57  {
58  switch (et) {
59  case eBoolResult: return "boolean expression result";
60  case eBool: return "boolean const";
61  case eInt: return "int const";
62  case eFloat: return "float const";
63  case eString: return "non-convertible string";
64  case eSeqID: return "possible seq-id identifier";
65  case eStringBool: return "bool converted from a string";
66  case eStringInt: return "integer converted form a string";
67  case eStringFloat: return "float converted from a string";
68  case eFieldSeqID: return "possible seq-id field value";
69  case eFieldString: return "string field value";
70  case eFieldBool: return "boolean field value";
71  case eFieldFloat: return "float field value";
72  case eFieldInt: return "int field value";
73  case eUndefined: return "undefined field value";
74  default: return "";
75  };
76  }
77 
78 }
79 
80 /******************************************************************************
81  *
82  * CQueryNodeValue namespace Implementation
83  *
84  *****************************************************************************/
85 
87 {
88  string str = "Type: " + QueryValueType::GetTypeAsString(m_DataType) +
89  string("Data Field: ") + (IsDataField() ? "True " : "False ") +
90  string("Bool Value: ") + (GetValue() ? "True" : "False");
91 
92  return str;
93 }
94 
96 {
98  Dereference();
99  }
100 
101  switch (m_DataType) {
106  {
107  switch (pt) {
109  m_Int = m_Bool ? 1 : 0;
110  break;
112  m_Double = m_Bool ? 1.0 : 0.0;
113  break;
116  m_String = m_Bool ? "true" : "false";
117  break;
119  // Can't convert a bool to a seq-id. Throw an exception
120  // if we are not in query evaluation mode or if the
121  // field in error was not a data field (if it was taken
122  // taken directly from the query string).
124  NCBI_THROW(CQueryExecException, eIncompatibleType,
125  "Query error. Unable to promote boolean to type: " +
126  GetTypeAsString(pt));
127  }
128  break;
129  default:
130  break;
131  }
132  }
133  break;
137  {
138  switch (pt) {
140  m_Bool = (m_Int==0) ? false : true;
141  break;
143  m_Double = (double)m_Int;
144  break;
148  break;
150  // We use the string field for seq-id compares so
151  // convert the int to a string (the string value will
152  // already be set if type is eStringInt or eFieldInt).
156  break;
157  default:
158  break;
159  }
160  }
161  break;
165  {
166  switch (pt) {
168  m_Bool = (m_Double==0.0) ? false : true;
169  break;
171  m_Int = (int)m_Double;
172  break;
176  break;
178  // We use the string field for seq-id compares so
179  // convert the double to a string (the string value will
180  // already be set if type is eStringFloat or eFieldFloat).
181  //??Should we truncate here? double to int? floor?
185  break;
186  default:
187  break;
188  }
189  }
190  break;
192  // leave it as a string if its a seq-id
193  if (pt == QueryValueType::eSeqID) {
194  break;
195  }
196  else if (pt != QueryValueType::eString) {
197  // If the type is string, and the string does not come from
198  // a field, it will not convert for any data, so throw error.
199  NCBI_THROW(CQueryExecException, eNotPromotable,
200  "Query error. Unable to promote string " +
201  m_String + " to type: " + GetTypeAsString(pt));
202  }
203  break;
205  if (pt != QueryValueType::eString &&
206  pt != QueryValueType::eSeqID) {
207 
208  //If this is the type, we already feel it is not promotable
209  NCBI_THROW(CQueryExecException, eIncompatibleType,
210  "Query error. Unable to promote string " +
211  m_String + " to type: " + GetTypeAsString(pt));
212 
213  }
214  break;
215  default:
216  break;
217  }
218 }
219 
221 {
222  return (arg_idx < m_PromoteRules.size() ?
223  m_PromoteRules[arg_idx].m_PromotedType : QueryValueType::eUndefined);
224 }
225 
229 {
230  if (arg_idx < m_PromoteRules.size()) {
231  return (m_PromoteRules[arg_idx].m_Type1 == t1 &&
232  m_PromoteRules[arg_idx].m_Type2 == t2);
233  }
234 
235  return false;
236 }
237 void CQueryNodeValue::SetString(const string& data)
238 {
239  m_String = data;
242 }
243 
245 {
246  m_Bool = data;
249 }
250 
252 {
253  m_Double = data;
256 }
257 
259 {
260  m_Int = data;
263 }
264 
266 {
267  m_Ref = node;
269 }
270 
271 
273 {
274  while (m_DataType == QueryValueType::eRef) {
275  CRef<CQueryNodeValue> tmp_obj = m_Ref;
276  *this = *tmp_obj;
277  }
278 }
279 
281 {
283  return false;
284 
285  *m_Ref = source;
286  return true;
287 }
288 
class CQueryExecException
class CQueryNodeValue
Int8 m_Int
Int data, if data was an integer or converted into one.
QueryValueType::EBaseType m_DataType
Data type, including source of the data (const, string field, or tree)
bool IsDataField() const
bool HasPromoteType(size_t arg_idx, QueryValueType::EBaseType t1, QueryValueType::EBaseType t2)
Return true if there is a promote entry defined for the specified argument pair at 'idx' only if the ...
bool AssignToRef(const CQueryNodeValue &source)
CRef< CQueryNodeValue > m_Ref
Reference to similar object.
virtual void SetString(const string &data)
Set/get underlying data type.
void PromoteTo(QueryValueType::EBaseType pt)
Convert current value to the type 'pt'. Does not update m_DataType.
bool m_Bool
Bool data, if data base a constant boolean or converted into one.
string m_String
String data, if data came from a string or data field in the tree.
virtual void SetDouble(double data)
virtual string GetVisibleValue() const
String value for debuging.
virtual void SetBool(bool data)
double m_Double
Floating point data, if data was a double or converted into one.
vector< CPromoteRule > m_PromoteRules
The promote rules defined for the current operator.
bool GetValue() const
Set boolean result value (result of (sub)expression).
void SetRef(CRef< CQueryNodeValue > node)
QueryValueType::EBaseType GetPromoteType(size_t arg_idx)
Get the promotion type for a specific argument pair, or eUndefined if no rule is available.
virtual void SetInt(Int8 data)
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
string
Definition: cgiapp.hpp:687
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#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 string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
EBaseType
Set of all possible types for nodes.
string GetTypeAsString(EBaseType et)
const CharType(& source)[N]
Definition: pointer.h:1149
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Fri Jun 14 16:54:09 2024 by modify_doxy.py rev. 669887