File: sqlitepragma.cpp

package info (click to toggle)
sqlitestudio 3.4.21%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 54,880 kB
  • sloc: ansic: 406,208; cpp: 123,872; yacc: 2,692; tcl: 497; sh: 462; xml: 426; makefile: 19
file content (164 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download
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
#include "sqlitepragma.h"
#include "sqlitequerytype.h"

#include <parser/statementtokenbuilder.h>

SqlitePragma::SqlitePragma()
{
    queryType = SqliteQueryType::Pragma;
}

SqlitePragma::SqlitePragma(const SqlitePragma& other) :
    SqliteQuery(other), database(other.database), pragmaName(other.pragmaName), value(other.value), equalsOp(other.equalsOp), parenthesis(other.parenthesis)
{
}

SqlitePragma::SqlitePragma(const QString &name1, const QString &name2)
    : SqlitePragma()
{
    initName(name1, name2);
}

SqlitePragma::SqlitePragma(const QString &name1, const QString &name2, const QVariant& value, bool equals)
    : SqlitePragma()
{
    initName(name1, name2);
    if (!handleBoolValue(value))
        this->value = value;

    if (equals)
        equalsOp = true;
    else
        parenthesis = true;
}

SqlitePragma::SqlitePragma(const QString &name1, const QString &name2, const QString &value, bool equals)
    : SqlitePragma()
{
    initName(name1, name2);
    if (!handleBoolValue(value))
        this->value = value;

    if (equals)
        equalsOp = true;
    else
        parenthesis = true;
}

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

QString SqlitePragma::getBoolLiteralValue() const
{
    if (onOffKw)
        return value.toBool() ? "on" : "off";
    else if (yesNoKw)
        return value.toBool() ? "yes" : "no";
    else
        return value.toBool() ? "true" : "false";
}

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

TokenList SqlitePragma::getDatabaseTokensInStatement()
{
    if (database.isNull())
        return TokenList();

    return getTokenListFromNamedKey("nm");
}

QList<SqliteStatement::FullObject> SqlitePragma::getFullObjectsInStatement()
{
    QList<FullObject> result;
    if (database.isNull())
        return result;

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

    return result;
}

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

bool SqlitePragma::handleBoolValue(const QVariant &value)
{
    QString lower = value.toString().toLower();
    if (lower == "true")
        this->value = true;
    else if (lower == "false")
        this->value = false;
    else if (lower == "on")
    {
        this->value = true;
        this->onOffKw = true;
    }
    else if (lower == "off")
    {
        this->value = false;
        this->onOffKw = true;
    }
    else if (lower == "yes")
    {
        this->value = true;
        this->yesNoKw = true;
    }
    else if (lower == "no")
    {
        this->value = false;
        this->yesNoKw = true;
    }
    else
        return false;

    return true;
}

TokenList SqlitePragma::rebuildTokensFromContents()
{
    StatementTokenBuilder builder;
    builder.withTokens(SqliteQuery::rebuildTokensFromContents());
    builder.withKeyword("PRAGMA").withSpace();

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

    builder.withOther(pragmaName);

    if (equalsOp)
        builder.withSpace().withOperator("=").withSpace();
    else if (parenthesis)
        builder.withParLeft();

    if (value.userType() == QVariant::Bool)
        builder.withOther(getBoolLiteralValue(), false);
    else
        builder.withLiteralValue(value);

    if (parenthesis)
        builder.withParRight();

    builder.withOperator(";");

    return builder.build();
}