Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
QH::SqlDBWriter Class Reference

The SqlDBWriter class. This class write and read objects from database (hard level). Before working with database you need to initialize it. The SqlDBWriter support a any sql databases, For list of supported drivers see the Qt Docummentation https://doc.qt.io/qt-5/sql-driver.html For initialize a custom database driver you need to set an own params see the SqlDBWriter::initDb metthod. This class work only on own thread. For change working thread use the Qt method moveToThread. More...

#include <sqldbwriter.h>

Inheritance diagram for QH::SqlDBWriter:
Inheritance graph
Collaboration diagram for QH::SqlDBWriter:
Collaboration graph

Public Member Functions

 SqlDBWriter (QThread *thread, QObject *ptr=nullptr)
 
virtual bool initDb (const QString &initDbParams=DEFAULT_DB_PATH)
 initDb This method is wraper of the initDb(const QVariantMap &params) method. This implementation read a initDbParams file and get all params from json.
 
virtual bool initDb (const QVariantMap &params)
 initDb This method initialize a database params and create a new database if the old database not exists.
 
virtual bool isValid () const
 isValid This method return true if database is successful inited and working correctly.
 
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 selected from databaes or cache objects
 
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 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.
 
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.
 
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, all changes into database.
 
void setSQLSources (const QStringList &list) override
 setSQLSources This method set sql sources for deployed database.
 
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 doSql (const QString &sqlFile, bool wait) const override
 doSql This method execute a query in this database.
 
QString databaseLocation () const
 databaseLocation This method return location of database. If it is sqlite then return path to db file else return database name.
 
virtual ~SqlDBWriter () override
 
virtual bool updateQuery (const QSharedPointer< QH::PKG::DBObject > &ptr) const
 updateQuery This method execute update query of object. For more Information see DBObject::prepareUpdateQuery.
 
virtual bool selectQuery (const QH::PKG::DBObject &requestObject, QList< QSharedPointer< QH::PKG::DBObject > > &result)
 selectQuery generate select query to database from parameters.
 
virtual bool deleteQuery (const QSharedPointer< QH::PKG::DBObject > &deleteObject) const
 deleteQuery This method prepare the delete object query.
 
virtual bool insertQuery (const QSharedPointer< QH::PKG::DBObject > &insertObject, const QWeakPointer< unsigned int > &autoIncrementID={}) const
 insertQuery This method prepare the insert object query.
 
virtual bool replaceQuery (const QSharedPointer< QH::PKG::DBObject > &insertObject) const
 replaceQuery This method prepare the replce object query.
 
- Public Member Functions inherited from QH::Async
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.
 
- Public Member Functions inherited from QH::iObjectProvider
template<class TYPE >
QSharedPointer< TYPE > getObject (const TYPE &templateVal)
 getObject this method return a strong pointer to DBObject created by select method of the template object (templateVal).
 
QSharedPointer< PKG::DBObjectgetObjectRaw (const PKG::DBObject &templateVal)
 getObjectRaw This method return object without test object type.
 

Protected Member Functions

bool enableFK ()
 enableFK This method enable foreign key for the sqlite database.
 
bool disableFK ()
 disableFK This method disable foreign key for the sqlite database.
 
virtual QVariantMap getInitParams (const QString &initFile) const
 getInitPararm This method read parameters of database.
 
virtual QVariantMap defaultInitPararm () const
 defaultInitPararm This method return default parameters of the database.
 
virtual QSqlDatabase initSqlDataBasse (const QString &driverName, const QString &name)
 initSqlDataBasse This method create ad database connection. Without configuration. This method invoked in the initDbPrivate method.
 
QSqlDatabase * db ()
 db This method return db connection. If SqlDBWriter in not inited then return nullptr. For more information about initialization the SqlDBWriter see the SqlDBWriter::initDb method.
 
const QSqlDatabase * db () const
 db This is constant implementation of the SqlDBWriter::db method.
 
- Protected Member Functions inherited from QH::Async
 Async (QThread *thread, QObject *ptr=nullptr)
 Async This is default constructor of the async object.
 
 ~Async ()
 
bool waitFor (bool *condition, int timeout=WAIT_TIME, bool freaze=true) const
 waitFor This is base wait function.
 
