-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsqlitetest.cc
More file actions
124 lines (103 loc) · 3.65 KB
/
sqlitetest.cc
File metadata and controls
124 lines (103 loc) · 3.65 KB
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
#include "sqlitetest.hpp"
#include "sqliteutils.hpp"
#include <QDir>
#include <QMutex>
#include <QSqlError>
#include <QSqlQuery>
#include <QThread>
#include <mutex>
class SqliteTest::SqliteTestPrivate
{
public:
explicit SqliteTestPrivate(SqliteTest *q)
: q_ptr(q)
{
dataBaseConnection.dataBasePath = QString("%1/%2").arg(QDir::tempPath()).arg("test.db");
static std::once_flag onceFlag;
std::call_once(onceFlag, [this]() { QFile::remove(this->dataBaseConnection.dataBasePath); });
dataBaseConnection.connectionName = getDatabaseConnectionName();
createTable();
qInfo() << "DataBaseTestPrivate connectionName: " << dataBaseConnection.connectionName
<< QThread::currentThread();
}
~SqliteTestPrivate()
{
QMutexLocker locker(&mutex);
removeDatabase(dataBaseConnection);
}
bool createTable()
{
const auto createTable
= QString(
"CREATE TABLE IF NOT EXISTS [%1]("
" [id] INTEGER NOT NULL ON CONFLICT REPLACE UNIQUE ON CONFLICT "
"REPLACE COLLATE BINARY, "
" [brand] TEXT, "
" [num] TEXT, "
" [create_time] TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
" [local_time] TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime')), "
" PRIMARY KEY([id] COLLATE [BINARY] ASC) ON CONFLICT REPLACE)")
.arg(tableName);
QMutexLocker locker(&mutex);
auto db = getDatabase(dataBaseConnection);
CHECK_DATABASE_VALIDITY(db)
QSqlQuery query(db);
if (!query.exec(createTable)) {
qWarning() << query.lastError().text();
return false;
}
return true;
}
SqliteTest *q_ptr;
SqliteConnection dataBaseConnection;
const QString tableName = "phone";
static QMutex mutex;
};
QMutex SqliteTest::SqliteTestPrivate::mutex;
SqliteTest::SqliteTest(QObject *parent)
: QObject{parent}
, d_ptr(new SqliteTestPrivate(this))
{}
SqliteTest::~SqliteTest() {}
bool SqliteTest::insert(const QString &brand, int num)
{
auto db = getDatabase(d_ptr->dataBaseConnection);
CHECK_DATABASE_VALIDITY(db)
QSqlQuery query(db);
query.prepare(
QString("INSERT INTO %1 (brand, num) VALUES (:brand, :num)").arg(d_ptr->tableName));
query.bindValue(":brand", brand);
query.bindValue(":num", num);
QMutexLocker locker(&d_ptr->mutex);
if (!query.exec()) {
qCritical() << query.lastError().text();
return false;
}
qInfo() << "Last inserted id:" << query.lastInsertId().toInt();
return true;
}
bool SqliteTest::readLastRecord()
{
auto db = getDatabase(d_ptr->dataBaseConnection);
CHECK_DATABASE_VALIDITY(db)
QSqlQuery query(db);
if (!query.exec(QString("SELECT id, brand, num, create_time, local_time "
"FROM %1 ORDER BY id DESC LIMIT 1")
.arg(d_ptr->tableName))) {
qCritical() << query.lastError().text();
return false;
}
if (!query.next()) {
qWarning() << "No records found.";
return false;
}
auto text = QString(
"Last Record - ID: %1, Brand: %2\t, Num: %3\t, Created At: %4, Local Time: %5")
.arg(QString::number(query.value("id").toInt()),
query.value("brand").toString(),
QString::number(query.value("num").toInt()),
query.value("create_time").toString(),
query.value("local_time").toString());
qDebug().noquote() << text;
return true;
}