-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (69 loc) · 1.66 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/kj455/simple-db/pkg/driver"
"golang.org/x/exp/rand"
)
func main() {
const driverName = "simple"
dataSourceName := RandomString(30)
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
log.Fatalln("Failed to open database:", err)
}
defer db.Close()
query := "create table T1(A int, B varchar(9))"
if _, err := db.Exec(query); err != nil {
log.Fatalln("Failed to create table:", err)
}
defer func() {
_, err := db.Exec("delete from T1")
if err != nil {
log.Fatalln("Failed to delete table:", err)
}
log.Println("Successfully deleted table.")
}()
n := 200
log.Print("Inserting", n, "random records.")
for i := 0; i < n; i++ {
a := i
b := "rec" + fmt.Sprint(a)
stmt := fmt.Sprintf("insert into T1(A,B) values(%d, '%s')", a, b)
_, err := db.Exec(stmt)
if err != nil {
log.Fatalln("Failed to execute insert:", err)
}
}
log.Println("Inserted", n, "records.")
query = "select A, B from T1 where A = 100"
rows, err := db.Query(query)
if err != nil {
log.Fatalln("Failed to execute query:", err)
}
defer rows.Close()
fields, err := rows.Columns()
if err != nil {
log.Fatalln("Failed to get columns:", err)
}
log.Println("Columns:", fields)
for rows.Next() {
var a int
var b string
err = rows.Scan(&a, &b)
if err != nil {
log.Fatalln("Failed to scan row:", err)
}
log.Printf("Matched: A=%d, B=%s\n", a, b)
}
log.Println("Done.")
}
func RandomString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}