forked from troyane/SQLiteTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
168 lines (138 loc) · 4.67 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "sleeperthread.h"
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QThread>
#include <QtConcurrentRun>
#include <QFuture>
const QString tableName = QString ("test_table");
const QString createTableText = QString("CREATE TABLE IF NOT EXISTS test_table "
"( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, "
"test_string CHAR( 255 ) )");
const QString connectionName = QString("main_connection");
const QString insertStartInfo = QString("INSERT INTO %1 (test_string) VALUES ('START: %2')")
.arg(tableName)
.arg(QDateTime::currentDateTime().toString());
const QString selectQuery = QString("SELECT * from %1").arg(tableName);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!prepareAndOpenDb()) {
qDebug() << "Error while opening DB";
}
}
bool MainWindow::prepareAndOpenDb()
{
QStringList driverList( QSqlDatabase::drivers() );
if (!driverList.contains(QString("QSQLITE"), Qt::CaseInsensitive)) {
qDebug() << "No SQLITE support";
return false;
} else {
qDebug() << "SQLITE support is present";
}
mWorkDb = QSqlDatabase::addDatabase("QSQLITE", connectionName);
mWorkDb.setHostName("localhost");
mWorkDb.setDatabaseName("sqlitetest");
if (!mWorkDb.open()) {
qDebug() << "DB was not opened!";
return false;
}
// QString journalMode("OFF"); // default: "DELETE", "WAL"
// if (setDbJournalMode(mWorkDb, journalMode) )
// {
// qDebug() << "Sucessfuly switched to" << journalMode;
// }
QStringList tables = mWorkDb.tables(QSql::Tables);
if ( !tables.contains("test_table") ) {
QSqlQuery createTableQuery(mWorkDb);
if ( !createTableQuery.exec(createTableText) ) {
qDebug() << "Can't create table" << tableName << "in" << mWorkDb.databaseName()
<< ":" << createTableQuery.lastError().text();
return false;
}
} else {
qDebug() << mWorkDb.databaseName() << "already has table" << tableName;
}
QSqlQuery q(insertStartInfo, mWorkDb);
return true;
}
bool MainWindow::setDbJournalMode(QSqlDatabase &db, const QString &mode)
{
QSqlQuery q(db);
q.prepare( QString("PRAGMA journal_mode = %1").arg(mode) );
return q.exec();
}
void MainWindow::readFromDb()
{
const int threadId = QThread::currentThreadId();
qDebug() << "READER:" << threadId;
// wait for transaction in writer begins
while (transInProcess==false)
{
qDebug() << " # Reader is waiting";
SleeperThread::msSleep(7);
}
qDebug() << "# reader got transInProcess==true";
QString connectionName = QString ("Reader_%1").arg(threadId);
QSqlDatabase readDb = QSqlDatabase::cloneDatabase(mWorkDb, connectionName);
if (!readDb.open()) {
qDebug() << "DB was not opened to read!";
return;
}
QSqlQuery q(selectQuery, readDb);
q.exec();
outpuQuery(q);
}
void MainWindow::writeToDb()
{
const int threadId = QThread::currentThreadId();
qDebug() << "WRITER:" << threadId;
QString connectionName = QString ("Writer_%1").arg(threadId);
QSqlDatabase writeDb = QSqlDatabase::cloneDatabase(mWorkDb, connectionName);
if (!writeDb.open()) {
qDebug() << "DB was not opened to write!";
return;
}
const QString insertWriterInfo = QString("INSERT INTO %1 (test_string) VALUES ('WRITER at %2, %3')")
.arg(tableName)
.arg(QString::number(threadId))
.arg(QDateTime::currentDateTime().toString());
// insert some pause just to feel waiting process in Reader
SleeperThread::msSleep(21);
writeDb.transaction();
transInProcess = true;
SleeperThread::msSleep(21);
QSqlQuery q(insertWriterInfo, writeDb);
writeDb.commit();
transInProcess = false;
}
void MainWindow::startRace()
{
transInProcess = false;
QtConcurrent::run(this, &MainWindow::readFromDb);
QtConcurrent::run(this, &MainWindow::writeToDb);
// probably all queries will be applied
SleeperThread::msSleep(600);
qDebug() << "Summary:";
QSqlQuery q(selectQuery, mWorkDb);
q.exec();
outpuQuery(q);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::outpuQuery(QSqlQuery &query)
{
if (query.isActive()) {
while (query.next()) {
qDebug("%4i: %s", query.value(0).toInt(), qPrintable(query.value(1).toString())); // << query.value(0).toInt() << ":" << query.value(1).toString();
}
}
}