From 6940609ad227ef0a76a41276c5d313fedbcd1af5 Mon Sep 17 00:00:00 2001 From: hoteas Date: Wed, 23 Aug 2017 01:40:54 +0000 Subject: [PATCH] demo --- .idea/workspace.xml | 307 +++++++++++++++++++++++++++++++++----------- db.go | 44 ++++++- example/main.go | 8 +- 3 files changed, 277 insertions(+), 82 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 098edff..008c91f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,8 +5,9 @@ - - + + + - + - + - - - - - - - - @@ -42,16 +35,10 @@ - - + + - - - - - - @@ -60,7 +47,7 @@ - + @@ -80,15 +67,35 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + @@ -115,7 +122,6 @@ @@ -165,8 +172,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - @@ -437,7 +602,8 @@ - + + 1500458878821 @@ -530,11 +696,18 @@ - - @@ -547,13 +720,14 @@ + - + - + @@ -589,13 +763,6 @@ - - - - - - - @@ -806,7 +973,6 @@ - @@ -814,7 +980,6 @@ - @@ -822,15 +987,6 @@ - - - - - - - - - @@ -846,7 +1002,6 @@ - @@ -868,7 +1023,6 @@ - @@ -886,7 +1040,6 @@ - @@ -894,7 +1047,6 @@ - @@ -902,7 +1054,6 @@ - @@ -922,42 +1073,28 @@ - + - - - - - - - - - - + + - - - - - - - + @@ -971,12 +1108,28 @@ + + + + + + + + + + + + + + + + - - + + - + diff --git a/db.go b/db.go index d005258..cd459da 100644 --- a/db.go +++ b/db.go @@ -18,6 +18,8 @@ type HoTimeDB struct { ConnectFunc func(err ...*Error) *sql.DB //LastErr Error limit Slice + Tx *sql.Tx //事务对象 + } //设置数据库配置连接 @@ -26,6 +28,28 @@ func (this *HoTimeDB) SetConnect(connect func(err ...*Error) *sql.DB, err ...*Er this.InitDb() } +//事务,如果action返回true则执行成功;false则回滚 +func (this *HoTimeDB) Action(action func() bool) Error { + tx, err := this.DB.Begin() + if err != nil { + this.LastErr.SetError(err) + return this.LastErr + } + this.Tx = tx + + result := action() + + if !result { + this.Tx.Rollback() + this.Tx = nil + return nil + } + + this.Tx.Commit() + this.Tx = nil + return nil +} + func (this *HoTimeDB) InitDb(err ...*Error) Error { if len(err) != 0 { this.LastErr = *(err[0]) @@ -258,7 +282,14 @@ func (this *HoTimeDB) Query(query string, args ...interface{}) []Map { this.LastErr.SetError(err) return nil } - resl, err = this.DB.Query(query, args...) + + if this.Tx!=nil{ + resl, err = this.Tx.Query(query, args...) + }else{ + resl, err = this.DB.Query(query, args...) + } + + this.LastErr.SetError(err) if err != nil { if err = this.DB.Ping(); err != nil { @@ -279,13 +310,22 @@ func (this *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error this.LastQuery = query this.LastData = args + var e error + var resl sql.Result + + if this.DB == nil { err := errors.New("没有初始化数据库") this.LastErr.SetError(err) return nil, this.LastErr } - resl, e := this.DB.Exec(query, args...) + if this.Tx!=nil{ + resl, e = this.Tx.Exec(query, args...) + }else{ + resl, e = this.DB.Exec(query, args...) + } + this.LastErr.SetError(e) diff --git a/example/main.go b/example/main.go index 0d7967b..fce8dd9 100644 --- a/example/main.go +++ b/example/main.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + _ "github.com/go-sql-driver/mysql" "go.hoteas.com/hotime" "golang.org/x/net/websocket" ) @@ -48,11 +49,12 @@ func main() { "app": hotime.Proj{ "index": hotime.Ctr{ "test": func(this *hotime.Context) { + this.Display(0, "chenggong") }, "websocket": func(this *hotime.Context) { - hdler:=websocket.Handler(func(ws *websocket.Conn) { - for true { + hdler := websocket.Handler(func(ws *websocket.Conn) { + for true { msg := make([]byte, 5120) n, err := ws.Read(msg) @@ -69,7 +71,7 @@ func main() { fmt.Printf("Send: %s\n", msg[:m]) } }) - hdler.ServeHTTP(this.Resp,this.Req) + hdler.ServeHTTP(this.Resp, this.Req) }, }, },