Revision 3938a37e
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/database.cpp | ||
---|---|---|
#include "schema/mysqlschema.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
Database::Database(QSqlDatabase database, bool loggerActivated, bool logQueries,
|
||
Database::Database(QSqlDatabase database, bool logQueries,
|
||
bool logErrors, MsgType type) {
|
||
this->database = database;
|
||
this->init();
|
||
this->connectionName = this->database.connectionName();
|
||
this->initLogger(loggerActivated, logQueries, logErrors, type);
|
||
this->initLogger(logQueries, logErrors, type);
|
||
}
|
||
|
||
Database::Database(QString databaseType, QString connectionName,
|
||
... | ... | |
this->database.setConnectOptions(databaseOptions);
|
||
}
|
||
this->init();
|
||
this->initLogger(loggerActivated, logQueries, logErrors, type);
|
||
this->initLogger(logQueries, logErrors, type);
|
||
}
|
||
|
||
Logger *Database::getLogger() const {
|
||
return logger;
|
||
}
|
||
|
||
void Database::init() {
|
||
... | ... | |
QSqlDriver::Transactions);
|
||
}
|
||
|
||
void Database::initLogger(bool activated, bool logQueries, bool logErrors,
|
||
void Database::initLogger(bool logQueries, bool logErrors,
|
||
MsgType type) {
|
||
this->logQueries = logQueries;
|
||
this->logErrors = logErrors;
|
||
if (activated) {
|
||
this->logger = new Logger(QDir::currentPath() + "/db" + this->connectionName +
|
||
".log", type);
|
||
}
|
||
this->logger = new Logger(QDir::currentPath() + "/db" + this->connectionName +
|
||
".log", type);
|
||
}
|
||
|
||
Database::~Database() {
|
||
... | ... | |
}
|
||
|
||
bool Database::rollbackTransaction() {
|
||
qWarning() << "Transaction rolled back!" << this->database.lastError().text();
|
||
this->logger->logMsg("Transaction rolled back!" + this->database.lastError().text(),MsgType::WARNING);
|
||
return supportTransactions && this->database.rollback();
|
||
}
|
||
|
src/database.h | ||
---|---|---|
bool supportTransactions;
|
||
Logger *logger = nullptr;
|
||
void init();
|
||
void initLogger(bool activated, bool logQueries, bool logErrors, MsgType type);
|
||
void initLogger(bool logQueries, bool logErrors, MsgType type);
|
||
bool logQueries;
|
||
bool logErrors;
|
||
|
||
public:
|
||
Database(QSqlDatabase database, bool loggerActivated = true,
|
||
bool logQueries = false, bool logErrors = true,MsgType type= DEFAULTMSGTYPE);
|
||
Database(QSqlDatabase database, bool logQueries = false, bool logErrors = true, MsgType type = DEFAULTMSGTYPE);
|
||
~Database();
|
||
Database(QString databaseType, QString connectionName = QString(""),
|
||
QString hostname = QString(""),
|
||
QString databasename = QString("") ,
|
||
QString username = QString(""), QString password = QString(""),
|
||
qint64 port = 0, bool loggerActivated = true, bool logQueries = false,
|
||
bool logErrors = true, QString databaseOptions = "",MsgType type= DEFAULTMSGTYPE);
|
||
bool logErrors = true, QString databaseOptions = "",
|
||
MsgType type = DEFAULTMSGTYPE);
|
||
QSqlDatabase getDatabase();
|
||
QString getConnectionName();
|
||
QSqlQuery getQuery();
|
||
... | ... | |
QSqlQuery select(const QString &query);
|
||
void startTransaction();
|
||
bool commitTransaction();
|
||
void logMsg(const QString &value);
|
||
bool rollbackTransaction();
|
||
static DatabaseType getDatabaseType(QString s);
|
||
static QSharedPointer<Schema> getSchema(DatabaseType db,
|
||
QSharedPointer<Database> database);
|
||
Logger *getLogger() const;
|
||
};
|
||
}
|
||
#endif // DATABASE_H
|
src/entitymanager.cpp | ||
---|---|---|
*/
|
||
|
||
#include <QVariantList>
|
||
#include <QHash>
|
||
#include <QDir>
|
||
#include "entitymanager.h"
|
||
#include "enums/databasetype.h"
|
||
#include "databasemigration.h"
|
||
... | ... | |
#include "validators/validator.h"
|
||
#include "validators/validatorrule.h"
|
||
#include "entityinspector.h"
|
||
#include <QHash>
|
||
|
||
using namespace CuteEntityManager;
|
||
|
||
... | ... | |
return EntityManager::connectionNames;
|
||
}
|
||
|
||
EntityManager::EntityManager(QSqlDatabase database, bool logQueries,
|
||
const bool inspectEntities,
|
||
MsgType logActions) : QObject() {
|
||
auto db = new Database(database, true, logQueries, logActions);
|
||
this->db = QSharedPointer<Database>(db);
|
||
this->init(inspectEntities, logActions);
|
||
}
|
||
|
||
void EntityManager::init(bool inspect, const MsgType msgType) {
|
||
auto schema = Database::getSchema(Database::getDatabaseType(
|
||
this->db->getDatabase().driverName()), this->db);
|
||
this->schema = QSharedPointer<Schema>(schema);
|
||
this->schema->setTables(this->schema->getTableSchemas());
|
||
this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
||
this->schema->getQueryBuilder()));
|
||
this->appendToInstanceList();
|
||
if (inspect) {
|
||
EntityInspector inspector = EntityInspector(msgType);
|
||
inspector.checkRegisteredEntities();
|
||
}
|
||
QString loggerFile = "em" + this->objectName() + ".log";
|
||
loggerFile.replace("[", "");
|
||
loggerFile.replace("]", "");
|
||
this->logger = QSharedPointer<Logger>(new Logger(QDir::currentPath() + "/" +
|
||
loggerFile, msgType));
|
||
}
|
||
|
||
EntityManager::EntityManager(const QString &databaseType, QString databasename,
|
||
QString hostname, QString username, QString password, QString port,
|
||
bool logQueries, QString databaseOptions, const bool inspectEntities,
|
||
CuteEntityManager::MsgType logActions) : QObject() {
|
||
auto db = new Database(databaseType, this->createConnection(), hostname,
|
||
databasename, username,
|
||
password,
|
||
port.toInt(), true, logQueries, true, databaseOptions, logActions);
|
||
this->db = QSharedPointer<Database>(db);
|
||
this->init(inspectEntities, logActions);
|
||
}
|
||
|
||
EntityManager::~EntityManager() {
|
||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
EntityManager::instances.remove(this->objectName());
|
||
}
|
||
|
||
|
||
QSharedPointer<QueryBuilder> EntityManager::getQueryBuilder() const {
|
||
return this->schema->getQueryBuilder();
|
||
}
|
||
... | ... | |
}
|
||
rc = this->db->exec(query);
|
||
if (!rc) {
|
||
qWarning() << "class is erroneous:" << EntityHelper::getClassname(
|
||
entity.data());
|
||
this->logger->logMsg("class is erroneous:" + EntityHelper::getClassName(
|
||
entity.data()), MsgType::WARNING);
|
||
break;
|
||
}
|
||
if (first) {
|
||
... | ... | |
relationsIgnoreHasChanged);
|
||
}
|
||
|
||
/**
|
||
* @brief EntityManager::create
|
||
* Will persist an entity in the database. Before its persisted it has the id -1(invalid id). If database persisting was succesfull it has a valid id.
|
||
* @param entity
|
||
* @param persistRelations
|
||
* @param checkDuplicate
|
||
* @param validate
|
||
* @return
|
||
*/
|
||
bool EntityManager::create(QSharedPointer<Entity> &entity,
|
||
const bool persistRelations, const bool checkDuplicate, const bool validate,
|
||
const bool relationsIgnoreHasChanged) {
|
||
... | ... | |
return this->createObject(entity, merged, persistRelations,
|
||
checkDuplicate, validate, relationsIgnoreHasChanged);
|
||
}
|
||
/**
|
||
* @brief EntityManager::save
|
||
* If the entity has no valid ID, this method will call the create() method. Else it would call the merge method.
|
||
* @param entity
|
||
* @param persistRelations
|
||
* @param ignoreHasChanged
|
||
* @param validate
|
||
* @return bool
|
||
*/
|
||
|
||
bool EntityManager::save(QSharedPointer<Entity> &entity,
|
||
const bool persistRelations, const bool ignoreHasChanged, const bool validate,
|
||
const bool relationsIgnoreHasChanged) {
|
||
... | ... | |
ignoreHasChanged, validate, relationsIgnoreHasChanged);
|
||
}
|
||
|
||
EntityManager::EntityManager(QSqlDatabase database, bool logQueries,
|
||
const bool inspectEntities,
|
||
MsgType logActions) : QObject() {
|
||
auto db = new Database(database, true, logQueries, true, logActions);
|
||
this->db = QSharedPointer<Database>(db);
|
||
this->init(inspectEntities,logActions);
|
||
}
|
||
|
||
void EntityManager::init(bool inspect,const MsgType msgType) {
|
||
auto schema = Database::getSchema(Database::getDatabaseType(
|
||
this->db->getDatabase().driverName()), this->db);
|
||
this->schema = QSharedPointer<Schema>(schema);
|
||
this->schema->setTables(this->schema->getTableSchemas());
|
||
this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
||
this->schema->getQueryBuilder()));
|
||
this->appendToInstanceList();
|
||
if (inspect) {
|
||
EntityInspector inspector = EntityInspector(msgType);
|
||
inspector.checkRegisteredEntities();
|
||
}
|
||
}
|
||
|
||
EntityManager::EntityManager(const QString &databaseType, QString databasename,
|
||
QString hostname, QString username, QString password, QString port,
|
||
bool logQueries, QString databaseOptions, const bool inspectEntities,
|
||
CuteEntityManager::MsgType logActions) : QObject() {
|
||
auto db = new Database(databaseType, this->createConnection(), hostname,
|
||
databasename, username,
|
||
password,
|
||
port.toInt(), true, logQueries, true, databaseOptions, logActions);
|
||
this->db = QSharedPointer<Database>(db);
|
||
this->init(inspectEntities,logActions);
|
||
}
|
||
|
||
EntityManager::~EntityManager() {
|
||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
EntityManager::instances.remove(this->objectName());
|
||
}
|
||
|
||
bool EntityManager::startup(QString version, QStringList toInitialize,
|
||
bool createIndices) {
|
||
QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
|
||
... | ... | |
ok = this->createTable(entity, false);
|
||
entities.append(entity);
|
||
} else {
|
||
qWarning() << "startup of version " << version << " failed";
|
||
qWarning() << "erroneous entity:" << (var == 0 ?
|
||
"null, this should not happen!" : toInitialize.at(
|
||
var - 1));
|
||
this->logger->logMsg("startup of version " + version + " failed",
|
||
MsgType::CRITICAL);
|
||
this->logger->logMsg( "erroneous entity:" + (var == 0 ?
|
||
"null, this should not happen!" : toInitialize.at(var - 1)), MsgType::CRITICAL);
|
||
break;
|
||
}
|
||
}
|
||
... | ... | |
}
|
||
}
|
||
|
||
|
||
void EntityManager::missingManyToManyTable(const QString &tblName,
|
||
const QSharedPointer<Entity> &e, const Relation &r) {
|
||
qWarning() << "MANY_TO_MANY Table " << tblName << " is missing";
|
||
qWarning() << "Entity " << EntityHelper::getClassName(e.data()) <<
|
||
" is affected";
|
||
qWarning() << "Relation of property: " << r.getPropertyName();
|
||
//qDebug() << "RelationType:" << r.getType() << " MappedBy:" << r.getMappedBy();
|
||
this->logger->logMsg("MANY_TO_MANY Table " + tblName + " is missing.\n" +
|
||
"Entity " + EntityHelper::getClassName(e.data()) +
|
||
" is affected.\n" + "Relation of property: " + r.getPropertyName(),
|
||
MsgType::CRITICAL);
|
||
}
|
||
|
||
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
src/entitymanager.h | ||
---|---|---|
bool create(QList<QSharedPointer<Entity>> entities,
|
||
const bool persistRelations = true, const bool validate = true,
|
||
const bool relationsIgnoreHasChanged = false);
|
||
/**
|
||
* @brief EntityManager::create
|
||
* Will persist an entity in the database. Before its persisted it has the id -1(invalid id). If database persisting was succesfull it has a valid id.
|
||
* @param entity
|
||
* @param persistRelations
|
||
* @param checkDuplicate
|
||
* @param validate
|
||
* @return
|
||
*/
|
||
bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool checkDuplicate = false, const bool validate = true,
|
||
const bool relationsIgnoreHasChanged = false) ;
|
||
/**
|
||
* @brief EntityManager::save
|
||
* If the entity has no valid ID, this method will call the create() method. Else it would call the merge method.
|
||
* @param entity
|
||
* @param persistRelations
|
||
* @param ignoreHasChanged
|
||
* @param validate
|
||
* @return bool
|
||
*/
|
||
bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool ignoreHasChanged = false, const bool validate = true,
|
||
const bool relationsIgnoreHasChanged = false);
|
||
... | ... | |
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
|
||
const char *classname, const bool refresh = false,
|
||
const bool resolveRelations = true);
|
||
/**
|
||
@todo wait for Qt 5.5.1
|
||
@see https://codereview.qt-project.org/#/c/122232/
|
||
*/
|
||
void missingManyToManyTable(const QString &tblName,
|
||
const QSharedPointer<Entity> &e, const Relation &r);
|
||
/**
|
||
... | ... | |
private:
|
||
static QStringList connectionNames;
|
||
static QHash<QString, EntityManager *> instances;
|
||
QSharedPointer<Logger> logger;
|
||
QString id;
|
||
QSharedPointer<Schema> schema;
|
||
static void setConnectionNames(QStringList list);
|
src/logger.cpp | ||
---|---|---|
|
||
void Logger::logMsg(const QString &value, const MsgType type) {
|
||
if (!value.isEmpty() && this->shouldBeLogged(type)) {
|
||
this->outputToConsole(type, value);
|
||
QString msg = value;
|
||
QFile log(this->getPath());
|
||
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
|
||
log.seek(log.size());
|
||
QTextStream stream(&log);
|
||
stream.setCodec("UTF-8");
|
||
stream << value;
|
||
stream << "\n";
|
||
if (value.size() - 1 != value.lastIndexOf("\n")) {
|
||
msg.append("\n");
|
||
}
|
||
stream << msg;
|
||
this->outputToConsole(type, msg.replace("\"", "'"));
|
||
stream.flush();
|
||
log.close();
|
||
}
|
src/querybuilder.cpp | ||
---|---|---|
#include <QRegularExpression>
|
||
#include "entityinstancefactory.h"
|
||
#include "entityhelper.h"
|
||
#include "logger.h"
|
||
|
||
using namespace CuteEntityManager;
|
||
|
||
... | ... | |
&& this->createRelationTables(entity)))) {
|
||
rc = true;
|
||
} else {
|
||
qWarning() << "Table " << entity->getTablename() << " could not be created.";
|
||
this->database->getLogger()->logMsg("Table " + entity->getTablename() +
|
||
" could not be created.", MsgType::WARNING);
|
||
}
|
||
this->schema->getTableSchema(tableName);
|
||
}
|
||
... | ... | |
auto query = this->database->getQuery(this->createTable(i.key(), i.value()));
|
||
if (!this->database->exec(query)) {
|
||
ok = false;
|
||
qWarning() << "Relational table for table " << entity->getTablename() <<
|
||
" could not be created." << " Tablename:" << i.key();
|
||
this->database->getLogger()->logMsg("Relational table for table " +
|
||
entity->getTablename() +
|
||
" could not be created." + " Tablename:" + i.key(), MsgType::WARNING);
|
||
break;
|
||
}
|
||
++i;
|
||
... | ... | |
if (entity) {
|
||
return this->generateColumnNameID(entity->getTablename());
|
||
}
|
||
qWarning() << "Entity is empty!";
|
||
this->database->getLogger()->logMsg("Entity is empty!", MsgType::WARNING);
|
||
return "";
|
||
}
|
||
|
||
... | ... | |
this->saveAttributes(entity, this->processProperties(e, usedProperties),
|
||
this->processRelations(e, usedRelations)), e->getPrimaryKey()));
|
||
} else {
|
||
qWarning() << "Instance of " << metaObj->className() << " could not be created";
|
||
this->database->getLogger()->logMsg("Instance of " + QString(metaObj->className()) +
|
||
" could not be created", MsgType::CRITICAL);
|
||
break;
|
||
}
|
||
}
|
src/querybuilder.h | ||
---|---|---|
class Schema;
|
||
class Entity;
|
||
class Database;
|
||
class Logger;
|
||
enum DbForeignKeyCascade {
|
||
RESTRICT,
|
||
CASCADE,
|
Auch abrufbar als: Unified diff
logger