1
This commit is contained in:
parent
096c5046c9
commit
1f205705b3
4
app.go
4
app.go
@ -21,6 +21,7 @@ type App interface {
|
|||||||
NotifyLoopCond()
|
NotifyLoopCond()
|
||||||
NowUnix() int64
|
NowUnix() int64
|
||||||
NowUnixMilli() int64
|
NowUnixMilli() int64
|
||||||
|
RegisterMainThreadCb(func ())
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserApp interface {
|
type UserApp interface {
|
||||||
@ -159,6 +160,9 @@ func (this *app) RegisterIMMsgHandle(msgId uint16, handle func(q5.Args)) {
|
|||||||
this.imMsgHandlers[msgId] = handle
|
this.imMsgHandlers[msgId] = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *app) RegisterMainThreadCb(func ()) {
|
||||||
|
}
|
||||||
|
|
||||||
func (this *app) goLoopTimer() {
|
func (this *app) goLoopTimer() {
|
||||||
var waitMs int64 = 1000 * 10
|
var waitMs int64 = 1000 * 10
|
||||||
for {
|
for {
|
||||||
|
@ -32,6 +32,14 @@ func (this *DataSet) GetColumns() []string {
|
|||||||
return this.columns
|
return this.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *DataSet) GetValues() *[]*string {
|
||||||
|
values := []*string{}
|
||||||
|
for _, val := range this.values {
|
||||||
|
values = append(values, val.(*string))
|
||||||
|
}
|
||||||
|
return &values
|
||||||
|
}
|
||||||
|
|
||||||
func (this *DataSet) GetByName(name string) *string {
|
func (this *DataSet) GetByName(name string) *string {
|
||||||
this.GetColumns()
|
this.GetColumns()
|
||||||
for i := 0; i < len(this.columns); i++ {
|
for i := 0; i < len(this.columns); i++ {
|
||||||
|
203
dbpool.go
Normal file
203
dbpool.go
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
package f5
|
||||||
|
|
||||||
|
import (
|
||||||
|
"q5"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GO_STYLE_DB = 1
|
||||||
|
JS_STYLE_DB = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
type dbPool struct {
|
||||||
|
style int32
|
||||||
|
dataSourceHash map[string]*q5.Mysql
|
||||||
|
}
|
||||||
|
n
|
||||||
|
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 := "SELECT " + this.joinSelectFields(fields) + " FROM " + tblName + " WHERE 1=1"
|
||||||
|
this.joinWhere(&sql, ¶ms, whereKv)
|
||||||
|
this.internalQuery(dataSource, sql, params, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *dbPool) OrmSelect(
|
||||||
|
dataSource string,
|
||||||
|
tblName string,
|
||||||
|
whereKv [][]string,
|
||||||
|
cb QueryResultCb) {
|
||||||
|
params := []string{}
|
||||||
|
sql := "SELECT * FROM " + tblName + " WHERE 1=1"
|
||||||
|
this.joinWhere(&sql, ¶ms, whereKv)
|
||||||
|
this.internalQuery(dataSource, sql, params, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *dbPool) SelectOne(
|
||||||
|
dataSource string,
|
||||||
|
tblName string,
|
||||||
|
fields []string,
|
||||||
|
whereKv [][]string,
|
||||||
|
cb QueryOneCb) {
|
||||||
|
params := []string{}
|
||||||
|
sql := "SELECT " + this.joinSelectFields(fields) + " FROM " + tblName + " WHERE 1=1"
|
||||||
|
this.joinWhere(&sql, ¶ms, whereKv)
|
||||||
|
this.internalQueryOne(dataSource, sql, params, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *dbPool) OrmSelectOne(
|
||||||
|
dataSource string,
|
||||||
|
tblName string,
|
||||||
|
fields []string,
|
||||||
|
whereKv [][]string,
|
||||||
|
cb QueryOneCb) {
|
||||||
|
params := []string{}
|
||||||
|
sql := "SELECT * FROM " + tblName + " WHERE 1=1"
|
||||||
|
this.joinWhere(&sql, ¶ms, 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, ¶ms) +
|
||||||
|
" WHERE 1=1"
|
||||||
|
this.joinWhere(&sql, ¶ms, 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, ¶ms)
|
||||||
|
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,
|
||||||
|
tblName string,
|
||||||
|
whereKv [][]string,
|
||||||
|
fieldsKv [][]string,
|
||||||
|
cb QueryResultCb) {
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
10
export.go
10
export.go
@ -5,6 +5,8 @@ var _timer *Timer
|
|||||||
var _sysLog *SysLog_
|
var _sysLog *SysLog_
|
||||||
var _tgLog *TGLog_
|
var _tgLog *TGLog_
|
||||||
var _httpCliMgr *HttpCliMgr
|
var _httpCliMgr *HttpCliMgr
|
||||||
|
var _goStyleDb *dbPool
|
||||||
|
var _jsStyleDb *dbPool
|
||||||
|
|
||||||
func GetApp() App {
|
func GetApp() App {
|
||||||
return _app
|
return _app
|
||||||
@ -26,6 +28,14 @@ func GetHttpCliMgr() *HttpCliMgr {
|
|||||||
return _httpCliMgr
|
return _httpCliMgr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetGoStyleDb() *dbPool {
|
||||||
|
return _goStyleDb
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetJsStyleDb() *dbPool {
|
||||||
|
return _jsStyleDb
|
||||||
|
}
|
||||||
|
|
||||||
func Run(userApp UserApp) {
|
func Run(userApp UserApp) {
|
||||||
_app = new(app)
|
_app = new(app)
|
||||||
_app.init(userApp)
|
_app.init(userApp)
|
||||||
|
4
types.go
4
types.go
@ -22,6 +22,10 @@ type IMMsgNode struct {
|
|||||||
|
|
||||||
type HandlerFunc func(*Context)
|
type HandlerFunc func(*Context)
|
||||||
|
|
||||||
|
type QueryResultCb func (error, *DataSet);
|
||||||
|
type QueryOneCb func (error, *[]*string);
|
||||||
|
type ExecResultCb func (error, int64, int64);
|
||||||
|
|
||||||
type middleware struct {
|
type middleware struct {
|
||||||
middlewareType int32
|
middlewareType int32
|
||||||
handlerFunc HandlerFunc
|
handlerFunc HandlerFunc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user