Revision 28d2f01a
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
samples/example/main.cpp | ||
---|---|---|
// QDir::currentPath() + "/db.sqlite", "", "", "", 0, true);
|
||
CuteEntityManager::EntityManager("QSQLITE",
|
||
":memory:", "", "", "", "", true, "foreign_keys = ON");
|
||
qDebug() << "EntityManagerObjectName:" << e->objectName();
|
||
SqliteBackupProcessor *sqliteproc = new SqliteBackupProcessor(e->getDb(),
|
||
QDir::currentPath());
|
||
qWarning() << "DB Loaded:" << sqliteproc->sqliteDBMemFile(false, "db.sqlite");
|
src/entitymanager.cpp | ||
---|---|---|
|
||
QStringList EntityManager::connectionNames = QStringList();
|
||
|
||
QHash<QString, EntityManager *> EntityManager::instances =
|
||
QHash<QString, EntityManager *>();
|
||
|
||
QStringList EntityManager::getConnectionNames() {
|
||
return EntityManager::connectionNames;
|
||
}
|
||
... | ... | |
this->schema->setTables(this->schema->getTableSchemas());
|
||
this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
||
this->schema->getQueryBuilder()));
|
||
this->appendToInstanceList();
|
||
}
|
||
|
||
EntityManager::~EntityManager() {
|
||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
EntityManager::instances.remove(this->objectName());
|
||
}
|
||
|
||
bool EntityManager::startup(QString version, QStringList toInitialize,
|
||
... | ... | |
return propertyIsValid;
|
||
}
|
||
|
||
QString EntityManager::generateObjectName() {
|
||
int i = 0;
|
||
QString name = "em[";
|
||
while (true) {
|
||
if (!EntityManager::instances.contains(name + QString::number(i) + "]")) {
|
||
name += QString::number(i) + "]";
|
||
break;
|
||
}
|
||
}
|
||
return name;
|
||
}
|
||
|
||
void EntityManager::appendToInstanceList() {
|
||
this->setObjectName(this->generateObjectName());
|
||
EntityManager::instances.insert(this->objectName(), this);
|
||
}
|
||
QHash<QString, EntityManager *> EntityManager::getInstances() {
|
||
return instances;
|
||
}
|
||
|
||
EntityManager *EntityManager::getDefaultInstance() {
|
||
for (auto i = EntityManager::instances.constBegin();
|
||
i != EntityManager::instances.constEnd(); ++i) {
|
||
return i.value();
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
EntityManager *EntityManager::getInstance(QString name) {
|
||
if (EntityManager::instances.contains(name)) {
|
||
return EntityManager::instances.value(name);
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
|
||
bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
|
||
const Relation &r) {
|
||
return entity && (r.getCascadeType().contains(CascadeType::ALL)
|
src/entitymanager.h | ||
---|---|---|
bool startup(QString version, QStringList toInitialize,
|
||
bool createIndices = false);
|
||
bool executeQuery(const QString &query);
|
||
static void removeConnectionName(const QString &name);
|
||
QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
|
||
QList<QSharedPointer<Entity>> findEntityByAttributes(const
|
||
QSharedPointer<Entity> &entity,
|
||
... | ... | |
const bool persistRelations = true, const bool validate = true);
|
||
bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool checkDuplicate = false, const bool validate = true);
|
||
bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true, const bool ignoreHasChanged=true);
|
||
bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool ignoreHasChanged = true);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true,
|
||
const bool validate = true);
|
||
... | ... | |
QString username = "",
|
||
QString password = "", QString port = "", bool logQueries = false,
|
||
QString databaseOptions = "");
|
||
~EntityManager();
|
||
virtual ~EntityManager();
|
||
static QStringList getConnectionNames();
|
||
static void removeConnectionName(const QString &name);
|
||
static QHash<QString, EntityManager *> getInstances();
|
||
static EntityManager *getDefaultInstance();
|
||
static EntityManager *getInstance(QString name);
|
||
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
||
|
||
template<class T> QList<QSharedPointer<T>> find(Query &q,
|
||
... | ... | |
const QSharedPointer<Entity> &e, const Relation &r);
|
||
bool isRelationPropertyValid(const QMetaProperty &prop, const Relation &r,
|
||
const QSharedPointer<Entity> &e, const QSharedPointer<Entity> &relatedEntity);
|
||
QString generateObjectName();
|
||
void appendToInstanceList();
|
||
|
||
private:
|
||
static QStringList connectionNames;
|
||
static QHash<QString, EntityManager *> instances;
|
||
QString id;
|
||
QSharedPointer<Schema> schema;
|
||
static void setConnectionNames(QStringList list);
|
||
QSharedPointer<Database> db;
|
src/query.cpp | ||
---|---|---|
this->having = having;
|
||
}
|
||
|
||
Query::Query(QString from, Expression where, Join join,
|
||
QHash<QString, QVariant> params, quint64 limit, quint64 offset,
|
||
Expression select, QString groupBy, bool distinct, QList<Expression> having) {
|
||
this->from.append(from);
|
||
this->where.append(where);
|
||
this->joins.append(join);
|
||
this->params = params;
|
||
this->limit = limit;
|
||
this->offset = offset;
|
||
this->select.append(select);
|
||
this->groupBy.append(groupBy);
|
||
this->distinct = distinct;
|
||
this->having = having;
|
||
|
||
}
|
||
|
||
void Query::appendWhere(const QString &condition) {
|
||
this->where.append(Expression(condition));
|
||
}
|
src/query.h | ||
---|---|---|
public:
|
||
Query();
|
||
~Query();
|
||
Query(QStringList from, QList<Expression> where = QList<Expression>(),
|
||
QList<Join> joins = QList<Join>(),
|
||
QHash<QString, QVariant> params = QHash<QString, QVariant>(), quint64 limit = 0,
|
||
quint64 offset = 0,
|
||
QList<Expression> select = QList<Expression>(),
|
||
QStringList groupBy = QStringList(), bool distinct = false,
|
||
QList<Expression> having = QList<Expression>());
|
||
explicit Query(QStringList from, QList<Expression> where = QList<Expression>(),
|
||
QList<Join> joins = QList<Join>(),
|
||
QHash<QString, QVariant> params = QHash<QString, QVariant>(), quint64 limit = 0,
|
||
quint64 offset = 0,
|
||
QList<Expression> select = QList<Expression>(),
|
||
QStringList groupBy = QStringList(), bool distinct = false,
|
||
QList<Expression> having = QList<Expression>());
|
||
|
||
explicit Query(QString from, Expression where = Expression(),
|
||
Join join = Join(),
|
||
QHash<QString, QVariant> params = QHash<QString, QVariant>(), quint64 limit = 0,
|
||
quint64 offset = 0,
|
||
Expression select = Expression(),
|
||
QString groupBy = "", bool distinct = false,
|
||
QList<Expression> having = QList<Expression>());
|
||
|
||
QString getSelectOption() const;
|
||
void setSelectOption(const QString &value);
|
src/validators/comparevalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
CompareValidator();
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
QString getDefaultOperator() const;
|
||
void setDefaultOperator(const QString &value);
|
||
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
QString defaultOperator = QStringLiteral("==");
|
||
};
|
||
}
|
src/validators/datevalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
DateValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/defaultvalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
DefaultValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/emailvalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
EmailValidator();
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
QString getPattern() const;
|
||
void setPattern(const QString &value);
|
||
|
||
... | ... | |
void setFullPattern(const QString &value);
|
||
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
QString pattern =
|
||
QStringLiteral("/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\\\\.[a-zA-Z0-9!#$%&\'*+\\\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/");
|
||
QString fullPattern =
|
src/validators/existvalidator.cpp | ||
---|---|---|
#include "existvalidator.h"
|
||
#include "../entity.h"
|
||
#include "../entityinstancefactory.h"
|
||
using namespace CuteEntityManager;
|
||
ExistValidator::ExistValidator() : Validator() {
|
||
}
|
||
|
||
ErrorMsg ExistValidator::validateParam(QVariant value, Param param) const {
|
||
|
||
Q_UNUSED(param)
|
||
QSharedPointer<Entity> entity = EntityInstanceFactory::castQVariant(value);
|
||
if (!entity || entity->getId() <= -1) {
|
||
return ErrorMsg("", "<property> is invalid.");
|
||
}
|
||
return ErrorMsg();
|
||
}
|
||
|
src/validators/existvalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
ExistValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/filevalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
FileValidator();
|
||
protected:
|
||
virtual ErrorMsg validateParam(QVariant value, Param param) const override;
|
||
virtual ErrorMsg validateMIMEType(const QFileInfo &file,
|
||
const Param ¶m) const;
|
src/validators/imagevalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
ImageValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
virtual ErrorMsg validateImage(QVariant value, Param param) const;
|
||
};
|
src/validators/numbervalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
NumberValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/patternvalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
PatternValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/requiredvalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
RequiredValidator();
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
src/validators/uniquevalidator.cpp | ||
---|---|---|
#include "uniquevalidator.h"
|
||
|
||
#include "src/entitymanager.h"
|
||
using namespace CuteEntityManager;
|
||
UniqueValidator::UniqueValidator() {
|
||
UniqueValidator::UniqueValidator() : Validator() {
|
||
}
|
||
|
||
QList<ErrorMsg> UniqueValidator::validate(QVariant value,
|
||
QList<Param> params) const {
|
||
QList<ErrorMsg> msgs = QList<ErrorMsg>();
|
||
if (!params.isEmpty()) {
|
||
QString targetAttribute = "";
|
||
QString targetClass = "";
|
||
QString entityManagerID = "";
|
||
for (int i = 0; i < params.size(); ++i) {
|
||
if (params.at(i).getName() == "targetAttribute") {
|
||
targetAttribute = params.at(i).getValue().toString();
|
||
} else if (params.at(i).getName() == "entityManagerID") {
|
||
entityManagerID = params.at(i).getValue().toString();
|
||
} else if (params.at(i).getName() == "targetClass") {
|
||
targetClass = params.at(i).getValue().toString();
|
||
}
|
||
}
|
||
EntityManager *em = nullptr;
|
||
if (entityManagerID.isEmpty()) {
|
||
em = EntityManager::getDefaultInstance();
|
||
} else {
|
||
em = EntityManager::getInstance(entityManagerID);
|
||
}
|
||
if (!em) {
|
||
msgs.append(ErrorMsg("", "No entitymanager."));
|
||
} else {
|
||
Query q = Query();
|
||
auto e = EntityInstanceFactory::createInstance(
|
||
targetClass.toLatin1().constData());
|
||
q.appendFrom(e->getTablename());
|
||
delete e;
|
||
e = nullptr;
|
||
QHash<QString, QVariant> params = QHash<QString, QVariant>();
|
||
params.insert(targetAttribute, value);
|
||
q.appendWhere(Expression(targetAttribute, params));
|
||
if (em->count(q) > 0) {
|
||
msgs.append(ErrorMsg("",
|
||
"<property> \"" + value.toString() + "\" has already been taken."));
|
||
}
|
||
}
|
||
} else {
|
||
msgs.append(ErrorMsg("", "UniqueValidator has not enough parameters."));
|
||
}
|
||
return msgs;
|
||
}
|
||
|
||
ErrorMsg UniqueValidator::validateParam(QVariant value, Param param) const {
|
||
|
||
Q_UNUSED(value)
|
||
Q_UNUSED(param)
|
||
return ErrorMsg();
|
||
}
|
src/validators/uniquevalidator.h | ||
---|---|---|
Q_OBJECT
|
||
public:
|
||
UniqueValidator();
|
||
virtual QList<ErrorMsg> validate(QVariant value, QList<Param> params) const override;
|
||
protected:
|
||
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
};
|
||
}
|
Auch abrufbar als: Unified diff
validators