UI: Move 3rd party integrations to 3rdparty/.

This commit is contained in:
Nodir Temirkhodjaev 2019-03-30 13:12:54 +05:00
parent 12c9e3d3a5
commit b1ab98e615
13 changed files with 220 additions and 104 deletions

4
src/ui/3rdparty/3rdparty.pri vendored Normal file
View File

@ -0,0 +1,4 @@
include (qcustomplot/qcustomplot.pri)
include (sqlite/sqlite.pri)
INCLUDEPATH += $$PWD

165
src/ui/3rdparty/sqlite/sqlitedb.cpp vendored Normal file
View File

@ -0,0 +1,165 @@
#include "sqlitedb.h"
#include <QDir>
#include <sqlite3.h>
SqliteDb::SqliteDb() :
m_db(nullptr)
{
}
SqliteDb::~SqliteDb()
{
close();
}
bool SqliteDb::open(const QString &filePath)
{
return sqlite3_open16(filePath.utf16(), &m_db) == SQLITE_OK;
}
void SqliteDb::close()
{
if (m_db != nullptr) {
sqlite3_close(m_db);
m_db = nullptr;
}
}
bool SqliteDb::execute(const char *sql)
{
return sqlite3_exec(m_db, sql, nullptr, nullptr, nullptr) == SQLITE_OK;
}
bool SqliteDb::execute16(const ushort *sql)
{
sqlite3_stmt *stmt = nullptr;
int res = sqlite3_prepare16_v2(db(), sql, -1, &stmt, nullptr);
if (stmt != nullptr) {
res = sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
return res;
}
bool SqliteDb::executeStr(const QString &sql)
{
return execute16(sql.utf16());
}
QVariant SqliteDb::executeOut(const char *sql)
{
QVariant res;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db(), sql, -1, &stmt, nullptr);
if (stmt != nullptr) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
switch (sqlite3_column_type(stmt, 0)) {
case SQLITE_INTEGER:
res = sqlite3_column_int64(stmt, 0);
break;
case SQLITE_FLOAT:
res = sqlite3_column_double(stmt, 0);
break;
case SQLITE_TEXT:
res = QString::fromUtf8(reinterpret_cast<const char *>(
sqlite3_column_text(stmt, 0)));
break;
default:
Q_UNREACHABLE();
}
}
sqlite3_finalize(stmt);
}
return res;
}
qint64 SqliteDb::lastInsertRowid() const
{
return sqlite3_last_insert_rowid(m_db);
}
int SqliteDb::changes() const
{
return sqlite3_changes(m_db);
}
bool SqliteDb::beginTransaction()
{
return execute("BEGIN;");
}
bool SqliteDb::commitTransaction()
{
return execute("COMMIT;");
}
bool SqliteDb::rollbackTransaction()
{
return execute("ROLLBACK;");
}
bool SqliteDb::beginSavepoint(const char *name)
{
return (name == nullptr)
? execute("SAVEPOINT _;")
: executeStr(QString("SAVEPOINT %1;").arg(name));
}
bool SqliteDb::releaseSavepoint(const char *name)
{
return (name == nullptr)
? execute("RELEASE _;")
: executeStr(QString("RELEASE %1;").arg(name));
}
bool SqliteDb::rollbackSavepoint(const char *name)
{
return (name == nullptr)
? execute("ROLLBACK TO _;")
: executeStr(QString("ROLLBACK TO %1;").arg(name));
}
QString SqliteDb::errorMessage() const
{
const char *text = sqlite3_errmsg(m_db);
return QString::fromUtf8(text);
}
bool SqliteDb::migrate(const QString &sqlDir, int version)
{
// Check version
const int userVersion = executeOut("PRAGMA user_version;").toInt();
if (userVersion == version)
return true;
// Run migration SQL scripts
QDir dir(sqlDir);
bool res = true;
beginTransaction();
for (int i = userVersion + 1; i <= version; ++i) {
const QString filePath = dir.filePath(QString::number(i) + ".sql");
QFile file(filePath);
if (!file.open(QFile::ReadOnly | QFile::Text))
continue;
const QByteArray data = file.readAll();
if (data.isEmpty())
continue;
beginSavepoint();
if (!execute(data.constData())) {
res = false;
rollbackSavepoint();
break;
}
releaseSavepoint();
}
commitTransaction();
return res;
}

