-
Notifications
You must be signed in to change notification settings - Fork 1
/
qabstractsqlite.cpp
148 lines (129 loc) · 3.54 KB
/
qabstractsqlite.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
#include "qabstractsqlite.h"
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QStringBuilder>
#include <QVariant>
#define HEX_NUMBERS "0123456789abcdef"
#define HEX_SIZE 16
class QAbstractSQLitePrivate {
Q_DECLARE_PUBLIC(QAbstractSQLite)
public:
QAbstractSQLitePrivate(QAbstractSQLite * parent) :
q_ptr(parent),
connection("__connection__" % parent->id() % "__"),
db(QSqlDatabase::addDatabase("QSQLITE", connection)),
queryCounter(0)
{
}
QAbstractSQLite * const q_ptr;
QString table;
QString filename;
QString connection;
QSqlDatabase db;
int queryCounter;
};
QAbstractSQLite::QAbstractSQLite() :
QObject(0),
d_ptr(new QAbstractSQLitePrivate(this))
{
}
QAbstractSQLite::QAbstractSQLite(const QString& table) :
QObject(0),
d_ptr(new QAbstractSQLitePrivate(this))
{
Q_D(QAbstractSQLite);
d->table = table;
}
QAbstractSQLite::QAbstractSQLite(const QString& table, const QString& filename) :
QObject(0),
d_ptr(new QAbstractSQLitePrivate(this))
{
Q_D(QAbstractSQLite);
d->table = table;
d->filename = filename;
d->db.setDatabaseName(filename);
}
QAbstractSQLite::~QAbstractSQLite() {
Q_D(QAbstractSQLite);
QString connection = d->connection;
delete d_ptr;
QSqlDatabase::removeDatabase(connection);
}
void QAbstractSQLite::setFilename(const QString& filename) {
Q_D(QAbstractSQLite);
if (d->filename != filename) {
d->filename = filename;
if (d->db.isOpen()) d->db.close();
d->db.setDatabaseName(filename);
}
}
void QAbstractSQLite::setTable(const QString& table) {
Q_D(QAbstractSQLite);
d->table = table;
}
QString QAbstractSQLite::filename() const {
Q_D(const QAbstractSQLite);
return d->filename;
}
QString QAbstractSQLite::table() const {
Q_D(const QAbstractSQLite);
return d->table;
}
bool QAbstractSQLite::isValid() const {
Q_D(const QAbstractSQLite);
return !(d->table.isEmpty() || d->filename.isEmpty());
}
bool QAbstractSQLite::tableExists() const {
Q_D(const QAbstractSQLite);
QSqlQuery *query;
if ((query = open())) {
QString t;
bool r = query->exec("SELECT name FROM sqlite_master WHERE type='table' AND name='" % d->table % "';");
if (!r) emit error(query->lastError().text());
while (query->next()) t = query->value(0).toString();
close(query);
return !t.isEmpty();
}
return false;
}
QSqlQuery* QAbstractSQLite::open() const {
QSqlQuery * query = 0;
if (d_ptr->db.isOpen() || d_ptr->db.open()) {
query = new QSqlQuery(d_ptr->db);
d_ptr->queryCounter++;
}
return query;
}
QSqlQuery* QAbstractSQLite::close(QSqlQuery* query) const {
if (d_ptr->db.isOpen()) {
if (query) {
query->finish();
delete query;
query = 0;
d_ptr->queryCounter--;
}
if (!d_ptr->queryCounter)
d_ptr->db.close();
}
return query;
}
QString QAbstractSQLite::id() const {
char * c = (char*)malloc(33*sizeof(char));
char * pc = c;
for (short i=0; i<32; ++i,++pc) {
if (i == 8 || i == 13 || i == 18 || i == 23) *pc = '-';
else *pc = HEX_NUMBERS[qrand() % HEX_SIZE];
}
*pc = '\0';
QString result(c);
free(c);
return result;
}
QString QAbstractSQLite::timestamp() const {
return QDateTime::currentDateTimeUtc().toString(timestampFormat());
}
QString QAbstractSQLite::timestampFormat() const {
return "yyyy-MM-dd HH:mm:ss t";
}