Revision 9cf4747e
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/database.cpp | ||
|---|---|---|
|
*/
|
||
|
|
||
|
#include "database.h"
|
||
|
namespace CuteEntityManager {
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
Database::Database(QSqlDatabase database) {
|
||
|
this->database = database;
|
||
| ... | ... | |
|
this->database.open();
|
||
|
this->databasetype = this->getDatabaseType();
|
||
|
this->supportTransactions = this->database.driver()->hasFeature(QSqlDriver::Transactions);
|
||
|
this->tableList = new QStringList();
|
||
|
this->tableList = QStringList();
|
||
|
this->getTableListFromDatabase();
|
||
|
this->createSequenceTable();
|
||
|
}
|
||
|
|
||
|
|
||
| ... | ... | |
|
void Database::getTableListFromDatabase() {
|
||
|
if (this->database.open()) {
|
||
|
QString q = "";
|
||
|
if (this->databasetype == CuteEntityManager::SQLITE) {
|
||
|
q = this->sqliteTableList();
|
||
|
} else if (this->databasetype == CuteEntityManager::MYSQL) {
|
||
|
q = this->mysqlTableList();
|
||
|
} else if (this->databasetype == CuteEntityManager::PGSQL) {
|
||
|
q = this->pgsqlSeqTable();
|
||
|
}
|
||
|
QSqlQuery query = QSqlQuery(this->database);
|
||
|
query.prepare(q);
|
||
|
this->select(query);
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
void Database::setTableList(QSqlQuery &q) {
|
||
|
while (q.next()) {
|
||
|
this->tableList->append(q.value(0).toString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//QString Database::mysqlTableList() {
|
||
|
// return "SHOW TABLES;";
|
||
|
//}
|
||
| ... | ... | |
|
return this->connectionName;
|
||
|
}
|
||
|
|
||
|
void Database::setSeqTable(bool seqTable) {
|
||
|
this->seqTable = seqTable;
|
||
|
}
|
||
|
|
||
|
bool Database::isSeqTable() {
|
||
|
return this->seqTable;
|
||
|
}
|
||
|
|
||
|
//QString Database::pgsqlSeqTable() {
|
||
|
// return "CREATE TABLE IF NOT EXISTS sequence (SEQ_NAME varchar(255) NOT NULL UNIQUE , SEQ_COUNT bigint NOT NULL);";
|
||
|
//}
|
||
| ... | ... | |
|
return this->tableList->contains(tblname);
|
||
|
}
|
||
|
|
||
|
void Database::createSequenceTable() {
|
||
|
if (this->database.open() && this->getLastId() == -1) {
|
||
|
QString query = "";
|
||
|
QStringList l = QStringList();
|
||
|
if (this->databasetype == CuteEntityManager::MYSQL) {
|
||
|
query = this->mysqlSeqTable();
|
||
|
} else if (this->databasetype == CuteEntityManager::SQLITE) {
|
||
|
query = this->sqliteSeqTable();
|
||
|
} else if (this->databasetype == CuteEntityManager::PGSQL) {
|
||
|
query = this->pgsqlSeqTable();
|
||
|
}
|
||
|
l.append(query);
|
||
|
l.append(this->querySequenceCounter());
|
||
|
if (this->transaction(l)) {
|
||
|
this->setSeqTable(true);
|
||
|
} else {
|
||
|
this->setSeqTable(false);
|
||
|
}
|
||
|
} else {
|
||
|
this->setSeqTable(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool Database::updateSequenceCounter(QSqlQuery &q) {
|
||
|
QList<QSqlQuery> l = QList<QSqlQuery>();
|
||
|
l.append(QSqlQuery("UPDATE sequence SET SEQ_COUNT=(SEQ_COUNT+1);", this->database));
|
||
| ... | ... | |
|
QSqlDatabase Database::getDatabase() {
|
||
|
return this->database;
|
||
|
}
|
||
|
|
||
|
}
|
||
| src/database.h | ||
|---|---|---|
|
bool seqTable;
|
||
|
DatabaseType databasetype;
|
||
|
bool supportTransactions;
|
||
|
void setSeqTable(bool seqTable);
|
||
|
void init();
|
||
|
void createSequenceTable();
|
||
|
QString querySequenceCounter();
|
||
|
QStringList *tableList;
|
||
|
QStringList tableList;
|
||
|
|
||
|
protected:
|
||
|
// inline QString pgsqlSeqTable();
|
||
| ... | ... | |
|
void setTableList(QSqlQuery &q);
|
||
|
void refreshTableList();
|
||
|
bool containsTable(QString tblname);
|
||
|
bool updateSequenceCounter(QSqlQuery &q);
|
||
|
DatabaseType getDatabaseType();
|
||
|
QChar escapeChar();
|
||
|
};
|
||
| src/entity.cpp | ||
|---|---|---|
|
|
||
|
#include "entity.h"
|
||
|
|
||
|
namespace CuteEntityManager {
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
Entity::Entity() : QObject() {
|
||
|
this->id = -1;
|
||
| ... | ... | |
|
QString Entity::getTablename() {
|
||
|
return QString(this->metaObject()->className());
|
||
|
}
|
||
|
}
|
||
| src/schema.cpp | ||
|---|---|---|
|
#include "schema.h"
|
||
|
#include <QRegularExpression>
|
||
|
#include <QSqlRecord>
|
||
|
#include <QSqlQuery>
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
Schema::Schema() {
|
||
|
Schema::Schema(std::shared_ptr<Database> database) {
|
||
|
this->database = database;
|
||
|
this->typeMap = QSharedPointer<QHash<QString, QString>>(new QHash<QString, QString>());
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
QString Schema::quoteTableName(QString name) {
|
||
|
|
||
|
if (name.indexOf("(") || name.indexOf("{{")) {
|
||
|
return name;
|
||
|
}
|
||
|
if (name.indexOf(".") == -1) {
|
||
|
return this->quoteSimpleTableName(name);
|
||
|
}
|
||
|
QStringList parts = name.split(".");
|
||
|
for (int i = 0; i < parts.size(); ++i) {
|
||
|
parts.replace(i, this->quoteSimpleTableName(parts.at(i)));
|
||
|
}
|
||
|
return parts.join(".");
|
||
|
}
|
||
|
|
||
|
QString Schema::quoteColumnName(QString name) {
|
||
|
|
||
|
if (name.indexOf("(") || name.indexOf("[[") || name.indexOf("{{")) {
|
||
|
return name;
|
||
|
}
|
||
|
int pos = name.indexOf(".");
|
||
|
QString prefix = "";
|
||
|
if (pos) {
|
||
|
prefix = this->quoteTableName(name.mid(0, pos)) + ".";
|
||
|
name = name.mid(pos + 1);
|
||
|
}
|
||
|
return prefix + this->quoteSimpleColumnName(name);
|
||
|
}
|
||
|
|
||
|
QString Schema::quoteSimpleColumnName(QString name) {
|
||
|
return name.indexOf("`") || name == "*" ? name : "`" + name + "`";
|
||
|
}
|
||
|
|
||
|
QList<TableSchema> Schema::getTableSchemas(QString schema) {
|
||
|
|
||
|
QHash<QString, QSharedPointer<TableSchema> > Schema::getTableSchemas(QString schema, bool refresh) {
|
||
|
QStringList names = this->getTableNames();
|
||
|
for (int i = 0; i < names.size(); ++i) {
|
||
|
QString name;
|
||
|
if (schema != "") {
|
||
|
name = schema + "." + names.at(i);
|
||
|
}
|
||
|
TableSchema *t = this->getTableSchema(name, refresh);
|
||
|
if (t) {
|
||
|
this->tables.insert(name, QSharedPointer<TableSchema>(t));
|
||
|
}
|
||
|
}
|
||
|
return this->tables;
|
||
|
}
|
||
|
|
||
|
QStringList Schema::getTableNames(QString schema) {
|
||
|
//7QStringList QSqlDriver::tables(QSql::TableType tableType) const
|
||
|
return this->database.get()->getDatabase().tables();
|
||
|
}
|
||
|
|
||
|
QStringList Schema::findUniqueIndexes(TableSchema schema) {
|
||
|
|
||
|
}
|
||
|
|
||
|
QString Schema::getLastInsertID(QString sequenceName) {
|
||
|
|
||
|
QVariant Schema::getLastInsertID() {
|
||
|
QSqlQuery q(this->database.get()->getDatabase());
|
||
|
return q.lastInsertId();
|
||
|
}
|
||
|
|
||
|
void Schema::refresh() {
|
||
|
|
||
|
this->tables.clear();
|
||
|
}
|
||
|
|
||
|
QString Schema::getRawTable(QString name) {
|
||
|
|
||
|
}
|
||
|
|
||
|
QStringList Schema::findTableNames(QString schema) {
|
||
|
|
||
|
}
|
||
|
|
||
|
QStringList Schema::findUniqueIndexes(QString tableName) {
|
||
|
|
||
|
}
|
||
|
|
||
|
TableSchema Schema::findConstraints(TableSchema ts) {
|
||
|
}
|
||
|
|
||
|
QString Schema::getCreateTableSql(TableSchema ts) {
|
||
|
|
||
|
}
|
||
|
|
||
|
bool Schema::findColumns(TableSchema ts) {
|
||
|
|
||
|
}
|
||
|
|
||
|
TableSchema Schema::getTableSchema(QString name, bool refresh) {
|
||
|
|
||
|
if (name.indexOf("{{")) {
|
||
|
QRegularExpression re("/\\{\\{(.*?)\\}\\}/");
|
||
|
QRegularExpression re2("\1");
|
||
|
return name.replace(re, re2);
|
||
|
}
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
TableSchema *Schema::getTableSchema(QString name, bool refresh) {
|
||
|
if (refresh) {
|
||
|
this->refresh();
|
||
|
}
|
||
|
if (this->tables.contains(name)) {
|
||
|
return this->tables.value(name);
|
||
|
}
|
||
|
QString realName = this->getRawTable(name);
|
||
|
QSharedPointer<TableSchema> ts = this->loadTableSchema(realName);
|
||
|
if (ts.data()) {
|
||
|
this->tables.insert(name, ts);
|
||
|
}
|
||
|
return ts.data();
|
||
|
|
||
|
}
|
||
|
QHash<QString, QSharedPointer<TableSchema> > Schema::getTables() const {
|
||
|
return this->tables;
|
||
|
}
|
||
|
|
||
|
void Schema::setTables(const QHash<QString, QSharedPointer<TableSchema> > &value) {
|
||
|
tables = value;
|
||
|
}
|
||
|
|
||
| src/schema.h | ||
|---|---|---|
|
#ifndef SCHEMA_H
|
||
|
#define SCHEMA_H
|
||
|
#include <QString>
|
||
|
#include "tableschema.h"
|
||
|
#include <QStringList>
|
||
|
#include <QHash>
|
||
|
#include <QSharedPointer>
|
||
|
#include <QSqlDatabase>
|
||
|
#include <memory>
|
||
|
#include "database.h"
|
||
|
|
||
|
namespace CuteEntityManager {
|
||
|
|
||
|
class Schema {
|
||
|
public:
|
||
|
Schema();
|
||
|
Schema(std::shared_ptr<Database> database);
|
||
|
virtual ~Schema();
|
||
|
const QString TYPE_PK = "pk";
|
||
|
const QString TYPE_BIGPK = "bigpk";
|
||
| ... | ... | |
|
virtual QString quoteTableName(QString name);
|
||
|
virtual QString quoteColumnName(QString name);
|
||
|
virtual QString quoteSimpleColumnName(QString name);
|
||
|
virtual QList<TableSchema> getTableSchemas(QString schema = "");
|
||
|
virtual QHash<QString, QSharedPointer<TableSchema>> getTableSchemas(QString schema = "", bool refresh = false);
|
||
|
virtual QStringList getTableNames(QString schema = "");
|
||
|
//virtual QueryBuilder getQueryBuilder();
|
||
|
//virtual QueryBuilder createQueryBuilder();
|
||
|
virtual QStringList findUniqueIndexes(TableSchema schema);
|
||
|
virtual QString getLastInsertID(QString sequenceName = "");
|
||
|
virtual QVariant getLastInsertID();
|
||
|
virtual void refresh();
|
||
|
virtual QString getRawTable(QString name);
|
||
|
|
||
|
|
||
|
QHash<QString, QSharedPointer<TableSchema> > getTables() const;
|
||
|
void setTables(const QHash<QString, QSharedPointer<TableSchema> > &value);
|
||
|
|
||
|
protected:
|
||
|
virtual QStringList findTableNames(QString schema = "");
|
||
|
virtual QStringList findUniqueIndexes(QString tableName);
|
||
|
virtual TableSchema findConstraints(TableSchema ts);
|
||
|
virtual QString getCreateTableSql(TableSchema ts);
|
||
|
virtual bool findColumns(TableSchema ts);
|
||
|
virtual QStringList findTableNames(QString schema = "") = 0;
|
||
|
virtual QStringList findUniqueIndexes(QString tableName) = 0;
|
||
|
virtual QSharedPointer<TableSchema> findConstraints(TableSchema ts) = 0;
|
||
|
virtual QString getCreateTableSql(TableSchema ts) = 0;
|
||
|
virtual bool findColumns(TableSchema ts) = 0;
|
||
|
QSharedPointer<QHash<QString, QString>> typeMap;
|
||
|
virtual TableSchema loadTableSchema(QString name) = 0;
|
||
|
virtual TableSchema getTableSchema(QString name, bool refresh = false);
|
||
|
virtual QStringList findTableNames(QString schema = "");
|
||
|
virtual QSharedPointer<TableSchema> *loadTableSchema(QString name) = 0;
|
||
|
virtual TableSchema *getTableSchema(QString name, bool refresh = false);
|
||
|
std::shared_ptr<Database> database;
|
||
|
QHash<QString, QSharedPointer<TableSchema>> tables;
|
||
|
|
||
|
|
||
|
};
|
||
|
}
|
||
| src/schema/mysqlschema.cpp | ||
|---|---|---|
|
#include "mysqlschema.h"
|
||
|
#include <QSqlQuery>
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
MysqlSchema::MysqlSchema() : Schema() {
|
||
|
MysqlSchema::MysqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
|
||
|
}
|
||
|
|
||
|
QHash<QString, QString>* MysqlSchema::getTypeMap() {
|
||
|
if(this->typeMap.data()->empty()) {
|
||
|
QHash<QString, QString> *MysqlSchema::getTypeMap() {
|
||
|
if (this->typeMap.data()->empty()) {
|
||
|
// this->typeMap->data()->insert(TYPE_SMALLINT, 'tinyint');
|
||
|
// this->typeMap->data()->insert(TYPE_SMALLINT, 'bit');
|
||
|
// this->typeMap->data()->insert(TYPE_BOOLEAN, 'boolean');
|
||
| src/schema/mysqlschema.h | ||
|---|---|---|
|
#ifndef MYSQLSCHEMA_H
|
||
|
#define MYSQLSCHEMA_H
|
||
|
#include <memory>
|
||
|
#include "../database.h"
|
||
|
#include "../schema.h"
|
||
|
namespace CuteEntityManager {
|
||
|
class MysqlSchema : public Schema {
|
||
|
public:
|
||
|
MysqlSchema();
|
||
|
MysqlSchema(std::shared_ptr<Database> database);
|
||
|
~MysqlSchema();
|
||
|
QHash<QString, QString> *getTypeMap();
|
||
|
};
|
||
| src/schema/pgsqlschema.cpp | ||
|---|---|---|
|
#include "pgsqlschema.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
PgSqlSchema::PgSqlSchema() : Schema() {
|
||
|
PgSqlSchema::PgSqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
|
||
|
}
|
||
|
|
||
|
QHash<QString, QString>* PgSqlSchema::getTypeMap() {
|
||
|
if(this->typeMap.data()->empty()) {
|
||
|
QHash<QString, QString> *PgSqlSchema::getTypeMap() {
|
||
|
if (this->typeMap.data()->empty()) {
|
||
|
// this->typeMap->data()->insert(TYPE_SMALLINT, 'tinyint');
|
||
|
// this->typeMap->data()->insert(TYPE_SMALLINT, 'bit');
|
||
|
// this->typeMap->data()->insert(TYPE_BOOLEAN, 'boolean');
|
||
| src/schema/pgsqlschema.h | ||
|---|---|---|
|
#ifndef PGSQLSCHEMA_H
|
||
|
#define PGSQLSCHEMA_H
|
||
|
#include <memory>
|
||
|
#include "../database.h"
|
||
|
#include "../schema.h"
|
||
|
namespace CuteEntityManager {
|
||
|
class PgSqlSchema : public Schema {
|
||
|
public:
|
||
|
PgSqlSchema();
|
||
|
PgSqlSchema(std::shared_ptr<Database>database);
|
||
|
~PgSqlSchema();
|
||
|
QHash<QString, QString> *getTypeMap();
|
||
|
};
|
||
| src/schema/sqliteschema.cpp | ||
|---|---|---|
|
#include "sqliteschema.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
SqliteSchema::SqliteSchema() : Schema() {
|
||
|
SqliteSchema::SqliteSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
|
||
|
}
|
||
|
|
||
| src/schema/sqliteschema.h | ||
|---|---|---|
|
#ifndef SQLITESCHEMA_H
|
||
|
#define SQLITESCHEMA_H
|
||
|
|
||
|
#include <memory>
|
||
|
#include "../database.h"
|
||
|
#include "../schema.h"
|
||
|
namespace CuteEntityManager {
|
||
|
class SqliteSchema : public Schema {
|
||
|
public:
|
||
|
SqliteSchema();
|
||
|
SqliteSchema(std::shared_ptr<Database> database);
|
||
|
~SqliteSchema();
|
||
|
QHash<QString, QString> *getTypeMap();
|
||
|
};
|
||
Auch abrufbar als: Unified diff
wip