mirror of
https://github.com/arcan1s/queued.git
synced 2025-06-30 15:35:54 +00:00
more pretty schema control
This commit is contained in:
@ -29,8 +29,6 @@
|
|||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
|
|
||||||
|
|
||||||
typedef QHash<QString, QStringList> QueuedDBSchema;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief queued adaptor to databases
|
* @brief queued adaptor to databases
|
||||||
*/
|
*/
|
||||||
@ -39,13 +37,6 @@ class QueuedDatabase : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QString path READ path)
|
Q_PROPERTY(QString path READ path)
|
||||||
|
|
||||||
// TODO add fields SQL parameters (type whatever), Qt specific types
|
|
||||||
const QueuedDBSchema DBSchema = {
|
|
||||||
{"users",
|
|
||||||
{"_id", "name", "uid", "gid", "password_sha512", "email", "cpu", "gpu",
|
|
||||||
"memory", "gpumemory", "storage"}},
|
|
||||||
{"tasks", {"_id", "userId", "command", "arguments", "workDirectory"}}};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief QueuedDatabase class constructor
|
* @brief QueuedDatabase class constructor
|
||||||
|
85
sources/queued/include/queued/QueuedDatabaseSchema.h
Normal file
85
sources/queued/include/queued/QueuedDatabaseSchema.h
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Evgeniy Alekseev
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
*
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @file QueuedDatabaseSchema.h
|
||||||
|
* Header of Queued library
|
||||||
|
* @author Evgeniy Alekseev
|
||||||
|
* @copyright MIT
|
||||||
|
* @bug https://github.com/arcan1s/queued/issues
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef QUEUEDDATABASESCHEMA_H
|
||||||
|
#define QUEUEDDATABASESCHEMA_H
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @struct QueuedDBField
|
||||||
|
* @brief describes database column
|
||||||
|
* @var name
|
||||||
|
* column name
|
||||||
|
* @var sqlDescription
|
||||||
|
* description to create column
|
||||||
|
* @var type
|
||||||
|
* Qt type of column for cast
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
QString name;
|
||||||
|
QString sqlDescription;
|
||||||
|
QVariant::Type type;
|
||||||
|
} QueuedDBField;
|
||||||
|
/**
|
||||||
|
* @typedef QueuedDBSchema
|
||||||
|
* custom map for database schemas descriptions
|
||||||
|
*/
|
||||||
|
typedef QHash<QString, QHash<QString, QueuedDBField>> QueuedDBSchema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup QueuedDB
|
||||||
|
* @brief Queued database related constants
|
||||||
|
*/
|
||||||
|
namespace QueuedDB
|
||||||
|
{
|
||||||
|
const QueuedDBSchema DBSchema = {
|
||||||
|
{"users",
|
||||||
|
{{"_id",
|
||||||
|
{"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}},
|
||||||
|
{"name", {"name", "TEXT NOT NULL DEFAULT '0'", QVariant::String}},
|
||||||
|
{"uid", {"uid", "INT NOT NULL DEFAULT 0", QVariant::UInt}},
|
||||||
|
{"gid", {"gid", "INT NOT NULL DEFAULT 0", QVariant::UInt}},
|
||||||
|
{"passwordSHA512", {"passwordSHA512", "TEXT", QVariant::String}},
|
||||||
|
{"email", {"email", "TEXT", QVariant::String}},
|
||||||
|
{"cpu", {"cpu", "INT", QVariant::LongLong}},
|
||||||
|
{"gpu", {"gpu", "INT", QVariant::LongLong}},
|
||||||
|
{"memory", {"memory", "INT", QVariant::LongLong}},
|
||||||
|
{"gpumemory", {"gpumemory", "INT", QVariant::LongLong}},
|
||||||
|
{"storage", {"storage", "INT", QVariant::LongLong}},
|
||||||
|
{"permissions", {"permissions", "INT", QVariant::LongLong}}}},
|
||||||
|
{"tasks",
|
||||||
|
{{"_id",
|
||||||
|
{"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}},
|
||||||
|
{"userId", {"userId", "INT NOT NULL DEFAULT 0", QVariant::LongLong}},
|
||||||
|
{"command", {"command", "TEXT", QVariant::String}},
|
||||||
|
{"arguments", {"arguments", "TEXT", QVariant::String}},
|
||||||
|
{"workDirectory", {"workDirectory", "TEXT", QVariant::String}},
|
||||||
|
{"nice", {"nice", "INT", QVariant::Int}},
|
||||||
|
{"startTime", {"startTime", "INT", QVariant::LongLong}},
|
||||||
|
{"endTime", {"endTime", "INT", QVariant::LongLong}}}}};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* QUEUEDDATABASESCHEMA_H */
|
@ -48,6 +48,10 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_PL)
|
|||||||
*/
|
*/
|
||||||
Q_DECLARE_LOGGING_CATEGORY(LOG_SERV)
|
Q_DECLARE_LOGGING_CATEGORY(LOG_SERV)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup QueuedDebug
|
||||||
|
* @brief Queued debug functions
|
||||||
|
*/
|
||||||
namespace QueuedDebug
|
namespace QueuedDebug
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -24,13 +24,22 @@
|
|||||||
#ifndef QUEUEDPROCESSMANAGER_H
|
#ifndef QUEUEDPROCESSMANAGER_H
|
||||||
#define QUEUEDPROCESSMANAGER_H
|
#define QUEUEDPROCESSMANAGER_H
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include "queued/QueuedProcess.h"
|
#include "queued/QueuedProcess.h"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef QueuedProcessMap
|
||||||
|
* map of indices to QueuedProcess pointers
|
||||||
|
*/
|
||||||
typedef QHash<long long, QueuedProcess *> QueuedProcessMap;
|
typedef QHash<long long, QueuedProcess *> QueuedProcessMap;
|
||||||
|
/**
|
||||||
|
* @typedef QueuedProcessConnectionMap
|
||||||
|
* map of indices to related QMetaObject::Connection
|
||||||
|
*/
|
||||||
typedef QHash<long long, QMetaObject::Connection> QueuedProcessConnectionMap;
|
typedef QHash<long long, QMetaObject::Connection> QueuedProcessConnectionMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,6 +101,20 @@ public:
|
|||||||
*/
|
*/
|
||||||
void remove(const long long _index);
|
void remove(const long long _index);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
/**
|
||||||
|
* @brief signal which will be called on task start
|
||||||
|
* @param _index task index
|
||||||
|
* @param _time task start time
|
||||||
|
*/
|
||||||
|
void taskStartTimeReceived(const long long _index, const QDateTime _time);
|
||||||
|
/**
|
||||||
|
* @brief signal which will be called on task end
|
||||||
|
* @param _index task index
|
||||||
|
* @param _time task stop time
|
||||||
|
*/
|
||||||
|
void taskStopTimeReceived(const long long _index, const QDateTime _time);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
/**
|
/**
|
||||||
* @brief slot for catching finished tasks
|
* @brief slot for catching finished tasks
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QSqlRecord>
|
#include <QSqlRecord>
|
||||||
|
|
||||||
|
#include "queued/QueuedDatabaseSchema.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn QueuedDatabase
|
* @fn QueuedDatabase
|
||||||
@ -80,7 +82,8 @@ void QueuedDatabase::open(const QString _hostname, const int _port,
|
|||||||
void QueuedDatabase::checkDatabase()
|
void QueuedDatabase::checkDatabase()
|
||||||
{
|
{
|
||||||
QStringList tables = m_database.tables();
|
QStringList tables = m_database.tables();
|
||||||
for (auto table : DBSchema.keys()) {
|
QStringList schemaTables = QueuedDB::DBSchema.keys();
|
||||||
|
for (auto &table : schemaTables) {
|
||||||
// create table if does not exist
|
// create table if does not exist
|
||||||
if (!tables.contains(table))
|
if (!tables.contains(table))
|
||||||
createTable(table);
|
createTable(table);
|
||||||
@ -113,13 +116,16 @@ void QueuedDatabase::createSchema(const QString _table)
|
|||||||
columns.append(record.fieldName(i));
|
columns.append(record.fieldName(i));
|
||||||
|
|
||||||
// check and append if any
|
// check and append if any
|
||||||
for (auto column : DBSchema[_table]) {
|
QStringList schemaColumns = QueuedDB::DBSchema[_table].keys();
|
||||||
|
for (auto &column : schemaColumns) {
|
||||||
if (columns.contains(column))
|
if (columns.contains(column))
|
||||||
continue;
|
continue;
|
||||||
|
QueuedDBField field = QueuedDB::DBSchema[_table][column];
|
||||||
QSqlQuery query
|
QSqlQuery query
|
||||||
= m_database.exec(QString("ALTER TABLE '%1' add column `%2`")
|
= m_database.exec(QString("ALTER TABLE '%1' ADD `%2` %3")
|
||||||
.arg(_table)
|
.arg(_table)
|
||||||
.arg(column));
|
.arg(column)
|
||||||
|
.arg(field.sqlDescription));
|
||||||
QSqlError error = query.lastError();
|
QSqlError error = query.lastError();
|
||||||
if (error.isValid())
|
if (error.isValid())
|
||||||
qCCritical(LOG_LIB) << "Could not insert column" << column
|
qCCritical(LOG_LIB) << "Could not insert column" << column
|
||||||
|
@ -142,5 +142,8 @@ void QueuedProcessManager::taskFinished(const int _exitCode,
|
|||||||
qCDebug(LOG_LIB) << "Process" << _index << "finished with code" << _exitCode
|
qCDebug(LOG_LIB) << "Process" << _index << "finished with code" << _exitCode
|
||||||
<< "and status" << _exitStatus;
|
<< "and status" << _exitStatus;
|
||||||
|
|
||||||
|
emit(taskStopTimeReceived(_index, QDateTime::currentDateTimeUtc()));
|
||||||
// TODO implementation
|
// TODO implementation
|
||||||
|
// TODO emit signal for new task here
|
||||||
|
// emit(taskStartTimeReceived(_index, QDateTime::currentDateTimeUtc()));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user