Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (4,71 KB) Statistiken
| Zweig: | Revision:
4d58ef6a Christian Ehringfeld
#include "sqliteschema.h"
14f9beed Christian Ehringfeld
#include "../database.h"
#include <QSqlRecord>
#include <QSqlResult>
b0bf458e Christian Ehringfeld
#include "sqlitequerybuilder.h"
b0b8dac3 Christian Ehringfeld
using namespace CuteEntityManager;
4d58ef6a Christian Ehringfeld
14f9beed Christian Ehringfeld
SqliteSchema::SqliteSchema(QSharedPointer<Database> database) : Schema(database) {
b0bf458e Christian Ehringfeld
this->queryBuilder = QSharedPointer<QueryBuilder>(new SqliteQueryBuilder(QSharedPointer<Schema>(this), database));
4d58ef6a Christian Ehringfeld
}

b0b8dac3 Christian Ehringfeld
SqliteSchema::~SqliteSchema() {
4d58ef6a Christian Ehringfeld
}

24425325 Christian Ehringfeld
QSharedPointer<QHash<QString, QString>> SqliteSchema::getTypeMap() {
14f9beed Christian Ehringfeld
if (this->typeMap.data()->empty()) {
6dc7d533 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_PK, "integer PRIMARY KEY AUTOINCREMENT NOT NULL");
this->typeMap.data()->insert(TYPE_BIGPK, "integer PRIMARY KEY AUTOINCREMENT NOT NULL");
b0b8dac3 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_BOOLEAN, "boolean");
this->typeMap.data()->insert(TYPE_SMALLINT, "smallint");
6dc7d533 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_INTEGER, "integer");
b0b8dac3 Christian Ehringfeld
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");
6dc7d533 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_DECIMAL, "decimal(10,0)");
b0b8dac3 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_TEXT, "text");
6dc7d533 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_STRING, "text");
14f9beed Christian Ehringfeld
this->typeMap.data()->insert(TYPE_CHAR, "char");
b0b8dac3 Christian Ehringfeld
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");
6dc7d533 Christian Ehringfeld
this->typeMap.data()->insert(TYPE_MONEY, "decimal(19,4)");
b0b8dac3 Christian Ehringfeld
}
24425325 Christian Ehringfeld
return this->typeMap;
4d58ef6a Christian Ehringfeld
}
b0b8dac3 Christian Ehringfeld
14f9beed Christian Ehringfeld
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()) {
b0bf458e Christian Ehringfeld
q2.exec("PRAGMA index_info(" + this->quoteSimpleTableName(indexName) + ")");
14f9beed Christian Ehringfeld
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);
}
b0b8dac3 Christian Ehringfeld
14f9beed Christian Ehringfeld
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;
}