Revision 696666eb
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
example/main.cpp | ||
---|---|---|
using namespace CuteEntityManager;
|
||
int main(int argc, char *argv[]) {
|
||
// Q_UNUSED(argc) Q_UNUSED(argv)
|
||
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);
|
||
// e->create(ep);
|
||
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);
|
||
e->create(ep);
|
||
|
||
// QSharedPointer<CuteEntityManager::Entity> p = QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann", Person::MALE, "", "", "",
|
||
// QDate::currentDate()));
|
src/entitymanager.cpp | ||
---|---|---|
}
|
||
|
||
bool EntityManager::create(QSharedPointer<Entity> &entity,
|
||
const bool persistRelations) {
|
||
const bool persistRelations, const bool checkDuplicate) {
|
||
bool rc = false;
|
||
if (this->checkTable(entity) && this->count(entity) == 0) {
|
||
if (this->checkTable(entity) && !(checkDuplicate && this->count(entity) == 0)) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->create(entity);
|
||
rc = this->db->transaction(q);
|
||
if (rc) {
|
src/entitymanager.h | ||
---|---|---|
bool ignoreID = false);
|
||
bool create(QList<QSharedPointer<Entity>> entities,
|
||
const bool persistRelations = true);
|
||
bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true);
|
||
bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
||
const bool checkDuplicate = false);
|
||
bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
src/schema.cpp | ||
---|---|---|
#include "database.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
Schema::Schema(QSharedPointer<Database> database) {
|
||
Schema::Schema(QSharedPointer<Database> database, QSharedPointer<QueryBuilder> builder)
|
||
{
|
||
this->database = database;
|
||
this->abstractTypeMap = QSharedPointer<QHash<QString, QString>>
|
||
(new QHash<QString, QString>());
|
||
this->queryBuilder = QSharedPointer<QueryBuilder>();
|
||
this->queryBuilder = builder;
|
||
this->typeMap = QSharedPointer<QHash<QString, QString>>(new
|
||
QHash<QString, QString>());
|
||
this->initAbstractDatabaseTypes();
|
||
... | ... | |
}
|
||
|
||
void Schema::initAbstractDatabaseTypes() {
|
||
this->abstractTypeMap.create();
|
||
this->abstractTypeMap = QSharedPointer<QHash<QString, QString>>::create();
|
||
this->abstractTypeMap.data()->insert("bool", TYPE_SMALLINT);
|
||
this->abstractTypeMap.data()->insert("short", TYPE_SMALLINT);
|
||
this->abstractTypeMap.data()->insert("int", TYPE_INTEGER);
|
src/schema.h | ||
---|---|---|
class Database;
|
||
class Schema {
|
||
public:
|
||
Schema(QSharedPointer<Database> database);
|
||
Schema(QSharedPointer<Database> database, QSharedPointer<QueryBuilder> builder);
|
||
virtual ~Schema();
|
||
//http://doc.qt.io/qt-5/sql-types.html
|
||
const QString TYPE_PK = "pk";
|
||
... | ... | |
virtual void findConstraints(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual QSharedPointer<TableSchema> loadTableSchema(QString name) = 0;
|
||
void initAbstractDatabaseTypes();
|
||
virtual void initAbstractDatabaseTypes();
|
||
QSharedPointer<Database> database;
|
||
QSharedPointer<QHash<QString, QString>> typeMap;
|
||
QSharedPointer<QHash<QString, QString>> abstractTypeMap;
|
src/schema/sqliteschema.cpp | ||
---|---|---|
using namespace CuteEntityManager;
|
||
|
||
SqliteSchema::SqliteSchema(QSharedPointer<Database> database) : Schema(
|
||
database) {
|
||
this->queryBuilder = QSharedPointer<QueryBuilder>(new SqliteQueryBuilder(
|
||
QSharedPointer<Schema>
|
||
(this), database));
|
||
database, QSharedPointer<QueryBuilder>(new SqliteQueryBuilder(
|
||
QSharedPointer<Schema>
|
||
(this), database))) {
|
||
|
||
}
|
||
|
||
SqliteSchema::~SqliteSchema() {
|
Auch abrufbar als: Unified diff
small improvements