diff --git a/go.mod b/go.mod index f195170..7781927 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module q5 go 1.15 + +require ( + github.com/garyburd/redigo v1.6.2 + github.com/go-sql-driver/mysql v1.5.0 + github.com/gomodule/redigo v1.8.3 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5ace556 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/garyburd/redigo v1.6.2 h1:yE/pwKCrbLpLpQICzYTeZ7JsTA/C53wFTJHaEtRqniM= +github.com/garyburd/redigo v1.6.2/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/gomodule/redigo v1.8.3 h1:HR0kYDX2RJZvAup8CsiJwxB4dTCSC0AaUq6S4SiLwUc= +github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/httpcli.go b/httpcli.go new file mode 100644 index 0000000..919823d --- /dev/null +++ b/httpcli.go @@ -0,0 +1,37 @@ +package q5 + +import ( + "net/http" + "io/ioutil" + "errors" +) + +func HttpGet(url string, params *XObject) (string, error) { + if !StrContains(url, "?") { + url = url + "?" + } + if resp, err := http.Get(url); err == nil { + if resp.StatusCode != 200 { + return "", errors.New("HttpGet error Status:" + resp.Status) + } + if bytes, err := ioutil.ReadAll(resp.Body); err == nil { + return string(bytes), nil + } else { + return "", err + } + } else { + return "", err + } +} + +func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) { + respStr, err := HttpGet(url, params) + if err != nil { + return nil, respStr, err + } + respObj := NewXoFromJsonStr(respStr) + if !respObj.IsObject() { + return nil, respStr, errors.New("HttpGetAsJson error invalid json format") + } + return respObj, respStr, err +} diff --git a/mysql.go b/mysql.go new file mode 100644 index 0000000..781f82c --- /dev/null +++ b/mysql.go @@ -0,0 +1,59 @@ +package q5 + +import ( + "database/sql" + _ "github.com/go-sql-driver/mysql" +) + +type Mysql struct { + host string + port int32 + user string + passwd string + database string + db *sql.DB +} + +func (this* Mysql) init(host string, port int32, user string, passwd string, database string) { + this.host = host + this.port = port + this.user = user + this.passwd = passwd + this.database = database +} + +func (this* Mysql) Open() error { + db, err := sql.Open("mysql", "") + this.db = db + return err +} + +func (this* Mysql) Close() { + if this.db != nil { + this.db.Close() + } +} + +func (this* Mysql) Ping() error { + return this.db.Ping() +} + +func (this* Mysql) Query(query string, args ...interface{}) (*sql.Rows, error) { + 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) +} + +func (this* Mysql) Exec(query string, args ...interface{}) (sql.Result, error) { + result, err := this.db.Exec(query, args) + return result, err +} + +func NewMysql(host string, port int32, user string, passwd string, database string) *Mysql { + conn := new(Mysql) + conn.init(host, port, user, passwd, database) + return conn +} diff --git a/redis.go b/redis.go new file mode 100644 index 0000000..6d8f983 --- /dev/null +++ b/redis.go @@ -0,0 +1,24 @@ +package q5 + +import ( + "github.com/gomodule/redigo/redis" +) + +type Redis struct { + host string + port int32 + passwd string + conn *redis.Conn +} + +func (this* Redis) Init(host string, port int32, passwd string) { + this.host = host + this.port = port + this.passwd = passwd +} + +func (this* Redis) Open() { +} + +func (this* Redis) Close() { +} diff --git a/strutils.go b/strutils.go index c709ea0..ebdbb64 100644 --- a/strutils.go +++ b/strutils.go @@ -56,3 +56,11 @@ func EncodeJson(p interface{}) string { return "" } } + +func StrSplit(s string, sep string) []string { + return strings.Split(s, sep) +} + +func StrContains(s string, substr string) bool { + return strings.Contains(s, substr) +} diff --git a/sysutils.go b/sysutils.go index 010cab1..1abf756 100644 --- a/sysutils.go +++ b/sysutils.go @@ -128,3 +128,13 @@ func FormatUnixDateTime(sec int64) string { strTime := time.Unix(sec, 0).Format("2006-01-02 15:04:05") return strTime } + +func FormatUnixDate(sec int64) string { + strTime := time.Unix(sec, 0).Format("2006-01-02") + return strTime +} + +func FormatUnixDateEx(sec int64) string { + strTime := time.Unix(sec, 0).Format("20060102") + return strTime +}