f5/dbpool.go
2023-08-20 10:58:44 +08:00

210 lines
4.6 KiB
Go

package f5
import (
"q5"
"fmt"
"strings"
)
const (
GO_STYLE_DB = 1
JS_STYLE_DB = 2
)
type dbPool struct {
style int32
dataSourceHash map[string]*q5.Mysql
}
func (this *dbPool) init() {
}
func (this *dbPool) unInit() {
}
func (this *dbPool) Select(
dataSource string,
tblName string,
fields []string,
whereKv [][]string,
cb QueryResultCb) {
params := []string{}
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
this.joinWhere(&sql, &params, whereKv)
this.internalQuery(dataSource, sql, params, cb)
}
func (this *dbPool) OrmSelect(
dataSource string,
tblName string,
whereKv [][]string,
cb QueryResultCb) {
params := []string{}
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
this.joinWhere(&sql, &params, whereKv)
this.internalQuery(dataSource, sql, params, cb)
}
func (this *dbPool) SelectOne(
dataSource string,
tblName string,
fields []string,
whereKv [][]string,
cb QueryOneCb) {
params := []string{}
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
this.joinWhere(&sql, &params, whereKv)
this.internalQueryOne(dataSource, sql, params, cb)
}
func (this *dbPool) OrmSelectOne(
dataSource string,
tblName string,
fields []string,
whereKv [][]string,
cb QueryOneCb) {
params := []string{}
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
this.joinWhere(&sql, &params, whereKv)
this.internalQueryOne(dataSource, sql, params, cb)
}
func (this *dbPool) Update(
dataSource string,
tblName string,
fieldsKv [][]string,
whereKv [][]string,
cb ExecResultCb) {
params := []string{}
sql := "UPDATE `" + tblName + "` SET " + this.joinUpdateFields(fieldsKv, &params) +
" WHERE 1=1"
this.joinWhere(&sql, &params, whereKv)
this.internalExec(dataSource, sql, params, cb)
}
func (this *dbPool) Insert(
dataSource string,
tblName string,
fieldsKv [][]string,
cb ExecResultCb) {
params := []string{}
sql := "INSERT INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params)
this.internalExec(dataSource, sql, params, cb)
}
func (this *dbPool) Upsert(
dataSource string,
tblName string,
whereKv map[string]string,
updateKv map[string]string,
insertKv map[string]string,
cb ExecResultCb) {
}
func (this *dbPool) PageQuery(
dataSource string,
perPage int32,
page int32,
sql string,
params []string,
queryParam *PageQueryParam,
cb PageQueryCb) {
var pagination Pagination
pagination.PerPage = q5.Max(1, perPage)
pagination.CurrentPage = q5.Max(1, page)
}
func (this *dbPool) borrowConn(dataSource string) *q5.Mysql {
return nil
}
func (this *dbPool) returnConn(conn *q5.Mysql) {
}
func (this *dbPool) joinSelectFields(fields []string) string {
return strings.Join(
q5.Map(fields,
func(val string) string {
return "`" + val + "`"
}),
", ")
}
func (this *dbPool) joinWhere(sql *string, params *[]string, whereKv [][]string) {
for _, items := range whereKv {
*sql += " AND " + items[0] + "=?"
*params = append(*params, items[1])
}
}
func (this *dbPool) joinUpdateFields(fieldsKv [][]string, params *[]string) string {
return ""
}
func (this *dbPool) joinInsertFields(fieldsKv [][]string, params *[]string) string {
return ""
}
func (this *dbPool) internalExec(dataSource string, sql string, params []string,
cb ExecResultCb) {
conn := this.borrowConn(dataSource)
defer this.returnConn(conn)
result, err := conn.Exec(sql, q5.ToInterfaces(params)...)
var lastInsertId int64
var rowsAffected int64
if err == nil {
if id, err := result.LastInsertId(); err == nil {
lastInsertId = id
}
if id, err := result.RowsAffected(); err == nil {
rowsAffected = id
}
}
if this.style == GO_STYLE_DB {
cb(err, lastInsertId, rowsAffected)
} else {
_app.RegisterMainThreadCb(
func () {
cb(err, lastInsertId, rowsAffected)
})
}
}
func (this *dbPool) internalQuery(dataSource string, sql string, params []string,
cb QueryResultCb) {
conn := this.borrowConn(dataSource)
defer this.returnConn(conn)
rows, err := conn.Query(sql, q5.ToInterfaces(params)...)
if this.style == GO_STYLE_DB {
cb(err, NewDataSet(rows))
} else {
_app.RegisterMainThreadCb(
func () {
cb(err, NewDataSet(rows))
})
}
}
func (this *dbPool) internalQueryOne(dataSource string, sql string, params []string,
cb QueryOneCb) {
conn := this.borrowConn(dataSource)
defer this.returnConn(conn)
rows, err := conn.Query(sql, q5.ToInterfaces(params)...)
values := &[]*string{}
if err == nil {
dataSet := NewDataSet(rows)
if dataSet.Next() {
values = dataSet.GetValues()
}
}
if this.style == GO_STYLE_DB {
cb(err, values)
} else {
_app.RegisterMainThreadCb(
func () {
cb(err, values)
})
}
}