Revision fc14f551
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
EntityManager.pro | ||
---|---|---|
src/validators/param.h \
|
||
src/validators/errormsg.h \
|
||
src/validators/defaultvalidator.h \
|
||
src/validators/validatorfactory.h
|
||
src/validators/validatorfactory.h \
|
||
src/validators/validatorrule.h
|
||
|
||
SOURCES += \
|
||
src/entity.cpp \
|
||
... | ... | |
src/validators/param.cpp \
|
||
src/validators/errormsg.cpp \
|
||
src/validators/defaultvalidator.cpp \
|
||
src/validators/validatorfactory.cpp
|
||
src/validators/validatorfactory.cpp \
|
||
src/validators/validatorrule.cpp
|
||
|
||
CONFIG += c++14
|
||
QMAKE_CXXFLAGS += -std=c++14
|
samples/example/main.cpp | ||
---|---|---|
q.setDistinct(true);
|
||
q.appendOrderBy(OrderBy(QString("birthday"), Direction::SORT_DESC));
|
||
q.setLimit(10);
|
||
QList<QSharedPointer<Pupil>> list = e->find<Pupil>(q, true,false);
|
||
QList<QSharedPointer<Pupil>> list = e->find<Pupil>(q, true);
|
||
for (int i = 0; i < list.size(); ++i) {
|
||
// qWarning() << "-----------------------------";
|
||
// qWarning() << "Merge Pupil";
|
||
// qWarning() << "-----------------------------";
|
||
// list.at(i)->setBirthday(QDate(2222,12,22));
|
||
// QSharedPointer<Entity> pupilE = list.at(i).objectCast<Entity>();
|
||
// e->merge(pupilE,true);
|
||
qWarning() << list.at(i)->toString();
|
||
}
|
||
qWarning() << "-----------------------------";
|
||
... | ... | |
qWarning() << "Remove Group";
|
||
qWarning() << "-----------------------------";
|
||
e->remove(entityGroupFindPtr);
|
||
|
||
|
||
|
||
|
||
sqliteproc->sqliteDBMemFile(true, "db.sqlite");
|
||
qWarning() << "Duration:" << t.elapsed();
|
||
delete sqliteproc;
|
src/entity.cpp | ||
---|---|---|
r.append("id: ") + this->getId() + "}";
|
||
return r;
|
||
}
|
||
QList<ErrorMsg> Entity::getErrors() const {
|
||
return errors;
|
||
}
|
||
|
||
void Entity::setErrors(const QList<ErrorMsg> &value) {
|
||
errors = value;
|
||
}
|
||
|
||
Entity::~Entity() {
|
||
|
||
}
|
||
|
||
QList<ValidationRule> Entity::validationRules() const {
|
||
return QList<ValidationRule>();
|
||
}
|
||
|
||
QString Entity::getTablename() const {
|
||
return QString(this->metaObject()->className()).toLower();
|
||
}
|
||
... | ... | |
emit idChanged();
|
||
}
|
||
}
|
||
|
||
bool Entity::hasErrors() const {
|
||
return !this->errors.isEmpty();
|
||
}
|
src/entity.h | ||
---|---|---|
#include <QStack>
|
||
#include <QQueue>
|
||
#include "entityinstancefactory.h"
|
||
#include "validators/validatorrule.h"
|
||
#include "validators/errormsg.h"
|
||
namespace CuteEntityManager {
|
||
|
||
/**
|
||
... | ... | |
public:
|
||
virtual QString toString() const;
|
||
virtual ~Entity();
|
||
virtual QList<ValidationRule> validationRules() const;
|
||
virtual QString getTablename() const;
|
||
virtual const QHash<QString, Relation> getRelations() const;
|
||
virtual const QStringList getTransientAttributes() const;
|
||
... | ... | |
qint64 getId() const;
|
||
void setId(const qint64 &value);
|
||
|
||
protected:
|
||
bool hasErrors() const;
|
||
|
||
QList<ErrorMsg> getErrors() const;
|
||
void setErrors(const QList<ErrorMsg> &value);
|
||
|
||
protected:
|
||
explicit Entity (QObject *parent = 0);
|
||
virtual QString slimToString() const;
|
||
QList<ErrorMsg> errors;
|
||
qint64 id;
|
||
};
|
||
}
|
src/entitymanager.cpp | ||
---|---|---|
#include "entitymanager.h"
|
||
#include "enums/databasetype.h"
|
||
#include "databasemigration.h"
|
||
#include "validators/validatorfactory.h"
|
||
#include "validators/validator.h"
|
||
#include "validators/validatorrule.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
QStringList EntityManager::connectionNames = QStringList();
|
||
... | ... | |
return rc;
|
||
}
|
||
|
||
bool EntityManager::validate(QSharedPointer<Entity> &entity) {
|
||
QList<ValidationRule> rules = entity->validationRules();
|
||
QList<ErrorMsg> list = QList<ErrorMsg>();
|
||
for (int i = 0; i < rules.size(); ++i) {
|
||
ValidationRule rule = rules.at(i);
|
||
QSharedPointer<Validator> validator = ValidatorFactory::getValidatorObject(
|
||
rule.getValidatorName());
|
||
if (validator) {
|
||
for (int var = 0; var < rule.getAttributes().size(); ++var) {
|
||
QString attr = rule.getAttributes().at(var);
|
||
list.append(validator->validate(entity->getProperty(attr), rule.getParams()));
|
||
}
|
||
}
|
||
}
|
||
entity->setErrors(list);
|
||
return !entity->hasErrors();
|
||
}
|
||
|
||
QString EntityManager::createConnection() {
|
||
QStringList l = EntityManager::getConnectionNames();
|
||
QString conName = "";
|
||
... | ... | |
* @return
|
||
*/
|
||
bool EntityManager::create(QList<QSharedPointer<Entity> > entities,
|
||
const bool persistRelations) {
|
||
const bool persistRelations, const bool validate) {
|
||
bool ok = true;
|
||
foreach (QSharedPointer<Entity> ent, entities) {
|
||
ok = this->create(ent, persistRelations);
|
||
ok = this->create(ent, persistRelations, validate);
|
||
if (!ok) {
|
||
break;
|
||
}
|
||
... | ... | |
}
|
||
|
||
bool EntityManager::create(QSharedPointer<Entity> &entity,
|
||
const bool persistRelations, const bool checkDuplicate) {
|
||
const bool persistRelations, const bool checkDuplicate, const bool validate) {
|
||
bool rc = false;
|
||
if (this->checkTable(entity) && !(checkDuplicate && this->count(entity) > 0)) {
|
||
if (this->checkTable(entity) && ((validate && this->validate(entity))
|
||
|| !validate) && !(checkDuplicate && this->count(entity) > 0)) {
|
||
if (persistRelations) {
|
||
this->savePrePersistedRelations(entity);
|
||
}
|
||
... | ... | |
return rc;
|
||
}
|
||
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations,
|
||
const bool validate) {
|
||
bool ok = false;
|
||
if (entity->getId() > -1) {
|
||
if (entity->getId() > -1 && ((validate && this->validate(entity))
|
||
|| !validate)) {
|
||
if (withRelations) {
|
||
this->savePrePersistedRelations(entity);
|
||
}
|
src/entitymanager.h | ||
---|---|---|
#include "queryinterpreter.h"
|
||
#include "cache.h"
|
||
#include "querybuilder.h"
|
||
#include "validators/errormsg.h"
|
||
namespace CuteEntityManager {
|
||
|
||
class Logger;
|
||
... | ... | |
QSharedPointer<Entity> &entity,
|
||
bool ignoreID = false);
|
||
bool create(QList<QSharedPointer<Entity>> entities,
|
||
const bool persistRelations = true);
|
||
const bool persistRelations = true, const bool validate = true);
|
||
bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool checkDuplicate = false);
|
||
const bool checkDuplicate = false, const bool validate = true);
|
||
bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true,
|
||
const bool validate = true);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity,
|
||
... | ... | |
QList<QHash<QString, QVariant> > selectByQuery(Query &query);
|
||
QList<QHash<QString, QVariant> > selectBySql(const QString &sql);
|
||
qint8 count(Query &query);
|
||
bool validate(QSharedPointer<Entity> &entity);
|
||
|
||
public:
|
||
EntityManager(QSqlDatabase database, bool logQueries = false);
|
src/validators/validatorfactory.cpp | ||
---|---|---|
#include "validatorfactory.h"
|
||
#include "defaultvalidator.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
ValidatorFactory::ValidatorFactory() {
|
||
}
|
||
|
||
|
||
QHash<QByteArray, ValidatorFactory::Constructor> ValidatorFactory::instance =
|
||
QHash<QByteArray, ValidatorFactory::Constructor>();
|
||
|
||
QHash<QString, QSharedPointer<Validator>> ValidatorFactory::validatorInstances =
|
||
QHash<QString, QSharedPointer<Validator>>();
|
||
|
||
|
||
Validator *ValidatorFactory::createValidator(QString shortname) {
|
||
Validator *ValidatorFactory::createValidator(const QString &shortname) {
|
||
if (Validator::builtInValidators().contains(shortname)) {
|
||
return ValidatorFactory::createObject(shortname.toLatin1());
|
||
return ValidatorFactory::createObject(Validator::builtInValidators().value(
|
||
shortname).toLatin1());
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
Validator *ValidatorFactory::createObject(const QByteArray &className) {
|
||
Constructor constructor = ValidatorFactory::instance.value(className);
|
||
if ( constructor == nullptr ) {
|
||
return nullptr;
|
||
}
|
||
return (*constructor)();
|
||
}
|
||
|
||
QSharedPointer<Validator> ValidatorFactory::getValidatorObject(
|
||
const QString &shortname) {
|
||
if (!ValidatorFactory::validatorInstances.contains(shortname)) {
|
||
ValidatorFactory::validatorInstances.insert(shortname,
|
||
QSharedPointer<Validator>(ValidatorFactory::createValidator(shortname)));
|
||
}
|
||
return ValidatorFactory::validatorInstances.value(shortname);
|
||
}
|
||
|
||
void ValidatorFactory::registerClasses() {
|
||
if (ValidatorFactory::instance.isEmpty()) {
|
||
// Validator::registerClass<CompareValidator>();
|
||
// Validator::registerClass<CompareValidator>();
|
||
ValidatorFactory::registerClass<DefaultValidator>();
|
||
// Validator::registerClass<EmailValidator>();
|
||
// Validator::registerClass<ExistValidator>();
|
||
// Validator::registerClass<EmailValidator>();
|
||
// Validator::registerClass<ExistValidator>();
|
||
// Validator::registerClass<ImageValidator>();
|
||
// Validator::registerClass<SizeValidator>();
|
||
// Validator::registerClass<NumberValidator>();
|
src/validators/validatorfactory.h | ||
---|---|---|
#define VALIDATORFACTORY_H
|
||
#include <QString>
|
||
#include <QHash>
|
||
#include <QSharedPointer>
|
||
namespace CuteEntityManager {
|
||
class Validator;
|
||
class ValidatorFactory {
|
||
public:
|
||
static void registerClasses();
|
||
static Validator *createValidator(QString shortname);
|
||
static Validator *createValidator(const QString &shortname);
|
||
template<typename T>
|
||
static void registerClass() {
|
||
if (!ValidatorFactory::instance.contains(
|
||
... | ... | |
}
|
||
}
|
||
|
||
static Validator *createObject( const QByteArray &className) {
|
||
Constructor constructor = ValidatorFactory::instance.value(className);
|
||
if ( constructor == nullptr ) {
|
||
return nullptr;
|
||
}
|
||
return (*constructor)();
|
||
}
|
||
static Validator *createObject( const QByteArray &className);
|
||
|
||
static QSharedPointer<Validator> getValidatorObject(const QString &shortname);
|
||
|
||
typedef Validator *(*Constructor)();
|
||
template<typename T>
|
||
static Validator *constructorHelper() {
|
||
return new T();
|
||
}
|
||
static QHash<QByteArray, Constructor> instance;
|
||
static QHash<QString, QSharedPointer<Validator>> validatorInstances;
|
||
|
||
protected:
|
||
ValidatorFactory();
|
src/validators/validatorrule.cpp | ||
---|---|---|
#include "validatorrule.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
ValidationRule::ValidationRule() {
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QString attributeName, QList<Param> params) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = QStringList(attributeName);
|
||
this->params = params;
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QString attributeName, Param param) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = QStringList(attributeName);
|
||
if (!param.getName().isEmpty()) {
|
||
this->params.append(param);
|
||
}
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QStringList attributeNames, QList<Param> params) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = attributeNames;
|
||
this->params = params;
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QStringList attributeNames, Param param) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = attributeNames;
|
||
if (!param.getName().isEmpty()) {
|
||
this->params.append(param);
|
||
}
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName, QString attributeName,
|
||
QString paramName, QString paramValue) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = QStringList(attributeName);
|
||
this->params.append(Param(paramName, paramValue));
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName, QString attributeName,
|
||
QString paramName1, QVariant paramValue1, QString paramName2,
|
||
QVariant paramValue2) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = QStringList(attributeName);
|
||
params.append(Param(paramName1, paramValue1));
|
||
params.append(Param(paramName2, paramValue2));
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QStringList attributeNames,
|
||
QString paramName, QVariant paramValue) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = attributeNames;
|
||
this->params.append(Param(paramName, paramValue));
|
||
}
|
||
|
||
ValidationRule::ValidationRule(QString validatorName,
|
||
QStringList attributeNames,
|
||
QString paramName1, QVariant paramValue1, QString paramName2,
|
||
QVariant paramValue2) {
|
||
this->validatorName = validatorName;
|
||
this->attributes = attributeNames;
|
||
params.append(Param(paramName1, paramValue1));
|
||
params.append(Param(paramName2, paramValue2));
|
||
}
|
||
|
||
ValidationRule::~ValidationRule() {
|
||
}
|
||
|
||
QString ValidationRule::getValidatorName() const {
|
||
return validatorName;
|
||
}
|
||
|
||
void ValidationRule::setValidatorName(const QString &value) {
|
||
validatorName = value;
|
||
}
|
||
QStringList ValidationRule::getAttributes() const {
|
||
return attributes;
|
||
}
|
||
|
||
void ValidationRule::setAttributes(const QStringList &value) {
|
||
attributes = value;
|
||
}
|
||
QList<Param> ValidationRule::getParams() const {
|
||
return params;
|
||
}
|
||
|
||
void ValidationRule::setParams(const QList<Param> &value) {
|
||
params = value;
|
||
}
|
||
|
||
|
||
|
||
|
src/validators/validatorrule.h | ||
---|---|---|
#ifndef VALIDATORATTRIBUTE_H
|
||
#define VALIDATORATTRIBUTE_H
|
||
#include <QString>
|
||
#include <QStringList>
|
||
#include "param.h"
|
||
namespace CuteEntityManager {
|
||
class ValidationRule {
|
||
public:
|
||
ValidationRule();
|
||
explicit ValidationRule(QString validatorName, QString attributeName,
|
||
QList<Param> params = QList<Param>());
|
||
explicit ValidationRule(QString validatorName, QString attributeName,
|
||
Param param);
|
||
explicit ValidationRule(QString validatorName, QStringList attributeNames,
|
||
QList<Param> params = QList<Param>());
|
||
explicit ValidationRule(QString validatorName, QStringList attributeNames,
|
||
Param param);
|
||
explicit ValidationRule(QString validatorName, QString attributeName,
|
||
QString paramName, QString paramValue);
|
||
explicit ValidationRule(QString validatorName, QString attributeName,
|
||
QString paramName1, QVariant paramValue1, QString paramName2,
|
||
QVariant paramValue2);
|
||
explicit ValidationRule(QString validatorName, QStringList attributeNames,
|
||
QString paramName, QVariant paramValue = QVariant());
|
||
explicit ValidationRule(QString validatorName, QStringList attributeNames,
|
||
QString paramName1, QVariant paramValue1, QString paramName2,
|
||
QVariant paramValue2);
|
||
|
||
~ValidationRule();
|
||
QString getValidatorName() const;
|
||
void setValidatorName(const QString &value);
|
||
|
||
QStringList getAttributes() const;
|
||
void setAttributes(const QStringList &value);
|
||
|
||
QList<Param> getParams() const;
|
||
void setParams(const QList<Param> &value);
|
||
|
||
private:
|
||
QString validatorName;
|
||
QStringList attributes;
|
||
QList<Param> params;
|
||
};
|
||
}
|
||
|
||
|
||
#endif // VALIDATORATTRIBUTE_H
|
Auch abrufbar als: Unified diff
validator stuff