This repository has been archived by the owner on Oct 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagdb.cpp
222 lines (186 loc) · 6.37 KB
/
tagdb.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "tagdb.h"
#include <QUuid>
TAGDB::TAGDB(QObject *parent) : QObject(parent)
{
QDir collectionDBPath_dir(TAG::TaggingPath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString();
if(!UTIL::fileExists(TAG::TaggingPath + TAG::DBName))
{
this->openDB(this->name);
qDebug()<<"Collection doesn't exists, trying to create it" << TAG::TaggingPath + TAG::DBName;
this->prepareCollectionDB();
}else this->openDB(this->name);
}
TAGDB::~TAGDB()
{
this->m_db.close();
}
void TAGDB::openDB(const QString &name)
{
if(!QSqlDatabase::contains(name))
{
this->m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), name);
this->m_db.setDatabaseName(TAG::TaggingPath + TAG::DBName);
}
if (!this->m_db.isOpen())
{
if(!this->m_db.open())
qDebug()<<"ERROR OPENING DB"<<this->m_db.lastError().text()<<m_db.connectionName();
}
auto query = this->getQuery("PRAGMA synchronous=OFF");
query.exec();
}
void TAGDB::prepareCollectionDB() const
{
QSqlQuery query(this->m_db);
QFile file(":/script.sql");
if (!file.exists())
{
QString log = QStringLiteral("Fatal error on build database. The file '");
log.append(file.fileName() + QStringLiteral("' for database and tables creation query cannot be not found!"));
qDebug()<<log;
return;
}
if (!file.open(QIODevice::ReadOnly))
{
qDebug()<<QStringLiteral("Fatal error on try to create database! The file with sql queries for database creation cannot be opened!");
return;
}
bool hasText;
QString line;
QByteArray readLine;
QString cleanedLine;
QStringList strings;
while (!file.atEnd())
{
hasText = false;
line = "";
readLine = "";
cleanedLine = "";
strings.clear();
while (!hasText)
{
readLine = file.readLine();
cleanedLine = readLine.trimmed();
strings = cleanedLine.split("--");
cleanedLine = strings.at(0);
if (!cleanedLine.startsWith("--") && !cleanedLine.startsWith("DROP") && !cleanedLine.isEmpty())
line += cleanedLine;
if (cleanedLine.endsWith(";"))
break;
if (cleanedLine.startsWith("COMMIT"))
hasText = true;
}
if (!line.isEmpty())
{
if (!query.exec(line))
{
qDebug()<<"exec failed"<<query.lastQuery()<<query.lastError();
}
} else qDebug()<<"exec wrong"<<query.lastError();
}
file.close();
}
bool TAGDB::checkExistance(const QString &tableName, const QString &searchId, const QString &search)
{
auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(searchId, tableName, searchId, search);
auto query = this->getQuery(queryStr);
if (query.exec())
{
if (query.next()) return true;
}else qDebug()<<query.lastError().text();
return false;
}
bool TAGDB::checkExistance(const QString &queryStr)
{
auto query = this->getQuery(queryStr);
if (query.exec())
{
if (query.next()) return true;
}else qDebug()<<query.lastError().text();
return false;
}
QSqlQuery TAGDB::getQuery(const QString &queryTxt)
{
QSqlQuery query(queryTxt, this->m_db);
return query;
}
bool TAGDB::insert(const QString &tableName, const QVariantMap &insertData)
{
if (tableName.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The table name is empty!");
return false;
} else if (insertData.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The insertData is empty!");
return false;
}
QStringList strValues;
QStringList fields = insertData.keys();
QVariantList values = insertData.values();
int totalFields = fields.size();
for (int i = 0; i < totalFields; ++i)
strValues.append("?");
QString sqlQueryString = "INSERT INTO " + tableName + " (" + QString(fields.join(",")) + ") VALUES(" + QString(strValues.join(",")) + ")";
QSqlQuery query(this->m_db);
query.prepare(sqlQueryString);
int k = 0;
foreach (const QVariant &value, values)
query.bindValue(k++, value);
return query.exec();
}
bool TAGDB::update(const QString &tableName, const TAG::DB &updateData, const QVariantMap &where)
{
if (tableName.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The table name is empty!");
return false;
} else if (updateData.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The insertData is empty!");
return false;
}
QStringList set;
for (auto key : updateData.keys())
set.append(TAG::KEYMAP[key]+" = '"+updateData[key]+"'");
QStringList condition;
for (auto key : where.keys())
condition.append(key+" = '"+where[key].toString()+"'");
QString sqlQueryString = "UPDATE " + tableName + " SET " + QString(set.join(",")) + " WHERE " + QString(condition.join(",")) ;
auto query = this->getQuery(sqlQueryString);
qDebug()<<sqlQueryString;
return query.exec();
}
bool TAGDB::update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id)
{
auto queryStr = QString("UPDATE %1 SET %2 = \"%3\" WHERE %4 = \"%5\"").arg(table, column, newValue.toString().replace("\"","\"\""), op.toString(), id);
auto query = this->getQuery(queryStr);
return query.exec();
}
bool TAGDB::remove(const QString &tableName, const TAG::DB &removeData)
{
if (tableName.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on removing! The table name is empty!");
return false;
} else if (removeData.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The removeData is empty!");
return false;
}
QString strValues;
auto i = 0;
for (auto key : removeData.keys())
{
strValues.append(QString("%1 = \"%2\"").arg(TAG::KEYMAP[key], removeData[key]));
i++;
if(removeData.keys().size() > 1 && i<removeData.keys().size())
strValues.append(" AND ");
}
QString sqlQueryString = "DELETE FROM " + tableName + " WHERE " + strValues;
qDebug()<< sqlQueryString;
return this->getQuery(sqlQueryString).exec();
}