-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmain.go
108 lines (89 loc) · 2.3 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"github.com/astaxie/beedb"
_ "github.com/mattn/go-sqlite3"
"time"
"database/sql"
)
/*
CREATE TABLE `userinfo` (
`uid` integer primary key AUTOINCREMENT,
`username` VARCHAR(64) ,
`departname` VARCHAR(64) ,
`created` DATE
);
CREATE TABLE `userdeatail` (
`uid` integer primary key AUTOINCREMENT,
`intro` TEXT ,
`profile` TEXT
)
*/
type Userinfo struct {
Uid int `PK`
Username string
Departname string
Created time.Time
}
func main() {
db, err := sql.Open("sqlite3", "db.db")
if err != nil {
panic(err)
}
orm := beedb.New(db)
//Original SQL Join Table
a, _ := orm.SetTable("userinfo").Join("LEFT", "userdeatail", "userinfo.uid=userdeatail.uid").Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdeatail.profile").FindMap()
fmt.Println(a)
//Original SQL Group By
b, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='astaxie'").FindMap()
fmt.Println(b)
//Original SQL Backinfo resultsSlice []map[string][]byte
//default PrimaryKey id
c, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap()
fmt.Println(c)
//original SQL update
t := make(map[string]interface{})
var j interface{}
j = "astaxie"
t["username"] = j
//update one
orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t)
//update batch
orm.SetTable("userinfo").Where("uid>?", 3).Update(t)
// add one
add := make(map[string]interface{})
j = "astaxie"
add["username"] = j
j = "cloud develop"
add["departname"] = j
j = "2012-12-02"
add["created"] = j
orm.SetTable("userinfo").Insert(add)
//original SQL delete
orm.SetTable("userinfo").Where("uid>?", 3).DelectRow()
//get all data
var alluser []Userinfo
orm.Limit(10).Where("uid>?", 1).FindAll(&alluser)
fmt.Println(alluser)
//get one info
var one Userinfo
orm.Where("uid=?", 27).Find(&one)
fmt.Println(one)
//save data
var saveone Userinfo
saveone.Username = "Test Add User"
saveone.Departname = "Test Add Departname"
saveone.Created = time.Now()
orm.Save(&saveone)
fmt.Println(saveone)
// //update data
saveone.Username = "Update Username"
saveone.Departname = "Update Departname"
saveone.Created = time.Now()
orm.Save(&saveone)
fmt.Println(saveone)
// // //delete one data
orm.Delete(&saveone)
// //delete all data
orm.DeleteAll(&alluser)
}