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()
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 mysqltypeUserTbstruct {
Host sql.NullString`TbField:"Host"`User sql.NullString`TbField:"User"`SelectPriv sql.NullString`TbField:"Select_priv"`
}
//new mysql daomyUserDao:= &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()