From 63278e72f3474d225c5f70e9152021e9528e5efd Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Sun, 31 Jan 2016 14:48:19 +0100 Subject: [PATCH 1/3] Encapsulate database to its own type for standard error handling. --- db/files.go | 13 ++--- db/tasks.go | 143 +++++++++++++++++++++++----------------------------- 2 files changed, 65 insertions(+), 91 deletions(-) diff --git a/db/files.go b/db/files.go index 9e57bd8..3c549e4 100644 --- a/db/files.go +++ b/db/files.go @@ -7,15 +7,8 @@ import ( // AddFile is used to add the md5 of a file name which is uploaded to our application // this will enable us to randomize the URL without worrying about the file names func AddFile(fileName, token string) error { - SQL, err := database.Prepare("insert into files values(?,?)") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - - if err != nil { - log.Println(err) - } + SQL := database.prepare("insert into files values(?,?)") + tx := database.begin() _, err = tx.Stmt(SQL).Exec(fileName, token) if err != nil { log.Println(err) @@ -30,7 +23,7 @@ func AddFile(fileName, token string) error { func GetFileName(token string) (string, error) { sql := "select name from files where autoName=?" var fileName string - rows, err := database.Query(sql, fileName) + rows := database.query(sql, fileName) if rows.Next() { err := rows.Scan(&fileName) if err != nil { diff --git a/db/tasks.go b/db/tasks.go index 03d3767..8a55fb0 100644 --- a/db/tasks.go +++ b/db/tasks.go @@ -2,19 +2,49 @@ package db import ( "database/sql" - _ "github.com/mattn/go-sqlite3" //we want to use sqlite natively - md "github.com/shurcooL/github_flavored_markdown" - "github.com/thewhitetulip/Tasks/types" "log" "strings" "time" + + _ "github.com/mattn/go-sqlite3" //we want to use sqlite natively + md "github.com/shurcooL/github_flavored_markdown" + "github.com/thewhitetulip/Tasks/types" ) -var database *sql.DB +var database Database var err error +//Database encapsulates database +type Database struct { + db *sql.DB +} + +func (db Database) begin() (tx *sql.Tx) { + tx, err := db.db.Begin() + if err != nil { + log.Println(err) + } + return tx +} + +func (db Database) prepare(q string) (stmt *sql.Stmt) { + stmt, err := db.db.Prepare(q) + if err != nil { + log.Println(err) + } + return stmt +} + +func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) { + rows, err := db.db.Query(q, args) + if err != nil { + log.Println(err) + } + return rows +} + func init() { - database, err = sql.Open("sqlite3", "./tasks.db") + database.db, err = sql.Open("sqlite3", "./tasks.db") if err != nil { log.Println(err) } @@ -22,7 +52,7 @@ func init() { //Close function closes this database connection func Close() { - database.Close() + database.db.Close() } //GetTasks retrieves all the tasks depending on the @@ -46,10 +76,7 @@ func GetTasks(status string) types.Context { getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc" } - rows, err := database.Query(getTasksql) - if err != nil { - log.Println(err) - } + rows := database.query(getTasksql) defer rows.Close() for rows.Next() { err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority) @@ -73,10 +100,7 @@ func GetTaskByID(id int) types.Context { getTasksql := "select id, title, content, priority from task where id=?" - rows, err := database.Query(getTasksql, id) - if err != nil { - log.Println(err) - } + rows := database.query(getTasksql, id) defer rows.Close() if rows.Next() { err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority) @@ -92,14 +116,9 @@ func GetTaskByID(id int) types.Context { //TrashTask is used to delete the task func TrashTask(id int) error { - trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + trashSQL := database.prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?") + tx := database.begin() + _, err = tx.Stmt(trashSQL).Exec(id) if err != nil { log.Println("doing rollback") @@ -112,14 +131,8 @@ func TrashTask(id int) error { //CompleteTask is used to mark tasks as complete func CompleteTask(id int) error { - stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + stmt := database.prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") + tx := database.begin() _, err = tx.Stmt(stmt).Exec(id) if err != nil { log.Println(err) @@ -132,14 +145,10 @@ func CompleteTask(id int) error { //DeleteAll is used to empty the trash func DeleteAll() error { - stmt, err := database.Prepare("delete from task where is_deleted='Y'") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + stmt := database.prepare("delete from task where is_deleted='Y'") + + tx := database.begin() + _, err = tx.Stmt(stmt).Exec() if err != nil { log.Println("doing rollback") @@ -152,14 +161,9 @@ func DeleteAll() error { //RestoreTask is used to restore tasks from the Trash func RestoreTask(id int) error { - restoreSQL, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + restoreSQL := database.prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?") + + tx := database.begin() _, err = tx.Stmt(restoreSQL).Exec(id) if err != nil { log.Println("doing rollback") @@ -170,16 +174,10 @@ func RestoreTask(id int) error { return err } -//RestoreTask is used to restore tasks from the Trash +//RestoreTaskFromComplete is used to restore tasks from the Trash func RestoreTaskFromComplete(id int) error { - restoreSQL, err := database.Prepare("update task set finish_date=null,last_modified_at=datetime() where id=?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + restoreSQL := database.prepare("update task set finish_date=null,last_modified_at=datetime() where id=?") + tx := database.begin() _, err = tx.Stmt(restoreSQL).Exec(id) if err != nil { log.Println("doing rollback") @@ -192,14 +190,8 @@ func RestoreTaskFromComplete(id int) error { //DeleteTask is used to delete the task from the database func DeleteTask(id int) error { - deleteSQL, err := database.Prepare("delete from task where id = ?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - if err != nil { - log.Println(err) - } + deleteSQL := database.prepare("delete from task where id = ?") + tx := database.begin() _, err = tx.Stmt(deleteSQL).Exec(id) if err != nil { log.Println(err) @@ -212,11 +204,9 @@ func DeleteTask(id int) error { //AddTask is used to add the task in the database func AddTask(title, content string, taskPriority int) error { - restoreSQL, err := database.Prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() + restoreSQL := database.prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())") + + tx := database.begin() _, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority) if err != nil { log.Println(err) @@ -229,15 +219,8 @@ func AddTask(title, content string, taskPriority int) error { //UpdateTask is used to update the tasks in the database func UpdateTask(id int, title string, content string) error { - SQL, err := database.Prepare("update task set title=?, content=? where id=?") - if err != nil { - log.Println(err) - } - tx, err := database.Begin() - - if err != nil { - log.Println(err) - } + SQL := database.prepare("update task set title=?, content=? where id=?") + tx := database.begin() _, err = tx.Stmt(SQL).Exec(title, content, id) if err != nil { log.Println(err) @@ -258,10 +241,8 @@ func SearchTask(query string) types.Context { var TaskCreated time.Time var context types.Context - rows, err := database.Query(stmt, query, query) - if err != nil { - log.Println(err) - } + rows := database.query(stmt, query, query) + for rows.Next() { err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated) if err != nil { From cfcec48ca42169ca2774c0e2d29aea81498ab3ee Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Sun, 31 Jan 2016 14:56:14 +0100 Subject: [PATCH 2/3] Started to extract out the common query which is performed by task related actions. --- db/tasks.go | 82 +++++++++-------------------------------------------- 1 file changed, 13 insertions(+), 69 deletions(-) diff --git a/db/tasks.go b/db/tasks.go index 8a55fb0..326ba32 100644 --- a/db/tasks.go +++ b/db/tasks.go @@ -116,112 +116,56 @@ func GetTaskByID(id int) types.Context { //TrashTask is used to delete the task func TrashTask(id int) error { - trashSQL := database.prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?") - tx := database.begin() - - _, err = tx.Stmt(trashSQL).Exec(id) - if err != nil { - log.Println("doing rollback") - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("update task set is_deleted='Y',last_modified_at=datetime() where id=?", id) return err } //CompleteTask is used to mark tasks as complete func CompleteTask(id int) error { - stmt := database.prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") - tx := database.begin() - _, err = tx.Stmt(stmt).Exec(id) - if err != nil { - log.Println(err) - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?", id) return err } //DeleteAll is used to empty the trash func DeleteAll() error { - stmt := database.prepare("delete from task where is_deleted='Y'") - - tx := database.begin() - - _, err = tx.Stmt(stmt).Exec() - if err != nil { - log.Println("doing rollback") - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("delete from task where is_deleted='Y'") return err } //RestoreTask is used to restore tasks from the Trash func RestoreTask(id int) error { - restoreSQL := database.prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?") - - tx := database.begin() - _, err = tx.Stmt(restoreSQL).Exec(id) - if err != nil { - log.Println("doing rollback") - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("update task set is_deleted='N',last_modified_at=datetime() where id=?", id) return err } //RestoreTaskFromComplete is used to restore tasks from the Trash func RestoreTaskFromComplete(id int) error { - restoreSQL := database.prepare("update task set finish_date=null,last_modified_at=datetime() where id=?") - tx := database.begin() - _, err = tx.Stmt(restoreSQL).Exec(id) - if err != nil { - log.Println("doing rollback") - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("update task set finish_date=null,last_modified_at=datetime() where id=?", id) return err } //DeleteTask is used to delete the task from the database func DeleteTask(id int) error { - deleteSQL := database.prepare("delete from task where id = ?") - tx := database.begin() - _, err = tx.Stmt(deleteSQL).Exec(id) - if err != nil { - log.Println(err) - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("delete from task where id = ?", id) return err } //AddTask is used to add the task in the database func AddTask(title, content string, taskPriority int) error { - restoreSQL := database.prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())") - - tx := database.begin() - _, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority) - if err != nil { - log.Println(err) - tx.Rollback() - } else { - tx.Commit() - } + err := taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority) return err } //UpdateTask is used to update the tasks in the database func UpdateTask(id int, title string, content string) error { + err := taskQuery("update task set title=?, content=? where id=?", title, content) + return err +} + +func taskQuery(sql string, args ...interface{}) error { SQL := database.prepare("update task set title=?, content=? where id=?") tx := database.begin() - _, err = tx.Stmt(SQL).Exec(title, content, id) + _, err = tx.Stmt(SQL).Exec(args) if err != nil { log.Println(err) tx.Rollback() From d2e2d035f265537b15348ec395d9800e24d1b947 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Sun, 31 Jan 2016 15:00:31 +0100 Subject: [PATCH 3/3] Fixed argument passing as variadic. --- db/tasks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/tasks.go b/db/tasks.go index 326ba32..2b911b2 100644 --- a/db/tasks.go +++ b/db/tasks.go @@ -36,7 +36,7 @@ func (db Database) prepare(q string) (stmt *sql.Stmt) { } func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) { - rows, err := db.db.Query(q, args) + rows, err := db.db.Query(q, args...) if err != nil { log.Println(err) } @@ -165,7 +165,7 @@ func UpdateTask(id int, title string, content string) error { func taskQuery(sql string, args ...interface{}) error { SQL := database.prepare("update task set title=?, content=? where id=?") tx := database.begin() - _, err = tx.Stmt(SQL).Exec(args) + _, err = tx.Stmt(SQL).Exec(args...) if err != nil { log.Println(err) tx.Rollback()