2019年9月24日 星期二

golang sqlbuilder

https://github.com/eehsiao/sqlbuilder

sqlbuilder

sqlbuilder is a simple sql query string builder
sqlbuilder its recursive struct call, that you can easy to build sql string
ex: dao.Select().From().Join().Where().Limit()

SqlBuilder functions

  • build select :
    • Select(f ...string)
    • Distinct(b bool)
      • its default in builder is set false
    • Top(i int)
      • only support mssql
    • From(t ...string)
      • t is table name
    • Where(c string)
      • c is condition, ex Where("field1=1 and filed2='b'")
    • WhereAnd(c ...string)
    • WhereOr(c ...string)
    • Limit(i ...int)
      • support 2 parms
      • only support mysql
    • Join(t string, c string)
      • t is table name
      • c is condition
    • InnerJoin(t string, c string)
    • LeftJoin(t string, c string)
    • RightJoin(t string, c string)
    • FullJoin(t string, c string)
    • GroupBy(f ...string)
      • f is a fileds list of strings
    • OrderBy(f ...string)
      • f is a fileds list of strings
    • OrderByAsc(f ...string)
    • OrderByDesc(f ...string)
    • Having(s string)
      • s is having condition string
    • BuildSelectSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build update :
    • Set(s map[string]interface{})
    • FromOne(t string)
      • reset the table for only one
    • BuildUpdateSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build insert :
    • Into(t string)
      • set the insert table
    • Fields(f ...string)
      • f is a fileds list of strings
    • Values(v ...[]interface{})
      • v is a values list of interface{}
    • BuildInsertSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build delete :
    • BuildDeleteSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • common :
    • ClearBuilder()
      • reset builder
    • BuildedSQL()
      • return the builded sql string, if build success.
    • SetDbName(s string)
    • SetTbName(s string)
    • SwitchPanicToErrorLog(b bool)
    • PanicOrErrorLog(s string)

2019年9月17日 星期二

Inter-Process Communication via websocket (golang)

github : https://github.com/eehsiao/websocket-ipc

websocket-ipc

Inter-Process Communication via websocket

how to be a deamon

install : install into service start : run as a deamon
sudo ./example install
sudo ./example start

how to stop deamon

when the deamon start
sudo ./example stop

how to remove deamon

when the deamon stop
sudo ./example remove

how to Communication

when the deamon start
sudo ./example {any string otherwise commands}

golang 連接mysql, redis 讀取資料的套件,簡單又好用

套件位置:
https://github.com/eehsiao/go-models-mysql
https://github.com/eehsiao/go-models-redis
https://github.com/eehsiao/go-models-lib
https://github.com/eehsiao/sqlbuilder
godoc:
https://godoc.org/github.com/eehsiao/go-models-mysql
https://godoc.org/github.com/eehsiao/go-models-redis
https://godoc.org/github.com/eehsiao/go-models-lib
https://godoc.org/github.com/eehsiao/sqlbuilder

go-models-mysql

go-models-mysql its lite and easy model.

Features

  • Field scanning has become easier since the original driver was extended. Assumption: we have 5 fields to scan
type Tb struct {
 field0 sql.NullString,
 field1 sql.NullString,
 field2 sql.NullString,
 field3 sql.NullString,
 field4 sql.NullString,
}
In original driver, it can't dynamic. how many fields, that you must write fields many how. it you have 20 fileds, you must write 20 times.
var tb Tb
err = rows.Scan(&tb.field0, &tb.field1, &tb.field2, &tb.field3, &tb.field4)
In go-models-mysql , you just fill struct nil pointer.
if val, err = myDao.ScanRowType(row, (*Tb)(nil)); err == nil {
 u, _ := val.(*Tb)
 fmt.Println("Tb", u)
}
  • DAO layer let you operate mysql more Intuitively.
    • Original driver (sql.DB) was extended, so you can operate original commands.
      • ex: Query, QueryRow, Exec ....
    • Import the sqlbuilder that help access sql db easily.
     myDao.Select("Host", "User", "Select_priv").From("user").Where("User='root'").Limit(1)
    • Set the default table in DAO, that you can design your dao layer friendly.
     // set a struct for dao as default model (option)
     // (*UserTb)(nil) : nil pointer of the UserTb struct
     // "user" : is real table name in the db
     myUserDao.SetDefaultModel((*UserTb)(nil), "user")
    
     // call model's Get() , get all rows in user table
     // return (rows *sql.Rows, err error)
     rows, err = myDao.Get()