bool waitFor (const Job &condition, int timeout=WAIT_TIME, bool freaze=true) const
 waitFor This is base wait function.
 

Additional Inherited Members

- Public Types inherited from QH::Async
using Job = std::function< bool()>
 

Detailed Description

The SqlDBWriter class. This class write and read objects from database (hard level). Before working with database you need to initialize it. The SqlDBWriter support a any sql databases, For list of supported drivers see the Qt Docummentation https://doc.qt.io/qt-5/sql-driver.html For initialize a custom database driver you need to set an own params see the SqlDBWriter::initDb metthod. This class work only on own thread. For change working thread use the Qt method moveToThread.

Definition at line 35 of file sqldbwriter.h.

Constructor & Destructor Documentation

◆ SqlDBWriter()

QH::SqlDBWriter::SqlDBWriter ( QThread *  thread,
QObject *  ptr = nullptr 
)

Definition at line 258 of file sqldbwriter.cpp.

◆ ~SqlDBWriter()

QH::SqlDBWriter::~SqlDBWriter ( )
overridevirtual

Definition at line 353 of file sqldbwriter.cpp.

Member Function Documentation

◆ databaseLocation()

QString QH::SqlDBWriter::databaseLocation ( ) const

databaseLocation This method return location of database. If it is sqlite then return path to db file else return database name.

Returns
path or name of database.

Definition at line 346 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ db() [1/2]

QSqlDatabase * QH::SqlDBWriter::db ( )
protected

db This method return db connection. If SqlDBWriter in not inited then return nullptr. For more information about initialization the SqlDBWriter see the SqlDBWriter::initDb method.

Returns
pointer to actual database connection.

Definition at line 250 of file sqldbwriter.cpp.

Here is the caller graph for this function:

◆ db() [2/2]

const QSqlDatabase * QH::SqlDBWriter::db ( ) const
protected

db This is constant implementation of the SqlDBWriter::db method.

Returns
return const pointer to database connection.

Definition at line 254 of file sqldbwriter.cpp.

◆ defaultInitPararm()

QVariantMap QH::SqlDBWriter::defaultInitPararm ( ) const
protectedvirtual

defaultInitPararm This method return default parameters of the database.

Returns
map of database params.

Support parameters of database:

  • DBDriver - This is sql driver of data base for more information see The Qt Documentations https://doc.qt.io/qt-5/sql-driver.html (or QH_DB_DRIVER)
  • DBFilePath - This is path to file of data base (sqlite only). This is phusical location of sqlite database. Or (QH_DB_FILE_PATH)
  • DBInitFile - This is sql file with sql code (structure) with default structure of the database. Or (QH_DB_INIT_FILE)
  • DBPass - This is password of a remote database. Or (QH_DB_PASS)
  • DBLogin - This is login of a remote database. Or (QH_DB_LOGIN)
  • DBHost - This is host address of a remote database. Or (QH_DB_HOST)
  • DBPort - port of a remote database. or (QH_DB_PORT)
  • DBBackUpPath - path of database backups (sqlite only). Or (QH_DB_BACKUP_PATH)

Definition at line 236 of file sqldbwriter.cpp.

Here is the caller graph for this function:

◆ deleteObject()

bool QH::SqlDBWriter::deleteObject ( const QSharedPointer< PKG::DBObject > &  obj,
bool  wait = false 
)
overridevirtual

deleteObject This method execute a delete method of obj and remove current object from database.

Parameters
objThis is object for removing.
waitThis arguments force current thread wait for the function finishing.
Returns
true if object is removed successful else false.

Implements QH::iObjectProvider.

Definition at line 305 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteQuery()

bool QH::SqlDBWriter::deleteQuery ( const QSharedPointer< QH::PKG::DBObject > &  deleteObject) const
virtual

deleteQuery This method prepare the delete object query.

Parameters
deleteObjectThis is tempalte object for generate a delete query.
Returns
true if query generated successful.

Definition at line 491 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disableFK()

bool QH::SqlDBWriter::disableFK ( )
protected

disableFK This method disable foreign key for the sqlite database.

Returns
return true if new settings set is correctly.

Definition at line 197 of file sqldbwriter.cpp.

