aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitewindowdefinition.cpp
blob: 69138b61e14ca82cfef31f43805930e4a5564e55 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "sqlitewindowdefinition.h"
#include "sqliteexpr.h"
#include "sqliteorderby.h"
#include "parser/statementtokenbuilder.h"
#include "common/global.h"
#include <QDebug>

SqliteWindowDefinition::SqliteWindowDefinition()
{

}

SqliteWindowDefinition::SqliteWindowDefinition(const SqliteWindowDefinition& other) :
    SqliteStatement(other), name(other.name)
{
    DEEP_COPY_FIELD(Window, window);
}

SqliteWindowDefinition::SqliteWindowDefinition(const QString& name, SqliteWindowDefinition::Window* window)
{
    this->name = name;
    this->window = window;
    if (window)
        window->setParent(this);
}

SqliteStatement* SqliteWindowDefinition::clone()
{
    return new SqliteWindowDefinition(*this);
}

TokenList SqliteWindowDefinition::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    builder.withOther(name).withSpace().withKeyword("AS").withParLeft().withStatement(window).withParRight();

    return builder.build();
}

SqliteWindowDefinition::Window::Window()
{
}

SqliteWindowDefinition::Window::Window(const SqliteWindowDefinition::Window& other) :
    SqliteStatement(other), name(other.name), mode(other.mode)
{
    DEEP_COPY_COLLECTION(SqliteExpr, exprList);
    DEEP_COPY_COLLECTION(SqliteOrderBy, orderBy);
    DEEP_COPY_FIELD(Frame, frame);
}

SqliteStatement* SqliteWindowDefinition::Window::clone()
{
    return new Window(*this);
}

void SqliteWindowDefinition::Window::initPartitionBy(const QString& name, const QList<SqliteExpr*>& exprList, const QList<SqliteOrderBy*>& orderBy, SqliteWindowDefinition::Window::Frame* frame)
{
    this->mode = Mode::PARTITION_BY;
    this->name = name;
    initExprList(exprList);
    initOrderBy(orderBy);
    initFrame(frame);
}

void SqliteWindowDefinition::Window::initOrderBy(const QString& name, const QList<SqliteOrderBy*>& orderBy, SqliteWindowDefinition::Window::Frame* frame)
{
    this->mode = Mode::ORDER_BY;
    this->name = name;
    initOrderBy(orderBy);
    initFrame(frame);
}

void SqliteWindowDefinition::Window::init(const QString& name, SqliteWindowDefinition::Window::Frame* frame)
{
    this->mode = Mode::null;
    this->name = name;
    initFrame(frame);
}

TokenList SqliteWindowDefinition::Window::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    if (!name.isNull())
        builder.withOther(name).withSpace();

    switch (mode)
    {
        case SqliteWindowDefinition::Window::Mode::PARTITION_BY:
            builder.withKeyword("PARTITION").withSpace().withKeyword("BY").withSpace().withStatementList(exprList).withSpace();
            break;
        case SqliteWindowDefinition::Window::Mode::ORDER_BY:
            break;
        case SqliteWindowDefinition::Window::Mode::null:
            break;
    }

    if (orderBy.size() > 0)
        builder.withKeyword("ORDER").withSpace().withKeyword("BY").withSpace().withStatementList(orderBy);

    if (frame)
        builder.withStatement(frame);

    return builder.build();
}

void SqliteWindowDefinition::Window::initExprList(const QList<SqliteExpr*>& exprList)
{
    this->exprList = exprList;
    for (SqliteExpr* expr : exprList)
        expr->setParent(this);
}

void SqliteWindowDefinition::Window::initOrderBy(const QList<SqliteOrderBy*>& orderBy)
{
    this->orderBy = orderBy;
    for (SqliteOrderBy* order : orderBy)
        order->setParent(this);
}

void SqliteWindowDefinition::Window::initFrame(SqliteWindowDefinition::Window::Frame* frame)
{
    this->frame = frame;
    if (frame)
        frame->setParent(this);
}

SqliteWindowDefinition::Window::Frame::RangeOrRows SqliteWindowDefinition::Window::Frame::toRangeOrRows(const QString& value)
{
    QString upper = value.toUpper();
    if (upper == "RANGE")
        return RangeOrRows::RANGE;
    else if (upper == "ROWS")
        return RangeOrRows::ROWS;
    else if (upper == "GROUPS")
        return RangeOrRows::GROUPS;
    else
        return RangeOrRows::null;
}

QString SqliteWindowDefinition::Window::Frame::fromRangeOrRows(SqliteWindowDefinition::Window::Frame::RangeOrRows value)
{
    switch (value)
    {
        case SqliteWindowDefinition::Window::Frame::RangeOrRows::RANGE:
            return "RANGE";
        case SqliteWindowDefinition::Window::Frame::RangeOrRows::ROWS:
            return "ROWS";
        case SqliteWindowDefinition::Window::Frame::RangeOrRows::GROUPS:
            return "GROUPS";
        case SqliteWindowDefinition::Window::Frame::RangeOrRows::null:
            break;
    }
    return QString();
}

