aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/parser/ast/sqliteselect.h
blob: dfed9c2fae6ebb1af73eb008df444fe15e47deca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef SQLITESELECT_H
#define SQLITESELECT_H

#include "sqlitequery.h"
#include "sqliteexpr.h"
#include "sqlitelimit.h"
#include "sqliteorderby.h"

#include <QList>

class SqliteWith;
class SqliteWindowDefinition;

/**
 * @addtogroup sqlite_statement
 * @brief The SqliteSelect class
 */
class API_EXPORT SqliteSelect : public SqliteQuery
{
    public:
        enum class CompoundOperator
        {
            UNION,
            UNION_ALL,
            INTERSECT,
            EXCEPT,
            null
        };

        class API_EXPORT Core : public SqliteStatement
        {
            public:
                class API_EXPORT ResultColumn : public SqliteStatement
                {
                    public:
                        ResultColumn();
                        ResultColumn(const ResultColumn& other);
                        ResultColumn(SqliteExpr* expr, bool asKw, const QString& alias);
                        ResultColumn(bool star, const QString& table);
                        explicit ResultColumn(bool star);

                        bool isRowId();
                        SqliteStatement* clone();

                        SqliteExpr* expr = nullptr;
                        bool star = false;
                        bool asKw = false;
                        QString alias = QString();
                        QString table = QString();

                    protected:
                        QStringList getTablesInStatement();
                        TokenList getTableTokensInStatement();
                        QList<FullObject> getFullObjectsInStatement();
                        TokenList rebuildTokensFromContents();
                };

                class JoinSource; // forward declaration

                class API_EXPORT SingleSource : public SqliteStatement
                {
                    public:
                        SingleSource();
                        SingleSource(const SingleSource& other);
                        SingleSource(const QString& name1, const QString& name2,
                                     bool asKw, const QString& alias, bool notIndexedKw, const QString& indexedBy);
                        SingleSource(const QString& name1, const QString& name2,
                                     bool asKw, const QString& alias, const QList<SqliteExpr*>& exprList);
                        SingleSource(SqliteSelect* select, bool asKw, const QString& alias);
                        SingleSource(JoinSource* src, bool asKw, const QString& alias);

                        SqliteStatement* clone();

                        QString database = QString();
                        QString table = QString();
                        QString alias = QString();
                        QString funcName = QString();
                        QList<SqliteExpr*> funcParams;
                        bool asKw = false;
                        bool indexedByKw = false;
                        bool notIndexedKw = false;
                        QString indexedBy = QString();
                        SqliteSelect* select = nullptr;
                        JoinSource* joinSource = nullptr;

                    protected:
                        QStringList getTablesInStatement();
                        QStringList getDatabasesInStatement();
                        TokenList getTableTokensInStatement();
                        TokenList getDatabaseTokensInStatement();
                        QList<FullObject> getFullObjectsInStatement();
                        TokenList rebuildTokensFromContents();
                };

                class API_EXPORT JoinOp : public SqliteStatement
                {
                    public:
                        JoinOp();
                        JoinOp(const JoinOp& other);
                        explicit JoinOp(bool comma);
                        explicit JoinOp(const QString& joinToken);
                        JoinOp(const QString& joinToken, const QString& word1);
                        JoinOp(const QString& joinToken, const QString& word1, const QString& word2);

                        SqliteStatement* clone();

                    private:
                        void init(const QString& str);

                    public:
                        bool comma = false;
                        bool joinKw = false;
                        bool naturalKw = false;
                        bool leftKw = false;
                        bool outerKw = false;
                        bool innerKw = false;
                        bool crossKw = false;
                        bool rightKw = false;
                        bool fullKw = false;
                        QString customKw1 = QString();
                        QString customKw2 = QString();
                        QString customKw3 = QString();

                    protected:
                        TokenList rebuildTokensFromContents();
                };

