From 1bf5dfba6ad65bfb5b1a7f5a1adcf5d4d3e1ff7f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 10 May 2023 03:21:29 +0400 Subject: [PATCH] Added a to be completed Mindflow SQLite3 DB and queries. --- pkg/mindflow/database/sqlite/db.go | 90 +++++++++++++++++++ .../database/sqlite/queries/categoryById.sql | 4 + .../database/sqlite/queries/categoryNew.sql | 3 + .../database/sqlite/queries/postEdit.sql | 5 ++ .../database/sqlite/queries/postGetAll.sql | 9 ++ .../database/sqlite/queries/postNew.sql | 4 + .../database/sqlite/queries/schema.sql | 25 ++++++ 7 files changed, 140 insertions(+) create mode 100644 pkg/mindflow/database/sqlite/db.go create mode 100644 pkg/mindflow/database/sqlite/queries/categoryById.sql create mode 100644 pkg/mindflow/database/sqlite/queries/categoryNew.sql create mode 100644 pkg/mindflow/database/sqlite/queries/postEdit.sql create mode 100644 pkg/mindflow/database/sqlite/queries/postGetAll.sql create mode 100644 pkg/mindflow/database/sqlite/queries/postNew.sql create mode 100644 pkg/mindflow/database/sqlite/queries/schema.sql diff --git a/pkg/mindflow/database/sqlite/db.go b/pkg/mindflow/database/sqlite/db.go new file mode 100644 index 0000000..7060c70 --- /dev/null +++ b/pkg/mindflow/database/sqlite/db.go @@ -0,0 +1,90 @@ +package sqlite + +import ( + "database/sql" + _ "embed" + "fmt" + + "github.com/pkg/errors" +) + +var ( + //go:embed queries/schema.sql + querySchema string + + //go:embed queries/categoryById.sql + queryCategoryById string + //go:embed queries/categoryNew.sql + queryCategoryNew string + //go:embed queries/postGetAll.sql + queryPostGetAll string + //go:embed queries/postNew.sql + queryPostNew string + //go:embed queries/postEdit.sql + queryPostEdit string +) + +var ( + stmtCategoryById *sql.Stmt + stmtCategoryNew *sql.Stmt + stmtPostGetAll *sql.Stmt + stmtPostNew *sql.Stmt + stmtPostEdit *sql.Stmt +) + +func initDBStatements(db *sql.DB) error { + db.Exec("PRAGMA foreign_keys = ON;") + + _, err := db.Exec(querySchema) + if err != nil { + return errors.Wrap(err, "failed to init schema") + } + + stmtCategoryById, err = db.Prepare(queryCategoryById) + if err != nil { + return errors.Wrap(err, "failed to prepare queryCategoryById") + } + + stmtCategoryNew, err = db.Prepare(queryCategoryNew) + if err != nil { + return errors.Wrap(err, "failed to prepare queryCategoryNew") + } + + stmtPostGetAll, err = db.Prepare(queryPostGetAll) + if err != nil { + return errors.Wrap(err, "failed to prepare queryPostGetAll") + } + + stmtPostNew, err = db.Prepare(queryPostNew) + if err != nil { + return errors.Wrap(err, "failed to prepare queryPostNew") + } + + stmtPostEdit, err = db.Prepare(queryPostEdit) + if err != nil { + return errors.Wrap(err, "failed to prepare queryPostEdit") + } + + return nil +} + +type SQLiteMindflow struct { + db *sql.DB +} + +func NewSQLiteMindflow(path string) (*SQLiteMindflow, error) { + db, err := sql.Open("sqlite3", dsn(path)) + if err != nil { + return nil, err + } + + if err := initDBStatements(db); err != nil { + return nil, err + } + + return &SQLiteMindflow{db: db}, nil +} + +func dsn(filePath string) string { + return fmt.Sprintf("file:%s?_journal=WAL&_mutex=full", filePath) +} diff --git a/pkg/mindflow/database/sqlite/queries/categoryById.sql b/pkg/mindflow/database/sqlite/queries/categoryById.sql new file mode 100644 index 0000000..f1086a3 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/categoryById.sql @@ -0,0 +1,4 @@ +SELECT + `name` + FROM `category` + WHERE `category_id` = ?; \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/categoryNew.sql b/pkg/mindflow/database/sqlite/queries/categoryNew.sql new file mode 100644 index 0000000..cd2e166 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/categoryNew.sql @@ -0,0 +1,3 @@ +INSERT INTO `category` + (`name`) +VALUES (?); \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/postEdit.sql b/pkg/mindflow/database/sqlite/queries/postEdit.sql new file mode 100644 index 0000000..34f2ebe --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/postEdit.sql @@ -0,0 +1,5 @@ +UPDATE OR REPLACE `post` + SET + `title` = ?, + `body` = ? + WHERE `post_id` = ?; \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/postGetAll.sql b/pkg/mindflow/database/sqlite/queries/postGetAll.sql new file mode 100644 index 0000000..1f8b610 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/postGetAll.sql @@ -0,0 +1,9 @@ +SELECT + `post`.`post_id`, + `category`.`name` AS `category`, + `post`.`date`, + `post`.`title`, + `post`.`body` + FROM `post` + LEFT JOIN `category` + ON `post`.`post_id` = `category`.`category_id`; \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/postNew.sql b/pkg/mindflow/database/sqlite/queries/postNew.sql new file mode 100644 index 0000000..65eb413 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/postNew.sql @@ -0,0 +1,4 @@ +INSERT INTO `post` + (`category_id`, `date`, `title`, `body`) +VALUES + (?, ?, ?, ?); \ No newline at end of file diff --git a/pkg/mindflow/database/sqlite/queries/schema.sql b/pkg/mindflow/database/sqlite/queries/schema.sql new file mode 100644 index 0000000..597fa11 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/schema.sql @@ -0,0 +1,25 @@ +-- SQLite3 +CREATE TABLE IF NOT EXISTS `category` ( + `category_id` INTEGER NOT NULL, + `name` TEXT NOT NULL, + PRIMARY KEY (`category_id`), + UNIQUE (`name`) +); + +INSERT INTO `category` +VALUES + (1, 'Update'), + (2, 'Diary'); + +CREATE TABLE IF NOT EXISTS `post` ( + `post_id` INTEGER NOT NULL, + `category_id` INTEGER NOT NULL, + `date` INTEGER NOT NULL, + `title` TEXT NOT NULL, + `body` TEXT NOT NULL, + PRIMARY KEY (`post_id`), + FOREIGN KEY (`category_id`) + REFERENCES `category` (`category_id`) + ON DELETE CASCADE + ON UPDATE CASCADE ); +); \ No newline at end of file