|
| 1 | +#include "databasetest.hpp" |
| 2 | +#include "databaseutils.hpp" |
| 3 | + |
| 4 | +#include <QDir> |
| 5 | +#include <QMutex> |
| 6 | +#include <QSqlDriver> |
| 7 | +#include <QSqlError> |
| 8 | +#include <QSqlQuery> |
| 9 | +#include <QThread> |
| 10 | + |
| 11 | +class DataBaseTest::DataBaseTestPrivate |
| 12 | +{ |
| 13 | +public: |
| 14 | + explicit DataBaseTestPrivate(DataBaseTest *q) |
| 15 | + : q_ptr(q) |
| 16 | + { |
| 17 | + dataBaseConnection.dataBasePath = QString("%1/%2").arg(QDir::tempPath()).arg("test.db"); |
| 18 | + dataBaseConnection.connectionName = getDatabaseConnectionName(); |
| 19 | + |
| 20 | + createTable(); |
| 21 | + |
| 22 | + qInfo() << "DataBaseTestPrivate connectionName: " << dataBaseConnection.connectionName |
| 23 | + << QThread::currentThread(); |
| 24 | + } |
| 25 | + |
| 26 | + ~DataBaseTestPrivate() { removeDatabase(dataBaseConnection.connectionName); } |
| 27 | + |
| 28 | + bool createTable() |
| 29 | + { |
| 30 | + const auto createTable |
| 31 | + = QString("CREATE TABLE [%1](" |
| 32 | + " [id] INTEGER NOT NULL ON CONFLICT REPLACE UNIQUE ON CONFLICT " |
| 33 | + "REPLACE COLLATE BINARY, " |
| 34 | + " [brand] TEXT, " |
| 35 | + " [num] TEXT, " |
| 36 | + " [create_time] TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " |
| 37 | + " PRIMARY KEY([id] COLLATE [BINARY] ASC) ON CONFLICT REPLACE)") |
| 38 | + .arg(tableName); |
| 39 | + |
| 40 | + QMutexLocker locker(&mutex); |
| 41 | + auto db = getDatabase(dataBaseConnection); |
| 42 | + CHECK_DATABASE_VALIDITY(db) |
| 43 | + |
| 44 | + auto *driver = db.driver(); |
| 45 | + qInfo() << "Support Transactions: " << driver->hasFeature(QSqlDriver::Transactions) |
| 46 | + << "Support LastInsertId: " << driver->hasFeature(QSqlDriver::LastInsertId) |
| 47 | + << "Support BatchOperations: " << driver->hasFeature(QSqlDriver::BatchOperations) |
| 48 | + << "Support SimpleLocking: " << driver->hasFeature(QSqlDriver::SimpleLocking); |
| 49 | + |
| 50 | + if (db.tables().contains(tableName)) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + QSqlQuery query(db); |
| 55 | + if (!query.exec(createTable)) { |
| 56 | + qWarning() << query.lastError().text(); |
| 57 | + return false; |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + DataBaseTest *q_ptr; |
| 63 | + |
| 64 | + DataBaseConnection dataBaseConnection; |
| 65 | + const QString tableName = "phone"; |
| 66 | + |
| 67 | + static QMutex mutex; |
| 68 | +}; |
| 69 | + |
| 70 | +QMutex DataBaseTest::DataBaseTestPrivate::mutex; |
| 71 | + |
| 72 | +DataBaseTest::DataBaseTest(QObject *parent) |
| 73 | + : QObject{parent} |
| 74 | + , d_ptr(new DataBaseTestPrivate(this)) |
| 75 | +{} |
| 76 | + |
| 77 | +DataBaseTest::~DataBaseTest() {} |
| 78 | + |
| 79 | +bool DataBaseTest::insert(const QString &brand, int num) |
| 80 | +{ |
| 81 | + QMutexLocker locker(&d_ptr->mutex); |
| 82 | + auto db = getDatabase(d_ptr->dataBaseConnection); |
| 83 | + CHECK_DATABASE_VALIDITY(db) |
| 84 | + QSqlQuery query(db); |
| 85 | + query.prepare( |
| 86 | + QString("INSERT INTO %1 (brand, num) VALUES (:brand, :num)").arg(d_ptr->tableName)); |
| 87 | + query.bindValue(":brand", brand); |
| 88 | + query.bindValue(":num", num); |
| 89 | + if (!query.exec()) { |
| 90 | + qCritical() << query.lastError().text(); |
| 91 | + return false; |
| 92 | + } |
| 93 | + qInfo() << "Last inserted id:" << query.lastInsertId().toInt(); |
| 94 | + return true; |
| 95 | +} |
0 commit comments