-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsingleton.go
92 lines (85 loc) · 1.95 KB
/
singleton.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
package main
import (
"fmt"
"log"
"github.com/cassiobotaro/60-days-of-go/day15/database"
)
// This example is based on https://www.goinggo.net/2013/07/singleton-design-pattern-in-go.html
// also https://github.com/mattn/go-sqlite3/blob/master/_example/simple/simple.go
// CreateTable creates table foo without data
func CreateTable() {
// Get an instance from db, not a connection
// instance should be unique
db := database.Get()
sqlStmt := `
create table foo (id integer not null primary key, name text);
delete from foo;
`
// get a connection from pool and execute some statement
_, err := db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
}
// PopulateTable insert some records in table foo
func PopulateTable() {
// Get an instance from db, not a connection
// instance should be unique
db := database.Get()
// start a trasanction
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
// prepare the query that should be executed
stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
// insert 100 records on database
for i := 0; i < 100; i++ {
_, err = stmt.Exec(i, fmt.Sprintf("Hello World %03d", i))
if err != nil {
log.Fatal(err)
}
}
// finally commit
tx.Commit()
}
// ListFoo list all records from foo
func ListFoo() {
// Get an instance from db, not a connection
// instance should be unique
db := database.Get()
// start a transaction
tx, err := db.Begin()
// execute a query
rows, err := tx.Query("select id, name from foo")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// read the rows returned
for rows.Next() {
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
}
fmt.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func main() {
// initialize the database
database.MustConnect()
CreateTable()
PopulateTable()
ListFoo()
}