mirror of
https://github.com/arcan1s/queued.git
synced 2025-04-24 15:37:19 +00:00
use 100 symbols
This commit is contained in:
parent
4f9e3d6639
commit
2b885ebee5
@ -69,6 +69,18 @@ public:
|
||||
* administrator password SHA512
|
||||
*/
|
||||
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
|
||||
* @param _table
|
||||
@ -165,28 +177,13 @@ private:
|
||||
* @brief database 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
|
||||
* @param _columns
|
||||
* columns mapping
|
||||
* @param _record
|
||||
* 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,
|
||||
const QSqlRecord &_record) const;
|
||||
QStringList getColumnsInRecord(const QSqlRecord &_record) const;
|
||||
/**
|
||||
* @brief last insertion ID
|
||||
* @param _table
|
||||
|
@ -68,6 +68,14 @@ public:
|
||||
* @brief QueuedProcessManager class destructor
|
||||
*/
|
||||
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
|
||||
* @param _properties
|
||||
|
@ -35,6 +35,7 @@ class QueuedAdvancedSettings;
|
||||
class QueuedCorePrivate;
|
||||
class QueuedDatabase;
|
||||
class QueuedPluginManager;
|
||||
class QueuedProcess;
|
||||
class QueuedProcessManager;
|
||||
class QueuedUserManager;
|
||||
|
||||
@ -177,15 +178,15 @@ private:
|
||||
const bool _add);
|
||||
/**
|
||||
* @brief edit task
|
||||
* @param _id
|
||||
* task ID to edit
|
||||
* @param _process
|
||||
* task object
|
||||
* @param _taskData
|
||||
* task data to edit
|
||||
* @remark _taskData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful task edition
|
||||
*/
|
||||
QueuedResult<bool> editTaskPrivate(const long long _id,
|
||||
QueuedResult<bool> editTaskPrivate(QueuedProcess *_process,
|
||||
const QVariantHash &_taskData);
|
||||
/**
|
||||
* @brief edit user
|
||||
|
@ -197,9 +197,17 @@ QueuedResult<bool> QueuedCorePrivate::editTask(const long long _id,
|
||||
|
||||
auto task = m_processes->process(_id);
|
||||
if (!task) {
|
||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
||||
return QueuedError("Task does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
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;
|
||||
return QueuedError("Task does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
auto defs = QueuedProcessManager::parseDefinitions(data);
|
||||
task = new QueuedProcess(this, defs, _id);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
@ -248,7 +256,7 @@ QueuedResult<bool> QueuedCorePrivate::editTask(const long long _id,
|
||||
payload["nice"]
|
||||
= 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
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
QueuedCorePrivateHelper::editTaskPrivate(const long long _id,
|
||||
QueuedCorePrivateHelper::editTaskPrivate(QueuedProcess *_process,
|
||||
const QVariantHash &_taskData)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit task with ID" << _id;
|
||||
|
||||
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);
|
||||
}
|
||||
qCDebug(LOG_LIB) << "Edit task with ID" << _process->index();
|
||||
|
||||
// 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) {
|
||||
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";
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
// modify values stored in memory
|
||||
for (auto &property : _taskData.keys())
|
||||
task->setProperty(qPrintable(property), _taskData[property]);
|
||||
_process->setProperty(qPrintable(property), _taskData[property]);
|
||||
// notify plugins
|
||||
if (plugins())
|
||||
emit(plugins()->interface()->onEditTask(_id, _taskData));
|
||||
emit(plugins()->interface()->onEditTask(_process->index(), _taskData));
|
||||
|
||||
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
|
||||
*/
|
||||
@ -136,12 +186,11 @@ QList<QVariantHash> QueuedDatabase::get(const QString &_table,
|
||||
}
|
||||
QSqlRecord record = query.record();
|
||||
|
||||
QStringList columns = QueuedDB::DBSchema[_table].keys();
|
||||
auto dbColumns = getColumnsInRecord(columns, record);
|
||||
auto columns = getColumnsInRecord(record);
|
||||
while (query.next()) {
|
||||
QVariantHash entry;
|
||||
for (auto &column : columns)
|
||||
entry[column] = query.value(dbColumns[column]);
|
||||
entry[column] = query.value(column);
|
||||
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
|
||||
*/
|
||||
QHash<QString, int>
|
||||
QueuedDatabase::getColumnsInRecord(const QStringList &_columns,
|
||||
const QSqlRecord &_record) const
|
||||
QStringList QueuedDatabase::getColumnsInRecord(const QSqlRecord &_record) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Search for columns" << _columns;
|
||||
qCDebug(LOG_LIB) << "Search for columns" << _record;
|
||||
|
||||
return std::accumulate(
|
||||
_columns.begin(), _columns.end(), QHash<QString, int>(),
|
||||
[&_record](QHash<QString, int> &map, const QString &column) {
|
||||
map[column] = _record.indexOf(column);
|
||||
return map;
|
||||
});
|
||||
QStringList output;
|
||||
for (int i = 0; i < _record.count(); i++)
|
||||
output += _record.fieldName(i);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@ -432,9 +428,10 @@ QueuedDatabase::getQueryPayload(const QString &_table,
|
||||
qCDebug(LOG_LIB) << "Add record" << _value << "to table" << _table;
|
||||
|
||||
QHash<QString, QString> output;
|
||||
QStringList schemaColumns = QueuedDB::DBSchema[_table].keys();
|
||||
auto schemaColumns = QueuedDB::DBSchema[_table].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)
|
||||
<< "No key" << key << "found in schema of" << _table;
|
||||
continue;
|
||||
|
@ -59,13 +59,12 @@ QueuedProcessManager::~QueuedProcessManager()
|
||||
|
||||
|
||||
/**
|
||||
* @fn add
|
||||
* @fn parseDefinitions
|
||||
*/
|
||||
QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties,
|
||||
const long long _index)
|
||||
QueuedProcess::QueuedProcessDefinitions
|
||||
QueuedProcessManager::parseDefinitions(const QVariantHash &_properties)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add new process" << _properties << "with index"
|
||||
<< _index;
|
||||
qCDebug(LOG_LIB) << "Parse definitions from" << _properties;
|
||||
|
||||
QueuedProcess::QueuedProcessDefinitions defs;
|
||||
// parameters
|
||||
@ -84,7 +83,20 @@ QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties,
|
||||
defs.endTime = QDateTime::fromString(_properties["endTime"].toString(),
|
||||
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