forked from ondra/server-sdk
QSqlQuery selectSQL = db.execPlainSql("SELECT * FROM names");
auto list = utils.tableFromQuery<Names>(selectSQL);
158 lines
3.9 KiB
C++
158 lines
3.9 KiB
C++
#include "databaseutils.h"
|
|
#include "databasemanager.h"
|
|
#include "databasetable.h"
|
|
#include "logger.h"
|
|
|
|
#include <QMetaProperty>
|
|
|
|
|
|
namespace serversdk {
|
|
|
|
/**
|
|
* @brief DatabaseUtils::createTableSQL
|
|
* @param table
|
|
* @return
|
|
*
|
|
* @code{sql}
|
|
* "CREATE TABLE colsunod_bject ("
|
|
"id character(36) primary key NOT NULL,"
|
|
"description character varying(255),"
|
|
"last_update timestamp NOT NULL,"
|
|
"name character varying(255),"
|
|
"pin character varying(255),"
|
|
"last_modified timestamp,"
|
|
"deleted BOOLEAN)"
|
|
* @endcode
|
|
*
|
|
* Default types:
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
DatabaseUtils::DatabaseUtils(DatabaseManager *manager, const QString type)
|
|
: mDatabaseManager(manager), mSqlType(type)
|
|
{
|
|
Q_ASSERT(mDatabaseManager);
|
|
}
|
|
|
|
|
|
QString DatabaseUtils::createTableText(const DatabaseTable *table, const QString table_name) const
|
|
{
|
|
QString userData;
|
|
|
|
if (table_name.isEmpty()) {
|
|
return QString();
|
|
}
|
|
|
|
// out: "CREATE TABLE colnod_subject ( "
|
|
QString sql = QString("CREATE TABLE %1 ( ").arg(table_name);
|
|
|
|
const QMetaObject *metaObj = table->metaObject();
|
|
int total = metaObj->propertyCount();
|
|
int first = true;
|
|
for (int i = metaObj->propertyOffset(); i < total; ++i) {
|
|
QMetaProperty prop = metaObj->property(i);
|
|
// Skip non USER so -> ( Q_PROPERTY( ...... USER false ) only
|
|
if (!prop.isUser())
|
|
continue;
|
|
|
|
QString name = prop.name();
|
|
QVariant::Type type = prop.type();
|
|
QString textType;
|
|
|
|
|
|
// Getting correct type based on QVariant
|
|
if (sqlType() == "SQLITE") {
|
|
textType = table->textSQLIteType(type);
|
|
} else if (sqlType() == "PSQL") {
|
|
textType = table->textSQLIteType(type);
|
|
//return QString();
|
|
} else {
|
|
qCDebug(sdkdb) << "Implemented only for SQLITE";
|
|
return QString();
|
|
}
|
|
|
|
// in: id TEXT
|
|
// out: "CREATE TABLE colnod_subject ( id TEXT "
|
|
sql += QString("%1 %2").arg(name,textType);
|
|
|
|
// in: PRIMARY KEY
|
|
// out: "CREATE TABLE colnod_subject ( id TEXT PRIMARY KEY"
|
|
if (first) { // first row
|
|
sql += " PRIMARY KEY";
|
|
first = false;
|
|
}
|
|
|
|
// in: NOT NULL
|
|
// out: "CREATE TABLE colnod_subject ( id TEXT PRIMARY KEY NOT NULL"
|
|
if (!userData.isEmpty()) {
|
|
sql += " " + userData;
|
|
}
|
|
|
|
// out: "CREATE TABLE colnod_subject ( id TEXT PRIMARY KEY NOT NULL,"
|
|
if (i < (total-1)) { // second last
|
|
sql += ", ";
|
|
}
|
|
}
|
|
|
|
// out: ".. subject_id TEXT)"
|
|
sql+= " )";
|
|
|
|
return sql;
|
|
}
|
|
|
|
QSqlQuery DatabaseUtils::createTable(const DatabaseTable *table, const QString tableName) const
|
|
{
|
|
// CREATE TABLE ...
|
|
QString sql = createTableText(table, tableName);
|
|
return mDatabaseManager->execPlainSql(sql);
|
|
}
|
|
|
|
|
|
QSqlQuery DatabaseUtils::insertIntoTable(const DatabaseTable *table, const QString table_name) const
|
|
{
|
|
QString columns;
|
|
QString values;
|
|
|
|
QVariantMap valueMap = table->toVariantMap();
|
|
|
|
|
|
auto keys = valueMap.keys();
|
|
for (auto key : keys) {
|
|
if (!columns.isEmpty()) {
|
|
columns += ", ";
|
|
values += ", ";
|
|
}
|
|
columns += key;
|
|
values += ":" + key;
|
|
}
|
|
|
|
QSqlQuery query(mDatabaseManager->database());
|
|
QString queryText = QString("INSERT INTO %1 ( %2 ) VALUES ( %3 );").arg(table_name).arg(columns).arg(values);
|
|
query.prepare(queryText);
|
|
|
|
for (auto key : keys ) {
|
|
auto value = valueMap.value(key);
|
|
query.bindValue(":"+key, value);
|
|
}
|
|
|
|
QSqlQuery res = mDatabaseManager->executeQuery(query);
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
QString DatabaseUtils::sqlType() const
|
|
{
|
|
return mSqlType;
|
|
}
|
|
|
|
void DatabaseUtils::setSqlType(const QString &sqlType)
|
|
{
|
|
mSqlType = sqlType;
|
|
}
|
|
|
|
|
|
|
|
} // namespace serversdk
|