QGIS API Documentation 4.1.0-Master (467af3bbe65)
Loading...
Searching...
No Matches
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 <memory>
21
22#include "qgis_core.h"
23#include "qgis_sip.h"
24
25#include <QCoreApplication>
26#include <QList>
27#include <QMetaType>
28#include <QSet>
29#include <QString>
30#include <QStringList>
31#include <QVariant>
32
33using namespace Qt::StringLiterals;
34
37 * \brief Parses SQL statements.
38 */
39class CORE_EXPORT QgsSQLStatement
40{
41 Q_DECLARE_TR_FUNCTIONS( QgsSQLStatement )
42 public:
46 QgsSQLStatement( const QString &statement );
47
49
50 QgsSQLStatement &operator=( const QgsSQLStatement &other );
51 virtual ~QgsSQLStatement();
52
54 bool hasParserError() const;
56 QString parserErrorString() const;
57
59
63 bool doBasicValidationChecks( QString &errorMsgOut SIP_OUT ) const;
64
65 class Node;
66
71 const QgsSQLStatement::Node *rootNode() const;
72
78 QString statement() const;
79
86 QString dump() const;
87
93 static QString quotedIdentifier( QString name );
94
101 static QString quotedIdentifierIfNeeded( const QString &name );
102
107 static QString stripQuotedIdentifier( QString text );
108
113 static QString stripMsQuotedIdentifier( QString text );
114
120 static QString quotedString( QString text );
121
131
137 {
138 // logical
141
142 // comparison
143 boEQ, // =
144 boNE, // <>
145 boLE, // <=
146 boGE, // >=
147 boLT, // <
148 boGT, // >
155
156 // math
164
165 // strings
167 };
168
184
186 static const char *BINARY_OPERATOR_TEXT[] SIP_SKIP;
187
189 static const char *UNARY_OPERATOR_TEXT[] SIP_SKIP;
190
192 static const char *JOIN_TYPE_TEXT[] SIP_SKIP;
193
195
196 class Visitor; // visitor interface is defined below
197
215
220 class CORE_EXPORT Node
221 {
222#ifdef SIP_RUN
224 switch ( sipCpp->nodeType() )
225 {
226 case QgsSQLStatement::ntUnaryOperator:
227 sipType = sipType_QgsSQLStatement_NodeUnaryOperator;
228 break;
229 case QgsSQLStatement::ntBinaryOperator:
230 sipType = sipType_QgsSQLStatement_NodeBinaryOperator;
231 break;
232 case QgsSQLStatement::ntInOperator:
233 sipType = sipType_QgsSQLStatement_NodeInOperator;
234 break;
235 case QgsSQLStatement::ntBetweenOperator:
236 sipType = sipType_QgsSQLStatement_NodeBetweenOperator;
237 break;
238 case QgsSQLStatement::ntFunction:
239 sipType = sipType_QgsSQLStatement_NodeFunction;
240 break;
241 case QgsSQLStatement::ntLiteral:
242 sipType = sipType_QgsSQLStatement_NodeLiteral;
243 break;
244 case QgsSQLStatement::ntColumnRef:
245 sipType = sipType_QgsSQLStatement_NodeColumnRef;
246 break;
247 case QgsSQLStatement::ntSelectedColumn:
248 sipType = sipType_QgsSQLStatement_NodeSelectedColumn;
249 break;
250 case QgsSQLStatement::ntSelect:
251 sipType = sipType_QgsSQLStatement_NodeSelect;
252 break;
253 case QgsSQLStatement::ntTableDef:
254 sipType = sipType_QgsSQLStatement_NodeTableDef;
255 break;
256 case QgsSQLStatement::ntJoin:
257 sipType = sipType_QgsSQLStatement_NodeJoin;
258 break;
259 case QgsSQLStatement::ntColumnSorted:
260 sipType = sipType_QgsSQLStatement_NodeColumnSorted;
261 break;
262 case QgsSQLStatement::ntCast:
263 sipType = sipType_QgsSQLStatement_NodeCast;
264 break;
265 default:
266 sipType = 0;
267 break;
268 }
269 SIP_END
270#endif
271
272 public:
273 virtual ~Node() = default;
274
281
287 virtual QString dump() const = 0;
288
298
314 virtual void accept( QgsSQLStatement::Visitor &v ) const = 0;
315 };
316
321 class CORE_EXPORT NodeList
322 {
323 public:
324 NodeList() = default;
325 virtual ~NodeList() { qDeleteAll( mList ); }
326
328 void append( QgsSQLStatement::Node *node SIP_TRANSFER ) { mList.append( node ); }
329
331 QList<QgsSQLStatement::Node *> list() { return mList; }
332
336 int count() const { return mList.count(); }
337
339 void accept( QgsSQLStatement::Visitor &v ) const;
340
343
345 virtual QString dump() const;
346
347 protected:
348 QList<Node *> mList;
349 };
350
355 class CORE_EXPORT NodeUnaryOperator : public QgsSQLStatement::Node
356 {
357 public:
363
364#ifdef SIP_RUN
365 SIP_PYOBJECT __repr__();
366 % MethodCode QString str = u"<QgsSQLStatement.NodeUnaryOperator: %1>"_s.arg( sipCpp->text() );
367 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
368 % End
369#endif
370
373 {
374 return mOp;
375 }
376
378 QgsSQLStatement::Node *operand() const { return mOperand.get(); }
379
381 QString dump() const override;
382
383 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
384 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
385
392 QString text() const;
393
394 private:
395 NodeUnaryOperator( const NodeUnaryOperator &other ) = delete;
396 NodeUnaryOperator &operator=( const NodeUnaryOperator &other ) = delete;
397
398#ifdef SIP_RUN
399 NodeUnaryOperator( const NodeUnaryOperator &other );
400#endif
401
402 protected:
404 std::unique_ptr<Node> mOperand;
405 };
406
411 class CORE_EXPORT NodeBinaryOperator : public QgsSQLStatement::Node
412 {
413 public:
420
421#ifdef SIP_RUN
422 SIP_PYOBJECT __repr__();
423 % MethodCode QString str = u"<QgsSQLStatement.NodeBinaryOperator: %1>"_s.arg( sipCpp->text() );
424 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
425 % End
426#endif
427
430 {
431 return mOp;
432 }
433
435 QgsSQLStatement::Node *opLeft() const { return mOpLeft.get(); }
436
438 QgsSQLStatement::Node *opRight() const { return mOpRight.get(); }
439
441 QString dump() const override;
442
443 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
444 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
445
447 int precedence() const;
448
450 bool leftAssociative() const;
451
458 QString text() const;
459
460 private:
461 NodeBinaryOperator( const NodeBinaryOperator &other ) = delete;
462 NodeBinaryOperator &operator=( const NodeBinaryOperator &other ) = delete;
463
464#ifdef SIP_RUN
466#endif
467
468 protected:
470 std::unique_ptr<Node> mOpLeft;
471 std::unique_ptr<Node> mOpRight;
472 };
473
478 class CORE_EXPORT NodeInOperator : public QgsSQLStatement::Node
479 {
480 public:
487
489 QgsSQLStatement::Node *node() const { return mNode.get(); }
490
492 bool isNotIn() const { return mNotIn; }
493
495 QgsSQLStatement::NodeList *list() const { return mList.get(); }
496
498 QString dump() const override;
499
500 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
501 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
502
503 private:
504 NodeInOperator( const NodeInOperator &other ) = delete;
505 NodeInOperator &operator=( const NodeInOperator &other ) = delete;
506
507#ifdef SIP_RUN
508 NodeInOperator( const NodeInOperator &other );
509#endif
510
511 protected:
512 std::unique_ptr<Node> mNode;
513 std::unique_ptr<NodeList> mList;
514 bool mNotIn;
515 };
516
522 {
523 public:
531
533 QgsSQLStatement::Node *node() const { return mNode.get(); }
534
536 bool isNotBetween() const { return mNotBetween; }
537
539 QgsSQLStatement::Node *minVal() const { return mMinVal.get(); }
540
542 QgsSQLStatement::Node *maxVal() const { return mMaxVal.get(); }
543
545 QString dump() const override;
546
547 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
548 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
549
550 private:
551 NodeBetweenOperator( const NodeBetweenOperator &other ) = delete;
552 NodeBetweenOperator &operator=( const NodeBetweenOperator &other ) = delete;
553
554#ifdef SIP_RUN
556#endif
557
558 protected:
559 std::unique_ptr<Node> mNode;
560 std::unique_ptr<Node> mMinVal;
561 std::unique_ptr<Node> mMaxVal;
563 };
564
569 class CORE_EXPORT NodeFunction : public QgsSQLStatement::Node
570 {
571 public:
574 : mName( name )
575 , mArgs( args )
576 {}
577
578
579#ifdef SIP_RUN
580 SIP_PYOBJECT __repr__();
581 % MethodCode QString function;
582 QString str = u"<QgsSQLStatement.NodeFunction: %1>"_s.arg( sipCpp->name() );
583 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
584 % End
585#endif
586
588 QString name() const
589 {
590 return mName;
591 }
592
594 QgsSQLStatement::NodeList *args() const { return mArgs.get(); }
595
596 QgsSQLStatement::NodeType nodeType() const override { return ntFunction; }
597 QString dump() const override;
598
599 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
600 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
601
602 private:
603 NodeFunction( const NodeFunction &other ) = delete;
604 NodeFunction &operator=( const NodeFunction &other ) = delete;
605
606#ifdef SIP_RUN
607 NodeFunction( const NodeFunction &other );
608#endif
609
610 protected:
611 QString mName;
612 std::unique_ptr<NodeList> mArgs;
613 };
614
619 class CORE_EXPORT NodeLiteral : public QgsSQLStatement::Node
620 {
621 public:
623 NodeLiteral( const QVariant &value )
624 : mValue( value )
625 {}
626
627#ifdef SIP_RUN
628 SIP_PYOBJECT __repr__();
629 % MethodCode QString str = u"<QgsSQLStatement.NodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
630 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
631 % End
632#endif
633
635 inline QVariant value() const
636 {
637 return mValue;
638 }
639
640 QgsSQLStatement::NodeType nodeType() const override { return ntLiteral; }
641 QString dump() const override;
642
643 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
644 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
645
650 QString valueAsString() const;
651
652
653 protected:
654 QVariant mValue;
655 };
656
661 class CORE_EXPORT NodeColumnRef : public QgsSQLStatement::Node
662 {
663 public:
665 NodeColumnRef( const QString &name, bool star )
666 : mName( name )
667 , mDistinct( false )
668 , mStar( star )
669 {}
670
671 NodeColumnRef( const QString &tableName, const QString &name, bool star )
673 , mName( name )
674 , mDistinct( false )
675 , mStar( star )
676 {}
677
678#ifdef SIP_RUN
679 SIP_PYOBJECT __repr__();
680 % MethodCode QString columnRef;
681 if ( sipCpp->star() )
682 {
683 columnRef = "*";
684 }
685 else
686 {
687 columnRef = sipCpp->name();
688 }
689 if ( !sipCpp->tableName().isEmpty() )
690 {
691 columnRef = u"\"%1.%2\""_s.arg( sipCpp->tableName(), columnRef );
692 }
693 if ( sipCpp->distinct() )
694 {
695 columnRef = u"DISTINCT %1"_s.arg( columnRef );
696 }
697 QString str = u"<QgsSQLStatement.NodeColumnRef: %1>"_s.arg( columnRef );
698 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
699 % End
700#endif
701
703 void setDistinct( bool distinct = true )
704 {
706 }
707
709 QString tableName() const { return mTableName; }
710
712 QString name() const { return mName; }
713
715 bool star() const { return mStar; }
716
718 bool distinct() const { return mDistinct; }
719
720 QgsSQLStatement::NodeType nodeType() const override { return ntColumnRef; }
721 QString dump() const override;
722
723 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
724 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
726 QgsSQLStatement::NodeColumnRef *cloneThis() const SIP_FACTORY;
727
728 protected:
729 QString mTableName;
730 QString mName;
732 bool mStar;
733 };
734
739 class CORE_EXPORT NodeSelectedColumn : public QgsSQLStatement::Node
740 {
741 public:
746
748 void setAlias( const QString &alias ) { mAlias = alias; }
749
751 QgsSQLStatement::Node *column() const { return mColumnNode.get(); }
752
754 QString alias() const { return mAlias; }
755
757 QString dump() const override;
758
759 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
760 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
763
764 private:
765 NodeSelectedColumn( const NodeSelectedColumn &other ) = delete;
766 NodeSelectedColumn &operator=( const NodeSelectedColumn &other ) = delete;
767
768#ifdef SIP_RUN
770#endif
771
772 protected:
773 std::unique_ptr<Node> mColumnNode;
774 QString mAlias;
775 };
776
781 class CORE_EXPORT NodeCast : public QgsSQLStatement::Node
782 {
783 public:
786 : mNode( node )
787 , mType( type )
788 {}
789
790#ifdef SIP_RUN
791 SIP_PYOBJECT __repr__();
792 % MethodCode QString str = u"<QgsSQLStatement.NodeCast: AS %1>"_s.arg( sipCpp->type() );
793 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
794 % End
795#endif
796
799 {
800 return mNode.get();
801 }
802
804 QString type() const { return mType; }
805
806 QgsSQLStatement::NodeType nodeType() const override { return ntCast; }
807 QString dump() const override;
808
809 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
810 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
811
812 private:
813 NodeCast( const NodeCast &other ) = delete;
814 NodeCast &operator=( const NodeCast &other ) = delete;
815
816#ifdef SIP_RUN
817 NodeCast( const NodeCast &other );
818#endif
819
820 protected:
821 std::unique_ptr<Node> mNode;
822 QString mType;
823 };
824
829 class CORE_EXPORT NodeTableDef : public QgsSQLStatement::Node
830 {
831 public:
833 NodeTableDef( const QString &name )
834 : mName( name )
835 {}
836
837 NodeTableDef( const QString &name, const QString &alias )
838 : mName( name )
839 , mAlias( alias )
840 {}
841
846 NodeTableDef( const QString &schema, const QString &name, const QString &alias )
847 : mName( name )
848 , mSchema( schema )
849 , mAlias( alias )
850 {}
851
852#ifdef SIP_RUN
853 SIP_PYOBJECT __repr__();
854 % MethodCode QString table;
855 if ( !sipCpp->schema().isEmpty() )
856 {
857 table = u"%1.%2"_s.arg( sipCpp->schema(), sipCpp->name() );
858 }
859 else
860 {
861 table = sipCpp->name();
862 }
863 QString str = u"<QgsSQLStatement.NodeTableDef: %1>"_s.arg( table );
864 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
865 % End
866#endif
867
869 QString name() const
870 {
871 return mName;
872 }
873
879 QString schema() const { return mSchema; }
880
882 QString alias() const { return mAlias; }
883
884 QgsSQLStatement::NodeType nodeType() const override { return ntTableDef; }
885 QString dump() const override;
886
887 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
888 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
890 QgsSQLStatement::NodeTableDef *cloneThis() const SIP_FACTORY;
891
892 protected:
893 QString mName;
894 QString mSchema;
895 QString mAlias;
896 };
897
902 class CORE_EXPORT NodeJoin : public QgsSQLStatement::Node
903 {
904 public:
911
913 : mTableDef( tabledef )
915 , mType( type )
916 {}
917
920
922 QgsSQLStatement::Node *onExpr() const { return mOnExpr.get(); }
923
925 QList<QString> usingColumns() const { return mUsingColumns; }
926
929
930 QgsSQLStatement::NodeType nodeType() const override { return ntJoin; }
931 QString dump() const override;
932
933 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
934 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
936 QgsSQLStatement::NodeJoin *cloneThis() const SIP_FACTORY;
937
938 private:
939 NodeJoin( const NodeJoin &other ) = delete;
940 NodeJoin &operator=( const NodeJoin &other ) = delete;
941
942#ifdef SIP_RUN
943 NodeJoin( const NodeJoin &other );
944#endif
945
946 protected:
947 std::unique_ptr<NodeTableDef> mTableDef;
948 std::unique_ptr<Node> mOnExpr;
949 QList<QString> mUsingColumns;
951 };
952
957 class CORE_EXPORT NodeColumnSorted : public QgsSQLStatement::Node
958 {
959 public:
965
968
970 bool ascending() const { return mAsc; }
971
973 QString dump() const override;
974
975 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
976 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
978 QgsSQLStatement::NodeColumnSorted *cloneThis() const SIP_FACTORY;
979
980 private:
981 NodeColumnSorted( const NodeColumnSorted &other ) = delete;
982 NodeColumnSorted &operator=( const NodeColumnSorted &other ) = delete;
983
984#ifdef SIP_RUN
985 NodeColumnSorted( const NodeColumnSorted &other );
986#endif
987
988 protected:
989 std::unique_ptr<NodeColumnRef> mColumn;
990 bool mAsc;
991 };
992
997 class CORE_EXPORT NodeSelect : public QgsSQLStatement::Node
998 {
999 public:
1001 NodeSelect( const QList<QgsSQLStatement::NodeTableDef *> &tableList SIP_TRANSFER, const QList<QgsSQLStatement::NodeSelectedColumn *> &columns SIP_TRANSFER, bool distinct )
1002 : mTableList( tableList )
1003 , mColumns( columns )
1004 , mDistinct( distinct )
1005 {}
1006 ~NodeSelect() override;
1007
1009 void setJoins( const QList<QgsSQLStatement::NodeJoin *> &joins SIP_TRANSFER )
1010 {
1011 qDeleteAll( mJoins );
1012 mJoins = joins;
1013 }
1014
1019 void setOrderBy( const QList<QgsSQLStatement::NodeColumnSorted *> &orderBy SIP_TRANSFER )
1020 {
1021 qDeleteAll( mOrderBy );
1022 mOrderBy = orderBy;
1023 }
1024
1026 QList<QgsSQLStatement::NodeTableDef *> tables() const { return mTableList; }
1028 QList<QgsSQLStatement::NodeSelectedColumn *> columns() const { return mColumns; }
1030 bool distinct() const { return mDistinct; }
1032 QList<QgsSQLStatement::NodeJoin *> joins() const { return mJoins; }
1034 QgsSQLStatement::Node *where() const { return mWhere.get(); }
1036 QList<QgsSQLStatement::NodeColumnSorted *> orderBy() const { return mOrderBy; }
1037
1038 QgsSQLStatement::NodeType nodeType() const override { return ntSelect; }
1039 QString dump() const override;
1040
1041 void accept( QgsSQLStatement::Visitor &v ) const override { v.visit( *this ); }
1042 QgsSQLStatement::Node *clone() const override SIP_FACTORY;
1043
1044 private:
1045 NodeSelect( const NodeSelect &other ) = delete;
1046 NodeSelect &operator=( const NodeSelect &other ) = delete;
1047
1048#ifdef SIP_RUN
1049 NodeSelect( const NodeSelect &other );
1050#endif
1051
1052 protected:
1053 QList<NodeTableDef *> mTableList;
1054 QList<NodeSelectedColumn *> mColumns;
1056 QList<NodeJoin *> mJoins;
1057 std::unique_ptr<Node> mWhere;
1058 QList<NodeColumnSorted *> mOrderBy;
1059 };
1060
1062
1068 class CORE_EXPORT Visitor
1069 {
1070 public:
1071 virtual ~Visitor() = default;
1073 virtual void visit( const QgsSQLStatement::NodeUnaryOperator &n ) = 0;
1075 virtual void visit( const QgsSQLStatement::NodeBinaryOperator &n ) = 0;
1077 virtual void visit( const QgsSQLStatement::NodeInOperator &n ) = 0;
1079 virtual void visit( const QgsSQLStatement::NodeBetweenOperator &n ) = 0;
1081 virtual void visit( const QgsSQLStatement::NodeFunction &n ) = 0;
1083 virtual void visit( const QgsSQLStatement::NodeLiteral &n ) = 0;
1085 virtual void visit( const QgsSQLStatement::NodeColumnRef &n ) = 0;
1087 virtual void visit( const QgsSQLStatement::NodeSelectedColumn &n ) = 0;
1089 virtual void visit( const QgsSQLStatement::NodeTableDef &n ) = 0;
1091 virtual void visit( const QgsSQLStatement::NodeSelect &n ) = 0;
1093 virtual void visit( const QgsSQLStatement::NodeJoin &n ) = 0;
1095 virtual void visit( const QgsSQLStatement::NodeColumnSorted &n ) = 0;
1097 virtual void visit( const QgsSQLStatement::NodeCast &n ) = 0;
1098 };
1099
1105 {
1106 public:
1107 RecursiveVisitor() = default;
1108
1109 void visit( const QgsSQLStatement::NodeUnaryOperator &n ) override { n.operand()->accept( *this ); }
1111 {
1112 n.opLeft()->accept( *this );
1113 n.opRight()->accept( *this );
1114 }
1115 void visit( const QgsSQLStatement::NodeInOperator &n ) override
1116 {
1117 n.node()->accept( *this );
1118 n.list()->accept( *this );
1119 }
1121 {
1122 n.node()->accept( *this );
1123 n.minVal()->accept( *this );
1124 n.maxVal()->accept( *this );
1125 }
1126 void visit( const QgsSQLStatement::NodeFunction &n ) override { n.args()->accept( *this ); }
1127 void visit( const QgsSQLStatement::NodeLiteral & ) override {}
1128 void visit( const QgsSQLStatement::NodeColumnRef & ) override {}
1129 void visit( const QgsSQLStatement::NodeSelectedColumn &n ) override { n.column()->accept( *this ); }
1130 void visit( const QgsSQLStatement::NodeTableDef & ) override {}
1131 void visit( const QgsSQLStatement::NodeSelect &n ) override;
1132 void visit( const QgsSQLStatement::NodeJoin &n ) override;
1133 void visit( const QgsSQLStatement::NodeColumnSorted &n ) override { n.column()->accept( *this ); }
1134 void visit( const QgsSQLStatement::NodeCast &n ) override { n.node()->accept( *this ); }
1135 };
1136
1138 void acceptVisitor( QgsSQLStatement::Visitor &v ) const;
1139
1140 protected:
1141 std::unique_ptr<QgsSQLStatement::Node> mRootNode;
1142 bool mAllowFragments = false;
1143 QString mStatement;
1145
1154 QgsSQLStatement( const QString &statement, bool allowFragments );
1155};
1156
1158
1159
1165{
1166 public:
1170 QgsSQLStatementFragment( const QString &fragment );
1171};
1172
1173
1174#endif // QGSSQLSTATEMENT_H
QgsSQLStatementFragment(const QString &fragment)
Constructor for QgsSQLStatementFragment of the specified fragment.
An 'X BETWEEN y and z' operator.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::Node * node() const
Variable at the left of BETWEEN.
QgsSQLStatement::Node * minVal() const
Minimum bound.
bool isNotBetween() const
Whether this is a NOT BETWEEN operator.
QgsSQLStatement::Node * maxVal() const
Maximum bound.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeBetweenOperator(QgsSQLStatement::Node *node, QgsSQLStatement::Node *minVal, QgsSQLStatement::Node *maxVal, bool notBetween=false)
Constructor.
Binary logical/arithmetical operator (AND, OR, =, +, ...).
QgsSQLStatement::Node * opLeft() const
Left operand.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeBinaryOperator(QgsSQLStatement::BinaryOperator op, QgsSQLStatement::Node *opLeft, QgsSQLStatement::Node *opRight)
Constructor.
QgsSQLStatement::BinaryOperator op() const
Operator.
QgsSQLStatement::Node * opRight() const
Right operand.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QgsSQLStatement::Node * node() const
Node that is referred to.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeCast(QgsSQLStatement::Node *node, const QString &type)
Constructor.
QString type() const
Type.
std::unique_ptr< Node > mNode
QString name() const
The name of the column.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeColumnRef(const QString &name, bool star)
Constructor with column name only.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
bool star() const
Whether this is the * column.
NodeColumnRef(const QString &tableName, const QString &name, bool star)
Constructor with table and column name.
QString tableName() const
The name of the table. May be empty.
void setDistinct(bool distinct=true)
Sets whether this is prefixed by DISTINCT.
bool distinct() const
Whether this is prefixed by DISTINCT.
bool ascending() const
Whether the column is sorted in ascending order.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeColumnRef > mColumn
QgsSQLStatement::NodeColumnRef * column() const
The name of the column.
NodeColumnSorted(QgsSQLStatement::NodeColumnRef *column, bool asc)
Constructor.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
Function with a name and arguments node.
NodeFunction(const QString &name, QgsSQLStatement::NodeList *args)
Constructor.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeList > mArgs
QgsSQLStatement::NodeList * args() const
Returns arguments.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QString name() const
Returns function name.
An 'x IN (y, z)' operator.
std::unique_ptr< NodeList > mList
bool isNotIn() const
Whether this is a NOT IN operator.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QgsSQLStatement::Node * node() const
Variable at the left of IN.
std::unique_ptr< Node > mNode
NodeInOperator(QgsSQLStatement::Node *node, QgsSQLStatement::NodeList *list, bool notin=false)
Constructor.
QgsSQLStatement::NodeList * list() const
Values list.
QList< QString > mUsingColumns
QgsSQLStatement::NodeTableDef * tableDef() const
Table definition.
std::unique_ptr< Node > mOnExpr
QgsSQLStatement::Node * onExpr() const
On expression. Will be nullptr if usingColumns() is not empty.
NodeJoin(QgsSQLStatement::NodeTableDef *tabledef, QgsSQLStatement::Node *onExpr, QgsSQLStatement::JoinType type)
Constructor with table definition, ON expression.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeTableDef > mTableDef
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeJoin(QgsSQLStatement::NodeTableDef *tabledef, const QList< QString > &usingColumns, QgsSQLStatement::JoinType type)
Constructor with table definition and USING columns.
QList< QString > usingColumns() const
Columns referenced by USING.
QgsSQLStatement::JoinType type() const
Join type.
void accept(QgsSQLStatement::Visitor &v) const
Accept visitor.
QList< QgsSQLStatement::Node * > list()
Returns list.
int count() const
Returns the number of nodes in the list.
void append(QgsSQLStatement::Node *node)
Takes ownership of the provided node.
Literal value (integer, integer64, double, string).
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeLiteral(const QVariant &value)
Constructor.
QVariant value() const
The value of the literal.
NodeSelect(const QList< QgsSQLStatement::NodeTableDef * > &tableList, const QList< QgsSQLStatement::NodeSelectedColumn * > &columns, bool distinct)
Constructor.
QList< QgsSQLStatement::NodeColumnSorted * > orderBy() const
Returns the list of order by columns.
void setJoins(const QList< QgsSQLStatement::NodeJoin * > &joins)
Sets joins.
void setWhere(QgsSQLStatement::Node *where)
Sets where clause.
QList< NodeColumnSorted * > mOrderBy
QList< QgsSQLStatement::NodeSelectedColumn * > columns() const
Returns the list of columns.
bool distinct() const
Returns if the SELECT is DISTINCT.
QList< NodeSelectedColumn * > mColumns
void appendJoin(QgsSQLStatement::NodeJoin *join)
Append a join.
void setOrderBy(const QList< QgsSQLStatement::NodeColumnSorted * > &orderBy)
Sets order by columns.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
std::unique_ptr< Node > mWhere
QList< QgsSQLStatement::NodeJoin * > joins() const
Returns the list of joins.
QList< NodeTableDef * > mTableList
QgsSQLStatement::Node * where() const
Returns the where clause.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QList< QgsSQLStatement::NodeTableDef * > tables() const
Returns the list of tables.
NodeSelectedColumn(QgsSQLStatement::Node *node)
Constructor.
void setAlias(const QString &alias)
Sets alias name.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::Node * column() const
Column that is referred to.
QString alias() const
Alias name.
std::unique_ptr< Node > mColumnNode
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QString name() const
Table name.
NodeTableDef(const QString &schema, const QString &name, const QString &alias)
Constructor with schema, table name and alias.
NodeTableDef(const QString &name)
Constructor with table name.
NodeTableDef(const QString &name, const QString &alias)
Constructor with table name and alias.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QString alias() const
Table alias.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QString schema() const
Returns the schema name.
Unary logical/arithmetical operator ( NOT, - ).
NodeUnaryOperator(QgsSQLStatement::UnaryOperator op, QgsSQLStatement::Node *operand)
Constructor.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::UnaryOperator op() const
Operator.
QgsSQLStatement::Node * operand() const
Operand.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
Abstract node class for SQL statement nodes.
virtual QgsSQLStatement::Node * clone() const =0
Generate a clone of this node.
virtual QString dump() const =0
Abstract virtual dump method.
virtual QgsSQLStatement::NodeType nodeType() const =0
Abstract virtual that returns the type of this node.
virtual void accept(QgsSQLStatement::Visitor &v) const =0
Support the visitor pattern.
void visit(const QgsSQLStatement::NodeFunction &n) override
Visit NodeFunction.
void visit(const QgsSQLStatement::NodeSelectedColumn &n) override
Visit NodeSelectedColumn.
void visit(const QgsSQLStatement::NodeUnaryOperator &n) override
Visit NodeUnaryOperator.
void visit(const QgsSQLStatement::NodeLiteral &) override
Visit NodeLiteral.
void visit(const QgsSQLStatement::NodeBetweenOperator &n) override
Visit NodeBetweenOperator.
void visit(const QgsSQLStatement::NodeBinaryOperator &n) override
Visit NodeBinaryOperator.
void visit(const QgsSQLStatement::NodeColumnRef &) override
Visit NodeColumnRef.
void visit(const QgsSQLStatement::NodeTableDef &) override
Visit NodeTableDef.
void visit(const QgsSQLStatement::NodeColumnSorted &n) override
Visit NodeColumnSorted.
void visit(const QgsSQLStatement::NodeInOperator &n) override
Visit NodeInOperator.
void visit(const QgsSQLStatement::NodeCast &n) override
Visit NodeCast.
Support for visitor pattern - algorithms dealing with the statement may be implemented without modify...
virtual void visit(const QgsSQLStatement::NodeBetweenOperator &n)=0
Visit NodeBetweenOperator.
virtual ~Visitor()=default
virtual void visit(const QgsSQLStatement::NodeFunction &n)=0
Visit NodeFunction.
virtual void visit(const QgsSQLStatement::NodeColumnRef &n)=0
Visit NodeColumnRef.
virtual void visit(const QgsSQLStatement::NodeBinaryOperator &n)=0
Visit NodeBinaryOperator.
virtual void visit(const QgsSQLStatement::NodeSelect &n)=0
Visit NodeSelect.
virtual void visit(const QgsSQLStatement::NodeCast &n)=0
Visit NodeCast.
virtual void visit(const QgsSQLStatement::NodeSelectedColumn &n)=0
Visit NodeSelectedColumn.
virtual void visit(const QgsSQLStatement::NodeJoin &n)=0
Visit NodeJoin.
virtual void visit(const QgsSQLStatement::NodeUnaryOperator &n)=0
Visit NodeUnaryOperator.
virtual void visit(const QgsSQLStatement::NodeInOperator &n)=0
Visit NodeInOperator.
virtual void visit(const QgsSQLStatement::NodeLiteral &n)=0
Visit NodeLiteral.
virtual void visit(const QgsSQLStatement::NodeColumnSorted &n)=0
Visit NodeColumnSorted.
virtual void visit(const QgsSQLStatement::NodeTableDef &n)=0
Visit NodeTableDef.
bool doBasicValidationChecks(QString &errorMsgOut) const
Performs basic validity checks.
JoinType
list of join types
BinaryOperator
list of binary operators
static QString stripQuotedIdentifier(QString text)
Remove double quotes from an identifier.
static QString quotedIdentifierIfNeeded(const QString &name)
Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.
static QString quotedIdentifier(QString name)
Returns a quoted column reference (in double quotes).
QString parserErrorString() const
Returns parser error.
static QString stripMsQuotedIdentifier(QString text)
Remove double quotes from an Microsoft style identifier.
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes).
QString dump() const
Returns the statement string, constructed from the internal abstract syntax tree.
std::unique_ptr< QgsSQLStatement::Node > mRootNode
UnaryOperator
list of unary operators
bool hasParserError() const
Returns true if an error occurred when parsing the input statement.
const QgsSQLStatement::Node * rootNode() const
Returns the root node of the statement.
QString statement() const
Returns the original, unmodified statement string.
QgsSQLStatement(const QString &statement)
Creates a new statement based on the provided string.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_END
Definition qgis_sip.h:215
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)