Requirements

Go-Module

create go.mod file in your package folder, and fill below
module github.com/eehsiao/go-models-example

go 1.13

require (
 github.com/eehsiao/go-models-lib latest
 github.com/eehsiao/go-models-mysql latest
 github.com/eehsiao/go-models-redis latest
 github.com/eehsiao/sqlbuilder latest
 github.com/go-sql-driver/mysql v1.4.1
)

Docker

Easy to start the test evn. That you can run the example code.
$ docker-compose up -d

Usage

import (
    "database/sql"
 "fmt"

 mysql "github.com/eehsiao/go-models-mysql"
 redis "github.com/eehsiao/go-models-redis"
)

// UserTb : sql table struct that to store into mysql
type UserTb struct {
 Host       sql.NullString `TbField:"Host"`
 User       sql.NullString `TbField:"User"`
 SelectPriv sql.NullString `TbField:"Select_priv"`
}

//new mysql dao
myUserDao := &MyUserDao{
    Dao: mysql.NewDao().SetConfig("root", "mYaDmin", "127.0.0.1:3306", "mysql").OpenDB(),
}

// example 1 : directly use the sqlbuilder
myUserDao.Select("Host", "User", "Select_priv").From("user").Where("User='root'").Limit(1)
fmt.Println("sqlbuilder", myUserDao.BuildSelectSQL())
if row, err = myUserDao.GetRow(); err == nil {
    if val, err = myUserDao.ScanRowType(row, (*UserTb)(nil)); err == nil {
        u, _ := val.(*UserTb)
        fmt.Println("UserTb", u)
    }
}
    
// set a struct for dao as default model (option)
// (*UserTb)(nil) : nil pointer of the UserTb struct
// "user" : is real table name in the db
myUserDao.SetDefaultModel((*UserTb)(nil), "user")

// call model's Get() , get all rows in user table
// return (rows *sql.Rows, err error)
rows, err = myDao.Get()

// call model's GetRow() , get first row in user rows
// return (row *sql.Row, err error)
row, err = myDao.GetRow()

Example

1 build-in

The example will connect to local mysql and get user data. Then connect to local redis and set user data, and get back.

2 example

https://github.com/eehsiao/go-models-example/

How-to

How to design model data logical

MySQL

1.

create a table struct, and add the tag TbField:"real table filed"
TbField the tag is musted. read table filed also be same the table field.
type UserTb struct {
 Host       sql.NullString `TbField:"Host"`
 User       sql.NullString `TbField:"User"`
 SelectPriv sql.NullString `TbField:"Select_priv"`
}

2.

use Struce4QuerySlice to gen the sqlbuilder select fields
m := mysql.NewDao().SetConfig("root", "mYaDmin", "127.0.0.1:3306", "mysql").OpenDB()
m.Select(lib.Struce4QuerySlice(m.DaoStructType)...).From(m.TbName).Limit(3)

3.

scan the sql result to the struct of object
row, err = m.GetRow()
if val, err = m.ScanRowType(row, (*UserTb)(nil)); err == nil {
    u, _ := val.(*UserTb)
    fmt.Println("UserTb", u)
}


2019年9月16日 星期一

golang go mod reading https://sum.golang.org/lookup/github.com/... 410 Gone (slove)

研究 go module 且跨多的module引用時, 發生

verifying github.com/eehsiao/go-models-lib@v0.0.1/go.mod: github.com/eehsiao/go-models-lib@v0.0.1/go.mod: reading https://sum.golang.org/lookup/github.com/eehsiao/go-models-lib@v0.0.1: 410 Gone

 解決方法 (solve)

go mod tidy