From bc3b2825a61e30f4a4f9108c89951cbfac7f23f7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 19 Dec 2020 16:47:12 +0800 Subject: [PATCH] 1 --- httpcli.go | 10 ++++++---- listhead.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ mysql.go | 15 +++++++++++---- strutils.go | 12 ++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/httpcli.go b/httpcli.go index 2734000..9cf02b1 100644 --- a/httpcli.go +++ b/httpcli.go @@ -67,15 +67,17 @@ func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) { return respObj, respStr, err } -func HttpPostContent(url string, contentType string, body string) (string, error) { +func HttpPostContent(url string, contentType string, body string, response *string) (error) { + *response = "" resp, err := http.Post(url, contentType, strings.NewReader(body)) if err != nil { - return "", err + return err } defer resp.Body.Close() if bytes, err := ioutil.ReadAll(resp.Body); err == nil { - return string(bytes), nil + *response = string(bytes) + return nil } else { - return "", err + return err } } diff --git a/listhead.go b/listhead.go index b9ec4e6..09b0def 100644 --- a/listhead.go +++ b/listhead.go @@ -1,5 +1,7 @@ package q5 +type ListHead_Foreach_Func func(interface{}) bool + type ListHead struct { next *ListHead prev *ListHead @@ -27,6 +29,16 @@ func (this *ListHead) AddTail(pnew *ListHead) { prev.next = pnew } +func (this *ListHead) AddHead(pnew *ListHead) { + prev := this + next := this.prev + + next.prev = pnew + pnew.next = next + pnew.prev = prev + prev.next = pnew +} + func (this *ListHead) FirstEntry() interface{} { return this.next.data } @@ -53,3 +65,38 @@ func (this *ListHead) DelInit() { this.next = this this.prev = this } + +func (this *ListHead) Next() *ListHead { + return this.next +} + +func (this *ListHead) ForEachFrom(from *ListHead, + cb ListHead_Foreach_Func) { + for pos := from; pos != this; pos = pos.next { + if !cb(pos.data) { + break + } + } +} + +func (this *ListHead) ForEach(cb ListHead_Foreach_Func) { + for pos := this.next; pos != this; pos = pos.next { + if !cb(pos.data) { + break + } + } +} + +func (this *ListHead) ForEach_r(cb ListHead_Foreach_Func) { + for pos := this.prev; pos != this; pos = pos.prev { + if !cb(pos.data) { + break + } + } +} + +func MakeListHead () *ListHead{ + l := new(ListHead) + l.Init(nil) + return l +} diff --git a/mysql.go b/mysql.go index 781f82c..0146b73 100644 --- a/mysql.go +++ b/mysql.go @@ -1,6 +1,7 @@ package q5 import ( + "fmt" "database/sql" _ "github.com/go-sql-driver/mysql" ) @@ -23,7 +24,13 @@ func (this* Mysql) init(host string, port int32, user string, passwd string, dat } func (this* Mysql) Open() error { - db, err := sql.Open("mysql", "") + connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", + this.user, + this.passwd, + this.host, + this.port, + this.database) + db, err := sql.Open("mysql", connStr) this.db = db return err } @@ -39,16 +46,16 @@ func (this* Mysql) Ping() error { } func (this* Mysql) Query(query string, args ...interface{}) (*sql.Rows, error) { - rows, err := this.db.Query(query, args) + rows, err := this.db.Query(query, args...) return rows, err } func (this* Mysql) QueryRow(query string, args ...interface{}) *sql.Row { - return this.db.QueryRow(query, args) + return this.db.QueryRow(query, args...) } func (this* Mysql) Exec(query string, args ...interface{}) (sql.Result, error) { - result, err := this.db.Exec(query, args) + result, err := this.db.Exec(query, args...) return result, err } diff --git a/strutils.go b/strutils.go index ebdbb64..b051676 100644 --- a/strutils.go +++ b/strutils.go @@ -6,6 +6,7 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "encoding/base64" "hash/crc32" ) @@ -21,6 +22,17 @@ func Md5Str(data string) string { return hex.EncodeToString(h.Sum(nil)) } +func Base64Encode(data string) string { + strbytes := []byte(data) + encoded := base64.StdEncoding.EncodeToString(strbytes) + return encoded +} + +func Base64Decode(data string) (string, error) { + decoded, err := base64.StdEncoding.DecodeString(data) + return string(decoded), err +} + func Crc32(data string) uint32 { ieee := crc32.NewIEEE() io.WriteString(ieee, data)