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
|
#include "sqliteupsert.h"
#include "common/global.h"
#include "parser/ast/sqliteorderby.h"
#include "parser/ast/sqliteexpr.h"
#include "parser/statementtokenbuilder.h"
#include <QDebug>
SqliteUpsert::SqliteUpsert()
{
doNothing = true;
}
SqliteUpsert::SqliteUpsert(const QList<SqliteOrderBy*>& conflictColumns, SqliteExpr* conflictWhere)
{
this->conflictColumns = conflictColumns;
this->conflictWhere = conflictWhere;
if (this->conflictWhere)
this->conflictWhere->setParent(this);
for (SqliteOrderBy* idxCol : conflictColumns)
idxCol->setParent(this);
doNothing = true;
}
SqliteUpsert::SqliteUpsert(const QList<SqliteOrderBy*>& conflictColumns, SqliteExpr* conflictWhere, const QList<ColumnAndValue>& values, SqliteExpr* setWhere)
{
this->conflictColumns = conflictColumns;
this->conflictWhere = conflictWhere;
this->keyValueMap = values;
this->setWhere = setWhere;
if (this->conflictWhere)
this->conflictWhere->setParent(this);
if (this->setWhere)
this->setWhere->setParent(this);
for (SqliteOrderBy* idxCol : conflictColumns)
idxCol->setParent(this);
doNothing = false;
}
SqliteUpsert::SqliteUpsert(const SqliteUpsert& other)
: SqliteStatement(other), doNothing(other.doNothing)
{
DEEP_COPY_COLLECTION(SqliteOrderBy, conflictColumns);
// Special case of deep collection copy
SqliteExpr* newExpr = nullptr;
for (const ColumnAndValue& keyValue : other.keyValueMap)
{
newExpr = new SqliteExpr(*keyValue.second);
newExpr->setParent(this);
keyValueMap << ColumnAndValue(keyValue.first, newExpr);
}
DEEP_COPY_FIELD(SqliteExpr, conflictWhere);
DEEP_COPY_FIELD(SqliteExpr, setWhere);
}
SqliteStatement* SqliteUpsert::clone()
{
return new SqliteUpsert(*this);
}
TokenList SqliteUpsert::rebuildTokensFromContents()
{
StatementTokenBuilder builder;
builder.withKeyword("ON").withSpace().withKeyword("CONFLICT");
if (!conflictColumns.isEmpty())
{
builder.withSpace().withParLeft().withStatementList(conflictColumns).withParRight();
if (conflictWhere)
builder.withSpace().withKeyword("WHERE").withStatement(conflictWhere);
}
builder.withSpace().withKeyword("DO").withSpace();
if (doNothing)
{
builder.withKeyword("NOTHING");
}
else
{
builder.withKeyword("UPDATE").withSpace().withKeyword("SET").withSpace();
bool first = true;
for (const ColumnAndValue& keyVal : keyValueMap)
{
if (!first)
builder.withOperator(",").withSpace();
if (keyVal.first.type() == QVariant::StringList)
builder.withParLeft().withOtherList(keyVal.first.toStringList()).withParRight();
else
builder.withOther(keyVal.first.toString());
builder.withSpace().withOperator("=").withStatement(keyVal.second);
first = false;
}
if (setWhere)
builder.withSpace().withKeyword("WHERE").withStatement(setWhere);
}
return builder.build();
}
QStringList SqliteUpsert::getColumnsInStatement()
{
QStringList columns;
for (const ColumnAndValue& keyValue : keyValueMap)
{
if (keyValue.first.type() == QVariant::StringList)
columns += keyValue.first.toStringList();
else
columns += keyValue.first.toString();
}
return columns;
}
TokenList SqliteUpsert::getColumnTokensInStatement()
{
// Alrorithm same as in UPDATE. See comments there for details.
TokenList list;
TokenList setListTokens = getTokenListFromNamedKey("setlist", -1);
int setListTokensSize = setListTokens.size();
int end;
int start = 0;
SqliteExpr* expr = nullptr;
for (const ColumnAndValue& keyValue : keyValueMap)
{
expr = keyValue.second;
end = setListTokens.indexOf(expr->tokens[0]);
if (end < 0 || end >= setListTokensSize)
{
qCritical() << "Went out of bounds while looking for column tokens in SqliteUpdate::getColumnTokensInStatement().";
continue;
}
// Before expression tokens there will be only column(s) token(s)
// and commans, and equal operator. Let's take only ID tokens, which are columns.
list += setListTokens.mid(start, end - start - 1).filter(Token::OTHER);
start = end + expr->tokens.size();
}
return list;
}
|