SqliteWindowDefinition::Window::Frame::Exclude SqliteWindowDefinition::Window::Frame::toExclude(const QString& value)
{
    QString upper = value.toUpper();
    if (upper == "NO OTHERS")
        return Exclude::NO_OTHERS;
    else if (upper == "CURRENT ROW")
        return Exclude::CURRENT_ROW;
    else if (upper == "GROUP")
        return Exclude::GROUP;
    else if (upper == "TIES")
        return Exclude::TIES;
    else
        return Exclude::null;
}

QString SqliteWindowDefinition::Window::Frame::fromExclude(SqliteWindowDefinition::Window::Frame::Exclude value)
{
    switch (value)
    {
        case SqliteWindowDefinition::Window::Frame::Exclude::TIES:
            return "TIES";
        case SqliteWindowDefinition::Window::Frame::Exclude::NO_OTHERS:
            return "NO OTHERS";
        case SqliteWindowDefinition::Window::Frame::Exclude::CURRENT_ROW:
            return "CURRENT ROW";
        case SqliteWindowDefinition::Window::Frame::Exclude::GROUP:
            return "GROUP";
        case SqliteWindowDefinition::Window::Frame::Exclude::null:
            break;
    }
    return QString();
}

SqliteWindowDefinition::Window::Frame::Frame()
{
}

SqliteWindowDefinition::Window::Frame::Frame(const SqliteWindowDefinition::Window::Frame& other) :
    SqliteStatement(other), rangeOrRows(other.rangeOrRows), exclude(other.exclude)
{
    DEEP_COPY_FIELD(Bound, startBound);
    DEEP_COPY_FIELD(Bound, endBound);
}

SqliteWindowDefinition::Window::Frame::Frame(SqliteWindowDefinition::Window::Frame::RangeOrRows rangeOrRows,
                                             SqliteWindowDefinition::Window::Frame::Bound* startBound,
                                             SqliteWindowDefinition::Window::Frame::Bound* endBound,
                                             SqliteWindowDefinition::Window::Frame::Exclude exclude)
{
    this->rangeOrRows = rangeOrRows;
    this->startBound = startBound;
    this->endBound = endBound;
    this->exclude = exclude;

    if (startBound)
        startBound->setParent(this);

    if (endBound)
        endBound->setParent(this);
}

SqliteStatement* SqliteWindowDefinition::Window::Frame::clone()
{
    return new Frame(*this);
}

TokenList SqliteWindowDefinition::Window::Frame::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    if (rangeOrRows != RangeOrRows::null)
        builder.withKeyword(fromRangeOrRows(rangeOrRows)).withSpace();

    if (endBound)
        builder.withKeyword("BETWEEN").withSpace().withStatement(startBound).withSpace()
                .withKeyword("AND").withSpace().withStatement(endBound);
    else
        builder.withStatement(startBound);

    if (exclude != Exclude::null)
    {
        builder.withSpace().withKeyword("EXCLUDE");
        for (const QString& kw : fromExclude(exclude).split(" "))
            builder.withSpace().withKeyword(kw);
    }

    return builder.build();
}

SqliteWindowDefinition::Window::Frame::Bound::Bound()
{
}

SqliteWindowDefinition::Window::Frame::Bound::Bound(const SqliteWindowDefinition::Window::Frame::Bound& other) :
    SqliteStatement(other), type(other.type)
{
    DEEP_COPY_FIELD(SqliteExpr, expr);
}

SqliteWindowDefinition::Window::Frame::Bound::Bound(SqliteExpr* expr, const QString& value)
{
    this->expr = expr;
    if (expr)
        expr->setParent(this);

    QString upper = value.toUpper();
    if (upper == "UNBOUNDED PRECEDING")
        type = Type::UNBOUNDED_PRECEDING;
    else if (expr && upper == "PRECEDING")
        type = Type::EXPR_PRECEDING;
    else if (upper == "UNBOUNDED FOLLOWING")
        type = Type::UNBOUNDED_FOLLOWING;
    else if (expr && upper == "FOLLOWING")
        type = Type::EXPR_FOLLOWING;
    else if (upper == "CURRENT ROW")
        type = Type::CURRENT_ROW;
    else
        qCritical() << "Unexpected Window Frame Bound:" << value;
}

SqliteStatement* SqliteWindowDefinition::Window::Frame::Bound::clone()
{
    return new Bound(*this);
}

TokenList SqliteWindowDefinition::Window::Frame::Bound::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    switch (type)
    {
        case SqliteWindowDefinition::Window::Frame::Bound::Type::UNBOUNDED_PRECEDING:
            builder.withKeyword("UNBOUNDED").withSpace().withKeyword("PRECEDING");
            break;
        case SqliteWindowDefinition::Window::Frame::Bound::Type::UNBOUNDED_FOLLOWING:
            builder.withKeyword("UNBOUNDED").withSpace().withKeyword("FOLLOWING");
            break;
        case SqliteWindowDefinition::Window::Frame::Bound::Type::EXPR_PRECEDING:
            builder.withStatement(expr).withSpace().withKeyword("PRECEDING");
            break;
        case SqliteWindowDefinition::Window::Frame::Bound::Type::EXPR_FOLLOWING:
            builder.withStatement(expr).withSpace().withKeyword("FOLLOWING");
            break;
        case SqliteWindowDefinition::Window::Frame::Bound::Type::CURRENT_ROW:
            builder.withKeyword("CURRENT").withSpace().withKeyword("ROW");
            break;
    }

    return builder.build();
}