Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (4,64 KB) Statistiken
| Zweig: | Revision:
c22391b2 Christian Ehringfeld
/*
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
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
e0e1ead8 Christian Ehringfeld
SqliteSchema::SqliteSchema(QSharedPointer<Database> database) : Schema(
57d6da31 Christian Ehringfeld
database, QSharedPointer<QueryBuilder>(new SqliteQueryBuilder(this, database))) {
4d58ef6a Christian Ehringfeld
}

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

24425325 Christian Ehringfeld
QSharedPointer<QHash<QString, QString>> SqliteSchema::getTypeMap() {
abb9e8c5 Christian Ehringfeld
if (this->typeMap->empty()) {
this->typeMap->insert(TYPE_PK,
66704054 Christian Ehringfeld
"integer PRIMARY KEY AUTOINCREMENT NOT NULL");
abb9e8c5 Christian Ehringfeld
this->typeMap->insert(TYPE_BIGPK,
ed9ffe4a Christian Ehringfeld
"integer PRIMARY KEY AUTOINCREMENT NOT NULL");
abb9e8c5 Christian Ehringfeld
this->typeMap->insert(TYPE_BOOLEAN, "boolean");
22b97d74 Christian Ehringfeld
this->typeMap->insert(TYPE_SMALLINT, "integer");
abb9e8c5 Christian Ehringfeld
this->typeMap->insert(TYPE_INTEGER, "integer");
22b97d74 Christian Ehringfeld
this->typeMap->insert(TYPE_BIGINT, "integer");
abb9e8c5 Christian Ehringfeld
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)");
b0b8dac3 Christian Ehringfeld
}
24425325 Christian Ehringfeld
return this->typeMap;
4d58ef6a Christian Ehringfeld
}
b0b8dac3 Christian Ehringfeld
14f9beed Christian Ehringfeld
QStringList SqliteSchema::findTableNames(QString schema) {
b0e92bc6 Christian Ehringfeld
Q_UNUSED(schema)
14f9beed Christian Ehringfeld
auto l = QStringList();
e0e1ead8 Christian Ehringfeld
QString sql =
"SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name";
abb9e8c5 Christian Ehringfeld
auto q = this->database->getQuery();
14f9beed Christian Ehringfeld
q.prepare(sql);
abb9e8c5 Christian Ehringfeld
this->database->select(q);
14f9beed Christian Ehringfeld
while (q.next()) {
l.append(q.value(0).toString());
}
return l;
}

e0e1ead8 Christian Ehringfeld
QHash<QString, QStringList> SqliteSchema::findUniqueIndexes(
const QSharedPointer<TableSchema>
&table) {
14f9beed Christian Ehringfeld
QHash<QString, QStringList> uniqueIndexes = QHash<QString, QStringList>();
abb9e8c5 Christian Ehringfeld
QSqlQuery q = this->database->getQuery();
14f9beed Christian Ehringfeld
q.setForwardOnly(true);
e0e1ead8 Christian Ehringfeld
q.exec("PRAGMA index_list(" + this->quoteSimpleTableName(
table->getName()) + ')');
14f9beed Christian Ehringfeld
while (q.next()) {
QString indexName = q.value("name").toString();
abb9e8c5 Christian Ehringfeld
QSqlQuery q2 = this->database->getQuery();
14f9beed Christian Ehringfeld
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) {
abb9e8c5 Christian Ehringfeld
QSqlQuery q = this->database->getQuery();
14f9beed Christian Ehringfeld
q.setForwardOnly(true);
e0e1ead8 Christian Ehringfeld
q.exec("PRAGMA foreign_key_list(" + this->quoteSimpleTableName(
abb9e8c5 Christian Ehringfeld
ts->getName()) + ')');
auto foreignKeys = ts->getRelations();
14f9beed Christian Ehringfeld
while (q.next()) {
bool ok;
int id = q.value("id").toInt(&ok);
if (ok) {
e0e1ead8 Christian Ehringfeld
auto rel = new QSqlRelation(q.value("table").toString(),
q.value("from").toString(),
q.value("to").toString());
14f9beed Christian Ehringfeld
auto ptr = QSharedPointer<QSqlRelation>(rel);
foreignKeys.insert(QString::number(id), ptr);
}
}
abb9e8c5 Christian Ehringfeld
ts->setRelations(foreignKeys);
14f9beed Christian Ehringfeld
}
b0b8dac3 Christian Ehringfeld
14f9beed Christian Ehringfeld
QSharedPointer<TableSchema> SqliteSchema::loadTableSchema(QString name) {
abb9e8c5 Christian Ehringfeld
auto ptr = QSharedPointer<TableSchema>(new TableSchema());
ptr->setName(name);
ptr->setFullName(name);
14f9beed Christian Ehringfeld
if (this->findColumns(ptr)) {
this->findConstraints(ptr);
} else {
ptr.clear();
}
return ptr;
}