aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitealtertable.cpp
blob: 42cb59b784e9638bd590aa295c692be6a26fb36e (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
#include "sqlitealtertable.h"
#include "sqlitequerytype.h"
#include "common/global.h"

SqliteAlterTable::SqliteAlterTable()
{
    queryType = SqliteQueryType::AlterTable;
}

SqliteAlterTable::SqliteAlterTable(const SqliteAlterTable& other)
    : SqliteQuery(other), command(other.command), newName(other.newName), database(other.database), table(other.table),
    dropColumnName(other.dropColumnName), columnKw(other.columnKw)
{
    DEEP_COPY_FIELD(SqliteCreateTable::Column, newColumn);
}

SqliteAlterTable::SqliteAlterTable(const QString &name1, const QString &name2, const QString &newName)
    : SqliteAlterTable()
{
    command = Command::RENAME;
    initName(name1, name2);
    this->newName = newName;
}

SqliteAlterTable::SqliteAlterTable(const QString& name1, const QString& name2, bool columnKw, SqliteCreateTable::Column *column)
    : SqliteAlterTable()
{
    command = Command::ADD_COLUMN;
    initName(name1, name2);
    this->columnKw = columnKw;
    this->newColumn = column;
    if (column)
        column->setParent(this);
}

SqliteAlterTable::SqliteAlterTable(const QString& name1, const QString& name2, bool columnKw, const QString& dropColumn)
    : SqliteAlterTable()
{
    command = Command::DROP_COLUMN;
    initName(name1, name2);
    this->columnKw = columnKw;
    this->dropColumnName = dropColumn;
}

SqliteAlterTable::~SqliteAlterTable()
{
}

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

QStringList SqliteAlterTable::getColumnsInStatement()
{
    QStringList list;
    if (!dropColumnName.isNull())
        list << dropColumnName;

    return list;
}

QStringList SqliteAlterTable::getTablesInStatement()
{
    QStringList list;
    if (!table.isNull())
        list << table;

    if (!newName.isNull())
        list << newName;

    return list;
}

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

TokenList SqliteAlterTable::getColumnTokensInStatement()
{
    if (command == Command::DROP_COLUMN && tokensMap.contains("nm"))
        return extractPrintableTokens(tokensMap["nm"]);

    return TokenList();
}

TokenList SqliteAlterTable::getTableTokensInStatement()
{
    return getObjectTokenListFromFullname();
}

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

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

    FullObject fullObj = getFullObjectFromFullname(FullObject::TABLE);
    if (fullObj.isValid())
        result << fullObj;

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

    return result;
}

void SqliteAlterTable::initName(const QString &name1, const QString &name2)
{
    if (!name2.isNull())
    {
        database = name1;
        table = name2;
    }
    else
        table = name1;
}

TokenList SqliteAlterTable::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;
    builder.withTokens(SqliteQuery::rebuildTokensFromContents());
    builder.withKeyword("ALTER").withSpace().withKeyword("TABLE").withSpace();

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

    builder.withOther(table).withSpace();

    switch (command) {
        case Command::RENAME:
        {
            builder.withKeyword("RENAME").withSpace().withKeyword("TO").withSpace().withOther(newName);
            break;
        }
        case Command::ADD_COLUMN:
        {
            builder.withKeyword("ADD").withSpace();
            if (columnKw)
                builder.withKeyword("COLUMN").withSpace();

            builder.withStatement(newColumn);
            break;
        }
        case Command::DROP_COLUMN:
        {
            builder.withKeyword("DROP").withSpace();
            if (columnKw)
                builder.withKeyword("COLUMN").withSpace();

            builder.withOther(dropColumnName);
            break;
        }
        case Command::null:
            break;
    }

    builder.withOperator(";");
    return builder.build();
}