PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tables/Tables/TaQLNodeHandler.h

http://casacore.googlecode.com/
C Header | 310 lines | 133 code | 48 blank | 129 comment | 0 complexity | e4d770a92ba9df47bd07f6950a93ce56 MD5 | raw file
Possible License(s): GPL-2.0
  1. //# TaQLNodeHandler.h: Classes to handle the nodes in the raw TaQL parse tree
  2. //# Copyright (C) 2005
  3. //# Associated Universities, Inc. Washington DC, USA.
  4. //#
  5. //# This library is free software; you can redistribute it and/or modify it
  6. //# under the terms of the GNU Library General Public License as published by
  7. //# the Free Software Foundation; either version 2 of the License, or (at your
  8. //# option) any later version.
  9. //#
  10. //# This library is distributed in the hope that it will be useful, but WITHOUT
  11. //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  13. //# License for more details.
  14. //#
  15. //# You should have received a copy of the GNU Library General Public License
  16. //# along with this library; if not, write to the Free Software Foundation,
  17. //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
  18. //#
  19. //# Correspondence concerning AIPS++ should be addressed as follows:
  20. //# Internet email: aips2-request@nrao.edu.
  21. //# Postal address: AIPS++ Project Office
  22. //# National Radio Astronomy Observatory
  23. //# 520 Edgemont Road
  24. //# Charlottesville, VA 22903-2475 USA
  25. //#
  26. //# $Id: TaQLNodeHandler.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
  27. #ifndef TABLES_TAQLNODEHANDLER_H
  28. #define TABLES_TAQLNODEHANDLER_H
  29. //# Includes
  30. #include <tables/Tables/TaQLNodeVisitor.h>
  31. #include <tables/Tables/TaQLNodeDer.h>
  32. #include <tables/Tables/TableParse.h>
  33. #include <tables/Tables/ExprNode.h>
  34. #include <tables/Tables/ExprNodeSet.h>
  35. #include <casa/Containers/Record.h>
  36. #include <vector>
  37. namespace casa { //# NAMESPACE CASA - BEGIN
  38. //# Forward Declarations
  39. class TaQLNodeHRValue;
  40. // <summary>
  41. // Class to handle the nodes in the raw TaQL parse tree.
  42. // </summary>
  43. // <use visibility=local>
  44. // <reviewed reviewer="" date="" tests="tTableGram">
  45. // </reviewed>
  46. // <prerequisite>
  47. //# Classes you should understand before using this one.
  48. // <li> <linkto class=TaQLNode>TaQLNode</linkto>
  49. // <li> Note 199 describing
  50. // <a href="../notes/199.html">
  51. // TaQL</a>
  52. // </prerequisite>
  53. // <synopsis>
  54. // TaQLNodeHandler is a specialization of class
  55. // <linkto class=TaQLNodeVisitor>TaQLNodeVisitor</linkto>.
  56. // It processes the raw TaQL parse tree generated by TableGram.
  57. // The processing is done in a recursive way. It starts at the top
  58. // (which is a SELECT, UPDATE, etc. expression) and the processing
  59. // results of a query are stored in a TableParseSelect object.
  60. // These objects are kept in a stack for possible nested queries.
  61. // After a query is fully processed, it is executed. Usually the result
  62. // is a table; only a CALC command gives a TableExprNode as result.
  63. // </synopsis>
  64. // <motivation>
  65. // Separating the raw query parsing from the query processing has
  66. // several advantages compared to the old situation where parsing
  67. // and processing were combined.
  68. // <ul>
  69. // <li> The full command is parsed before any processing is done.
  70. // So in case of a parse error, no possibly expensive processing
  71. // has been done yet.
  72. // <li> In the future query optimization can be done in an easier way.
  73. // <li> Nested parsing is not possible. In case a Table is opened
  74. // with a virtual TaQL column, the parsing of that TaQL string
  75. // does not interfere with parsing the TaQL command.
  76. // <li> It is possible to use expressions in the column list.
  77. // That could not be done before, because the column list was
  78. // parsed/processed before the table list.
  79. // </ul>
  80. // </motivation>
  81. class TaQLNodeHandler : public TaQLNodeVisitor
  82. {
  83. public:
  84. virtual ~TaQLNodeHandler();
  85. // Handle and process the raw parse tree.
  86. // The result contains a Table or TableExprNode object.
  87. TaQLNodeResult handleTree (const TaQLNode& tree,
  88. const std::vector<const Table*>&);
  89. // Define the functions to visit each node type.
  90. // <group>
  91. virtual TaQLNodeResult visitConstNode (const TaQLConstNodeRep& node);
  92. virtual TaQLNodeResult visitRegexNode (const TaQLRegexNodeRep& node);
  93. virtual TaQLNodeResult visitUnaryNode (const TaQLUnaryNodeRep& node);
  94. virtual TaQLNodeResult visitBinaryNode (const TaQLBinaryNodeRep& node);
  95. virtual TaQLNodeResult visitMultiNode (const TaQLMultiNodeRep& node);
  96. virtual TaQLNodeResult visitFuncNode (const TaQLFuncNodeRep& node);
  97. virtual TaQLNodeResult visitRangeNode (const TaQLRangeNodeRep& node);
  98. virtual TaQLNodeResult visitIndexNode (const TaQLIndexNodeRep& node);
  99. virtual TaQLNodeResult visitKeyColNode (const TaQLKeyColNodeRep& node);
  100. virtual TaQLNodeResult visitTableNode (const TaQLTableNodeRep& node);
  101. virtual TaQLNodeResult visitColNode (const TaQLColNodeRep& node);
  102. virtual TaQLNodeResult visitColumnsNode (const TaQLColumnsNodeRep& node);
  103. virtual TaQLNodeResult visitJoinNode (const TaQLJoinNodeRep& node);
  104. virtual TaQLNodeResult visitSortKeyNode (const TaQLSortKeyNodeRep& node);
  105. virtual TaQLNodeResult visitSortNode (const TaQLSortNodeRep& node);
  106. virtual TaQLNodeResult visitLimitOffNode (const TaQLLimitOffNodeRep& node);
  107. virtual TaQLNodeResult visitGivingNode (const TaQLGivingNodeRep& node);
  108. virtual TaQLNodeResult visitUpdExprNode (const TaQLUpdExprNodeRep& node);
  109. virtual TaQLNodeResult visitSelectNode (const TaQLSelectNodeRep& node);
  110. virtual TaQLNodeResult visitUpdateNode (const TaQLUpdateNodeRep& node);
  111. virtual TaQLNodeResult visitInsertNode (const TaQLInsertNodeRep& node);
  112. virtual TaQLNodeResult visitDeleteNode (const TaQLDeleteNodeRep& node);
  113. virtual TaQLNodeResult visitCountNode (const TaQLCountNodeRep& node);
  114. virtual TaQLNodeResult visitCalcNode (const TaQLCalcNodeRep& node);
  115. virtual TaQLNodeResult visitCreTabNode (const TaQLCreTabNodeRep& node);
  116. virtual TaQLNodeResult visitColSpecNode (const TaQLColSpecNodeRep& node);
  117. virtual TaQLNodeResult visitRecFldNode (const TaQLRecFldNodeRep& node);
  118. virtual TaQLNodeResult visitUnitNode (const TaQLUnitNodeRep& node);
  119. // </group>
  120. // Get the actual result object from the result.
  121. static const TaQLNodeHRValue& getHR (const TaQLNodeResult&);
  122. private:
  123. // Push a new TableParseSelect on the stack.
  124. TableParseSelect* pushStack (TableParseSelect::CommandType);
  125. // Get the top of the TableParseSelect stack.
  126. TableParseSelect* topStack() const;
  127. // Pop the top from the TableParseSelect stack.
  128. void popStack();
  129. // Clear the select stack.
  130. void clearStack();
  131. // Handle the select command.
  132. // Optionally the command is not executed (needed for the EXISTS operator).
  133. TaQLNodeResult handleSelect (const TaQLSelectNodeRep& node, Bool doExec);
  134. // Handle a MultiNode containing table info.
  135. void handleTables (const TaQLMultiNode&);
  136. // Handle the WHERE clause.
  137. void handleWhere (const TaQLNode&);
  138. // Handle the UPDATE SET clause.
  139. void handleUpdate (const TaQLMultiNode&);
  140. // Handle the INSERT columns.
  141. void handleInsCol (const TaQLMultiNode&);
  142. // Handle the INSERT values.
  143. void handleInsVal (const TaQLNode&);
  144. // Handle a column specification in a create table.
  145. void handleColSpec (const TaQLMultiNode&);
  146. // Handle a record specification.
  147. Record handleRecord (const TaQLMultiNodeRep*);
  148. // Handle a record field and add it to the Record.
  149. void handleRecFld (const TaQLNode&, Record&);
  150. // Handle a record field with multiple values and add it to the Record.
  151. // The field can be a record or a vector of values.
  152. void handleMultiRecFld (const String& fldName,
  153. const TaQLMultiNodeRep* node,
  154. Record& rec);
  155. // Determine 'highest' constant data type and check if they match.
  156. int checkConstDtype (int dt1, int dt2);
  157. //# Use vector instead of stack because it has random access
  158. //# (which is used in TableParse.cc).
  159. std::vector<TableParseSelect*> itsStack;
  160. //# The temporary tables referred to by $i in the TaQL string.
  161. std::vector<const Table*> itsTempTables;
  162. };
  163. // <summary>
  164. // Class containing the result value of the handling of a TaQLNode.
  165. // </summary>
  166. // <use visibility=local>
  167. // <reviewed reviewer="" date="" tests="tTableGram">
  168. // </reviewed>
  169. // <prerequisite>
  170. //# Classes you should understand before using this one.
  171. // <li> <linkto class=TaQLNode>TaQLNodeResult</linkto>
  172. // <li> <linkto class=TaQLNode>TaQLNodeHandler</linkto>
  173. // <li> Note 199 describing
  174. // <a href="../notes/199.html">
  175. // TaQL</a>
  176. // </prerequisite>
  177. // <synopsis>
  178. // TaQLNodeHRValue is a specialization of class
  179. // <linkto class=TaQLNodeResultRep>TaQLNodeResultRep</linkto>.
  180. // It contains the values resulting from handling a particular node.
  181. // The object is effectively a collection of all possible values that
  182. // need to be returned. Which values are filled in, depends on which node
  183. // has been processed.
  184. // <note> The getHR function in TaQLNodeHandler is very useful to
  185. // extract/cast the TaQLNodeHRValue object from the general
  186. // TaQLNodeResult object.
  187. // </note>
  188. // </synopsis>
  189. class TaQLNodeHRValue: public TaQLNodeResultRep
  190. {
  191. public:
  192. TaQLNodeHRValue()
  193. : itsInt(-1), itsElem(0), itsSet(0), itsNames(0) {}
  194. TaQLNodeHRValue (const TableExprNode& expr)
  195. : itsInt(-1), itsExpr(expr), itsElem(0), itsSet(0), itsNames(0) {}
  196. virtual ~TaQLNodeHRValue();
  197. // Get the values.
  198. // <group>
  199. Int getInt() const
  200. { return itsInt; }
  201. const String& getString() const
  202. { return itsString; }
  203. const String& getAlias() const
  204. { return itsAlias; }
  205. const String& getDtype() const
  206. { return itsDtype; }
  207. const Record& getRecord() const
  208. { return itsRecord; }
  209. const Table& getTable() const
  210. { return itsTable; }
  211. const TableExprNode& getExpr() const
  212. { return itsExpr; }
  213. const TableExprNodeSetElem* getElem() const
  214. { return itsElem; }
  215. const TableExprNodeSet& getExprSet() const
  216. { return *itsSet; }
  217. const Vector<String>* getNames() const
  218. { return itsNames; }
  219. // </group>
  220. // Set the values.
  221. // If a pointer is given, it takes over the pointer.
  222. // <group>
  223. void setInt (Int ival)
  224. { itsInt = ival; }
  225. void setString (const String& str)
  226. { itsString = str; }
  227. void setAlias (const String& alias)
  228. { itsAlias = alias; }
  229. void setDtype (const String& dtype)
  230. { itsDtype = dtype; }
  231. void setRecord (const Record& record)
  232. { itsRecord = record; }
  233. void setTable (const Table& table)
  234. { itsTable = table; }
  235. void setExpr (const TableExprNode& expr)
  236. { itsExpr = expr; }
  237. void setElem (TableExprNodeSetElem* elem)
  238. { itsElem = elem; }
  239. void setExprSet (TableExprNodeSet* set)
  240. { itsSet = set; }
  241. void setNames (Vector<String>* names)
  242. { itsNames = names; }
  243. // </group>
  244. private:
  245. Int itsInt;
  246. String itsString;
  247. String itsAlias;
  248. String itsDtype;
  249. Record itsRecord;
  250. Table itsTable;
  251. TableExprNode itsExpr;
  252. TableExprNodeSetElem* itsElem;
  253. TableExprNodeSet* itsSet;
  254. Vector<String>* itsNames;
  255. };
  256. //# This function can only be implemented after TaQLNodeHRBase is declared.
  257. inline const TaQLNodeHRValue& TaQLNodeHandler::getHR (const TaQLNodeResult& res)
  258. {
  259. return *(TaQLNodeHRValue*)(res.getRep());
  260. }
  261. } //# NAMESPACE CASA - END
  262. #endif