Merge pull request #12 from thewhitetulip/revert-11-master

Revert "Encapsulated database for extracting common error patterns."
This commit is contained in:
Suraj Patil 2016-01-31 18:04:56 +05:30
commit aadd8b31f0
2 changed files with 117 additions and 71 deletions

View File

@ -7,9 +7,15 @@ 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 := database.prepare("insert into files values(?,?)")
SQL, err := database.Prepare("insert into files values(?,?)")
if err != nil {
log.Println(err)
}
tx, err := database.Begin()
tx := database.begin()
if err != nil {
log.Println(err)
}
_, err = tx.Stmt(SQL).Exec(fileName, token)
if err != nil {
log.Println(err)
@ -24,7 +30,7 @@ func AddFile(fileName, token string) error {
func GetFileName(token string) (string, error) {
sql := "select name from files where autoName=?"
var fileName string
rows := database.query(sql, fileName)
rows, err := database.Query(sql, fileName)
if rows.Next() {
err := rows.Scan(&fileName)
if err != nil {
@ -32,6 +38,9 @@ func GetFileName(token string) (string, error) {
return "", err
}
}
if err != nil {
return "", err
}
return fileName, nil
}

View File

@ -2,49 +2,19 @@ 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
"github.com/thewhitetulip/Tasks/types"
)
var database Database
var database *sql.DB
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 = Database{}
database.db, err = sql.Open("sqlite3", "./tasks.db")
database, err = sql.Open("sqlite3", "./tasks.db")
if err != nil {
log.Println(err)
}
@ -52,7 +22,7 @@ func init() {
//Close function closes this database connection
func Close() {
database.db.Close()
database.Close()
}
//GetTasks retrieves all the tasks depending on the
@ -64,26 +34,32 @@ func GetTasks(status string) types.Context {
var TaskTitle string
var TaskContent string
var TaskCreated time.Time
var TaskPriority string
var getTasksql string
basicSQL := "select id, title, content, created_date, priority from task "
if status == "pending" {
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc"
getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, created_date asc"
} else if status == "deleted" {
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
getTasksql = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc"
} else if status == "completed" {
getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc"
getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc"
}
rows := database.query(getTasksql)
rows, err := database.Query(getTasksql)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
TaskContent = string(md.Markdown([]byte(TaskContent)))
// TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
if err != nil {
log.Println(err)
}
TaskCreated = TaskCreated.Local()
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20], Priority: TaskPriority}
task = append(task, a)
}
context = types.Context{Tasks: task, Navigation: status}
@ -94,19 +70,20 @@ func GetTasks(status string) types.Context {
func GetTaskByID(id int) types.Context {
var tasks []types.Task
var task types.Task
var TaskID int
var TaskTitle string
var TaskContent string
getTasksql := "select id, title, content from task where id=?"
rows := database.query(getTasksql, id)
getTasksql := "select id, title, content, priority from task where id=?"
rows, err := database.Query(getTasksql, id)
if err != nil {
log.Println(err)
}
defer rows.Close()
if rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority)
if err != nil {
log.Println(err)
//send email to respective people
}
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
}
tasks = append(tasks, task)
context := types.Context{Tasks: tasks, Navigation: "edit"}
@ -115,8 +92,14 @@ 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()
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)
}
_, err = tx.Stmt(trashSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
@ -129,8 +112,14 @@ func TrashTask(id int) error {
//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()
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)
}
_, err = tx.Stmt(stmt).Exec(id)
if err != nil {
log.Println(err)
@ -143,8 +132,14 @@ func CompleteTask(id int) error {
//DeleteAll is used to empty the trash
func DeleteAll() error {
stmt := database.prepare("delete from task where is_deleted='Y'")
tx := database.begin()
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)
}
_, err = tx.Stmt(stmt).Exec()
if err != nil {
log.Println("doing rollback")
@ -157,8 +152,34 @@ func DeleteAll() error {
//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()
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)
}
_, err = tx.Stmt(restoreSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
}
return err
}
//RestoreTask 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)
}
_, err = tx.Stmt(restoreSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
@ -171,8 +192,11 @@ func RestoreTask(id int) error {
//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()
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)
}
@ -187,10 +211,13 @@ func DeleteTask(id int) error {
}
//AddTask is used to add the task in the database
func AddTask(title, content string) error {
restoreSQL := database.prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())")
tx := database.begin()
_, err = tx.Stmt(restoreSQL).Exec(title, content)
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()
_, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority)
if err != nil {
log.Println(err)
tx.Rollback()
@ -202,8 +229,15 @@ func AddTask(title, content string) error {
//UpdateTask is used to update the tasks in the database
func UpdateTask(id int, title string, content string) error {
SQL := database.prepare("update task set title=?, content=? where id=?")
tx := database.begin()
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)
}
_, err = tx.Stmt(SQL).Exec(title, content, id)
if err != nil {
log.Println(err)
@ -224,8 +258,10 @@ func SearchTask(query string) types.Context {
var TaskCreated time.Time
var context types.Context
rows := database.query(stmt, query, query)
rows, err := database.Query(stmt, query, query)
if err != nil {
log.Println(err)
}
for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
if err != nil {
@ -233,6 +269,7 @@ func SearchTask(query string) types.Context {
}
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
TaskContent = string(md.Markdown([]byte(TaskContent)))
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
task = append(task, a)
}