146 lines
5.0 KiB
Plaintext
Executable File
146 lines
5.0 KiB
Plaintext
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Template for 'real' structures including insert/update/delete functions for db
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
|
|
{% autoescape off %}
|
|
// filename: {{ namespace_abbr }}_db.h
|
|
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <functional>
|
|
#include <QtSql/QSqlError>
|
|
|
|
#include <{{ include_prefix }}/{{ namespace_abbr }}_export.h>
|
|
#include <{{ include_prefix }}/{{ namespace_abbr }}_db_structs.h>
|
|
#include <{{ include_prefix }}/{{ namespace_abbr }}_enums.hpp>
|
|
|
|
{% for ext_service in ext_services.member %}
|
|
#include <{{ ext_service.baseFilePath }}.hpp>
|
|
{% endfor %}
|
|
|
|
namespace NGL
|
|
{
|
|
class INGLLogger;
|
|
}
|
|
class QSqlDatabase;
|
|
|
|
namespace {{ namespace }}
|
|
{
|
|
|
|
class {{ namespace_abbr_upper }}_COMMON_EXPORT DatabaseInterface : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
virtual ~DatabaseInterface() = default;
|
|
|
|
/**
|
|
* Create a new instance for {{ namespace }}::DatabaseInterface.
|
|
* @param exerciseId if <= 0 -> global service
|
|
*/
|
|
static std::shared_ptr<DatabaseInterface> create(int exerciseId);
|
|
|
|
/**
|
|
* @brief initialize the database interface (must be called after the db connection was opened!)
|
|
* @return true on succcess
|
|
*/
|
|
virtual bool init(const QSqlDatabase &dbConnection) = 0;
|
|
virtual bool init(const QString &dbUser,
|
|
const QString &dbPasswd,
|
|
const QString &dbDriver,
|
|
const QString &dbHost,
|
|
const QString &dbName) = 0;
|
|
|
|
/**
|
|
* @brief return the current schema name depending if exercise id > 0 or not
|
|
*/
|
|
virtual QString schemaName() const = 0;
|
|
|
|
/**
|
|
* @brief return the current database user dependening if exercise id > 0 or not
|
|
*/
|
|
virtual QString dbUser() const = 0;
|
|
|
|
/**
|
|
* @brief return the database connection used in this DatabaseInterface
|
|
*/
|
|
virtual const QSqlDatabase &database() const = 0;
|
|
|
|
/**
|
|
* emit an error message (ERROR_LOG) when a statement fails to execute (default: on)
|
|
*/
|
|
virtual void setEmitMessageOnError(bool bEmit) = 0;
|
|
|
|
virtual QSqlError getLastError() const = 0;
|
|
|
|
{% for cpp_struct in cpp_structs %}
|
|
//
|
|
// Database::{{ cpp_struct.name }}
|
|
//
|
|
public:
|
|
virtual bool insert(Database::{{ cpp_struct.name }} &data) = 0;
|
|
virtual bool upsert(Database::{{ cpp_struct.name }} &data) = 0;
|
|
virtual bool update(const Database::{{ cpp_struct.name }} &data) = 0;
|
|
{% if cpp_struct.insertStructures|length == 1 %}
|
|
virtual bool insert(std::vector<Database::{{ cpp_struct.name }}> &cont) = 0;
|
|
virtual bool upsert(std::vector<Database::{{ cpp_struct.name }}> &cont) = 0;
|
|
virtual bool update(const std::vector<Database::{{ cpp_struct.name }}> &cont) = 0;
|
|
{% endif %}
|
|
virtual bool select(QVector<Database::{{ cpp_struct.name }}> &dataVec) = 0;
|
|
virtual bool select(const std::function<void(const Database::{{ cpp_struct.name }} &data)> &cb) = 0;
|
|
{% for fk in cpp_struct.selectRemoveMember %}
|
|
virtual bool selectBy{{ fk.cppMemberNameCc }}(QVector<Database::{{ cpp_struct.name }}> &dataVec, const {{ fk.cppType }} &{{ fk.cppMemberName }}, const QString &orderBy = QString()) = 0;
|
|
virtual bool selectBy{{ fk.cppMemberNameCc }}(const std::function<void(const Database::{{ cpp_struct.name }} &data)> &cb, const {{ fk.cppType }} &{{ fk.cppMemberName }}, const QString &orderBy = QString()) = 0;
|
|
{% endfor %}
|
|
virtual bool select{{ cpp_struct.name }}({{ cpp_struct.primaryKey.cppType }} {{ cpp_struct.primaryKey.cppMemberName }}, Database::{{ cpp_struct.name }} &data) = 0;
|
|
virtual bool remove{{ cpp_struct.name }}({{ cpp_struct.primaryKey.cppType }} {{ cpp_struct.primaryKey.cppMemberName }}) = 0;
|
|
{% if cpp_struct.deleteWithoutPkAllowed %} {% for fk in cpp_struct.selectRemoveMember %}
|
|
virtual bool remove{{ cpp_struct.nameCc }}By{{ fk.cppMemberNameCc }}(const {{ fk.cppType }} &{{ fk.cppMemberName }}) = 0;
|
|
{% endfor %} {% endif %}
|
|
virtual bool fixUpSequence{{ cpp_struct.name }}() = 0;
|
|
virtual bool truncate{{ cpp_struct.name }}() = 0;
|
|
{% if cpp_struct.dbNotifications %}
|
|
Q_SIGNALS:
|
|
{% for dbnotify in cpp_struct.dbNotifications %}
|
|
// database notification on insert/update/delete
|
|
void {{ dbnotify }}(const QVariant &payload);
|
|
{% endfor %}
|
|
{% endif %}
|
|
//
|
|
// Database::{{ cpp_struct.name }} end
|
|
//
|
|
{% endfor %}
|
|
{% if data_structs|length > 0 %}
|
|
public:{% for data_struct in data_structs %}
|
|
// read only struct Database::{{ data_struct.name }}
|
|
virtual bool select(QVector<Database::{{ data_struct.name }}> &dataVec) = 0;
|
|
{% endfor %} {% endif %}
|
|
{% if custom_db_procs|length > 0 %}
|
|
public:{% for db_proc in custom_db_procs %}
|
|
// custom proc, body must be implemented by user
|
|
virtual {{ db_proc.signature }} = 0;
|
|
{% endfor %} {% endif %}
|
|
|
|
public:
|
|
|
|
// vector of database struct to/from JSON-array
|
|
// helper for BaseDownloader
|
|
template<typename T>
|
|
static QJsonArray toJson(const QVector<T> &dataVec)
|
|
{
|
|
return Database::toJson<T>(dataVec);
|
|
}
|
|
|
|
template<typename T>
|
|
static std::vector<T> fromJson(const QJsonArray &arr)
|
|
{
|
|
return Database::fromJson<T>(arr);
|
|
}
|
|
};// class DatabaseInterface
|
|
|
|
} // namespace {{ namespace }}
|
|
{% endautoescape %}
|