From 089ef27cef5e7a755335c74623b09c7a773316d2 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 22 May 2023 21:39:38 +0400 Subject: [PATCH] Added delete category query. --- pkg/mindflow/database/sqlite/db.go | 25 +++++++++++++++++++ .../sqlite/queries/categoryDelete.sql | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 pkg/mindflow/database/sqlite/queries/categoryDelete.sql diff --git a/pkg/mindflow/database/sqlite/db.go b/pkg/mindflow/database/sqlite/db.go index 3c5a130..1f24704 100644 --- a/pkg/mindflow/database/sqlite/db.go +++ b/pkg/mindflow/database/sqlite/db.go @@ -20,6 +20,8 @@ var ( queryCategoryGetAll string //go:embed queries/categoryNew.sql queryCategoryNew string + //go:embed queries/categoryDelete.sql + queryCategoryDelete string //go:embed queries/postGetAll.sql queryPostGetAll string //go:embed queries/postNew.sql @@ -34,6 +36,7 @@ var ( stmtCategoryById *sql.Stmt stmtCategoryGetAll *sql.Stmt stmtCategoryNew *sql.Stmt + stmtCategoryDelete *sql.Stmt stmtPostGetAll *sql.Stmt stmtPostNew *sql.Stmt stmtPostEdit *sql.Stmt @@ -63,6 +66,11 @@ func initDBStatements(db *sql.DB) error { return errors.Wrap(err, "failed to prepare queryCategoryNew") } + stmtCategoryDelete, err = db.Prepare(queryCategoryDelete) + if err != nil { + return errors.Wrap(err, "failed to prepare queryCategoryDelete") + } + stmtPostGetAll, err = db.Prepare(queryPostGetAll) if err != nil { return errors.Wrap(err, "failed to prepare queryPostGetAll") @@ -264,9 +272,26 @@ func (s *SQLiteMindflow) GetCategoryByID(id int64) (name string, err error) { return } +func (s *SQLiteMindflow) DeleteCategory(id int64) (err error) { + tx, err := s.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + if _, err = tx.Stmt(stmtCategoryDelete).Exec(id); err != nil { + return err + } + + tx.Commit() + + return nil +} + func (s *SQLiteMindflow) Close() error { stmtCategoryById.Close() stmtCategoryNew.Close() + stmtCategoryDelete.Close() stmtPostDelete.Close() stmtPostEdit.Close() stmtPostGetAll.Close() diff --git a/pkg/mindflow/database/sqlite/queries/categoryDelete.sql b/pkg/mindflow/database/sqlite/queries/categoryDelete.sql new file mode 100644 index 0000000..6da5a6d --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/categoryDelete.sql @@ -0,0 +1,2 @@ +DELETE FROM `category` + WHERE `category_id` = ?; \ No newline at end of file