Revision 3820ae33
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/cache.cpp | ||
|---|---|---|
|
return false;
|
||
|
}
|
||
|
|
||
|
void Cache::insert(QSharedPointer<Entity> entity) {
|
||
|
void Cache::insert(const QSharedPointer<Entity> &entity) {
|
||
|
if (entity.data() && entity.data()->getId() > -1) {
|
||
|
this->cache.insert(this->generateKey(entity.data()->getId(), QString(entity.data()->getClassname())),
|
||
|
entity.toWeakRef());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Cache::remove(QSharedPointer<Entity> entity) {
|
||
|
void Cache::remove(const QSharedPointer<Entity> &entity) {
|
||
|
if (entity.data() && entity.data()->getId() > -1) {
|
||
|
this->remove(entity.data()->getId(), QString(entity.data()->getClassname()));
|
||
|
}
|
||
| ... | ... | |
|
QSharedPointer<Entity> Cache::get(qint64 id, const QString &classname) {
|
||
|
QString key = this->generateKey(id, classname);
|
||
|
if (this->contains(key)) {
|
||
|
return this->cache.value(key).toStrongRef();
|
||
|
QSharedPointer<Entity> ptr = this->cache.value(key).toStrongRef();
|
||
|
if (!ptr.data()) {
|
||
|
this->remove(id, classname);
|
||
|
}
|
||
|
return ptr;
|
||
|
}
|
||
|
return QSharedPointer<Entity>();
|
||
|
}
|
||
| src/cache.h | ||
|---|---|---|
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
void insert(QSharedPointer<Entity> entity);
|
||
|
void remove(QSharedPointer<Entity> entity);
|
||
|
void insert(const QSharedPointer<Entity> &entity);
|
||
|
void remove(const QSharedPointer<Entity> &entity);
|
||
|
void remove(const qint64 &id, const QString &classname);
|
||
|
template<class T> void remove(qint64 id) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
| src/database.cpp | ||
|---|---|---|
|
return true;
|
||
|
}
|
||
|
|
||
|
DatabaseType Database::getDatabaseType(QString s) {
|
||
|
if (s == "qmysql") {
|
||
|
return DatabaseType::MYSQL;
|
||
|
} else if (s == "qpgsql") {
|
||
|
return DatabaseType::PGSQL;
|
||
|
} else {
|
||
|
return DatabaseType::SQLITE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Schema> Database::getSchema(int db, QSharedPointer<Database> database) {
|
||
|
switch (db) {
|
||
|
case SQLITE:
|
||
|
return QSharedPointer<Schema>(new SqliteSchema(database));;
|
||
|
break;
|
||
|
// case PGSQL:
|
||
|
// return QSharedPointer<Schema>(new PgSqlSchema());
|
||
|
// break;
|
||
|
// case MYSQL:
|
||
|
// return QSharedPointer<Schema>(new MysqlSchema());
|
||
|
// break;
|
||
|
default:
|
||
|
return QSharedPointer<Schema>(new SqliteSchema(database));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool Database::exec(const QString &query) {
|
||
|
QSqlQuery q = QSqlQuery(this->database);
|
||
|
bool ok = q.exec(query);
|
||
| src/database.h | ||
|---|---|---|
|
QSqlQuery select(const QString &query);
|
||
|
void startTransaction();
|
||
|
bool commitTransaction();
|
||
|
static DatabaseType getDatabaseType(QString s);
|
||
|
static QSharedPointer<Schema> getSchema(int db, QSharedPointer<Database> database);
|
||
|
|
||
|
};
|
||
|
}
|
||
| src/entitymanager.cpp | ||
|---|---|---|
|
QStringList EntityManager::connectionNames = QStringList();
|
||
|
|
||
|
void EntityManager::init() {
|
||
|
auto schema = CuteEntityManager::getSchema(CuteEntityManager::getDatabaseType(
|
||
|
auto schema = Database::getSchema(Database::getDatabaseType(
|
||
|
this->db.data()->getDatabase().driverName()), this->db);
|
||
|
this->schema = QSharedPointer<Schema>(schema);
|
||
|
this->schema.data()->setTables(this->schema.data()->getTableSchemas());
|
||
| ... | ... | |
|
return this->convert(maps, entity.data()->getClassname());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @todo should be an insert statement with much values
|
||
|
* not really usefull atm
|
||
|
* @brief EntityManager::create
|
||
|
* @param entities
|
||
|
* @return
|
||
|
*/
|
||
|
bool EntityManager::create(QList<QSharedPointer<Entity> > entities) {
|
||
|
|
||
|
bool ok = true;
|
||
|
foreach (QSharedPointer<Entity> ent, entities) {
|
||
|
ok = this->create(ent);
|
||
|
if (!ok) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
/**
|
||
| ... | ... | |
|
rc = this->db->transaction(q);
|
||
|
if (rc) {
|
||
|
entity.data()->setId(this->schema.data()->getLastInsertID().toLongLong(&rc));
|
||
|
this->cache.insert(entity);
|
||
|
}
|
||
|
}
|
||
|
return rc;
|
||
| src/entitymanager.h | ||
|---|---|---|
|
#include "entity.h"
|
||
|
#include "database.h"
|
||
|
#include "entityinstancefactory.h"
|
||
|
#include "cache.h"
|
||
|
|
||
|
namespace CuteEntityManager {
|
||
|
|
||
| ... | ... | |
|
QSharedPointer<Schema> schema;
|
||
|
static void setConnectionNames(QStringList list);
|
||
|
QSharedPointer<Database> db;
|
||
|
Cache cache;
|
||
|
QString createConnection();
|
||
|
QString createTableQuery(const QSharedPointer<Entity> &entity);
|
||
|
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
|
||
| src/enums/databasetype.h | ||
|---|---|---|
|
#include "../schema/sqliteschema.h"
|
||
|
|
||
|
namespace CuteEntityManager {
|
||
|
class Database;
|
||
|
enum DatabaseType {
|
||
|
SQLITE = 0,
|
||
|
PGSQL = 1,
|
||
|
MYSQL = 2
|
||
|
};
|
||
|
|
||
|
static DatabaseType getDatabaseType(QString s) {
|
||
|
if (s == "qmysql") {
|
||
|
return DatabaseType::MYSQL;
|
||
|
} else if (s == "qpgsql") {
|
||
|
return DatabaseType::PGSQL;
|
||
|
} else {
|
||
|
return DatabaseType::SQLITE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static QSharedPointer<Schema> getSchema(int db, QSharedPointer<Database> database) {
|
||
|
switch (db) {
|
||
|
case SQLITE:
|
||
|
return QSharedPointer<Schema>(new SqliteSchema(database));;
|
||
|
break;
|
||
|
// case PGSQL:
|
||
|
// return QSharedPointer<Schema>(new PgSqlSchema());
|
||
|
// break;
|
||
|
// case MYSQL:
|
||
|
// return QSharedPointer<Schema>(new MysqlSchema());
|
||
|
// break;
|
||
|
default:
|
||
|
return QSharedPointer<Schema>(new SqliteSchema(database));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
#endif // DATABASETYPE_H
|
||
Auch abrufbar als: Unified diff
...