Revision 14f9beed
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
EntityManager.pro | ||
---|---|---|
src/schema.h \
|
||
src/schema/sqliteschema.h \
|
||
src/tableschema.h \
|
||
src/columnschema.h \
|
||
src/schema/pgsqlschema.h \
|
||
src/schema/mysqlschema.h
|
||
src/schema/mysqlschema.h \
|
||
src/querybuilder.h
|
||
|
||
SOURCES += \
|
||
src/entity.cpp \
|
||
... | ... | |
src/schema.cpp \
|
||
src/schema/sqliteschema.cpp \
|
||
src/tableschema.cpp \
|
||
src/columnschema.cpp \
|
||
src/schema/pgsqlschema.cpp \
|
||
src/schema/mysqlschema.cpp
|
||
src/schema/mysqlschema.cpp \
|
||
src/querybuilder.cpp
|
||
|
||
unix {
|
||
target.path = /usr/lib
|
src/columnschema.cpp | ||
---|---|---|
#include "columnschema.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
ColumnSchema::ColumnSchema() {
|
||
|
||
}
|
||
|
||
ColumnSchema::~ColumnSchema() {
|
||
|
||
}
|
||
|
||
QString ColumnSchema::getName() const {
|
||
return name;
|
||
}
|
||
|
||
void ColumnSchema::setName(const QString &value) {
|
||
name = value;
|
||
}
|
||
|
||
bool ColumnSchema::getAllowNull() const {
|
||
return allowNull;
|
||
}
|
||
|
||
void ColumnSchema::setAllowNull(bool value) {
|
||
allowNull = value;
|
||
}
|
||
|
||
QString ColumnSchema::getDbType() const {
|
||
return dbType;
|
||
}
|
||
|
||
void ColumnSchema::setDbType(const QString &value) {
|
||
dbType = value;
|
||
}
|
||
|
||
QString ColumnSchema::getDefaultValue() const {
|
||
return defaultValue;
|
||
}
|
||
|
||
void ColumnSchema::setDefaultValue(const QString &value) {
|
||
defaultValue = value;
|
||
}
|
||
|
||
QList<QString> ColumnSchema::getEnumValues() const {
|
||
return enumValues;
|
||
}
|
||
|
||
void ColumnSchema::setEnumValues(const QList<QString> &value) {
|
||
enumValues = value;
|
||
}
|
||
|
||
quint8 ColumnSchema::getSize() const {
|
||
return size;
|
||
}
|
||
|
||
void ColumnSchema::setSize(const quint8 &value) {
|
||
size = value;
|
||
}
|
||
|
||
quint8 ColumnSchema::getPrecision() const {
|
||
return precision;
|
||
}
|
||
|
||
void ColumnSchema::setPrecision(const quint8 &value) {
|
||
precision = value;
|
||
}
|
||
|
||
quint8 ColumnSchema::getScale() const {
|
||
return scale;
|
||
}
|
||
|
||
void ColumnSchema::setScale(const quint8 &value) {
|
||
scale = value;
|
||
}
|
||
|
||
bool ColumnSchema::getPrimaryKey() const {
|
||
return primaryKey;
|
||
}
|
||
|
||
void ColumnSchema::setPrimaryKey(bool value) {
|
||
primaryKey = value;
|
||
}
|
||
|
||
bool ColumnSchema::getAutoIncrement() const {
|
||
return autoIncrement;
|
||
}
|
||
|
||
void ColumnSchema::setAutoIncrement(bool value) {
|
||
autoIncrement = value;
|
||
}
|
||
|
||
bool ColumnSchema::getUnsignedColumn() const {
|
||
return unsignedColumn;
|
||
}
|
||
|
||
void ColumnSchema::setUnsignedColumn(bool value) {
|
||
unsignedColumn = value;
|
||
}
|
||
|
||
QString ColumnSchema::getComment() const {
|
||
return comment;
|
||
}
|
||
|
||
void ColumnSchema::setComment(const QString &value) {
|
||
comment = value;
|
||
}
|
||
|
src/columnschema.h | ||
---|---|---|
#ifndef COLUMNSCHEMA_H
|
||
#define COLUMNSCHEMA_H
|
||
#include <QString>
|
||
#include <QList>
|
||
|
||
namespace CuteEntityManager {
|
||
|
||
class ColumnSchema {
|
||
public:
|
||
ColumnSchema();
|
||
~ColumnSchema();
|
||
QString getName() const;
|
||
void setName(const QString &value);
|
||
|
||
bool getAllowNull() const;
|
||
void setAllowNull(bool value);
|
||
|
||
QString getDbType() const;
|
||
void setDbType(const QString &value);
|
||
|
||
QString getDefaultValue() const;
|
||
void setDefaultValue(const QString &value);
|
||
|
||
QList<QString> getEnumValues() const;
|
||
void setEnumValues(const QList<QString> &value);
|
||
|
||
quint8 getSize() const;
|
||
void setSize(const quint8 &value);
|
||
|
||
quint8 getPrecision() const;
|
||
void setPrecision(const quint8 &value);
|
||
|
||
quint8 getScale() const;
|
||
void setScale(const quint8 &value);
|
||
|
||
bool getPrimaryKey() const;
|
||
void setPrimaryKey(bool value);
|
||
|
||
bool getAutoIncrement() const;
|
||
void setAutoIncrement(bool value);
|
||
|
||
bool getUnsignedColumn() const;
|
||
void setUnsignedColumn(bool value);
|
||
|
||
QString getComment() const;
|
||
void setComment(const QString &value);
|
||
|
||
private:
|
||
QString name;
|
||
bool allowNull;
|
||
QString dbType;
|
||
QString defaultValue;
|
||
QList<QString> enumValues;
|
||
quint8 size;
|
||
quint8 precision;
|
||
quint8 scale;
|
||
bool primaryKey;
|
||
bool autoIncrement;
|
||
bool unsignedColumn;
|
||
QString comment;
|
||
};
|
||
}
|
||
#endif // COLUMNSCHEMA_H
|
src/database.cpp | ||
---|---|---|
|
||
void Database::init() {
|
||
this->database.open();
|
||
this->databasetype = this->getDatabaseType();
|
||
this->supportTransactions = this->database.driver()->hasFeature(QSqlDriver::Transactions);
|
||
this->tableList = QStringList();
|
||
this->getTableListFromDatabase();
|
||
}
|
||
|
||
|
||
DatabaseType Database::getDatabaseType() {
|
||
return CuteEntityManager::getDatabaseType(this->database.driverName());
|
||
}
|
||
|
||
void Database::getTableListFromDatabase() {
|
||
if (this->database.open()) {
|
||
QString q = "";
|
||
QSqlQuery query = QSqlQuery(this->database);
|
||
query.prepare(q);
|
||
this->select(query);
|
||
this->setTableList(query);
|
||
}
|
||
}
|
||
|
||
//QString Database::mysqlTableList() {
|
||
// return "SHOW TABLES;";
|
||
//}
|
||
|
||
//QString Database::pgsqlTableList() {
|
||
// return "SELECT table_name FROM information_schema.tables WHERE table_catalog = '"+this->database.databaseName()+"';";
|
||
//}
|
||
|
||
Database::~Database() {
|
||
if (this->database.isOpen()) {
|
||
this->database.close();
|
||
}
|
||
QSqlDatabase::removeDatabase(this->connectionName);
|
||
delete this->tableList;
|
||
}
|
||
|
||
QString Database::getConnectionName() {
|
||
return this->connectionName;
|
||
}
|
||
|
||
//QString Database::pgsqlSeqTable() {
|
||
// return "CREATE TABLE IF NOT EXISTS sequence (SEQ_NAME varchar(255) NOT NULL UNIQUE , SEQ_COUNT bigint NOT NULL);";
|
||
//}
|
||
|
||
//QString Database::mysqlSeqTable() {
|
||
// return "CREATE TABLE IF NOT EXISTS `sequence` (`SEQ_NAME` varchar(255) NOT NULL UNIQUE , `SEQ_COUNT` bigint(20) unsigned NOT NULL) CHARSET = utf8";
|
||
//}
|
||
|
||
//QString Database::sqliteSeqTable() {
|
||
// return "CREATE TABLE IF NOT EXISTS \"sequence\" (\"SEQ_NAME\" TEXT PRIMARY KEY NOT NULL , \"SEQ_COUNT\" INTEGER NOT NULL );";
|
||
//}
|
||
|
||
QChar Database::escapeChar() {
|
||
QChar c = QChar();
|
||
if (this->databasetype == CuteEntityManager::SQLITE) {
|
||
c = '\'';
|
||
} else if (this->databasetype == CuteEntityManager::MYSQL) {
|
||
c = '`';
|
||
}
|
||
return c;
|
||
}
|
||
|
||
|
||
bool Database::transaction(const QString &query) {
|
||
bool rc = false;
|
||
if (supportTransactions) {
|
||
... | ... | |
return q;
|
||
}
|
||
|
||
QString Database::querySequenceCounter() {
|
||
return "INSERT INTO sequence (SEQ_NAME, SEQ_COUNT) VALUES(\'id_count\',\'0\');";
|
||
}
|
||
|
||
void Database::refreshTableList() {
|
||
this->tableList->clear();
|
||
this->getTableListFromDatabase();
|
||
}
|
||
|
||
bool Database::containsTable(QString tblname) {
|
||
return this->tableList->contains(tblname);
|
||
}
|
||
|
||
bool Database::updateSequenceCounter(QSqlQuery &q) {
|
||
QList<QSqlQuery> l = QList<QSqlQuery>();
|
||
l.append(QSqlQuery("UPDATE sequence SET SEQ_COUNT=(SEQ_COUNT+1);", this->database));
|
||
l.append(q);
|
||
return this->transaction(l);
|
||
}
|
||
|
||
qint64 Database::getLastId() {
|
||
qint64 id = -1;
|
||
QSqlQuery q = this->select("SELECT SEQ_COUNT FROM sequence WHERE SEQ_NAME=\'id_count\';");
|
||
if (q.next()) {
|
||
id = q.value(0).toInt();
|
||
}
|
||
return id;
|
||
}
|
||
|
||
QSqlDatabase Database::getDatabase() {
|
||
return this->database;
|
||
}
|
src/database.h | ||
---|---|---|
QSqlDatabase database;
|
||
QString connectionName;
|
||
bool seqTable;
|
||
DatabaseType databasetype;
|
||
bool supportTransactions;
|
||
void init();
|
||
QStringList tableList;
|
||
|
||
protected:
|
||
// inline QString pgsqlSeqTable();
|
||
... | ... | |
bool exec(QList<QSqlQuery> queries);
|
||
bool select(QSqlQuery &query);
|
||
QSqlQuery select(const QString &query);
|
||
bool isSeqTable();
|
||
qint64 getLastId();
|
||
void getTableListFromDatabase();
|
||
void setTableList(QSqlQuery &q);
|
||
void refreshTableList();
|
||
bool containsTable(QString tblname);
|
||
DatabaseType getDatabaseType();
|
||
QChar escapeChar();
|
||
// bool isSeqTable();
|
||
// qint64 getLastId();
|
||
// void getTableListFromDatabase();
|
||
// void setTableList(QSqlQuery &q);
|
||
// void refreshTableList();
|
||
// QChar escapeChar();
|
||
};
|
||
}
|
||
#endif // DATABASE_H
|
src/entitymanager.cpp | ||
---|---|---|
}
|
||
|
||
inline bool EntityManager::checkTable(Entity *entity) {
|
||
bool rc = true;
|
||
if (!this->db->containsTable(entity->getTablename())) {
|
||
qDebug() << "Tabelle" << entity->getTablename() << "existiert noch nicht.";
|
||
if (this->createTable(entity)) {
|
||
this->db->refreshTableList();
|
||
rc = this->db->containsTable(entity->getTablename());
|
||
}
|
||
}
|
||
return rc;
|
||
// bool rc = true;
|
||
// if (!this->db->containsTable(entity->getTablename())) {
|
||
// qDebug() << "Tabelle" << entity->getTablename() << "existiert noch nicht.";
|
||
// if (this->createTable(entity)) {
|
||
// this->db->refreshTableList();
|
||
// rc = this->db->containsTable(entity->getTablename());
|
||
// }
|
||
// }
|
||
// return rc;
|
||
}
|
||
|
||
QString EntityManager::createConnection() {
|
src/enums/databasetype.h | ||
---|---|---|
#ifndef DATABASETYPE_H
|
||
#define DATABASETYPE_H
|
||
#include <QString>
|
||
#include "../schema.h"
|
||
#include <QSharedPointer>
|
||
#include "../schema/sqliteschema.h"
|
||
#include "../schema/pgsqlschema.h"
|
||
#include "../schema/mysqlschema.h"
|
||
#include <memory>
|
||
|
||
namespace CuteEntityManager {
|
||
class Schema;
|
||
class Database;
|
||
enum DatabaseType {
|
||
SQLITE = 0,
|
||
PGSQL = 1,
|
||
... | ... | |
}
|
||
}
|
||
|
||
static const std::shared_ptr<Schema> getSchema(int db) {
|
||
static const QSharedPointer<Schema> getSchema(int db, QSharedPointer<Database> database) {
|
||
switch (db) {
|
||
case SQLITE:
|
||
return std::shared_ptr<Schema>(new SqliteSchema());
|
||
break;
|
||
case PGSQL:
|
||
return std::shared_ptr<Schema>(new PgSqlSchema());
|
||
break;
|
||
case MYSQL:
|
||
return std::shared_ptr<Schema>(new MysqlSchema());
|
||
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 std::shared_ptr<Schema>(new SqliteSchema());
|
||
return QSharedPointer<Schema>(new SqliteSchema(database));
|
||
break;
|
||
}
|
||
}
|
src/querybuilder.cpp | ||
---|---|---|
#include "querybuilder.h"
|
||
|
||
QueryBuilder::QueryBuilder() {
|
||
|
||
}
|
||
|
||
QueryBuilder::~QueryBuilder() {
|
||
|
||
}
|
||
|
||
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;
|
||
// }
|
||
|
||
}
|
||
|
src/querybuilder.h | ||
---|---|---|
#ifndef QUERYBUILDER_H
|
||
#define QUERYBUILDER_H
|
||
#include "schema.h"
|
||
|
||
class QueryBuilder
|
||
{
|
||
public:
|
||
QueryBuilder();
|
||
~QueryBuilder();
|
||
bool createTable(QString tablename, QHash<QString, QString> tableDefinition);
|
||
|
||
};
|
||
|
||
#endif // QUERYBUILDER_H
|
src/schema.cpp | ||
---|---|---|
#include <QRegularExpression>
|
||
#include <QSqlRecord>
|
||
#include <QSqlQuery>
|
||
#include "database.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
Schema::Schema(std::shared_ptr<Database> database) {
|
||
Schema::Schema(QSharedPointer<Database> database) {
|
||
this->database = database;
|
||
this->typeMap = QSharedPointer<QHash<QString, QString>>(new QHash<QString, QString>());
|
||
}
|
||
... | ... | |
}
|
||
|
||
QStringList Schema::getTableNames(QString schema) {
|
||
return this->database.get()->getDatabase().tables();
|
||
return this->database.data()->getDatabase().tables();
|
||
}
|
||
|
||
QVariant Schema::getLastInsertID() {
|
||
QSqlQuery q(this->database.get()->getDatabase());
|
||
QSqlQuery q(this->database.data()->getDatabase());
|
||
return q.lastInsertId();
|
||
}
|
||
|
||
... | ... | |
QString Schema::getRawTable(QString name) {
|
||
if (name.indexOf("{{")) {
|
||
QRegularExpression re("/\\{\\{(.*?)\\}\\}/");
|
||
QRegularExpression re2("\1");
|
||
return name.replace(re, re2);
|
||
return name.replace(re, "\\1");
|
||
}
|
||
return name;
|
||
}
|
||
|
||
bool Schema::containsTable(QString tblname) {
|
||
return this->tables.contains(tblname);
|
||
}
|
||
|
||
QString Schema::quoteValue(QString str) {
|
||
|
||
}
|
||
|
||
TableSchema *Schema::getTableSchema(QString name, bool refresh) {
|
||
if (refresh) {
|
||
this->refresh();
|
||
}
|
||
if (this->tables.contains(name)) {
|
||
return this->tables.value(name);
|
||
return this->tables.value(name).data();
|
||
}
|
||
QString realName = this->getRawTable(name);
|
||
QSharedPointer<TableSchema> ts = this->loadTableSchema(realName);
|
||
auto ts = this->loadTableSchema(realName);
|
||
if (ts.data()) {
|
||
this->tables.insert(name, ts);
|
||
}
|
||
return ts.data();
|
||
}
|
||
|
||
QSharedPointer<Database> Schema::getDatabase() const {
|
||
return database;
|
||
}
|
||
|
||
void Schema::setDatabase(const QSharedPointer<Database> &value) {
|
||
database = value;
|
||
}
|
||
|
||
QHash<QString, QSharedPointer<TableSchema> > Schema::getTables() const {
|
||
return this->tables;
|
||
}
|
src/schema.h | ||
---|---|---|
#include <QStringList>
|
||
#include <QHash>
|
||
#include <QSharedPointer>
|
||
#include <QSqlDatabase>
|
||
#include <memory>
|
||
#include "database.h"
|
||
|
||
#include <QSqlField>
|
||
namespace CuteEntityManager {
|
||
|
||
class Database;
|
||
class Schema {
|
||
public:
|
||
Schema(std::shared_ptr<Database> database);
|
||
Schema(QSharedPointer<Database> database);
|
||
virtual ~Schema();
|
||
const QString TYPE_PK = "pk";
|
||
const QString TYPE_BIGPK = "bigpk";
|
||
... | ... | |
const QString TYPE_TIMESTAMP = "timestamp";
|
||
const QString TYPE_TIME = "time";
|
||
const QString TYPE_DATE = "date";
|
||
const QString TYPE_CHAR = "char";
|
||
const QString TYPE_BINARY = "binary";
|
||
const QString TYPE_BOOLEAN = "boolean";
|
||
const QString TYPE_MONEY = "money";
|
||
... | ... | |
virtual QVariant getLastInsertID();
|
||
virtual void refresh();
|
||
virtual QString getRawTable(QString name);
|
||
virtual bool containsTable(QString tblname);
|
||
virtual QString quoteValue(QString str);
|
||
|
||
QHash<QString, QSharedPointer<TableSchema> > getTables() const;
|
||
void setTables(const QHash<QString, QSharedPointer<TableSchema> > &value);
|
||
|
||
protected:
|
||
QSharedPointer<Database> getDatabase() const;
|
||
void setDatabase(const QSharedPointer<Database> &value);
|
||
|
||
protected:
|
||
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 QSharedPointer<TableSchema> *loadTableSchema(QString name) = 0;
|
||
virtual QHash<QString, QStringList> findUniqueIndexes(const QSharedPointer<TableSchema> &table) = 0;
|
||
virtual void findConstraints(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual QSharedPointer<TableSchema> loadTableSchema(QString name) = 0;
|
||
virtual TableSchema *getTableSchema(QString name, bool refresh = false);
|
||
std::shared_ptr<Database> database;
|
||
QSharedPointer<Database> database;
|
||
QSharedPointer<QHash<QString, QString>> typeMap;
|
||
QHash<QString, QSharedPointer<TableSchema>> tables;
|
||
|
||
|
src/schema/mysqlschema.cpp | ||
---|---|---|
#include "mysqlschema.h"
|
||
#include <QSqlQuery>
|
||
using namespace CuteEntityManager;
|
||
//#include "mysqlschema.h"
|
||
//#include <QSqlQuery>
|
||
//using namespace CuteEntityManager;
|
||
|
||
MysqlSchema::MysqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
//MysqlSchema::MysqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
||
}
|
||
//}
|
||
|
||
MysqlSchema::~MysqlSchema() {
|
||
//MysqlSchema::~MysqlSchema() {
|
||
|
||
}
|
||
//}
|
||
|
||
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');
|
||
// this->typeMap->data()->insert(TYPE_BOOLEAN, 'bool');
|
||
// this->typeMap->data()->insert(TYPE_SMALLINT, 'smallint');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'mediumint');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'int');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'integer');
|
||
// this->typeMap->data()->insert(TYPE_BIGINT, 'bigint');
|
||
// this->typeMap->data()->insert(TYPE_FLOAT, 'float');
|
||
// this->typeMap->data()->insert(TYPE_DOUBLE, 'double');
|
||
// this->typeMap->data()->insert(TYPE_FLOAT, 'real');
|
||
// this->typeMap->data()->insert(TYPE_DECIMAL, 'decimal');
|
||
// this->typeMap->data()->insert(TYPE_DECIMAL, 'numeric');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'tinytext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'mediumtext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'longtext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'text');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'varchar');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'string');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'char');
|
||
// this->typeMap->data()->insert(TYPE_BINARY, 'blob');
|
||
// this->typeMap->data()->insert(TYPE_DATETIME, 'datetime');
|
||
// this->typeMap->data()->insert(TYPE_DATE, 'year');
|
||
// this->typeMap->data()->insert(TYPE_DATE, 'date');
|
||
// this->typeMap->data()->insert(TYPE_TIME, 'time');
|
||
// this->typeMap->data()->insert(TYPE_TIMESTAMP, 'timestamp');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'enum');
|
||
}
|
||
return this->typeMap.data();
|
||
}
|
||
//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');
|
||
//// this->typeMap->data()->insert(TYPE_BOOLEAN, 'bool');
|
||
//// this->typeMap->data()->insert(TYPE_SMALLINT, 'smallint');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'mediumint');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'int');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'integer');
|
||
//// this->typeMap->data()->insert(TYPE_BIGINT, 'bigint');
|
||
//// this->typeMap->data()->insert(TYPE_FLOAT, 'float');
|
||
//// this->typeMap->data()->insert(TYPE_DOUBLE, 'double');
|
||
//// this->typeMap->data()->insert(TYPE_FLOAT, 'real');
|
||
//// this->typeMap->data()->insert(TYPE_DECIMAL, 'decimal');
|
||
//// this->typeMap->data()->insert(TYPE_DECIMAL, 'numeric');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'tinytext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'mediumtext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'longtext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'text');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'varchar');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'string');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'char');
|
||
//// this->typeMap->data()->insert(TYPE_BINARY, 'blob');
|
||
//// this->typeMap->data()->insert(TYPE_DATETIME, 'datetime');
|
||
//// this->typeMap->data()->insert(TYPE_DATE, 'year');
|
||
//// this->typeMap->data()->insert(TYPE_DATE, 'date');
|
||
//// this->typeMap->data()->insert(TYPE_TIME, 'time');
|
||
//// this->typeMap->data()->insert(TYPE_TIMESTAMP, 'timestamp');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'enum');
|
||
// }
|
||
// return this->typeMap.data();
|
||
//}
|
||
|
||
////QString Database::mysqlTableList() {
|
||
//// return "SHOW TABLES;";
|
||
////}
|
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(std::shared_ptr<Database> database);
|
||
~MysqlSchema();
|
||
QHash<QString, QString> *getTypeMap();
|
||
};
|
||
}
|
||
#endif // MYSQLSCHEMA_H
|
||
//#ifndef MYSQLSCHEMA_H
|
||
//#define MYSQLSCHEMA_H
|
||
//#include "../schema.h"
|
||
//namespace CuteEntityManager {
|
||
//class MysqlSchema : public Schema {
|
||
// public:
|
||
// MysqlSchema(std::shared_ptr<Database> database);
|
||
// ~MysqlSchema();
|
||
// QHash<QString, QString> *getTypeMap();
|
||
//};
|
||
//}
|
||
//#endif // MYSQLSCHEMA_H
|
src/schema/pgsqlschema.cpp | ||
---|---|---|
#include "pgsqlschema.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
PgSqlSchema::PgSqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
||
}
|
||
|
||
PgSqlSchema::~PgSqlSchema() {
|
||
|
||
}
|
||
|
||
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');
|
||
// this->typeMap->data()->insert(TYPE_BOOLEAN, 'bool');
|
||
// this->typeMap->data()->insert(TYPE_SMALLINT, 'smallint');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'mediumint');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'int');
|
||
// this->typeMap->data()->insert(TYPE_INTEGER, 'integer');
|
||
// this->typeMap->data()->insert(TYPE_BIGINT, 'bigint');
|
||
// this->typeMap->data()->insert(TYPE_FLOAT, 'float');
|
||
// this->typeMap->data()->insert(TYPE_DOUBLE, 'double');
|
||
// this->typeMap->data()->insert(TYPE_FLOAT, 'real');
|
||
// this->typeMap->data()->insert(TYPE_DECIMAL, 'decimal');
|
||
// this->typeMap->data()->insert(TYPE_DECIMAL, 'numeric');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'tinytext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'mediumtext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'longtext');
|
||
// this->typeMap->data()->insert(TYPE_TEXT, 'text');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'varchar');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'string');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'char');
|
||
// this->typeMap->data()->insert(TYPE_BINARY, 'blob');
|
||
// this->typeMap->data()->insert(TYPE_DATETIME, 'datetime');
|
||
// this->typeMap->data()->insert(TYPE_DATE, 'year');
|
||
// this->typeMap->data()->insert(TYPE_DATE, 'date');
|
||
// this->typeMap->data()->insert(TYPE_TIME, 'time');
|
||
// this->typeMap->data()->insert(TYPE_TIMESTAMP, 'timestamp');
|
||
// this->typeMap->data()->insert(TYPE_STRING, 'enum');
|
||
}
|
||
return this->typeMap.data();
|
||
}
|
||
//#include "pgsqlschema.h"
|
||
//using namespace CuteEntityManager;
|
||
|
||
//PgSqlSchema::PgSqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
|
||
//}
|
||
|
||
//PgSqlSchema::~PgSqlSchema() {
|
||
|
||
//}
|
||
|
||
//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');
|
||
//// this->typeMap->data()->insert(TYPE_BOOLEAN, 'bool');
|
||
//// this->typeMap->data()->insert(TYPE_SMALLINT, 'smallint');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'mediumint');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'int');
|
||
//// this->typeMap->data()->insert(TYPE_INTEGER, 'integer');
|
||
//// this->typeMap->data()->insert(TYPE_BIGINT, 'bigint');
|
||
//// this->typeMap->data()->insert(TYPE_FLOAT, 'float');
|
||
//// this->typeMap->data()->insert(TYPE_DOUBLE, 'double');
|
||
//// this->typeMap->data()->insert(TYPE_FLOAT, 'real');
|
||
//// this->typeMap->data()->insert(TYPE_DECIMAL, 'decimal');
|
||
//// this->typeMap->data()->insert(TYPE_DECIMAL, 'numeric');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'tinytext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'mediumtext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'longtext');
|
||
//// this->typeMap->data()->insert(TYPE_TEXT, 'text');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'varchar');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'string');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'char');
|
||
//// this->typeMap->data()->insert(TYPE_BINARY, 'blob');
|
||
//// this->typeMap->data()->insert(TYPE_DATETIME, 'datetime');
|
||
//// this->typeMap->data()->insert(TYPE_DATE, 'year');
|
||
//// this->typeMap->data()->insert(TYPE_DATE, 'date');
|
||
//// this->typeMap->data()->insert(TYPE_TIME, 'time');
|
||
//// this->typeMap->data()->insert(TYPE_TIMESTAMP, 'timestamp');
|
||
//// this->typeMap->data()->insert(TYPE_STRING, 'enum');
|
||
// }
|
||
// return this->typeMap.data();
|
||
//}
|
||
|
||
|
||
////QString Database::pgsqlTableList() {
|
||
//// return "SELECT table_name FROM information_schema.tables WHERE table_catalog = '"+this->database.databaseName()+"';";
|
||
////}
|
||
|
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(std::shared_ptr<Database>database);
|
||
~PgSqlSchema();
|
||
QHash<QString, QString> *getTypeMap();
|
||
};
|
||
}
|
||
//#ifndef PGSQLSCHEMA_H
|
||
//#define PGSQLSCHEMA_H
|
||
//#include "../schema.h"
|
||
//namespace CuteEntityManager {
|
||
//class PgSqlSchema : public Schema {
|
||
// public:
|
||
// PgSqlSchema(std::shared_ptr<Database>database);
|
||
// ~PgSqlSchema();
|
||
// QHash<QString, QString> *getTypeMap();
|
||
//};
|
||
//}
|
||
|
||
#endif // PGSQLSCHEMA_H
|
||
//#endif // PGSQLSCHEMA_H
|
src/schema/sqliteschema.cpp | ||
---|---|---|
#include "sqliteschema.h"
|
||
#include "../database.h"
|
||
#include <QSqlRecord>
|
||
#include <QSqlResult>
|
||
using namespace CuteEntityManager;
|
||
|
||
SqliteSchema::SqliteSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
SqliteSchema::SqliteSchema(QSharedPointer<Database> database) : Schema(database) {
|
||
|
||
}
|
||
|
||
... | ... | |
|
||
}
|
||
|
||
QHash<QString, QString>* SqliteSchema::getTypeMap() {
|
||
if(this->typeMap.data()->empty()) {
|
||
QHash<QString, QString> *SqliteSchema::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");
|
||
this->typeMap.data()->insert(TYPE_BOOLEAN, "bool");
|
||
this->typeMap.data()->insert(TYPE_SMALLINT, "smallint");
|
||
this->typeMap.data()->insert(TYPE_INTEGER, "mediumint");
|
||
this->typeMap.data()->insert(TYPE_INTEGER, "int");
|
||
this->typeMap.data()->insert(TYPE_INTEGER, "integer");
|
||
this->typeMap.data()->insert(TYPE_BIGINT, "bigint");
|
||
this->typeMap.data()->insert(TYPE_FLOAT, "float");
|
||
this->typeMap.data()->insert(TYPE_DOUBLE, "double");
|
||
this->typeMap.data()->insert(TYPE_FLOAT, "real");
|
||
this->typeMap.data()->insert(TYPE_DECIMAL, "decimal");
|
||
this->typeMap.data()->insert(TYPE_DECIMAL, "numeric");
|
||
this->typeMap.data()->insert(TYPE_TEXT, "tinytext");
|
||
this->typeMap.data()->insert(TYPE_TEXT, "mediumtext");
|
||
this->typeMap.data()->insert(TYPE_TEXT, "longtext");
|
||
this->typeMap.data()->insert(TYPE_TEXT, "text");
|
||
this->typeMap.data()->insert(TYPE_STRING, "varchar");
|
||
this->typeMap.data()->insert(TYPE_STRING, "string");
|
||
this->typeMap.data()->insert(TYPE_STRING, "char");
|
||
this->typeMap.data()->insert(TYPE_CHAR, "char");
|
||
this->typeMap.data()->insert(TYPE_BINARY, "blob");
|
||
this->typeMap.data()->insert(TYPE_DATETIME, "datetime");
|
||
this->typeMap.data()->insert(TYPE_DATE, "year");
|
||
this->typeMap.data()->insert(TYPE_DATE, "date");
|
||
this->typeMap.data()->insert(TYPE_TIME, "time");
|
||
this->typeMap.data()->insert(TYPE_TIMESTAMP, "timestamp");
|
||
this->typeMap.data()->insert(TYPE_STRING, "enum");
|
||
}
|
||
return this->typeMap.data();
|
||
}
|
||
|
||
QStringList SqliteSchema::findTableNames(QString schema) {
|
||
auto l = QStringList();
|
||
QString sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name";
|
||
auto q = this->database.data()->getQuery();
|
||
q.prepare(sql);
|
||
this->database.data()->select(q);
|
||
while (q.next()) {
|
||
l.append(q.value(0).toString());
|
||
}
|
||
return l;
|
||
}
|
||
|
||
QHash<QString, QStringList> SqliteSchema::findUniqueIndexes(const QSharedPointer<TableSchema> &table) {
|
||
QHash<QString, QStringList> uniqueIndexes = QHash<QString, QStringList>();
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("PRAGMA index_list(" + this->quoteSimpleTableName(table->getName()) + ')');
|
||
while (q.next()) {
|
||
QString indexName = q.value("name").toString();
|
||
QSqlQuery q2 = this->database.data()->getQuery();
|
||
q2.setForwardOnly(true);
|
||
if (q.value("unique").toBool()) {
|
||
q2.exec("PRAGMA index_info(" + this->quoteValue(indexName) + ")");
|
||
QStringList indexInfo = QStringList();
|
||
while (q2.next()) {
|
||
indexInfo.append(q2.value("name").toString());
|
||
}
|
||
uniqueIndexes.insert(indexName, indexInfo);
|
||
}
|
||
}
|
||
return uniqueIndexes;
|
||
}
|
||
|
||
void SqliteSchema::findConstraints(const QSharedPointer<TableSchema> &ts) {
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("PRAGMA foreign_key_list(" + this->quoteSimpleTableName(ts.data()->getName()) + ')');
|
||
auto foreignKeys = ts.data()->getRelations();
|
||
while (q.next()) {
|
||
bool ok;
|
||
int id = q.value("id").toInt(&ok);
|
||
if (ok) {
|
||
auto rel = new QSqlRelation(q.value("table").toString(), q.value("from").toString(), q.value("to").toString());
|
||
auto ptr = QSharedPointer<QSqlRelation>(rel);
|
||
foreignKeys.insert(QString::number(id), ptr);
|
||
}
|
||
}
|
||
ts.data()->setRelations(foreignKeys);
|
||
}
|
||
|
||
//QString Database::sqliteTableList() {
|
||
// return "SELECT tbl_name FROM sqlite_master WHERE type="table";";
|
||
//}
|
||
bool SqliteSchema::findColumns(const QSharedPointer<TableSchema> &ts) {
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("SELECT * FROM " + this->quoteSimpleTableName(ts.data()->getName()) + " LIMIT 0");
|
||
QHash<QString, QSharedPointer<QSqlField>> columns = QHash<QString, QSharedPointer<QSqlField>>();
|
||
auto rec = q.record();
|
||
int count = rec.count();
|
||
if (count == 0) {
|
||
return false;
|
||
}
|
||
for (int var = 0; var < count; ++var) {
|
||
QSqlField f = rec.field(var);
|
||
columns.insert(f.name(), QSharedPointer<QSqlField>(new QSqlField(f)));
|
||
}
|
||
ts.data()->setColumns(columns);
|
||
return true;
|
||
}
|
||
|
||
QSharedPointer<TableSchema> SqliteSchema::loadTableSchema(QString name) {
|
||
auto table = new TableSchema();
|
||
auto ptr = QSharedPointer<TableSchema>(table);
|
||
table->setName(name);
|
||
table->setFullName(name);
|
||
if (this->findColumns(ptr)) {
|
||
this->findConstraints(ptr);
|
||
} else {
|
||
ptr.clear();
|
||
}
|
||
return ptr;
|
||
}
|
src/schema/sqliteschema.h | ||
---|---|---|
#ifndef SQLITESCHEMA_H
|
||
#define SQLITESCHEMA_H
|
||
#include <memory>
|
||
#include "../database.h"
|
||
#include "../schema.h"
|
||
namespace CuteEntityManager {
|
||
class TableSchema;
|
||
class SqliteSchema : public Schema {
|
||
public:
|
||
SqliteSchema(std::shared_ptr<Database> database);
|
||
SqliteSchema(QSharedPointer<Database> database);
|
||
~SqliteSchema();
|
||
QHash<QString, QString> *getTypeMap();
|
||
protected:
|
||
|
||
virtual QStringList findTableNames(QString schema = "");
|
||
virtual QHash<QString, QStringList> findUniqueIndexes(const QSharedPointer<TableSchema> &table);
|
||
virtual void findConstraints(const QSharedPointer<TableSchema> &ts);
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts);
|
||
virtual QSharedPointer<TableSchema> loadTableSchema(QString name);
|
||
};
|
||
}
|
||
#endif // SQLITESCHEMA_H
|
src/tableschema.cpp | ||
---|---|---|
|
||
}
|
||
|
||
const ColumnSchema TableSchema::getColumn(QString name) const {
|
||
|
||
const QSharedPointer<QSqlField> TableSchema::getColumn(QString name) const {
|
||
auto columns = this->getColumns();
|
||
foreach (auto schema, columns) {
|
||
if (schema.data()->name() == name) {
|
||
return schema;
|
||
}
|
||
}
|
||
return QSharedPointer<QSqlField>();
|
||
}
|
||
|
||
const QList<QString> TableSchema::getColumnNames() {
|
||
|
||
const QStringList TableSchema::getColumnNames() {
|
||
QStringList l;
|
||
auto columns = this->getColumns();
|
||
foreach (auto schema, columns) {
|
||
l.append(schema.data()->name());
|
||
}
|
||
return l;
|
||
}
|
||
|
||
QString TableSchema::getSchemaName() const {
|
||
... | ... | |
void TableSchema::setFullName(const QString &value) {
|
||
fullName = value;
|
||
}
|
||
QList<QString> TableSchema::getPrimaryKeys() const {
|
||
QStringList TableSchema::getPrimaryKeys() const {
|
||
return primaryKeys;
|
||
}
|
||
|
||
void TableSchema::setPrimaryKeys(const QList<QString> &value) {
|
||
void TableSchema::setPrimaryKeys(const QStringList &value) {
|
||
primaryKeys = value;
|
||
}
|
||
QString TableSchema::getSequenceName() const {
|
||
... | ... | |
void TableSchema::setSequenceName(const QString &value) {
|
||
sequenceName = value;
|
||
}
|
||
QHash<QString, QString> TableSchema::getForeignKeys() const {
|
||
return foreignKeys;
|
||
}
|
||
|
||
void TableSchema::setForeignKeys(const QHash<QString, QString> &value) {
|
||
foreignKeys = value;
|
||
}
|
||
QList<ColumnSchema> TableSchema::getColumns() const {
|
||
QHash<QString, QSharedPointer<QSqlField> > TableSchema::getColumns() const {
|
||
return columns;
|
||
}
|
||
|
||
void TableSchema::setColumns(const QList<ColumnSchema> &value) {
|
||
void TableSchema::setColumns(const QHash<QString, QSharedPointer<QSqlField> > &value) {
|
||
columns = value;
|
||
}
|
||
QHash<QString, QSharedPointer<QSqlRelation> > TableSchema::getRelations() const {
|
||
return relations;
|
||
}
|
||
|
||
void TableSchema::setRelations(const QHash<QString, QSharedPointer<QSqlRelation> > &value) {
|
||
relations = value;
|
||
}
|
src/tableschema.h | ||
---|---|---|
#define TABLESCHEMA_H
|
||
#include <QString>
|
||
#include <QList>
|
||
#include <QStringList>
|
||
#include <QHash>
|
||
#include "columnschema.h"
|
||
|
||
#include <QSharedPointer>
|
||
#include <QSqlField>
|
||
#include <QSqlRelation>
|
||
namespace CuteEntityManager {
|
||
|
||
class TableSchema {
|
||
public:
|
||
TableSchema();
|
||
~TableSchema();
|
||
virtual ColumnSchema const getColumn(QString name) const;
|
||
virtual QList<QString> const getColumnNames();
|
||
virtual QSharedPointer<QSqlField> const getColumn(QString name) const;
|
||
virtual const QStringList getColumnNames();
|
||
|
||
QString getSchemaName() const;
|
||
void setSchemaName(const QString &value);
|
||
... | ... | |
QString getFullName() const;
|
||
void setFullName(const QString &value);
|
||
|
||
QList<QString> getPrimaryKeys() const;
|
||
void setPrimaryKeys(const QList<QString> &value);
|
||
QStringList getPrimaryKeys() const;
|
||
void setPrimaryKeys(const QStringList &value);
|
||
|
||
QString getSequenceName() const;
|
||
void setSequenceName(const QString &value);
|
||
|
||
QHash<QString, QString> getForeignKeys() const;
|
||
void setForeignKeys(const QHash<QString, QString> &value);
|
||
QHash<QString, QSharedPointer<QSqlField> > getColumns() const;
|
||
void setColumns(const QHash<QString, QSharedPointer<QSqlField> > &value);
|
||
|
||
QList<ColumnSchema> getColumns() const;
|
||
void setColumns(const QList<ColumnSchema> &value);
|
||
QHash<QString, QSharedPointer<QSqlRelation> > getRelations() const;
|
||
void setRelations(const QHash<QString, QSharedPointer<QSqlRelation> > &value);
|
||
|
||
private:
|
||
private:
|
||
QString schemaName;
|
||
QString name;
|
||
QString fullName;
|
||
QList<QString> primaryKeys;
|
||
QStringList primaryKeys;
|
||
QString sequenceName;
|
||
QHash<QString, QString> foreignKeys;
|
||
QList<ColumnSchema> columns;
|
||
|
||
|
||
QHash<QString, QSharedPointer<QSqlRelation>> relations;
|
||
QHash<QString, QSharedPointer<QSqlField>> columns;
|
||
};
|
||
|
||
}
|
Auch abrufbar als: Unified diff
update