Revision 9c2f773f
Von Christian Ehringfeld vor fast 10 Jahren hinzugefügt
example/main.cpp | ||
---|---|---|
#include <QMetaProperty>
|
||
#include "models/group.h"
|
||
#include "entity.h"
|
||
#include "entitymanager.h"
|
||
#include <QGenericReturnArgument>
|
||
/**
|
||
* create,remove und merge funktionieren
|
||
*/
|
||
|
||
int main(int argc, char *argv[]) {
|
||
// OpenTeacherTool::EntityManager *e = new OpenTeacherTool::EntityManager("QSQLITE",QDir::currentPath() + "/db.sqlite");
|
||
|
||
CuteEntityManager::EntityManager *e = new CuteEntityManager::EntityManager("QSQLITE",QDir::currentPath() + "/db.sqlite");
|
||
QSharedPointer<Artikel> a = QSharedPointer<Artikel>(new Artikel(20.0,"Müsli"));
|
||
auto ep = a.dynamicCast<CuteEntityManager::Entity>();
|
||
qDebug() << e;
|
||
qDebug() << "Tabelle artikel erstellt:" << e->createTable(ep);
|
||
// OpenTeacherTool::Artikel *b= new OpenTeacherTool::Artikel(30,"Peter123");
|
||
// OpenTeacherTool::Entity *entity = b->getEntity();
|
||
// qDebug() << "findByAttributes:" << e->findByAttributes(entity,true);
|
example/models/group.h | ||
---|---|---|
#include <QVariantList>
|
||
//#include <QQuickView>
|
||
#include "artikel.h"
|
||
#include <QSharedPointer>
|
||
|
||
|
||
|
src/entity.h | ||
---|---|---|
#include <QMap>
|
||
#include <QDebug>
|
||
#include <QObject>
|
||
#include "enums/databasetype.h"
|
||
#include "relation.h"
|
||
#include <QStringList>
|
||
namespace CuteEntityManager {
|
||
|
||
/**
|
||
* You should name any persisted property objectName, because its pre used by Qt and will be ignored by Entity Manager
|
||
* @brief The Entity class
|
||
*/
|
||
class Entity : public QObject {
|
||
Q_OBJECT
|
||
Q_PROPERTY(qint64 id READ getId WRITE setId NOTIFY idChanged)
|
src/entitymanager.cpp | ||
---|---|---|
return rc;
|
||
}
|
||
|
||
bool EntityManager::createTable(const QSharedPointer<Entity> &entity) {
|
||
return this->schema.data()->getQueryBuilder().data()->createTable(entity);
|
||
}
|
||
|
||
void EntityManager::setConnectionNames(QStringList list) {
|
||
EntityManager::connectionNames = list;
|
||
}
|
src/entitymanager.h | ||
---|---|---|
bool merge(QSharedPointer<Entity> &entity);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(QSharedPointer<Entity> &entity);
|
||
bool createTable(const QSharedPointer<Entity> &entity);
|
||
qint8 count(Entity *entity, bool ignoreID = true);
|
||
QSharedPointer<Database> getDb() const;
|
||
void setDb(const QSharedPointer<Database> &value);
|
src/querybuilder.cpp | ||
---|---|---|
|
||
using namespace CuteEntityManager;
|
||
|
||
//bool QueryBuilder::createTable(QString tablename, QHash<QString, QString> tableDefinition) {
|
||
//// QHash<QString, QString> Artikel::getProperties(DatabaseType type) {
|
||
//// QHash<QString, QString> h = QHash<QString, QString>();
|
||
//// h.insert("id",this->idColumnSQL());
|
||
//// h.insert("preis","DOUBLE");
|
||
//// h.insert("name","TEXT");
|
||
//// return h;
|
||
//// }
|
||
//}
|
||
|
||
QueryBuilder::QueryBuilder(QSharedPointer<Schema> schema, QSharedPointer<Database> database) {
|
||
this->schema = schema;
|
||
this->database = database;
|
||
... | ... | |
if (first) {
|
||
first = false;
|
||
} else {
|
||
s.append(',');
|
||
s.append(", ");
|
||
}
|
||
s.append(this->schema.data()->quoteColumnName(i.key())).append(" " + i.value());
|
||
s.append(this->schema.data()->quoteColumnName(i.key())).append(" " + this->getColumnType(i.value()));
|
||
++i;
|
||
}
|
||
s.append("\n);");
|
||
... | ... | |
for (int var = 0; var < o->propertyCount(); ++var) {
|
||
o->property(var);
|
||
auto m = o->property(var);
|
||
if (m.isReadable() && !entity.data()->getTransientAttributes().contains(m.name())) {
|
||
if (m.name() != QString("objectName") && m.isReadable()
|
||
&& !entity.data()->getTransientAttributes().contains(m.name())) {
|
||
if (m.isEnumType()) {
|
||
map.insert(m.name(), this->schema.data()->getTypeMap().data()->value(this->schema.data()->TYPE_INTEGER));
|
||
} else if (relations.contains(m.name())) {
|
||
... | ... | |
}
|
||
//cant believe that this could work in Qt
|
||
//https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php
|
||
QRegularExpression reg = QRegularExpression(QRegularExpression::escape("/^(\w+)\((.+?)\)(.*)$/"));
|
||
QRegularExpression reg = QRegularExpression(QRegularExpression::escape("/^(\\w+)\((.+?)\\)(.*)$/"));
|
||
reg.optimize();
|
||
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0, QRegularExpression::PartialPreferFirstMatch);
|
||
short s = 0;
|
||
... | ... | |
ok = true;
|
||
}
|
||
if (ok) {
|
||
return before.replace(QRegularExpression::escape("/\(.+\)/"), "(" + i.next().captured() + ")");
|
||
return before.replace(QRegularExpression::escape("/\\(.+\\)/"), "(" + i.next().captured() + ")");
|
||
}
|
||
s++;
|
||
}
|
||
reg = QRegularExpression(QRegularExpression::escape("/^(\w+)\s+/"));
|
||
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0, QRegularExpression::PartialPreferFirstMatch);
|
||
reg = QRegularExpression(QRegularExpression::escape("/^(\\w+)\\s+/"));
|
||
i = reg.globalMatch(type, 0, QRegularExpression::PartialPreferFirstMatch);
|
||
if (i.hasNext()) {
|
||
return before.replace(QRegularExpression::escape("/^w+/"), i.next().captured());
|
||
}
|
||
... | ... | |
for (int var = 0; var < metaObject->propertyCount(); ++var) {
|
||
auto p = metaObject->property(var);
|
||
QString name = QString(p.name());
|
||
if (p.isValid() && !transientAttrs.contains(name)) {
|
||
if (p.isValid() && !transientAttrs.contains(name) && name != QString("objectName")) {
|
||
QVariant v = p.read(e);
|
||
//Relation
|
||
if (v.canConvert<Entity *>()) {
|
src/schema.cpp | ||
---|---|---|
this->database = database;
|
||
this->abstractTypeMap = QSharedPointer<QHash<QString, QString>>(new QHash<QString, QString>());
|
||
this->queryBuilder = QSharedPointer<QueryBuilder>();
|
||
this->typeMap = QSharedPointer<QHash<QString, QString>>(new QHash<QString, QString>());
|
||
this->initAbstractDatabaseTypes();
|
||
}
|
||
|
||
... | ... | |
|
||
QString Schema::getRawTable(QString name) {
|
||
if (name.indexOf("{{")) {
|
||
QRegularExpression re(QRegularExpression::escape("/\{\{(.*?)\}\}/"));
|
||
return name.replace(re, QRegularExpression::escape("\1"));
|
||
QRegularExpression re(QRegularExpression::escape("/\\{\{(.*?)\\}\\}/"));
|
||
return name.replace(re, QRegularExpression::escape("\\1"));
|
||
}
|
||
return name;
|
||
}
|
Auch abrufbar als: Unified diff
creating tables without relations works!!!!!!!!!