1
0

Added delete category query.

This commit is contained in:
Alexander Andreev 2023-05-22 21:39:38 +04:00
parent 3b78aa64e2
commit 089ef27cef
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 27 additions and 0 deletions

View File

@ -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()

View File

@ -0,0 +1,2 @@
DELETE FROM `category`
WHERE `category_id` = ?;