QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgssqlstatement.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssqlstatement.h
3  ---------------------
4  begin : April 2016
5  copyright : (C) 2011 by Martin Dobias
6  copyright : (C) 2016 by Even Rouault
7  email : even.rouault at spatialys.com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSSQLSTATEMENT_H
18 #define QGSSQLSTATEMENT_H
19 
20 #include <QCoreApplication>
21 #include <QMetaType>
22 #include <QStringList>
23 #include <QVariant>
24 #include <QList>
25 #include <QSet>
26 
32 class CORE_EXPORT QgsSQLStatement
33 {
34  Q_DECLARE_TR_FUNCTIONS( QgsSQLStatement )
35  public:
39  QgsSQLStatement( const QString& statement );
40 
44  QgsSQLStatement( const QgsSQLStatement& other );
48  QgsSQLStatement& operator=( const QgsSQLStatement& other );
49  ~QgsSQLStatement();
50 
52  bool hasParserError() const;
54  QString parserErrorString() const;
55 
59  bool doBasicValidationChecks( QString& errorMsgOut ) const;
60 
61  class Node;
62 
64  const Node* rootNode() const;
65 
69  QString statement() const;
70 
75  QString dump() const;
76 
80  static QString quotedIdentifier( QString name );
81 
86  static QString quotedIdentifierIfNeeded( QString name );
87 
91  static QString stripQuotedIdentifier( QString text );
92 
96  static QString quotedString( QString text );
97 
103  {
106  };
107 
113  {
114  // logical
117 
118  // comparison
119  boEQ, // =
120  boNE, // <>
121  boLE, // <=
122  boGE, // >=
123  boLT, // <
124  boGT, // >
131 
132  // math
140 
141  // strings
143  };
144 
149  enum JoinType
150  {
158  jtFull
159  };
160 
162  static const char* BinaryOperatorText[];
163 
165  static const char* UnaryOperatorText[];
166 
168  static const char* JoinTypeText[];
169 
171 
172  class Visitor; // visitor interface is defined below
173 
175  enum NodeType
176  {
189  ntCast
190  };
191 
194  class CORE_EXPORT Node
195  {
196  public:
197  virtual ~Node() {}
198 
204  virtual NodeType nodeType() const = 0;
205 
211  virtual QString dump() const = 0;
212 
221  virtual Node* clone() const = 0;
222 
238  virtual void accept( Visitor& v ) const = 0;
239  };
240 
243  class CORE_EXPORT NodeList
244  {
245  public:
247  NodeList() {}
248  virtual ~NodeList() { qDeleteAll( mList ); }
249 
251  void append( Node* node ) { mList.append( node ); }
252 
254  QList<Node*> list() { return mList; }
255 
258  int count() const { return mList.count(); }
259 
261  void accept( Visitor& v ) const { Q_FOREACH ( Node* node, mList ) { node->accept( v ); } }
262 
264  NodeList* clone() const;
265 
267  virtual QString dump() const;
268 
269  protected:
271  };
272 
275  class CORE_EXPORT NodeUnaryOperator : public Node
276  {
277  public:
279  NodeUnaryOperator( UnaryOperator op, Node* operand ) : mOp( op ), mOperand( operand ) {}
280  ~NodeUnaryOperator() { delete mOperand; }
281 
283  UnaryOperator op() const { return mOp; }
284 
286  Node* operand() const { return mOperand; }
287 
288  virtual NodeType nodeType() const override { return ntUnaryOperator; }
289  virtual QString dump() const override;
290 
291  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
292  virtual Node* clone() const override;
293 
294  protected:
297  };
298 
301  class CORE_EXPORT NodeBinaryOperator : public Node
302  {
303  public:
305  NodeBinaryOperator( BinaryOperator op, Node* opLeft, Node* opRight ) : mOp( op ), mOpLeft( opLeft ), mOpRight( opRight ) {}
306  ~NodeBinaryOperator() { delete mOpLeft; delete mOpRight; }
307 
309  BinaryOperator op() const { return mOp; }
310 
312  Node* opLeft() const { return mOpLeft; }
313 
315  Node* opRight() const { return mOpRight; }
316 
317  virtual NodeType nodeType() const override { return ntBinaryOperator; }
318  virtual QString dump() const override;
319 
320  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
321  virtual Node* clone() const override;
322 
324  int precedence() const;
325 
327  bool leftAssociative() const;
328 
329  protected:
330 
334  };
335 
338  class CORE_EXPORT NodeInOperator : public Node
339  {
340  public:
342  NodeInOperator( Node* node, NodeList* list, bool notin = false ) : mNode( node ), mList( list ), mNotIn( notin ) {}
343  virtual ~NodeInOperator() { delete mNode; delete mList; }
344 
346  Node* node() const { return mNode; }
347 
349  bool isNotIn() const { return mNotIn; }
350 
352  NodeList* list() const { return mList; }
353 
354  virtual NodeType nodeType() const override { return ntInOperator; }
355  virtual QString dump() const override;
356 
357  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
358  virtual Node* clone() const override;
359 
360  protected:
363  bool mNotIn;
364  };
365 
368  class CORE_EXPORT NodeBetweenOperator : public Node
369  {
370  public:
372  NodeBetweenOperator( Node* node, Node* minVal, Node* maxVal, bool notBetween = false ) : mNode( node ), mMinVal( minVal ), mMaxVal( maxVal ), mNotBetween( notBetween ) {}
373  virtual ~NodeBetweenOperator() { delete mNode; delete mMinVal; delete mMaxVal; }
374 
376  Node* node() const { return mNode; }
377 
379  bool isNotBetween() const { return mNotBetween; }
380 
382  Node* minVal() const { return mMinVal; }
383 
385  Node* maxVal() const { return mMaxVal; }
386 
387  virtual NodeType nodeType() const override { return ntBetweenOperator; }
388  virtual QString dump() const override;
389 
390  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
391  virtual Node* clone() const override;
392 
393  protected:
398  };
399 
402  class CORE_EXPORT NodeFunction : public Node
403  {
404  public:
406  NodeFunction( QString name, NodeList* args ) : mName( name ), mArgs( args ) {}
407  virtual ~NodeFunction() { delete mArgs; }
408 
410  QString name() const { return mName; }
411 
413  NodeList* args() const { return mArgs; }
414 
415  virtual NodeType nodeType() const override { return ntFunction; }
416  virtual QString dump() const override;
417 
418  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
419  virtual Node* clone() const override;
420 
421  protected:
424 
425  };
426 
429  class CORE_EXPORT NodeLiteral : public Node
430  {
431  public:
433  NodeLiteral( const QVariant& value ) : mValue( value ) {}
434 
436  inline QVariant value() const { return mValue; }
437 
438  virtual NodeType nodeType() const override { return ntLiteral; }
439  virtual QString dump() const override;
440 
441  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
442  virtual Node* clone() const override;
443 
444  protected:
446  };
447 
450  class CORE_EXPORT NodeColumnRef : public Node
451  {
452  public:
454  NodeColumnRef( const QString& name, bool star ) : mName( name ), mDistinct( false ), mStar( star ) {}
456  NodeColumnRef( const QString& tableName, const QString& name, bool star ) : mTableName( tableName ), mName( name ), mDistinct( false ), mStar( star ) {}
457 
459  void setDistinct( bool distinct = true ) { mDistinct = distinct; }
460 
462  QString tableName() const { return mTableName; }
463 
465  QString name() const { return mName; }
466 
468  bool star() const { return mStar; }
469 
471  bool distinct() const { return mDistinct; }
472 
473  virtual NodeType nodeType() const override { return ntColumnRef; }
474  virtual QString dump() const override;
475 
476  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
477  virtual Node* clone() const override;
479  NodeColumnRef* cloneThis() const;
480 
481  protected:
484  bool mDistinct;
485  bool mStar;
486  };
487 
490  class CORE_EXPORT NodeSelectedColumn : public Node
491  {
492  public:
494  NodeSelectedColumn( Node* node ) : mColumnNode( node ) {}
495  virtual ~NodeSelectedColumn() { delete mColumnNode; }
496 
498  void setAlias( const QString& alias ) { mAlias = alias; }
499 
501  Node* column() const { return mColumnNode; }
502 
504  QString alias() const { return mAlias; }
505 
506  virtual NodeType nodeType() const override { return ntSelectedColumn; }
507  virtual QString dump() const override;
508 
509  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
510  virtual Node* clone() const override;
512  NodeSelectedColumn* cloneThis() const;
513 
514  protected:
517  };
518 
521  class CORE_EXPORT NodeCast : public Node
522  {
523  public:
525  NodeCast( Node* node, const QString& type ) : mNode( node ), mType( type ) {}
526  virtual ~NodeCast() { delete mNode; }
527 
529  Node* node() const { return mNode; }
530 
532  QString type() const { return mType; }
533 
534  virtual NodeType nodeType() const override { return ntCast; }
535  virtual QString dump() const override;
536 
537  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
538  virtual Node* clone() const override;
539 
540  protected:
543  };
544 
547  class CORE_EXPORT NodeTableDef : public Node
548  {
549  public:
551  NodeTableDef( const QString& name ) : mName( name ) {}
553  NodeTableDef( const QString& name, const QString& alias ) : mName( name ), mAlias( alias ) {}
554 
556  QString name() const { return mName; }
557 
559  QString alias() const { return mAlias; }
560 
561  virtual NodeType nodeType() const override { return ntTableDef; }
562  virtual QString dump() const override;
563 
564  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
565  virtual Node* clone() const override;
567  NodeTableDef* cloneThis() const;
568 
569  protected:
572  };
573 
576  class CORE_EXPORT NodeJoin : public Node
577  {
578  public:
580  NodeJoin( NodeTableDef* tabledef, Node* onExpr, JoinType type ) : mTableDef( tabledef ), mOnExpr( onExpr ), mType( type ) {}
582  NodeJoin( NodeTableDef* tabledef, QList<QString> usingColumns, JoinType type ) : mTableDef( tabledef ), mOnExpr( nullptr ), mUsingColumns( usingColumns ), mType( type ) {}
583  virtual ~NodeJoin() { delete mTableDef; delete mOnExpr; }
584 
586  NodeTableDef* tableDef() const { return mTableDef; }
587 
589  Node* onExpr() const { return mOnExpr; }
590 
592  QList<QString> usingColumns() const { return mUsingColumns; }
593 
595  JoinType type() const { return mType; }
596 
597  virtual NodeType nodeType() const override { return ntJoin; }
598  virtual QString dump() const override;
599 
600  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
601  virtual Node* clone() const override;
603  NodeJoin* cloneThis() const;
604 
605  protected:
610  };
611 
614  class CORE_EXPORT NodeColumnSorted : public Node
615  {
616  public:
618  NodeColumnSorted( NodeColumnRef* column, bool asc ) : mColumn( column ), mAsc( asc ) {}
619  ~NodeColumnSorted() { delete mColumn; }
620 
622  NodeColumnRef* column() const { return mColumn; }
623 
625  bool ascending() const { return mAsc; }
626 
627  virtual NodeType nodeType() const override { return ntColumnSorted; }
628  virtual QString dump() const override;
629 
630  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
631  virtual Node* clone() const override;
633  NodeColumnSorted* cloneThis() const;
634 
635  protected:
637  bool mAsc;
638  };
639 
642  class CORE_EXPORT NodeSelect : public Node
643  {
644  public:
646  NodeSelect( QList<NodeTableDef*> tableList, QList<NodeSelectedColumn*> columns, bool distinct ) : mTableList( tableList ), mColumns( columns ), mDistinct( distinct ), mWhere( nullptr ) {}
647  virtual ~NodeSelect() { qDeleteAll( mTableList ); qDeleteAll( mColumns ); qDeleteAll( mJoins ); delete mWhere; qDeleteAll( mOrderBy ); }
648 
650  void setJoins( QList<NodeJoin*> joins ) { qDeleteAll( mJoins ); mJoins = joins; }
652  void appendJoin( NodeJoin* join ) { mJoins.append( join ); }
654  void setWhere( Node* where ) { delete mWhere; mWhere = where; }
656  void setOrderBy( QList<NodeColumnSorted*> orderBy ) { qDeleteAll( mOrderBy ); mOrderBy = orderBy; }
657 
659  QList<NodeTableDef*> tables() const { return mTableList; }
661  QList<NodeSelectedColumn*> columns() const { return mColumns; }
663  bool distinct() const { return mDistinct; }
665  QList<NodeJoin*> joins() const { return mJoins; }
667  Node* where() const { return mWhere; }
669  QList<NodeColumnSorted*> orderBy() const { return mOrderBy; }
670 
671  virtual NodeType nodeType() const override { return ntSelect; }
672  virtual QString dump() const override;
673 
674  virtual void accept( Visitor& v ) const override { v.visit( *this ); }
675  virtual Node* clone() const override;
676 
677  protected:
680  bool mDistinct;
684  };
685 
687 
691  class CORE_EXPORT Visitor
692  {
693  public:
694  virtual ~Visitor() {}
696  virtual void visit( const NodeUnaryOperator& n ) = 0;
698  virtual void visit( const NodeBinaryOperator& n ) = 0;
700  virtual void visit( const NodeInOperator& n ) = 0;
702  virtual void visit( const NodeBetweenOperator& n ) = 0;
704  virtual void visit( const NodeFunction& n ) = 0;
706  virtual void visit( const NodeLiteral& n ) = 0;
708  virtual void visit( const NodeColumnRef& n ) = 0;
710  virtual void visit( const NodeSelectedColumn& n ) = 0;
712  virtual void visit( const NodeTableDef& n ) = 0;
714  virtual void visit( const NodeSelect& n ) = 0;
716  virtual void visit( const NodeJoin& n ) = 0;
718  virtual void visit( const NodeColumnSorted& n ) = 0;
720  virtual void visit( const NodeCast& n ) = 0;
721  };
722 
725  class CORE_EXPORT RecursiveVisitor: public QgsSQLStatement::Visitor
726  {
727  public:
730 
731  void visit( const QgsSQLStatement::NodeUnaryOperator& n ) override { n.operand()->accept( *this ); }
732  void visit( const QgsSQLStatement::NodeBinaryOperator& n ) override { n.opLeft()->accept( *this ); n.opRight()->accept( *this ); }
733  void visit( const QgsSQLStatement::NodeInOperator& n ) override { n.node()->accept( *this ); n.list()->accept( *this ); }
734  void visit( const QgsSQLStatement::NodeBetweenOperator& n ) override { n.node()->accept( *this ); n.minVal()->accept( *this ); n.maxVal()->accept( *this ); }
735  void visit( const QgsSQLStatement::NodeFunction& n ) override { n.args()->accept( *this ); }
736  void visit( const QgsSQLStatement::NodeLiteral& ) override {}
737  void visit( const QgsSQLStatement::NodeColumnRef& ) override { }
738  void visit( const QgsSQLStatement::NodeSelectedColumn& n ) override { n.column()->accept( *this ); }
739  void visit( const QgsSQLStatement::NodeTableDef& ) override {}
740  void visit( const QgsSQLStatement::NodeSelect& n ) override;
741  void visit( const QgsSQLStatement::NodeJoin& n ) override;
742  void visit( const QgsSQLStatement::NodeColumnSorted& n ) override { n.column()->accept( *this ); }
743  void visit( const QgsSQLStatement::NodeCast& n ) override { n.node()->accept( *this ); }
744  };
745 
747  void acceptVisitor( Visitor& v ) const;
748 
749  protected:
753 };
754 
756 
757 #endif // QGSSQLSTATEMENT_H
bool ascending() const
Whether the column is sorted in ascending order.
QList< NodeTableDef * > mTableList
virtual void accept(Visitor &v) const override
Support the visitor pattern.
Node * node() const
Node that is refered to.
NodeTableDef(const QString &name)
Constructor with table name.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
JoinType type() const
Join type.
Function with a name and arguments node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
Node * onExpr() const
On expression.
JoinType
list of join types
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QList< NodeSelectedColumn * > mColumns
NodeColumnRef * column() const
The name of the column.
QString type() const
Type.
Node * operand() const
Operand.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
QList< NodeJoin * > mJoins
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
void appendJoin(NodeJoin *join)
Append a join.
UnaryOperator
list of unary operators
Node * column() const
Column that is refered to.
NodeBetweenOperator(Node *node, Node *minVal, Node *maxVal, bool notBetween=false)
Constructor.
NodeTableDef(const QString &name, const QString &alias)
Constructor with table name and alias.
void visit(const QgsSQLStatement::NodeSelectedColumn &n) override
Visit NodeSelectedColumn.
NodeTableDef * tableDef() const
Table definition.
Node * node() const
Variable at the left of BETWEEN.
Binary logical/arithmetical operator (AND, OR, =, +, ...)
NodeBinaryOperator(BinaryOperator op, Node *opLeft, Node *opRight)
Constructor.
Node * opLeft() const
Left operand.
QList< QString > mUsingColumns
NodeType
Node type.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
QString name() const
Return function name.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
bool distinct() const
Return if the SELECT is DISTINCT.
Abstract node class.
QList< QString > usingColumns() const
Columns referenced by USING.
QgsSQLStatement::Node * mRootNode
NodeColumnSorted(NodeColumnRef *column, bool asc)
Constructor.
A visitor that recursively explores all children.
bool isNotIn() const
Whether this is a NOT IN operator.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
Class for parsing SQL statements.
void accept(Visitor &v) const
Accept visitor.
Node * opRight() const
Right operand.
QString name() const
Table name.
void acceptVisitor(Visitor &v) const
Entry function for the visitor pattern.
QString alias() const
Table alias.
virtual QString dump() const =0
Abstract virtual dump method.
void visit(const QgsSQLStatement::NodeBetweenOperator &n) override
Visit NodeBetweenOperator.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeCast(Node *node, const QString &type)
Constructor.
NodeJoin(NodeTableDef *tabledef, QList< QString > usingColumns, JoinType type)
Constructor with table definition and USING columns.
void append(Node *node)
Takes ownership of the provided node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
QList< NodeColumnSorted * > mOrderBy
Literal value (integer, integer64, double, string)
Node * where() const
Return the where clause.
QString name() const
The name of the column.
Node * node() const
Variable at the left of IN.
void setAlias(const QString &alias)
Set alias name.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
Unary logicial/arithmetical operator ( NOT, - )
BinaryOperator
list of binary operators
QList< Node * > list()
Return list.
virtual Node * clone() const =0
Generate a clone of this node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
bool distinct() const
Whether this is prefixed by DISTINCT.
QString alias() const
Alias name.
bool isNotBetween() const
Whether this is a NOT BETWEEN operator.
virtual void accept(Visitor &v) const =0
Support the visitor pattern.
BinaryOperator op() const
Operator.
Reference to a column.
NodeList * args() const
Return arguments.
&#39;X BETWEEN y and z&#39; operator
virtual void accept(Visitor &v) const override
Support the visitor pattern.
void visit(const QgsSQLStatement::NodeColumnSorted &n) override
Visit NodeColumnSorted.
void setOrderBy(QList< NodeColumnSorted *> orderBy)
Set order by columns.
void setJoins(QList< NodeJoin *> joins)
Set joins.
QVariant value() const
The value of the literal.
void visit(const QgsSQLStatement::NodeLiteral &) override
Visit NodeLiteral.
QList< NodeColumnSorted * > orderBy() const
Return the list of order by columns.
Node * minVal() const
Minimum bound.
virtual void visit(const NodeUnaryOperator &n)=0
Visit NodeUnaryOperator.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
void visit(const QgsSQLStatement::NodeInOperator &n) override
Visit NodeInOperator.
bool star() const
Whether this is the * column.
NodeColumnRef(const QString &tableName, const QString &name, bool star)
Constructor with table and column name.
QList< NodeTableDef * > tables() const
Return the list of tables.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
void setDistinct(bool distinct=true)
Set whether this is prefixed by DISTINCT.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
void visit(const QgsSQLStatement::NodeUnaryOperator &n) override
Visit NodeUnaryOperator.
void visit(const QgsSQLStatement::NodeColumnRef &) override
Visit NodeColumnRef.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
void setWhere(Node *where)
Set where clause.
Support for visitor pattern - algorithms dealing with the statement may be implemented without modify...
void visit(const QgsSQLStatement::NodeTableDef &) override
Visit NodeTableDef.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeSelect(QList< NodeTableDef *> tableList, QList< NodeSelectedColumn *> columns, bool distinct)
Constructor.
QString tableName() const
The name of the table.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
NodeUnaryOperator(UnaryOperator op, Node *operand)
Constructor.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeFunction(QString name, NodeList *args)
Constructor.
NodeSelectedColumn(Node *node)
Constructor.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
UnaryOperator op() const
Operator.
Node * maxVal() const
Maximum bound.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
&#39;x IN (y, z)&#39; operator
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeJoin(NodeTableDef *tabledef, Node *onExpr, JoinType type)
Constructor with table definition, ON expression.
QList< NodeSelectedColumn * > columns() const
Return the list of columns.
void visit(const QgsSQLStatement::NodeFunction &n) override
Visit NodeFunction.
NodeColumnRef(const QString &name, bool star)
Constructor with colum name only.
NodeInOperator(Node *node, NodeList *list, bool notin=false)
Constructor.
void visit(const QgsSQLStatement::NodeCast &n) override
Visit NodeCast.
void visit(const QgsSQLStatement::NodeBinaryOperator &n) override
Visit NodeBinaryOperator.
NodeList * list() const
Values list.
NodeLiteral(const QVariant &value)
Constructor.
QString mParserErrorString
QList< NodeJoin * > joins() const
Return the list of joins.
int count() const
Returns the number of nodes in the list.