                class API_EXPORT JoinConstraint : public SqliteStatement
                {
                    public:
                        JoinConstraint();
                        JoinConstraint(const JoinConstraint& other);
                        explicit JoinConstraint(SqliteExpr* expr);
                        explicit JoinConstraint(const QList<QString>& strList);

                        SqliteStatement* clone();

                        SqliteExpr* expr = nullptr;
                        QList<QString> columnNames;

                    protected:
                        QStringList getColumnsInStatement();
                        TokenList getColumnTokensInStatement();
                        TokenList rebuildTokensFromContents();
                };

                class API_EXPORT JoinSourceOther : public SqliteStatement
                {
                    public:
                        JoinSourceOther();
                        JoinSourceOther(const JoinSourceOther& other);
                        JoinSourceOther(SqliteSelect::Core::JoinOp *op,
                                        SqliteSelect::Core::SingleSource* src,
                                        SqliteSelect::Core::JoinConstraint* constr);

                        SqliteStatement* clone();

                        JoinOp* joinOp = nullptr;
                        SingleSource* singleSource = nullptr;
                        JoinConstraint* joinConstraint = nullptr;

                    protected:
                        TokenList rebuildTokensFromContents();
                };

                class API_EXPORT JoinSource : public SqliteStatement
                {
                    public:
                        JoinSource();
                        JoinSource(const JoinSource& other);
                        JoinSource(SingleSource* singleSource, const QList<JoinSourceOther*>& list);

                        SqliteStatement* clone();

                        SingleSource* singleSource = nullptr;
                        QList<JoinSourceOther*> otherSources;

                    protected:
                        TokenList rebuildTokensFromContents();
                };

                Core();
                Core(const Core& other);
                Core(int distinct, const QList<ResultColumn*>& resCols, JoinSource* src, SqliteExpr* where,
                     const QList<SqliteExpr*>& groupBy, SqliteExpr* having,
                     const QList<SqliteOrderBy*>& orderBy, SqliteLimit* limit);
                Core(int distinct, const QList<ResultColumn*>& resCols, JoinSource* src, SqliteExpr* where,
                     const QList<SqliteExpr*>& groupBy, SqliteExpr* having, const QList<SqliteWindowDefinition*> windows,
                     const QList<SqliteOrderBy*>& orderBy, SqliteLimit* limit);

                SqliteStatement* clone();

                CompoundOperator compoundOp = CompoundOperator::null;
                QList<ResultColumn*> resultColumns;
                JoinSource* from = nullptr;
                bool distinctKw = false;
                bool allKw = false;
                SqliteExpr* where = nullptr;
                SqliteExpr* having = nullptr;
                QList<SqliteExpr*> groupBy;
                QList<SqliteOrderBy*> orderBy;
                QList<SqliteWindowDefinition*> windows;
                SqliteLimit* limit = nullptr;
                bool valuesMode = false;

            protected:
                TokenList rebuildTokensFromContents();
        };

        SqliteSelect();
        SqliteSelect(const SqliteSelect& other);

        static SqliteSelect* append(SqliteSelect::Core* core);
        static SqliteSelect* append(SqliteSelect* select, CompoundOperator op, SqliteSelect::Core* core);
        static SqliteSelect* append(const QList<QList<SqliteExpr*>>& values);
        static SqliteSelect* append(SqliteSelect* select, SqliteSelect::CompoundOperator op, const QList<QList<SqliteExpr*>>& values);

        SqliteStatement* clone();
        QString compoundOperator(CompoundOperator op);
        CompoundOperator compoundOperator(const QString& op);
        void reset();
        void setWith(SqliteWith* with);

        QList<Core*> coreSelects;
        SqliteWith* with = nullptr;

    protected:
        TokenList rebuildTokensFromContents();
};

typedef QSharedPointer<SqliteSelect> SqliteSelectPtr;
typedef SqliteSelect::Core::ResultColumn SqliteResultColumn;

#endif // SQLITESELECT_H