Here is the call graph for this function:

◆ doQuery()

bool QH::SqlDBWriter::doQuery ( const QString &  query,
const QVariantMap &  bindValues = {},
bool  wait = false,
QSqlQuery *  result = nullptr 
) const
overridevirtual

doQuery This method execute a query in this database.

Parameters
queryThis is query that will be executed.
bindValuesThis is values that need to bind before excute query.
resultThis is query result value.
Warning
The result works onlt on await mode. Set the wait param to true.
Returns
true if the query finished successful

Implements QH::iObjectProvider.

Definition at line 413 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doSql()

bool QH::SqlDBWriter::doSql ( const QString &  sqlFile,
bool  wait 
) const
overridevirtual

doSql This method execute a query in this database.

Parameters
sqlFileThis is sql file that will be executed.
Returns
true if the query finished successful

Implements QH::iObjectProvider.

Definition at line 425 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enableFK()

bool QH::SqlDBWriter::enableFK ( )
protected

enableFK This method enable foreign key for the sqlite database.

Returns
return true if new settings set is correctly.

Definition at line 182 of file sqldbwriter.cpp.

Here is the call graph for this function:

◆ getAllObjects()

bool QH::SqlDBWriter::getAllObjects ( const PKG::DBObject templateObject,
QList< QSharedPointer< PKG::DBObject > > &  result 
)
overridevirtual

getAllObjects This method execute a select method of the templateObject and return list of all selected from databaes or cache objects

Parameters
templateObjectThis is template object for prepare a select request.
resultThis is return value, list of selected objects.
Returns
true if objects have in db else false.

Implements QH::iObjectProvider.

Definition at line 286 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getInitParams()

QVariantMap QH::SqlDBWriter::getInitParams ( const QString &  initFile) const
protectedvirtual

getInitPararm This method read parameters of database.

Support parameters of database:

  • DBDriver - This is sql driver of data base for more information see The Qt Documentatuons https://doc.qt.io/qt-5/sql-driver.html
  • DBFilePath - This is path to file of data base (sqlite only). This is phusical location of sqlite database.
  • DBInitFile - This is sql file with sql code (structure) with default structure of the database.
  • DBPass - This is password of a remote database.
  • DBLogin - This is login of a remote database.
  • DBHost - This is host addres of a remote database.
  • DBPort - port of a remote database.
Parameters
initFileThis is path to json file with database configuration.
Returns
The Map with readed configuration. IF file is incorrect or not exists then return an empty map.

Definition at line 214 of file sqldbwriter.cpp.

Here is the caller graph for this function:

◆ initDb() [1/2]

bool QH::SqlDBWriter::initDb ( const QString &  initDbParams = DEFAULT_DB_PATH)
virtual

initDb This method is wraper of the initDb(const QVariantMap &params) method. This implementation read a initDbParams file and get all params from json.

Note
this method must be invoked in the thread as a working thread of this object.
Parameters
initDbParamsThis is path to json configuration file with settings of database. For more information see description of the SqlDBWriter::getInitParams method.
Returns
true if database initialize successful.

Definition at line 262 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initDb() [2/2]

bool QH::SqlDBWriter::initDb ( const QVariantMap &  params)
virtual

initDb This method initialize a database params and create a new database if the old database not exists.

Note
this method must be invoked in the some thread as a working thread of this object.
Parameters
paramsThis is params of initialize a database. For more information about available and supported parameters see the description SqlDBWriter::getInitParams method. For more information see description of the SqlDBWriter::getInitParams method.
Returns
true if database initialize successful.

Definition at line 274 of file sqldbwriter.cpp.

Here is the call graph for this function:

◆ initSqlDataBasse()

QSqlDatabase QH::SqlDBWriter::initSqlDataBasse ( const QString &  driverName,
const QString &  name 
)
protectedvirtual

initSqlDataBasse This method create ad database connection. Without configuration. This method invoked in the initDbPrivate method.

Note
Overrde this method for create f custom method of initialization of the db connection.
Parameters
driverNameThis is name of sql driver for more information see about Qt Sql Drivers.
nameThis is name of path of database.
Returns
database object.

Definition at line 243 of file sqldbwriter.cpp.

