10#include <QRegularExpression>
11#include <QSqlDatabase>
16#include <QJsonDocument>
21#include <QStandardPaths>
22#include <QCoreApplication>
27bool SqlDBWriter::exec(QSqlQuery *sq,
const QString& sqlFile)
const {
30 if (f.open(QIODevice::ReadOnly)) {
32 QString temp, delimiter =
";";
33 QTextStream stream(&f);
34#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
35 stream.setCodec(
"UTF8");
38 while(!stream.atEnd()) {
39 QString line = stream.readLine();
41 int linedelimiterIndex = line.lastIndexOf(delimiter, -1, Qt::CaseInsensitive);
42 int delimiterIndex = temp.size() + linedelimiterIndex;
44 if (line.contains(
"--"))
49 if (linedelimiterIndex > -1) {
50 result = result && sq->exec(temp.left(delimiterIndex));
51 temp = temp.remove(0, delimiterIndex + 1);
54 qCritical() <<
"Exec database error. File: " << sqlFile <<
" Line:" << lineNumber << sq->lastError().text();
65 qCritical() <<
"sql source file is not open: " << sqlFile;
70bool SqlDBWriter::initDbPrivate(
const QVariantMap ¶ms) {
77 _config[
"DBFilePath"].toString()));
79 if (_config.contains(
"DBFilePath")) {
81 auto path = QFileInfo(_config[
"DBFilePath"].toString());
82 if (!QDir(
"").mkpath(path.absolutePath())) {
86 _db->setDatabaseName(path.absoluteFilePath());
89 if (_config.contains(
"DBLogin")) {
90 _db->setPassword(_config[
"DBLogin"].toString());
93 if (_config.contains(
"DBPass")) {
94 _db->setPassword(_config[
"DBPass"].toString());
97 if (_config.contains(
"DBHost")) {
98 _db->setHostName(_config[
"DBHost"].toString());
101 if (_config.contains(
"DBPort")) {
102 _db->setPort(_config[
"DBPort"].toInt());
106 qCritical() << _db->lastError().text();
110 for (
const QString& sqlFile : std::as_const(_SQLSources)) {
111 QSqlQuery query(*_db);
112 if (!exec(&query, sqlFile)) {
117 if (_config.contains(
"DBInitFile")) {
118 auto path = QFileInfo(_config[
"DBInitFile"].toString()).absoluteFilePath();
120 QSqlQuery query(*_db);
121 if (!exec(&query, path)) {
126 initSuccessful = _db->isValid();
127 return initSuccessful;
130bool SqlDBWriter::doQueryPrivate(
const QString &query,
const QVariantMap &bindValues, QSqlQuery* result)
const {
137 if (bindValues.size()) {
138 if (!q.prepare(query)) {
139 qCritical() <<
"request error : " << q.lastError().text();
143 for (
auto it = bindValues.begin(); it != bindValues.end(); ++it) {
144 q.bindValue(it.key(), it.value());
148 qCritical() <<
"execute error : " << q.lastError().text();
153 if (!q.exec(query)) {
154 qCritical() <<
"bind values error : " << q.lastError().text();
161 *result = std::move(q);
168bool SqlDBWriter::doSqlPrivate(
const QString &sqlFile)
const {
169 QSqlQuery query(*_db);
170 if (!exec(&query, sqlFile)) {
182 QSqlQuery query(*
db());
183 QString request = QString(
"PRAGMA foreign_keys = ON");
184 if (!query.exec(request)) {
185 qDebug() <<
"request error : " << query.lastError().text();
198 QSqlQuery query(*
db());
199 QString request = QString(
"PRAGMA foreign_keys = OFF");
200 if (!query.exec(request)) {
201 qDebug() <<
"request error : " << query.lastError().text();
210 QFile file(initFile);
213 if (file.open(QIODevice::ReadOnly)) {
214 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
217 if (!doc.isObject()) {
221 QJsonObject obj = doc.object();
222 return obj.toVariantMap();
233 params[
"DBDriver"] =
"QSQLITE";
239 const QString& name) {
241 return QSqlDatabase::addDatabase(driverName,
242 QFileInfo(name).fileName());
260 if (initDbParams.isEmpty()) {
270 auto handleInitDb = [¶ms,
this]() {
271 return initDbPrivate(params);
278 return db() &&
db()->isValid() &&
db()->isOpen() && initSuccessful;
282 QList<QSharedPointer<DBObject>> &result) {
284 auto getAll = [&templateObject, &result,
this]() {
311 const QWeakPointer<unsigned int>& autoincrementIdResult) {
314 auto resultId = QSharedPointer<int>::create();
315 Async::Job job = [
this, ptr, autoincrementIdResult]() {
345 return db()->databaseName();
352 QString connectionName = _db->connectionName();
357 QSqlDatabase::removeDatabase(connectionName);
363 const QWeakPointer<unsigned int>& autoincrementIdResult)
const {
373 auto prepare = [ptr](QSqlQuery&q) {
374 return ptr->prepareInsertQuery(q,
false);
377 auto cb = [&q, autoincrementIdResult]() {
379 if (
auto&& ptr = autoincrementIdResult.lock()) {
380 *ptr = q.lastInsertId().toInt();
386 return workWithQuery(q, prepare, cb);
399 auto prepare = [ptr](QSqlQuery&q) {
400 return ptr->prepareInsertQuery(q,
true);
403 auto cb = [](){
return true;};
405 return workWithQuery(q, prepare, cb);
409 bool wait, QSqlQuery* result)
const {
411 wait = result || wait;
413 Async::Job job = [
this, query, bindValues, result]() {
414 return doQueryPrivate(query, bindValues, result);
423 return doSqlPrivate(sqlFile);
431 QList<QSharedPointer<QH::PKG::DBObject>> &result) {
438 auto prepare = [&requestObject](QSqlQuery&q) {
442 auto cb = [&q, &requestObject, &result]() ->
bool {
445 auto newObject = QSharedPointer<QH::PKG::DBObject>(requestObject.
createDBObject());
451 if (!newObject->fromSqlRecord(q.record())) {
452 qCritical() <<
"Select query finished successful but, "
453 "the fromSqlRecord method return false." << newObject->toString();
458 result.push_back(newObject);
462 auto newObject = QSharedPointer<QH::PKG::DBObject>(requestObject.
createDBObject());
467 if (!newObject->fromSqlRecord(q.record())) {
468 qCritical() <<
"Init sql object error.";
471 result.push_back(newObject);
477 return result.size();
480 return workWithQuery(q, prepare, cb);
493 auto cb = []() ->
bool {
498 return workWithQuery(q, prepare, cb);
507 auto prepare = [ptr](QSqlQuery&q) {
508 return ptr->prepareUpdateQuery(q);
511 auto cb = [](){
return true;};
513 return workWithQuery(q, prepare, cb);
516bool SqlDBWriter::workWithQuery(QSqlQuery &q,
517 const std::function<
PrepareResult (QSqlQuery &)> &prepareFunc,
518 const std::function<
bool ()> &cb)
const {
520 auto printError = [](
const QSqlQuery &q) {
522 qDebug() <<
"prepare sql error: " << q.lastError().text();
524 qCritical() <<
"exec sql error: " << q.lastError();
529 switch (prepareFunc(q)) {
530 case PrepareResult::Success: {
537#ifdef HEART_PRINT_SQL_QUERIES
538 qDebug() << QString(
"Query executed successfull into %0\n"
540 arg(_db->databaseName(), q.executedQuery();
545 case PrepareResult::Disabled: {
547 qWarning() <<
"call disabled operator!";
551 case PrepareResult::Fail: {
The Async class This is bundle of async templates and async wrappers.
std::function< bool()> Job
bool asyncLauncher(const Job &job, bool await=false, bool freaze=true) const
asyncLauncher This method invoke a job on the thread (using the asyncHandler method) of this object.
The DBObject class- main class for work with data base.
virtual PrepareResult prepareSelectQuery(QSqlQuery &q) const
prepareSelectQuery This method should be prepare a query for selected data. Override this method for ...
virtual bool isBundle() const
isBundle This method definef determines whether the object will be abstract (composite objects) or si...
virtual DBObject * createDBObject() const =0
createDBObject This method should be create a object with the some type as the object called this met...
bool disableFK()
disableFK This method disable foreign key for the sqlite database.
bool deleteObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
deleteObject This method execute a delete method of obj and remove current object from database.
virtual bool updateQuery(const QSharedPointer< QH::PKG::DBObject > &ptr) const
updateQuery This method execute update query of object. For more Information see DBObject::prepareUpd...
QString databaseLocation() const
databaseLocation This method return location of database. If it is sqlite then return path to db file...
bool updateObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
updateObject This method execute a update method of the saveObject and save all changes into database...
bool doSql(const QString &sqlFile, bool wait) const override
doSql This method execute a query in this database.
QSqlDatabase * db()
db This method return db connection. If SqlDBWriter in not inited then return nullptr....
bool enableFK()
enableFK This method enable foreign key for the sqlite database.
virtual bool replaceQuery(const QSharedPointer< QH::PKG::DBObject > &insertObject) const
replaceQuery This method prepare the replce object query.
virtual QSqlDatabase initSqlDataBasse(const QString &driverName, const QString &name)
initSqlDataBasse This method create ad database connection. Without configuration....
SqlDBWriter(QThread *thread, QObject *ptr=nullptr)
bool doQuery(const QString &query, const QVariantMap &bindValues={}, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
bool replaceObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
replaceObject This method execute a replace method of the saveObject and insert or save if not exists...
virtual bool selectQuery(const QH::PKG::DBObject &requestObject, QList< QSharedPointer< QH::PKG::DBObject > > &result)
selectQuery generate select query to database from parameters.
void setSQLSources(const QStringList &list) override
setSQLSources This method set sql sources for deployed database.
virtual bool isValid() const
isValid This method return true if database is successful inited and working correctly.
virtual bool deleteQuery(const QSharedPointer< QH::PKG::DBObject > &deleteObject) const
deleteQuery This method prepare the delete object query.
virtual bool initDb(const QString &initDbParams=DEFAULT_DB_PATH)
initDb This method is wraper of the initDb(const QVariantMap ¶ms) method. This implementation rea...
virtual QVariantMap getInitParams(const QString &initFile) const
getInitPararm This method read parameters of database.
virtual ~SqlDBWriter() override
bool getAllObjects(const PKG::DBObject &templateObject, QList< QSharedPointer< PKG::DBObject > > &result) override
getAllObjects This method execute a select method of the templateObject and return list of all select...
virtual bool insertQuery(const QSharedPointer< QH::PKG::DBObject > &insertObject, const QWeakPointer< unsigned int > &autoIncrementID={}) const
insertQuery This method prepare the insert object query.
virtual QVariantMap defaultInitPararm() const
defaultInitPararm This method return default parameters of the database.
bool insertObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false, const QWeakPointer< unsigned int > &autoincrementIdResult={}) override
insertObject This method execute a insert method of the saveObject and insert it into database.
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...