aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitecreateview.cpp
blob: 36d58521966f7fc10b502b49a603bf0bbc60680a (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
#include "sqlitecreateview.h"
#include "sqliteselect.h"
#include "sqlitequerytype.h"
#include "parser/statementtokenbuilder.h"
#include "common/global.h"
#include "sqliteindexedcolumn.h"
#include <QDebug>

SqliteCreateView::SqliteCreateView()
{
    queryType = SqliteQueryType::CreateView;
}

SqliteCreateView::SqliteCreateView(const SqliteCreateView& other) :
    SqliteQuery(other), tempKw(other.tempKw), temporaryKw(other.temporaryKw), ifNotExists(other.ifNotExists),
    database(other.database), view(other.view)
{
    DEEP_COPY_FIELD(SqliteSelect, select);
    DEEP_COPY_COLLECTION(SqliteIndexedColumn, columns);
}

SqliteCreateView::SqliteCreateView(int temp, bool ifNotExists, const QString &name1, const QString &name2, SqliteSelect *select) :
    SqliteCreateView()
{
    this->ifNotExists = ifNotExists;

    if (name2.isNull())
        view = name1;
    else
    {
        database = name1;
        view = name2;
    }

    if (temp == 2)
        temporaryKw = true;
    else if (temp == 1)
        tempKw = true;

    this->select = select;

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

SqliteCreateView::SqliteCreateView(int temp, bool ifNotExists, const QString& name1, const QString& name2, SqliteSelect* select, const QList<SqliteIndexedColumn*>& columns)
    : SqliteCreateView(temp, ifNotExists, name1, name2, select)
{
    this->columns = columns;

    for (SqliteIndexedColumn* col : columns)
        col->setParent(this);
}

SqliteCreateView::~SqliteCreateView()
{
}

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

QStringList SqliteCreateView::getDatabasesInStatement()
{
    return getStrListFromValue(database);
}

TokenList SqliteCreateView::getDatabaseTokensInStatement()
{
    return getDbTokenListFromFullname();
}

QList<SqliteStatement::FullObject> SqliteCreateView::getFullObjectsInStatement()
{
    QList<FullObject> result;

    // View object
    FullObject fullObj = getFullObjectFromFullname(FullObject::VIEW);

    if (fullObj.isValid())
        result << fullObj;

    // Db object
    fullObj = getFirstDbFullObject();
    if (fullObj.isValid())
    {
        result << fullObj;
        dbTokenForFullObjects = fullObj.database;
    }

    return result;
}

TokenList SqliteCreateView::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;
    builder.withTokens(SqliteQuery::rebuildTokensFromContents());
    builder.withKeyword("CREATE").withSpace();
    if (tempKw)
        builder.withKeyword("TEMP").withSpace();
    else if (temporaryKw)
        builder.withKeyword("TEMPORARY").withSpace();

    builder.withKeyword("VIEW").withSpace();
    if (ifNotExists)
        builder.withKeyword("IF").withSpace().withKeyword("NOT").withSpace().withKeyword("EXISTS").withSpace();

    if (!database.isNull())
        builder.withOther(database).withOperator(".");

    builder.withOther(view).withSpace();

    if (columns.size() > 0)
        builder.withParLeft().withStatementList<SqliteIndexedColumn>(columns).withParRight().withSpace();

    builder.withKeyword("AS").withStatement(select);

    builder.withOperator(";");

    return builder.build();
}

QString SqliteCreateView::getTargetDatabase() const
{
    return database;
}

void SqliteCreateView::setTargetDatabase(const QString& database)
{
    this->database = database;
}

QString SqliteCreateView::getObjectName() const
{
    return view;
}

void SqliteCreateView::setObjectName(const QString& name)
{
    view = name;
}

TokenList SqliteCreateView::equivalentSelectTokens() const
{
    if (columns.size() == 0)
        return select->tokens;

    SqliteSelect::Core *core = select->coreSelects.first();
    bool hasStar = std::any_of(core->resultColumns.cbegin(),
                               core->resultColumns.cend(),
                               [](auto col) { return col->star; });
    bool useCTE = false;
    if (!hasStar && columns.size() != core->resultColumns.size())
    {
        qWarning() << "View with a column list clause and non-matching count of columns in SELECT. "
                   << "Expect an error if result column count does not match column list length.";
        useCTE = true;
    }
    if (hasStar)
    {
        qWarning() << "View with a column list clause and SELECT *. "
                   << "Expect an error if result column count does not match column list length.";
        useCTE = true;
    }
    if (useCTE)
    {
        StatementTokenBuilder builder;
        builder.withKeyword("WITH").withSpace().withOther(view).withParLeft();
        bool first = true;
        for (SqliteIndexedColumn *column : columns)
        {
            if (!first)
                builder.withOperator(",");
            first = false;
            builder.withOther(column->name);
        }
        builder.withParRight().withKeyword("AS").withParLeft().withTokens(select->tokens).withParRight()
            .withKeyword("SELECT").withSpace().withOperator("*").withSpace()
            .withKeyword("FROM").withSpace().withOther(view);
        return builder.build();
    }

    // There are no * in SELECT, and view column list length matches SELECT column list length.
    // Apply view column names to SELECT as aliases.
    SqliteSelect* selectCopy = dynamic_cast<SqliteSelect*>(select->clone());
    QList<SqliteSelect::Core::ResultColumn*> resultColumns = selectCopy->coreSelects.first()->resultColumns;
    int n = 0;
    for (SqliteSelect::Core::ResultColumn* column : resultColumns)
    {
        column->asKw = true;
        column->alias = columns.at(n++)->name;
    }
    selectCopy->rebuildTokens();
    return selectCopy->tokens;
}