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 queryCategoryGetAll string
//go:embed queries/categoryNew.sql //go:embed queries/categoryNew.sql
queryCategoryNew string queryCategoryNew string
//go:embed queries/categoryDelete.sql
queryCategoryDelete string
//go:embed queries/postGetAll.sql //go:embed queries/postGetAll.sql
queryPostGetAll string queryPostGetAll string
//go:embed queries/postNew.sql //go:embed queries/postNew.sql
@ -34,6 +36,7 @@ var (
stmtCategoryById *sql.Stmt stmtCategoryById *sql.Stmt
stmtCategoryGetAll *sql.Stmt stmtCategoryGetAll *sql.Stmt
stmtCategoryNew *sql.Stmt stmtCategoryNew *sql.Stmt
stmtCategoryDelete *sql.Stmt
stmtPostGetAll *sql.Stmt stmtPostGetAll *sql.Stmt
stmtPostNew *sql.Stmt stmtPostNew *sql.Stmt
stmtPostEdit *sql.Stmt stmtPostEdit *sql.Stmt
@ -63,6 +66,11 @@ func initDBStatements(db *sql.DB) error {
return errors.Wrap(err, "failed to prepare queryCategoryNew") 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) stmtPostGetAll, err = db.Prepare(queryPostGetAll)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to prepare queryPostGetAll") return errors.Wrap(err, "failed to prepare queryPostGetAll")
@ -264,9 +272,26 @@ func (s *SQLiteMindflow) GetCategoryByID(id int64) (name string, err error) {
return 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 { func (s *SQLiteMindflow) Close() error {
stmtCategoryById.Close() stmtCategoryById.Close()
stmtCategoryNew.Close() stmtCategoryNew.Close()
stmtCategoryDelete.Close()
stmtPostDelete.Close() stmtPostDelete.Close()
stmtPostEdit.Close() stmtPostEdit.Close()
stmtPostGetAll.Close() stmtPostGetAll.Close()

View File

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