aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitewith.cpp
blob: c15b447a8c5ab55d3f085ddaf4da3afcd22b7125 (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
#include "sqlitewith.h"
#include "parser/statementtokenbuilder.h"
#include "sqliteselect.h"
#include "common/global.h"

SqliteWith::SqliteWith()
{
}

SqliteWith::SqliteWith(const SqliteWith& other) :
    SqliteStatement(other), recursive(other.recursive)
{
    DEEP_COPY_COLLECTION(CommonTableExpression, cteList);
}

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

TokenList SqliteWith::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    builder.withKeyword("WITH").withSpace();
    if (recursive)
        builder.withKeyword("RECURSIVE").withSpace();

    builder.withStatementList(cteList);

    return builder.build();
}

SqliteWith::CommonTableExpression::CommonTableExpression()
{
}

SqliteWith::CommonTableExpression::CommonTableExpression(const SqliteWith::CommonTableExpression& other) :
    SqliteStatement(other), table(other.table), asMode(other.asMode)
{
    DEEP_COPY_COLLECTION(SqliteIndexedColumn, indexedColumns);
    DEEP_COPY_FIELD(SqliteSelect, select);
}

SqliteWith::CommonTableExpression::CommonTableExpression(const QString& tableName, const QList<SqliteIndexedColumn*>& indexedColumns, SqliteSelect* select, AsMode asMode) :
    table(tableName), indexedColumns(indexedColumns), select(select), asMode(asMode)
{
    select->setParent(this);
}

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

TokenList SqliteWith::CommonTableExpression::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;
    builder.withOther(table);

    if (indexedColumns.size() > 0)
        builder.withSpace().withParLeft().withStatementList(indexedColumns).withParRight();

    builder.withSpace().withKeyword("AS");
    switch (asMode) {
        case ANY:
            break;
        case MATERIALIZED:
            builder.withKeyword("MATERIALIZED");
            break;
        case NOT_MATERIALIZED:
            builder.withKeyword("NOT").withSpace().withKeyword("MATERIALIZED");
            break;
    }

    builder.withSpace().withParLeft().withStatement(select).withParRight();

    return builder.build();
}