mirror of
https://github.com/arcan1s/queued.git
synced 2025-04-25 07:57:18 +00:00
use 100 symbols
This commit is contained in:
parent
4f9e3d6639
commit
2b885ebee5
@ -69,6 +69,18 @@ public:
|
|||||||
* administrator password SHA512
|
* administrator password SHA512
|
||||||
*/
|
*/
|
||||||
void createAdministrator(const QString &_user, const QString &_password);
|
void createAdministrator(const QString &_user, const QString &_password);
|
||||||
|
/**
|
||||||
|
* @brief create or update actual schema in table
|
||||||
|
* @param _table
|
||||||
|
* table name
|
||||||
|
*/
|
||||||
|
void createSchema(const QString &_table);
|
||||||
|
/**
|
||||||
|
* @brief create given table
|
||||||
|
* @param _table
|
||||||
|
* table name
|
||||||
|
*/
|
||||||
|
void createTable(const QString &_table);
|
||||||
/**
|
/**
|
||||||
* @brief get all records from table
|
* @brief get all records from table
|
||||||
* @param _table
|
* @param _table
|
||||||
@ -165,28 +177,13 @@ private:
|
|||||||
* @brief database path
|
* @brief database path
|
||||||
*/
|
*/
|
||||||
QString m_path;
|
QString m_path;
|
||||||
/**
|
|
||||||
* @brief create or update actual schema in table
|
|
||||||
* @param _table
|
|
||||||
* table name
|
|
||||||
*/
|
|
||||||
void createSchema(const QString &_table);
|
|
||||||
/**
|
|
||||||
* @brief create given table
|
|
||||||
* @param _table
|
|
||||||
* table name
|
|
||||||
*/
|
|
||||||
void createTable(const QString &_table);
|
|
||||||
/**
|
/**
|
||||||
* @brief additional function to get column numbers from table
|
* @brief additional function to get column numbers from table
|
||||||
* @param _columns
|
|
||||||
* columns mapping
|
|
||||||
* @param _record
|
* @param _record
|
||||||
* SQL record from query
|
* SQL record from query
|
||||||
* @return map of column names to their numbers
|
* @return list of columns in table
|
||||||
*/
|
*/
|
||||||
QHash<QString, int> getColumnsInRecord(const QStringList &_columns,
|
QStringList getColumnsInRecord(const QSqlRecord &_record) const;
|
||||||
const QSqlRecord &_record) const;
|
|
||||||
/**
|
/**
|
||||||
* @brief last insertion ID
|
* @brief last insertion ID
|
||||||
* @param _table
|
* @param _table
|
||||||
|
@ -68,6 +68,14 @@ public:
|
|||||||
* @brief QueuedProcessManager class destructor
|
* @brief QueuedProcessManager class destructor
|
||||||
*/
|
*/
|
||||||
virtual ~QueuedProcessManager();
|
virtual ~QueuedProcessManager();
|
||||||
|
/**
|
||||||
|
* @brief parse task definitions from table data
|
||||||
|
* @param _properties
|
||||||
|
* map of task properties
|
||||||
|
* @return data mapped to internal format
|
||||||
|
*/
|
||||||
|
static QueuedProcess::QueuedProcessDefinitions
|
||||||
|
parseDefinitions(const QVariantHash &_properties);
|
||||||
/**
|
/**
|
||||||
* @brief add task
|
* @brief add task
|
||||||
* @param _properties
|
* @param _properties
|
||||||
|
@ -35,6 +35,7 @@ class QueuedAdvancedSettings;
|
|||||||
class QueuedCorePrivate;
|
class QueuedCorePrivate;
|
||||||
class QueuedDatabase;
|
class QueuedDatabase;
|
||||||
class QueuedPluginManager;
|
class QueuedPluginManager;
|
||||||
|
class QueuedProcess;
|
||||||
class QueuedProcessManager;
|
class QueuedProcessManager;
|
||||||
class QueuedUserManager;
|
class QueuedUserManager;
|
||||||
|
|
||||||
@ -177,15 +178,15 @@ private:
|
|||||||
const bool _add);
|
const bool _add);
|
||||||
/**
|
/**
|
||||||
* @brief edit task
|
* @brief edit task
|
||||||
* @param _id
|
* @param _process
|
||||||
* task ID to edit
|
* task object
|
||||||
* @param _taskData
|
* @param _taskData
|
||||||
* task data to edit
|
* task data to edit
|
||||||
* @remark _taskData should contain only fields defined in schema, any other
|
* @remark _taskData should contain only fields defined in schema, any other
|
||||||
* fields will be ignored. No need to pass all properties here
|
* fields will be ignored. No need to pass all properties here
|
||||||
* @return true on successful task edition
|
* @return true on successful task edition
|
||||||
*/
|
*/
|
||||||
QueuedResult<bool> editTaskPrivate(const long long _id,
|
QueuedResult<bool> editTaskPrivate(QueuedProcess *_process,
|
||||||
const QVariantHash &_taskData);
|
const QVariantHash &_taskData);
|
||||||
/**
|
/**
|
||||||
* @brief edit user
|
* @brief edit user
|
||||||
|
@ -197,11 +197,19 @@ QueuedResult<bool> QueuedCorePrivate::editTask(const long long _id,
|
|||||||
|
|
||||||
auto task = m_processes->process(_id);
|
auto task = m_processes->process(_id);
|
||||||
if (!task) {
|
if (!task) {
|
||||||
|
qCInfo(LOG_LIB) << "Try to get information about task" << _id
|
||||||
|
<< "from database";
|
||||||
|
auto data = m_database->get(QueuedDB::TASKS_TABLE, _id);
|
||||||
|
if (data.isEmpty()) {
|
||||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
||||||
return QueuedError("Task does not exist",
|
return QueuedError("Task does not exist",
|
||||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto defs = QueuedProcessManager::parseDefinitions(data);
|
||||||
|
task = new QueuedProcess(this, defs, _id);
|
||||||
|
}
|
||||||
|
|
||||||
// check permissions
|
// check permissions
|
||||||
auto authUser = m_users->user(_token, true);
|
auto authUser = m_users->user(_token, true);
|
||||||
if (!authUser) {
|
if (!authUser) {
|
||||||
@ -248,7 +256,7 @@ QueuedResult<bool> QueuedCorePrivate::editTask(const long long _id,
|
|||||||
payload["nice"]
|
payload["nice"]
|
||||||
= std::min(payload["nice"].toUInt(), authUser->priority());
|
= std::min(payload["nice"].toUInt(), authUser->priority());
|
||||||
|
|
||||||
return m_helper->editTaskPrivate(_id, payload);
|
return m_helper->editTaskPrivate(task, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -297,32 +297,25 @@ QueuedCorePrivateHelper::editPluginPrivate(const QString &_plugin,
|
|||||||
* @fn editTaskPrivate
|
* @fn editTaskPrivate
|
||||||
*/
|
*/
|
||||||
QueuedResult<bool>
|
QueuedResult<bool>
|
||||||
QueuedCorePrivateHelper::editTaskPrivate(const long long _id,
|
QueuedCorePrivateHelper::editTaskPrivate(QueuedProcess *_process,
|
||||||
const QVariantHash &_taskData)
|
const QVariantHash &_taskData)
|
||||||
{
|
{
|
||||||
qCDebug(LOG_LIB) << "Edit task with ID" << _id;
|
qCDebug(LOG_LIB) << "Edit task with ID" << _process->index();
|
||||||
|
|
||||||
auto task = processes()->process(_id);
|
|
||||||
if (!task) {
|
|
||||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
|
||||||
return QueuedError("Task does not exist",
|
|
||||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
|
||||||
}
|
|
||||||
|
|
||||||
// modify record in database first
|
// modify record in database first
|
||||||
bool status = database()->modify(QueuedDB::TASKS_TABLE, _id, _taskData);
|
bool status = database()->modify(QueuedDB::TASKS_TABLE, _process->index(), _taskData);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
qCWarning(LOG_LIB) << "Could not modify task record" << _id
|
qCWarning(LOG_LIB) << "Could not modify task record" << _process->index()
|
||||||
<< "in database, do not edit it in memory";
|
<< "in database, do not edit it in memory";
|
||||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// modify values stored in memory
|
// modify values stored in memory
|
||||||
for (auto &property : _taskData.keys())
|
for (auto &property : _taskData.keys())
|
||||||
task->setProperty(qPrintable(property), _taskData[property]);
|
_process->setProperty(qPrintable(property), _taskData[property]);
|
||||||
// notify plugins
|
// notify plugins
|
||||||
if (plugins())
|
if (plugins())
|
||||||
emit(plugins()->interface()->onEditTask(_id, _taskData));
|
emit(plugins()->interface()->onEditTask(_process->index(), _taskData));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,56 @@ void QueuedDatabase::createAdministrator(const QString &_user,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn createSchema
|
||||||
|
*/
|
||||||
|
void QueuedDatabase::createSchema(const QString &_table)
|
||||||
|
{
|
||||||
|
qCDebug(LOG_LIB) << "Create schema for" << _table;
|
||||||
|
|
||||||
|
QSqlRecord record = m_database.record(_table);
|
||||||
|
// get column names
|
||||||
|
QStringList columns;
|
||||||
|
for (int i = 0; i < record.count(); i++)
|
||||||
|
columns.append(record.fieldName(i));
|
||||||
|
|
||||||
|
// check and append if any
|
||||||
|
QStringList schemaColumns = QueuedDB::DBSchema[_table].keys();
|
||||||
|
for (auto &column : schemaColumns) {
|
||||||
|
if (columns.contains(column))
|
||||||
|
continue;
|
||||||
|
QueuedDB::QueuedDBField field = QueuedDB::DBSchema[_table][column];
|
||||||
|
QSqlQuery query
|
||||||
|
= m_database.exec(QString("ALTER TABLE '%1' ADD `%2` %3")
|
||||||
|
.arg(_table)
|
||||||
|
.arg(column)
|
||||||
|
.arg(field.sqlDescription));
|
||||||
|
QSqlError error = query.lastError();
|
||||||
|
if (error.isValid())
|
||||||
|
qCCritical(LOG_LIB)
|
||||||
|
<< "Could not insert column" << column << "to table" << _table
|
||||||
|
<< "error:" << error.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn createTable
|
||||||
|
*/
|
||||||
|
void QueuedDatabase::createTable(const QString &_table)
|
||||||
|
{
|
||||||
|
qCDebug(LOG_LIB) << "Create table" << _table;
|
||||||
|
|
||||||
|
QSqlQuery query = m_database.exec(
|
||||||
|
QString("CREATE TABLE '%1' (`_id` INTEGER PRIMARY KEY AUTOINCREMENT)")
|
||||||
|
.arg(_table));
|
||||||
|
QSqlError error = query.lastError();
|
||||||
|
if (error.isValid())
|
||||||
|
qCCritical(LOG_LIB)
|
||||||
|
<< "Could not create table" << _table << "error:" << error.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn get
|
* @fn get
|
||||||
*/
|
*/
|
||||||
@ -136,12 +186,11 @@ QList<QVariantHash> QueuedDatabase::get(const QString &_table,
|
|||||||
}
|
}
|
||||||
QSqlRecord record = query.record();
|
QSqlRecord record = query.record();
|
||||||
|
|
||||||
QStringList columns = QueuedDB::DBSchema[_table].keys();
|
auto columns = getColumnsInRecord(record);
|
||||||
auto dbColumns = getColumnsInRecord(columns, record);
|
|
||||||
while (query.next()) {
|
while (query.next()) {
|
||||||
QVariantHash entry;
|
QVariantHash entry;
|
||||||
for (auto &column : columns)
|
for (auto &column : columns)
|
||||||
entry[column] = query.value(dbColumns[column]);
|
entry[column] = query.value(column);
|
||||||
output.append(entry);
|
output.append(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -331,71 +380,18 @@ void QueuedDatabase::removeUsers(const QDateTime &_lastLogin)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn createSchema
|
|
||||||
*/
|
|
||||||
void QueuedDatabase::createSchema(const QString &_table)
|
|
||||||
{
|
|
||||||
qCDebug(LOG_LIB) << "Create schema for" << _table;
|
|
||||||
|
|
||||||
QSqlRecord record = m_database.record(_table);
|
|
||||||
// get column names
|
|
||||||
QStringList columns;
|
|
||||||
for (int i = 0; i < record.count(); i++)
|
|
||||||
columns.append(record.fieldName(i));
|
|
||||||
|
|
||||||
// check and append if any
|
|
||||||
QStringList schemaColumns = QueuedDB::DBSchema[_table].keys();
|
|
||||||
for (auto &column : schemaColumns) {
|
|
||||||
if (columns.contains(column))
|
|
||||||
continue;
|
|
||||||
QueuedDB::QueuedDBField field = QueuedDB::DBSchema[_table][column];
|
|
||||||
QSqlQuery query
|
|
||||||
= m_database.exec(QString("ALTER TABLE '%1' ADD `%2` %3")
|
|
||||||
.arg(_table)
|
|
||||||
.arg(column)
|
|
||||||
.arg(field.sqlDescription));
|
|
||||||
QSqlError error = query.lastError();
|
|
||||||
if (error.isValid())
|
|
||||||
qCCritical(LOG_LIB)
|
|
||||||
<< "Could not insert column" << column << "to table" << _table
|
|
||||||
<< "error:" << error.text();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn createTable
|
|
||||||
*/
|
|
||||||
void QueuedDatabase::createTable(const QString &_table)
|
|
||||||
{
|
|
||||||
qCDebug(LOG_LIB) << "Create table" << _table;
|
|
||||||
|
|
||||||
QSqlQuery query = m_database.exec(
|
|
||||||
QString("CREATE TABLE '%1' (`_id` INTEGER PRIMARY KEY AUTOINCREMENT)")
|
|
||||||
.arg(_table));
|
|
||||||
QSqlError error = query.lastError();
|
|
||||||
if (error.isValid())
|
|
||||||
qCCritical(LOG_LIB)
|
|
||||||
<< "Could not create table" << _table << "error:" << error.text();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn getColumnsInRecord
|
* @fn getColumnsInRecord
|
||||||
*/
|
*/
|
||||||
QHash<QString, int>
|
QStringList QueuedDatabase::getColumnsInRecord(const QSqlRecord &_record) const
|
||||||
QueuedDatabase::getColumnsInRecord(const QStringList &_columns,
|
|
||||||
const QSqlRecord &_record) const
|
|
||||||
{
|
{
|
||||||
qCDebug(LOG_LIB) << "Search for columns" << _columns;
|
qCDebug(LOG_LIB) << "Search for columns" << _record;
|
||||||
|
|
||||||
return std::accumulate(
|
QStringList output;
|
||||||
_columns.begin(), _columns.end(), QHash<QString, int>(),
|
for (int i = 0; i < _record.count(); i++)
|
||||||
[&_record](QHash<QString, int> &map, const QString &column) {
|
output += _record.fieldName(i);
|
||||||
map[column] = _record.indexOf(column);
|
|
||||||
return map;
|
return output;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -432,9 +428,10 @@ QueuedDatabase::getQueryPayload(const QString &_table,
|
|||||||
qCDebug(LOG_LIB) << "Add record" << _value << "to table" << _table;
|
qCDebug(LOG_LIB) << "Add record" << _value << "to table" << _table;
|
||||||
|
|
||||||
QHash<QString, QString> output;
|
QHash<QString, QString> output;
|
||||||
QStringList schemaColumns = QueuedDB::DBSchema[_table].keys();
|
auto schemaColumns = QueuedDB::DBSchema[_table].keys();
|
||||||
for (auto &key : _value.keys()) {
|
for (auto &key : _value.keys()) {
|
||||||
if (!schemaColumns.contains(key)) {
|
// we would check it only if there is data about this table
|
||||||
|
if (!schemaColumns.isEmpty() && !schemaColumns.contains(key)) {
|
||||||
qCWarning(LOG_LIB)
|
qCWarning(LOG_LIB)
|
||||||
<< "No key" << key << "found in schema of" << _table;
|
<< "No key" << key << "found in schema of" << _table;
|
||||||
continue;
|
continue;
|
||||||
|
@ -59,13 +59,12 @@ QueuedProcessManager::~QueuedProcessManager()
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn add
|
* @fn parseDefinitions
|
||||||
*/
|
*/
|
||||||
QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties,
|
QueuedProcess::QueuedProcessDefinitions
|
||||||
const long long _index)
|
QueuedProcessManager::parseDefinitions(const QVariantHash &_properties)
|
||||||
{
|
{
|
||||||
qCDebug(LOG_LIB) << "Add new process" << _properties << "with index"
|
qCDebug(LOG_LIB) << "Parse definitions from" << _properties;
|
||||||
<< _index;
|
|
||||||
|
|
||||||
QueuedProcess::QueuedProcessDefinitions defs;
|
QueuedProcess::QueuedProcessDefinitions defs;
|
||||||
// parameters
|
// parameters
|
||||||
@ -84,7 +83,20 @@ QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties,
|
|||||||
defs.endTime = QDateTime::fromString(_properties["endTime"].toString(),
|
defs.endTime = QDateTime::fromString(_properties["endTime"].toString(),
|
||||||
Qt::ISODateWithMs);
|
Qt::ISODateWithMs);
|
||||||
|
|
||||||
return add(defs, _index);
|
return defs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn add
|
||||||
|
*/
|
||||||
|
QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties,
|
||||||
|
const long long _index)
|
||||||
|
{
|
||||||
|
qCDebug(LOG_LIB) << "Add new process" << _properties << "with index"
|
||||||
|
<< _index;
|
||||||
|
|
||||||
|
return add(parseDefinitions(_properties), _index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user