-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
97 lines (86 loc) · 2.36 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package repository
import (
"database/sql"
"log"
"os"
"sync"
_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library
)
func init() {
os.Remove("sqlite-database.db") // I delete the file to avoid duplicated records.
// SQLite is a file based database.
log.Println("Creating sqlite-database.db...")
file, err := os.Create("sqlite-database.db") // Create SQLite file
if err != nil {
log.Fatal(err.Error())
}
file.Close()
log.Println("sqlite-database.db created")
sqliteDatabase, _ := sql.Open("sqlite3", "./sqlite-database.db") // Open the created SQLite File
// defer sqliteDatabase.Close() // Defer Closing the database
createTable(sqliteDatabase) // Create Database Tables
r = &repository{
db: sqliteDatabase,
}
}
type repository struct {
db *sql.DB
mu sync.RWMutex
}
var (
r *repository
)
func Repository() *repository {
return r
}
type Todo struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
}
func createTable(db *sql.DB) {
createTodoTableSQL := `CREATE TABLE todo (
"idTodo" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"id" TEXT,
"title" TEXT,
"description" TEXT
);` // SQL Statement for Create Table
log.Println("Create Todo table...")
statement, err := db.Prepare(createTodoTableSQL) // Prepare SQL Statement
if err != nil {
log.Fatal(err.Error())
}
statement.Exec() // Execute SQL Statements
log.Println("Todo table created")
}
// We are passing db reference connection from main to our method with other parameters
func (r *repository) InsertTodo(t Todo) {
log.Println("Inserting Todo record ...")
insertTodoSQL := `INSERT INTO todo(id, title, description) VALUES (?, ?, ?)`
statement, err := r.db.Prepare(insertTodoSQL) // Prepare statement.
// This is good to avoid SQL injections
if err != nil {
log.Fatalln(err.Error())
}
_, err = statement.Exec(t.Id, t.Title, t.Description)
if err != nil {
log.Fatalln(err.Error())
}
}
func (r *repository) ListTodos() []Todo {
row, err := r.db.Query("SELECT * FROM todo ORDER BY title")
if err != nil {
log.Fatal(err)
}
defer row.Close()
todos := []Todo{}
for row.Next() { // Iterate and fetch the records from result cursor
var rowid string
var id string
var title string
var description string
row.Scan(&rowid, &id, &title, &description)
todos = append(todos, Todo{id, title, description})
}
return todos
}