Skip to content

Commit f13d6d8

Browse files
committed
new
1 parent dd8311e commit f13d6d8

File tree

5 files changed

+88
-30
lines changed

5 files changed

+88
-30
lines changed

examples/demo.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,32 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/gohouse/gorose"
6-
_ "github.com/gohouse/gorose/driver/mysql"
5+
_ "github.com/go-sql-driver/mysql"
6+
"github.com/gohouse/gorose/examples/config"
7+
"github.com/gohouse/gorose/utils"
78
)
89

910
func main() {
10-
fmt.Println(gorose.VERSION)
11+
connection := config.GetConnection()
12+
connection.DbConfig.Master.EnableQueryLog = true
13+
// close DB
14+
defer connection.Close()
15+
16+
db := connection.NewSession()
17+
res, err := db.Table("gp_user").Get()
18+
if err != nil {
19+
fmt.Println(err)
20+
return
21+
}
22+
for _,item := range res {
23+
//fmt.Println(item)
24+
//return
25+
db2 := connection.NewSession()
26+
res,err:=db2.Table("gp_user").Where("Id",item["Id"]).Data(map[string]interface{}{
27+
"Utm": fmt.Sprintf("%s%v",utils.GetRandomAlarm(2),utils.GetRandomNum(4)),
28+
}).Update()
29+
//fmt.Println(db2.LastSql)
30+
fmt.Println(res,err)
31+
//return
32+
}
1133
}

examples/execute.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@ import (
77
)
88

99
func main() {
10-
db := config.GetConnection()
10+
connection := config.GetConnection()
1111
// close DB
12-
defer db.Close()
12+
defer connection.Close()
13+
14+
db := connection.NewSession()
15+
16+
user, err := db.Execute("update users set job=? where id = ?", "it",55)
17+
//user, err := db.Query("select * from users where id > ? limit 1", 1)
1318

14-
user, err := db.Execute("update users set name=? where id=?", "fizz8", 4)
1519
if err != nil {
1620
fmt.Println(err)
1721
return
1822
}
1923

24+
fmt.Println(db.LastSql)
2025
fmt.Println(user)
26+
27+
// return json
28+
//res2 := user.Limit(2).Get()
29+
//fmt.Println(db.LastSql())
30+
//fmt.Println(user)
31+
2132
}

examples/mysql.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,20 @@ func main() {
1212
defer connection.Close()
1313

1414
db := connection.NewSession()
15-
fmt.Println(db)
16-
res, err := db.Table("users").Where("aid", 1545).Count()
17-
if err != nil {
18-
fmt.Println(err)
19-
return
20-
}
21-
//fmt.Println(len(res))
22-
fmt.Println(db.LastSql)
23-
fmt.Println(res)
24-
2515
var db2 = connection.NewSession()
26-
res2, err := db2.Table("users").Limit(2).Get()
16+
res2, err := db2.Table("users").Fields("id").Limit(2).Get()
2717
if err != nil {
2818
fmt.Println(err)
2919
return
3020
}
3121
fmt.Println(res2)
3222
fmt.Println(db.JsonEncode(res2))
3323

34-
//============== result ======================
35-
36-
//SELECT * FROM users WHERE id > '2' LIMIT 1
37-
//map[id:3 name:gorose age:18 website:go-rose.com job:go orm]
38-
//SELECT * FROM users LIMIT 2
39-
//[map[id:1 name:fizz age:18 website:fizzday.net job:it] map[id:2 name:fizzday age:18 website:fizzday.net job:engineer]]
40-
24+
var db3 = connection.NewSession()
25+
res3,err := db3.Table("users").Fields("id,age,job").
26+
//Where("id", "in", []interface{}{1,55}).
27+
Where([][]interface{}{{"id", "in", []interface{}{1, 55}}}).
28+
Get()
29+
fmt.Println(db3.LastSql)
30+
fmt.Println(db.JsonEncode(res3))
4131
}

parser/json_parser.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package parser
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"reflect"
87
"strings"
@@ -40,7 +39,7 @@ func (c *JsonConfigParser) Parse(file string, dbConfCluster interface{}) (err er
4039

4140
func jsonDecoder(str []byte, dbConfCluster interface{}) (err error) {
4241
srcElem := reflect.Indirect(reflect.ValueOf(dbConfCluster))
43-
fmt.Println(srcElem)
42+
//fmt.Println(srcElem)
4443
fieldType := srcElem.FieldByName("Master").Type().Elem()
4544
fieldElem := reflect.New(fieldType)
4645
err = json.Unmarshal(str, fieldElem.Interface())

utils/util.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"crypto/md5"
45
"encoding/json"
56
"log"
67
"math"
@@ -496,21 +497,37 @@ func StrutForScan(u interface{}) []interface{} {
496497
return v
497498
}
498499

500+
func GetRandomAlarm(l int) string {
501+
if l<=0 {
502+
return ""
503+
}
504+
str := "abcdefghijkmnpqrstuvwxyz"
505+
myBytes := []byte(str)
506+
myBytesLen := len(myBytes)
507+
result := []byte{}
508+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
509+
for i := 0; i < l; i++ {
510+
result = append(result, myBytes[r.Intn(myBytesLen)])
511+
}
512+
return string(result)
513+
}
514+
499515
func GetRandomString(l int) string {
516+
if l<=0 {
517+
return ""
518+
}
500519
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
501520
myBytes := []byte(str)
521+
myBytesLen := len(myBytes)
502522
result := []byte{}
503523
r := rand.New(rand.NewSource(time.Now().UnixNano()))
504524
for i := 0; i < l; i++ {
505-
result = append(result, myBytes[r.Intn(len(myBytes))])
525+
result = append(result, myBytes[r.Intn(myBytesLen)])
506526
}
507527
return string(result)
508528
}
509529

510530
func GetRandomNum(l int) int {
511-
//str := "123456789"
512-
//str := "0123456789"
513-
//myBytes := []byte(str)
514531
var result []int
515532
r := rand.New(rand.NewSource(time.Now().UnixNano()))
516533

@@ -531,6 +548,25 @@ func GetRandomNum(l int) int {
531548
return tmp
532549
}
533550

551+
func GetOrderNO() string {
552+
rand.Seed(time.Now().UnixNano()) //利用当前时间的UNIX时间戳初始化rand包
553+
var suffix string
554+
for i := 0; i < 6; i++ {
555+
x := rand.Intn(10)
556+
suffix += strconv.Itoa(x)
557+
}
558+
559+
return fmt.Sprintf("%v%v",time.Now().UnixNano(),suffix)
560+
}
561+
562+
func Md5(str interface{}) string {
563+
data := []byte(fmt.Sprintf("%v", str))
564+
has := md5.Sum(data)
565+
md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
566+
567+
return md5str
568+
}
569+
534570
type DateTime struct {
535571
LastMonthStart string
536572
LastMonthEnd string

0 commit comments

Comments
 (0)