◆ insertObject()

bool QH::SqlDBWriter::insertObject ( const QSharedPointer< PKG::DBObject > &  saveObject,
bool  wait = false,
const QWeakPointer< unsigned int > &  autoincrementIdResult = {} 
)
overridevirtual

insertObject This method execute a insert method of the saveObject and insert it into database.

Note
This method insert object into database only. IF object is exits in the database then this method return false.
Parameters
saveObjectThis is object for inserting.
waitThis arguments force current thread wait for the function finishing.
autoincrementIdResultis id of the insert query to the Table with autoincrement id field.
Returns
true if objects is saved successful else false. Note return two value. First is boolean result, second is id of inserted value.
Note
id will be returned only for the autoincement records.

Implements QH::iObjectProvider.

Definition at line 314 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insertQuery()

bool QH::SqlDBWriter::insertQuery ( const QSharedPointer< QH::PKG::DBObject > &  insertObject,
const QWeakPointer< unsigned int > &  autoIncrementID = {} 
) const
virtual

insertQuery This method prepare the insert object query.

Parameters
insertObjectThis is strong pointer of object for generate the insert query.
autoIncrementIDWeek pointer to result id of new inserteed record.
Returns
true if query generated successful.
Note
Works only of the int autoincrement ids...

Definition at line 367 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValid()

bool QH::SqlDBWriter::isValid ( ) const
virtual

isValid This method return true if database is successful inited and working correctly.

Returns
true if database working is correctly.

Definition at line 282 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replaceObject()

bool QH::SqlDBWriter::replaceObject ( const QSharedPointer< PKG::DBObject > &  saveObject,
bool  wait = false 
)
overridevirtual

replaceObject This method execute a replace method of the saveObject and insert or save if not exists, all changes into database.

Note
This method update object in the database only. If you try update not exists object then this method return false.
Parameters
saveObjectThis is object for updating.
waitThis arguments force current thread wait for the function finishing.
Returns
true if objects is updated successful else false.

Implements QH::iObjectProvider.

Definition at line 334 of file sqldbwriter.cpp.

Here is the call graph for this function:

◆ replaceQuery()

bool QH::SqlDBWriter::replaceQuery ( const QSharedPointer< QH::PKG::DBObject > &  insertObject) const
virtual

replaceQuery This method prepare the replce object query.

Parameters
insertObjectThis is strong pointer of object for generate the insert query.
Returns
true if query generated successful.
Note
This method generate query for replace objects in the database.

Definition at line 394 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ selectQuery()

bool QH::SqlDBWriter::selectQuery ( const QH::PKG::DBObject requestObject,
QList< QSharedPointer< QH::PKG::DBObject > > &  result 
)
virtual

selectQuery generate select query to database from parameters.

Parameters
requestObjectThis is template object for generate select query.
resultThis is return values.
Returns
true if all good else false.

Definition at line 435 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSQLSources()

void QH::SqlDBWriter::setSQLSources ( const QStringList &  list)
overridevirtual

setSQLSources This method set sql sources for deployed database.

Parameters
listThis is list of sql files with database sources.

For more information about sql sources see the DataBaseNode::SQLSources method.

Implements QH::iObjectProvider.

Definition at line 342 of file sqldbwriter.cpp.

◆ updateObject()

bool QH::SqlDBWriter::updateObject ( const QSharedPointer< PKG::DBObject > &  saveObject,
bool  wait = false 
)
overridevirtual

updateObject This method execute a update method of the saveObject and save all changes into database.

Note
This method update object in the database only.
Warning
This method do not guarantee that return false if The updated object is not exist.
Parameters
saveObjectThis is object for updating.
waitThis arguments force current thread wait for the function finishing.
Returns
true if objects is updated successful else false.

Implements QH::iObjectProvider.

Definition at line 296 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateQuery()

bool QH::SqlDBWriter::updateQuery ( const QSharedPointer< QH::PKG::DBObject > &  ptr) const
virtual

updateQuery This method execute update query of object. For more Information see DBObject::prepareUpdateQuery.

Parameters
ptrThis is strong pointer to object that need to update into a database.
Returns
true if function finished successful.

Definition at line 509 of file sqldbwriter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: