-
Notifications
You must be signed in to change notification settings - Fork 0
/
testgorp.go
85 lines (78 loc) · 1.83 KB
/
testgorp.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
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/gorp.v1"
"log"
"fmt"
"os"
"reflect"
)
func perr(err error){
if err != nil {
//panic(err.Error())
panic(err)
}
}
type Test struct {
Id int64 `db:"id"`
Name string `db:"name",size:25`
Cont string `db:"content"`
}
func initDb() *gorp.DbMap {
db,err := sql.Open("mysql","user:123456@tcp(localhost:3306)/laraveldb?charset=utf8")
perr(err)
dbmap := &gorp.DbMap{Db:db,Dialect:gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.AddTableWithName(Test{},"test").SetKeys(true,"Id")
err=dbmap.CreateTablesIfNotExists()
perr(err)
dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds))
//dbmap.TraceOff()
return dbmap
}
func testIt() {
dbmap := initDb()
defer dbmap.Db.Close()
err := dbmap.TruncateTables()
perr(err)
t1 := Test{
Name:"hao",
Cont:"haocontent",
}
t2 := Test{
Name:"haohao",
Cont:"haohaocontent",
}
dbmap.Insert(&t1, &t2)
count, err := dbmap.SelectInt("SELECT count(*) FROM test")
perr(err)
fmt.Println("count:", count)
var tests []Test
if _, err := dbmap.Select(&tests, "SELECT * FROM test WHERE id>?", 0); err != nil {
perr(err)
}
for _,t := range tests{
fmt.Println("Select",t)
var tt Test
err:= dbmap.SelectOne(&tt,"SELECT * FROM test WHERE id=?",t.Id)
perr(err)
fmt.Println("SelectOne:",tt)
o,err:=dbmap.Get(Test{},t.Id)
perr(err)
fmt.Println("GET",o.(*Test).Id,o.(*Test).Name)
}
var tests2 []Test
_,err2:=dbmap.Select(&tests2,"SELECT * FROM test WHERE name=:name",map[string]interface{}{
"name":"hao",
})
perr(err2)
expected := Test{tests2[0].Id,"hao","haocontent"}
if reflect.DeepEqual(expected,tests2[0]) {
fmt.Println("is expect")
} else {
perr(fmt.Errorf("not expect"))
}
}
func main() {
testIt()
}