View File

@ -2,6 +2,7 @@
#define SQLITEDB_H #define SQLITEDB_H
#include <QString> #include <QString>
#include <QVariant>
QT_FORWARD_DECLARE_STRUCT(sqlite3) QT_FORWARD_DECLARE_STRUCT(sqlite3)
@ -17,6 +18,10 @@ public:
struct sqlite3 *db() const { return m_db; } struct sqlite3 *db() const { return m_db; }
bool execute(const char *sql); bool execute(const char *sql);
bool execute16(const ushort *sql);
bool executeStr(const QString &sql);
QVariant executeOut(const char *sql);
qint64 lastInsertRowid() const; qint64 lastInsertRowid() const;
int changes() const; int changes() const;
@ -25,8 +30,14 @@ public:
bool commitTransaction(); bool commitTransaction();
bool rollbackTransaction(); bool rollbackTransaction();
bool beginSavepoint(const char *name = nullptr);
bool releaseSavepoint(const char *name = nullptr);
bool rollbackSavepoint(const char *name = nullptr);
QString errorMessage() const; QString errorMessage() const;
bool migrate(const QString &sqlDir, int version);
private: private:
sqlite3 *m_db; sqlite3 *m_db;
}; };

View File

@ -1,6 +1,6 @@
#include "sqliteengine.h" #include "sqliteengine.h"
#include "sqlite3.h" #include <sqlite3.h>
void SqliteEngine::initialize() void SqliteEngine::initialize()
{ {

View File

@ -1,6 +1,6 @@
#include "sqlitestmt.h" #include "sqlitestmt.h"
#include "sqlite3.h" #include <sqlite3.h>
SqliteStmt::SqliteStmt() : SqliteStmt::SqliteStmt() :
m_stmt(nullptr) m_stmt(nullptr)

View File

@ -181,6 +181,5 @@ installer_build {
PRE_TARGETDEPS += $$fortdrv32.target $$fortdrv64.target PRE_TARGETDEPS += $$fortdrv32.target $$fortdrv64.target
} }
include(db/sqlite/sqlite.pri) # 3rd party integrations
include(3rdparty/3rdparty.pri)
include(graph/qcustomplot/qcustomplot.pri)

View File

@ -1,63 +0,0 @@
#include "sqlitedb.h"
#include "sqlite3.h"
SqliteDb::SqliteDb() :
m_db(nullptr)
{
}
SqliteDb::~SqliteDb()
{
close();
}
bool SqliteDb::open(const QString &filePath)
{
return sqlite3_open16(filePath.utf16(), &m_db) == SQLITE_OK;
}
void SqliteDb::close()
{
if (m_db != nullptr) {
sqlite3_close(m_db);
m_db = nullptr;
}
}
bool SqliteDb::execute(const char *sql)
{
return sqlite3_exec(m_db, sql, nullptr, nullptr, nullptr) == SQLITE_OK;
}
qint64 SqliteDb::lastInsertRowid() const
{
return sqlite3_last_insert_rowid(m_db);
}
int SqliteDb::changes() const
{
return sqlite3_changes(m_db);
}
bool SqliteDb::beginTransaction()
{
return execute("BEGIN;");
}
bool SqliteDb::commitTransaction()
{
return execute("COMMIT;");
}
bool SqliteDb::rollbackTransaction()
{
return execute("ROLLBACK;");
}
QString SqliteDb::errorMessage() const
{
const char *text = sqlite3_errmsg(m_db);
return QString::fromUtf8(text);
}

View File

@ -1,7 +1,7 @@
#ifndef AXISTICKERSPEED_H #ifndef AXISTICKERSPEED_H
#define AXISTICKERSPEED_H #define AXISTICKERSPEED_H
#include "qcustomplot.h" #include <qcustomplot.h>
class AxisTickerSpeed : public QCPAxisTicker class AxisTickerSpeed : public QCPAxisTicker
{ {

View File

@ -1,7 +1,7 @@
#ifndef GRAPHPLOT_H #ifndef GRAPHPLOT_H
#define GRAPHPLOT_H #define GRAPHPLOT_H
#include "qcustomplot.h" #include <qcustomplot.h>
class GraphPlot : public QCustomPlot class GraphPlot : public QCustomPlot
{ {