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

SqliteForeignKey::Condition::Condition(SqliteForeignKey::Condition::Action action, SqliteForeignKey::Condition::Reaction reaction)
{
    this->action = action;
    this->reaction = reaction;
}

SqliteForeignKey::Condition::Condition(const QString &name)
{
    this->action = SqliteForeignKey::Condition::MATCH;
    this->name = name;
}

SqliteForeignKey::Condition::Condition(const SqliteForeignKey::Condition& other) :
    SqliteStatement(other), action(other.action), name(other.name), reaction(other.reaction)
{
}

QString SqliteForeignKey::Condition::toString(SqliteForeignKey::Condition::Reaction reaction)
{
    switch (reaction)
    {
        case SqliteForeignKey::Condition::SET_NULL:
            return "SET NULL";
        case SqliteForeignKey::Condition::SET_DEFAULT:
            return "SET DEFAULT";
        case SqliteForeignKey::Condition::CASCADE:
            return "CASCADE";
        case SqliteForeignKey::Condition::RESTRICT:
            return "RESTRICT";
        case SqliteForeignKey::Condition::NO_ACTION:
            return "NO ACTION";
    }
    return QString();
}

SqliteForeignKey::Condition::Reaction SqliteForeignKey::Condition::toEnum(const QString& reaction)
{
    QString upper = reaction.toUpper();
    if (upper == "SET NULL")
        return SET_NULL;

    if (upper == "SET DEFAULT")
        return SET_DEFAULT;

    if (upper == "CASCADE")
        return CASCADE;

    if (upper == "RESTRICT")
        return RESTRICT;

    if (upper == "NO ACTION")
        return NO_ACTION;

    qCritical() << "Unknown Reaction value. Cannot convert to Condition::Reaction. Returning default, the SET_NULL.";
    return SET_NULL;
}

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

SqliteForeignKey::SqliteForeignKey()
{
}

SqliteForeignKey::SqliteForeignKey(const SqliteForeignKey& other) :
    SqliteStatement(other), foreignTable(other.foreignTable), deferrable(other.deferrable), initially(other.initially)
{
    DEEP_COPY_COLLECTION(SqliteIndexedColumn, indexedColumns);
    DEEP_COPY_COLLECTION(Condition, conditions);
}

SqliteForeignKey::~SqliteForeignKey()
{
}

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

QStringList SqliteForeignKey::getTablesInStatement()
{
    return getStrListFromValue(foreignTable);
}

TokenList SqliteForeignKey::getTableTokensInStatement()
{
    return parentStatement()->getContextTableTokens(false, false);
}

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

    // Table object
    FullObject fullObj;
    TokenList tokens = getTableTokensInStatement();
    if (tokens.size() > 0)
        fullObj = getFullObject(FullObject::TABLE, dbTokenForFullObjects, tokens[0]);

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

    return result;
}

TokenList SqliteForeignKey::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    builder.withKeyword("REFERENCES").withSpace().withOther(foreignTable);

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

    if (conditions.size() > 0)
        builder.withSpace().withStatementList(conditions, "");

    if (deferrable != SqliteDeferrable::null)
    {
        if (deferrable == SqliteDeferrable::NOT_DEFERRABLE)
            builder.withSpace().withKeyword("NOT").withSpace().withKeyword("DEFERRABLE");
        else if (deferrable == SqliteDeferrable::DEFERRABLE)
            builder.withSpace().withKeyword("DEFERRABLE");

        if (initially != SqliteInitially::null)
            builder.withSpace().withKeyword("INITIALLY").withSpace().withKeyword(sqliteInitially(initially));
    }

    return builder.build();
}


TokenList SqliteForeignKey::Condition::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;

    switch (action)
    {
        case SqliteForeignKey::Condition::UPDATE:
            builder.withKeyword("ON").withSpace().withKeyword("UPDATE").withSpace();
            applyReactionToBuilder(builder);
            break;
        case SqliteForeignKey::Condition::INSERT:
            builder.withKeyword("ON").withSpace().withKeyword("INSERT").withSpace();
            applyReactionToBuilder(builder);
            break;
        case SqliteForeignKey::Condition::DELETE:
            builder.withKeyword("ON").withSpace().withKeyword("DELETE").withSpace();
            applyReactionToBuilder(builder);
            break;
        case SqliteForeignKey::Condition::MATCH:
            builder.withKeyword("MATCH").withSpace().withOther(name);
            break;
    }

    return builder.build();
}

void SqliteForeignKey::Condition::applyReactionToBuilder(StatementTokenBuilder& builder)
{
    switch (reaction)
    {
        case SqliteForeignKey::Condition::SET_NULL:
            builder.withKeyword("SET").withSpace().withKeyword("NULL");
            break;
        case SqliteForeignKey::Condition::SET_DEFAULT:
            builder.withKeyword("SET").withSpace().withKeyword("DEFAULT");
            break;
        case SqliteForeignKey::Condition::CASCADE:
            builder.withKeyword("CASCADE");
            break;
        case SqliteForeignKey::Condition::RESTRICT:
            builder.withKeyword("RESTRICT");
            break;
        case SqliteForeignKey::Condition::NO_ACTION:
            builder.withKeyword("NO").withSpace().withKeyword("ACTION");
            break;
    }
}