добавил создание, хранение, удаление действий
This commit is contained in:
+172
@@ -1399,3 +1399,175 @@ bool uDataBase::deleteNotification(int port)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::createActionsTable()
|
||||
{
|
||||
QSqlQuery query(m_db);
|
||||
QString sql =
|
||||
"CREATE TABLE IF NOT EXISTS actions ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" type INTEGER NOT NULL,"
|
||||
" key_combination TEXT,"
|
||||
" audio_file TEXT,"
|
||||
" notification_title TEXT,"
|
||||
" notification_description TEXT,"
|
||||
" notification_image TEXT,"
|
||||
" notification_sound TEXT,"
|
||||
" created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
|
||||
")";
|
||||
|
||||
if (!query.exec(sql)) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to create actions table:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::saveAction(const ActionData &action)
|
||||
{
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Создаём таблицу, если ещё нет
|
||||
if (!tableExists("actions")) {
|
||||
if (!createActionsTable())
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(
|
||||
"INSERT INTO actions ("
|
||||
" type, key_combination, audio_file, notification_title,"
|
||||
" notification_description, notification_image, notification_sound"
|
||||
") VALUES ("
|
||||
" :type, :key_combination, :audio_file, :notification_title,"
|
||||
" :notification_description, :notification_image, :notification_sound"
|
||||
")"
|
||||
);
|
||||
|
||||
query.bindValue(":type", action.type);
|
||||
query.bindValue(":key_combination", action.keyCombination);
|
||||
query.bindValue(":audio_file", action.audioFile);
|
||||
query.bindValue(":notification_title", action.notificationTitle);
|
||||
query.bindValue(":notification_description", action.notificationDescription);
|
||||
query.bindValue(":notification_image", action.notificationImage);
|
||||
query.bindValue(":notification_sound", action.notificationSound);
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to save action:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::updateAction(int id, const ActionData &action)
|
||||
{
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(
|
||||
"UPDATE actions SET "
|
||||
" type = :type,"
|
||||
" key_combination = :key_combination,"
|
||||
" audio_file = :audio_file,"
|
||||
" notification_title = :notification_title,"
|
||||
" notification_description = :notification_description,"
|
||||
" notification_image = :notification_image,"
|
||||
" notification_sound = :notification_sound "
|
||||
"WHERE id = :id"
|
||||
);
|
||||
|
||||
query.bindValue(":type", action.type);
|
||||
query.bindValue(":key_combination", action.keyCombination);
|
||||
query.bindValue(":audio_file", action.audioFile);
|
||||
query.bindValue(":notification_title", action.notificationTitle);
|
||||
query.bindValue(":notification_description", action.notificationDescription);
|
||||
query.bindValue(":notification_image", action.notificationImage);
|
||||
query.bindValue(":notification_sound", action.notificationSound);
|
||||
query.bindValue(":id", id);
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to update action:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uDataBase::deleteAction(int id)
|
||||
{
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("DELETE FROM actions WHERE id = :id");
|
||||
query.bindValue(":id", id);
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to delete action:" << m_lastError;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<ActionData> uDataBase::loadAllActions()
|
||||
{
|
||||
QList<ActionData> actions;
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return actions;
|
||||
}
|
||||
|
||||
if (!tableExists("actions")) {
|
||||
return actions; // таблицы нет – пустой список
|
||||
}
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("SELECT * FROM actions ORDER BY id");
|
||||
|
||||
if (!query.exec()) {
|
||||
m_lastError = query.lastError().text();
|
||||
qWarning() << "Failed to load actions:" << m_lastError;
|
||||
return actions;
|
||||
}
|
||||
|
||||
while (query.next()) {
|
||||
ActionData a;
|
||||
a.id = query.value("id").toInt();
|
||||
a.type = query.value("type").toInt();
|
||||
a.keyCombination = query.value("key_combination").toString();
|
||||
a.audioFile = query.value("audio_file").toString();
|
||||
a.notificationTitle = query.value("notification_title").toString();
|
||||
a.notificationDescription = query.value("notification_description").toString();
|
||||
a.notificationImage = query.value("notification_image").toString();
|
||||
a.notificationSound = query.value("notification_sound").toString();
|
||||
actions.append(a);
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
|
||||
bool uDataBase::clearActionsTable()
|
||||
{
|
||||
if (!m_db.isOpen()) {
|
||||
m_lastError = "Database is not open";
|
||||
return false;
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
if (!query.exec("DELETE FROM actions")) {
|
||||
m_lastError = query.lastError().text();
|
||||
return false;
|
||||
}
|
||||
// Сброс автоинкремента
|
||||
query.exec("DELETE FROM sqlite_sequence WHERE name='actions'");
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user