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;
|
||
|
}
|
||
|
|
||
|
}
|
||
Auch abrufbar als: Unified diff
wip