Revision 829c3e69
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/schema.cpp | ||
---|---|---|
return length;
|
||
}
|
||
|
||
bool Schema::findColumns(const QSharedPointer<TableSchema> &ts) {
|
||
QSqlQuery q = this->database->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("SELECT * FROM " + this->quoteSimpleTableName(ts->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->setColumns(columns);
|
||
return true;
|
||
}
|
||
|
||
|
||
QHash<QString, QSharedPointer<TableSchema> > Schema::getTables() {
|
||
if (this->tables.size() != this->getTableNames().size()) {
|
src/schema.h | ||
---|---|---|
QSharedPointer<TableSchema>
|
||
&table) = 0;
|
||
virtual void findConstraints(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts) = 0;
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts);
|
||
virtual QSharedPointer<TableSchema> loadTableSchema(QString name) = 0;
|
||
virtual void initAbstractDatabaseTypes();
|
||
QSharedPointer<Database> database;
|
src/schema/mysqlschema.cpp | ||
---|---|---|
//#include "mysqlschema.h"
|
||
//#include <QSqlQuery>
|
||
//using namespace CuteEntityManager;
|
||
#include "mysqlschema.h"
|
||
#include "../database.h"
|
||
#include <QSqlRecord>
|
||
#include <QSqlResult>
|
||
#include "sqlitequerybuilder.h"
|
||
using namespace CuteEntityManager;
|
||
|
||
//MysqlSchema::MysqlSchema(std::shared_ptr<Database> database) : Schema(database) {
|
||
QSharedPointer<QHash<QString, QString> > MysqlSchema::getTypeMap() {
|
||
if (this->typeMap->empty()) {
|
||
this->typeMap->insert(TYPE_PK,
|
||
"integer PRIMARY KEY AUTOINCREMENT NOT NULL");
|
||
this->typeMap->insert(TYPE_BIGPK,
|
||
"bigint PRIMARY KEY AUTOINCREMENT NOT NULL");
|
||
this->typeMap->insert(TYPE_BOOLEAN, "boolean");
|
||
this->typeMap->insert(TYPE_SMALLINT, "smallint");
|
||
this->typeMap->insert(TYPE_INTEGER, "integer");
|
||
this->typeMap->insert(TYPE_BIGINT, "bigint");
|
||
this->typeMap->insert(TYPE_FLOAT, "float");
|
||
this->typeMap->insert(TYPE_DOUBLE, "double");
|
||
this->typeMap->insert(TYPE_FLOAT, "real");
|
||
this->typeMap->insert(TYPE_DECIMAL, "decimal(10,0)");
|
||
this->typeMap->insert(TYPE_TEXT, "text");
|
||
this->typeMap->insert(TYPE_STRING, "text");
|
||
this->typeMap->insert(TYPE_CHAR, "char");
|
||
this->typeMap->insert(TYPE_BINARY, "blob");
|
||
this->typeMap->insert(TYPE_DATETIME, "datetime");
|
||
this->typeMap->insert(TYPE_DATE, "date");
|
||
this->typeMap->insert(TYPE_TIME, "time");
|
||
this->typeMap->insert(TYPE_TIMESTAMP, "timestamp");
|
||
this->typeMap->insert(TYPE_MONEY, "decimal(19,4)");
|
||
}
|
||
return this->typeMap;
|
||
}
|
||
|
||
//}
|
||
QStringList MysqlSchema::findTableNames(QString schema) {
|
||
QString sql = "SHOW TABLES";
|
||
if (!schema.isEmpty()) {
|
||
sql += " FROM " + this->quoteSimpleTableName(schema);
|
||
}
|
||
auto l = QStringList();
|
||
auto q = this->database->getQuery();
|
||
q.prepare(sql);
|
||
this->database->select(q);
|
||
while (q.next()) {
|
||
l.append(q.value(0).toString());
|
||
}
|
||
return l;
|
||
}
|
||
|
||
//MysqlSchema::~MysqlSchema() {
|
||
QHash<QString, QStringList> MysqlSchema::findUniqueIndexes(
|
||
const QSharedPointer<TableSchema> &table) {
|
||
QHash<QString, QStringList> uniqueIndexes = QHash<QString, QStringList>();
|
||
QSqlQuery q = this->database->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("SHOW INDEX FROM " + this->quoteSimpleTableName(
|
||
table->getName()));
|
||
while (q.next()) {
|
||
QString indexName = q.value("Key_name").toString();
|
||
if (!(q.value("Non_unique").toBool())) {
|
||
QStringList indexInfo = QStringList();
|
||
indexInfo.append(q.value("Column_name").toString());
|
||
uniqueIndexes.insert(indexName, indexInfo);
|
||
}
|
||
}
|
||
return uniqueIndexes;
|
||
}
|
||
|
||
//}
|
||
void MysqlSchema::findConstraints(const QSharedPointer<TableSchema> &ts) {
|
||
// QString sql = "SHOW INDEX";
|
||
// sql += " FROM " + this->quoteSimpleTableName(table->getFullName());
|
||
// QSqlQuery q = this->database->getQuery();
|
||
// q.setForwardOnly(true);
|
||
// q.exec(sql);
|
||
auto foreignKeys = ts->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);
|
||
// }
|
||
// }
|
||
|
||
//QHash<QString, QString> *MysqlSchema::getTypeMap() {
|
||
// if (this->typeMap.data()->empty()) {
|
||
// this->typeMap.data()->insert(TYPE_SMALLINT, "tinyint");
|
||
// this->typeMap.data()->insert(TYPE_BOOLEAN, "boolean");
|
||
// this->typeMap.data()->insert(TYPE_SMALLINT, "smallint");
|
||
// this->typeMap.data()->insert(TYPE_INTEGER, "int");
|
||
// 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_TEXT, "text");
|
||
// this->typeMap.data()->insert(TYPE_STRING, "varchar");
|
||
// 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, "date");
|
||
// this->typeMap.data()->insert(TYPE_TIME, "time");
|
||
// this->typeMap.data()->insert(TYPE_TIMESTAMP, "timestamp");
|
||
// }
|
||
// return this->typeMap.data();
|
||
//}
|
||
ts->setRelations(foreignKeys);
|
||
}
|
||
|
||
////QString Database::mysqlTableList() {
|
||
//// return "SHOW TABLES;";
|
||
////}
|
||
QSharedPointer<TableSchema> MysqlSchema::loadTableSchema(QString name) {
|
||
auto ptr = QSharedPointer<TableSchema>(new TableSchema());
|
||
this->resolveTableNames(ptr, name);
|
||
if (this->findColumns(ptr)) {
|
||
this->findConstraints(ptr);
|
||
} else {
|
||
ptr.clear();
|
||
}
|
||
return ptr;
|
||
}
|
||
|
||
void MysqlSchema::resolveTableNames(const QSharedPointer<TableSchema> &ts,
|
||
QString name) {
|
||
QStringList parts = name.replace("`", "").split(".");
|
||
if (parts.size() > 1) {
|
||
ts->setSchemaName(parts.at(0));
|
||
ts->setName(parts.at(1));
|
||
ts->setFullName(ts->getSchemaName() + "." + ts->getName());
|
||
} else {
|
||
ts->setFullName(parts.at(0));
|
||
ts->setName(parts.at(0));
|
||
}
|
||
}
|
src/schema/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
|
||
#ifndef MYSQLSCHEMA_H
|
||
#define MYSQLSCHEMA_H
|
||
#include "../schema.h"
|
||
namespace CuteEntityManager {
|
||
class TableSchema;
|
||
class MysqlSchema : public Schema {
|
||
public:
|
||
MysqlSchema(QSharedPointer<Database> database);
|
||
QSharedPointer<QHash<QString, QString> > getTypeMap() override;
|
||
|
||
protected:
|
||
QStringList findTableNames(QString schema = "") override;
|
||
QHash<QString, QStringList> findUniqueIndexes(const
|
||
QSharedPointer<TableSchema>
|
||
&table) override;
|
||
void findConstraints(const QSharedPointer<TableSchema> &ts) override;
|
||
QSharedPointer<TableSchema> loadTableSchema(QString name) override;
|
||
void resolveTableNames(const QSharedPointer<TableSchema> &ts, QString name);
|
||
|
||
};
|
||
}
|
||
|
||
#endif // MYSQLSCHEMA_H
|
src/schema/sqliteschema.cpp | ||
---|---|---|
this->typeMap->insert(TYPE_PK,
|
||
"integer PRIMARY KEY AUTOINCREMENT NOT NULL");
|
||
this->typeMap->insert(TYPE_BIGPK,
|
||
"integer PRIMARY KEY AUTOINCREMENT NOT NULL");
|
||
"bigint PRIMARY KEY AUTOINCREMENT NOT NULL");
|
||
this->typeMap->insert(TYPE_BOOLEAN, "boolean");
|
||
this->typeMap->insert(TYPE_SMALLINT, "smallint");
|
||
this->typeMap->insert(TYPE_INTEGER, "integer");
|
||
... | ... | |
ts->setRelations(foreignKeys);
|
||
}
|
||
|
||
bool SqliteSchema::findColumns(const QSharedPointer<TableSchema> &ts) {
|
||
QSqlQuery q = this->database->getQuery();
|
||
q.setForwardOnly(true);
|
||
q.exec("SELECT * FROM " + this->quoteSimpleTableName(ts->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->setColumns(columns);
|
||
return true;
|
||
}
|
||
|
||
QSharedPointer<TableSchema> SqliteSchema::loadTableSchema(QString name) {
|
||
auto ptr = QSharedPointer<TableSchema>(new TableSchema());
|
||
ptr->setName(name);
|
src/schema/sqliteschema.h | ||
---|---|---|
QSharedPointer<TableSchema>
|
||
&table) override;
|
||
virtual void findConstraints(const QSharedPointer<TableSchema> &ts) override;
|
||
virtual bool findColumns(const QSharedPointer<TableSchema> &ts) override;
|
||
virtual QSharedPointer<TableSchema> loadTableSchema(QString name) override;
|
||
};
|
||
}
|
Auch abrufbar als: Unified diff
mysql work