This commit is contained in:
殷勇 2023-08-21 14:52:02 +08:00
parent 1a8c40f038
commit 7fa1685792
2 changed files with 40 additions and 39 deletions

View File

@ -1,13 +1,13 @@
package f5 package f5
import ( import (
"q5" "errors"
"time"
"fmt" "fmt"
"math" "math"
"q5"
"strings" "strings"
"sync" "sync"
"errors" "time"
) )
type DBStyle int32 type DBStyle int32
@ -18,14 +18,14 @@ const (
) )
type dataSource struct { type dataSource struct {
name string name string
conn *q5.Mysql conn *q5.Mysql
entry q5.ListHead entry q5.ListHead
} }
type dbPool struct { type dbPool struct {
style DBStyle style DBStyle
lock sync.Mutex lock sync.Mutex
dataSourceHash map[string]*q5.ListHead dataSourceHash map[string]*q5.ListHead
} }
@ -162,21 +162,23 @@ func (this *dbPool) PageQuery(
dataSource, dataSource,
fmt.Sprintf("SELECT COUNT(*) FROM (%s)", finalySql), fmt.Sprintf("SELECT COUNT(*) FROM (%s)", finalySql),
params, params,
func (err error, row *[]*string) { func(err error, rows *DataSet) {
if err != nil { if err != nil {
cb(err, &pagination) cb(err, &pagination)
return return
} }
pagination.Total = q5.ToInt32(*(*row)[0]) if rows != nil && rows.Next() {
pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*(*row)[0]) / pagination.Total = q5.ToInt32(*rows.GetByIndex(0))
float64(pagination.PerPage))) pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*rows.GetByIndex(0)) /
float64(pagination.PerPage)))
}
start := pagination.PerPage * (pagination.CurrentPage - 1) start := pagination.PerPage * (pagination.CurrentPage - 1)
limit := pagination.PerPage limit := pagination.PerPage
this.internalQuery( this.internalQuery(
dataSource, dataSource,
fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit), fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit),
params, params,
func (err error, rows *DataSet) { func(err error, rows *DataSet) {
if err != nil { if err != nil {
cb(err, &pagination) cb(err, &pagination)
return return
@ -239,7 +241,7 @@ func (this *dbPool) joinUpdateFields(fieldsKv [][]string, params *[]string) stri
sql := "" sql := ""
for index, items := range fieldsKv { for index, items := range fieldsKv {
suffix := "" suffix := ""
if index + 1 < len(fieldsKv) { if index+1 < len(fieldsKv) {
suffix = "," suffix = ","
} }
if items[0][0] == '!' { if items[0][0] == '!' {
@ -256,7 +258,7 @@ func (this *dbPool) joinInsertFields(fieldsKv [][]string, params *[]string) stri
sql := " (" sql := " ("
for index, items := range fieldsKv { for index, items := range fieldsKv {
suffix := "" suffix := ""
if index + 1 < len(fieldsKv) { if index+1 < len(fieldsKv) {
suffix = "," suffix = ","
} }
sql += "`" + items[0] + "`" + suffix sql += "`" + items[0] + "`" + suffix
@ -265,7 +267,7 @@ func (this *dbPool) joinInsertFields(fieldsKv [][]string, params *[]string) stri
sql += " VALUES(" sql += " VALUES("
for index, items := range fieldsKv { for index, items := range fieldsKv {
suffix := "" suffix := ""
if index + 1 < len(fieldsKv) { if index+1 < len(fieldsKv) {
suffix = "," suffix = ","
} }
sql += "?" + suffix sql += "?" + suffix
@ -299,7 +301,7 @@ func (this *dbPool) internalExec(dataSource string, sql string, params []string,
cb(err, lastInsertId, rowsAffected) cb(err, lastInsertId, rowsAffected)
} else { } else {
_app.RegisterMainThreadCb( _app.RegisterMainThreadCb(
func () { func() {
cb(err, lastInsertId, rowsAffected) cb(err, lastInsertId, rowsAffected)
}) })
} }
@ -319,7 +321,7 @@ func (this *dbPool) internalQuery(dataSource string, sql string, params []string
cb(err, NewDataSet(rows)) cb(err, NewDataSet(rows))
} else { } else {
_app.RegisterMainThreadCb( _app.RegisterMainThreadCb(
func () { func() {
cb(err, NewDataSet(rows)) cb(err, NewDataSet(rows))
}) })
} }
@ -335,19 +337,16 @@ func (this *dbPool) internalQueryOne(dataSource string, sql string, params []str
rows, err := ds.conn.Query(sql, q5.ToInterfaces(params)...) rows, err := ds.conn.Query(sql, q5.ToInterfaces(params)...)
this.returnConn(ds) this.returnConn(ds)
values := &[]*string{} var dataSet *DataSet
if err == nil { if err == nil {
dataSet := NewDataSet(rows) dataSet = NewDataSet(rows)
if dataSet.Next() {
values = dataSet.GetValues()
}
} }
if this.style == GO_STYLE_DB { if this.style == GO_STYLE_DB {
cb(err, values) cb(err, dataSet)
} else { } else {
_app.RegisterMainThreadCb( _app.RegisterMainThreadCb(
func () { func() {
cb(err, values) cb(err, dataSet)
}) })
} }
} }

View File

@ -7,36 +7,38 @@ import (
const ( const (
WSPROXYPACKHEAD_C_SIZE = 20 WSPROXYPACKHEAD_C_SIZE = 20
WSPROXYPACKHEAD_S_SIZE = 16 WSPROXYPACKHEAD_S_SIZE = 16
NET_MSG_MAGIC_CODE = uint16('K') + uint16('S') << 8 NET_MSG_MAGIC_CODE = uint16('K') + uint16('S')<<8
) )
type MsgNode struct { type MsgNode struct {
} }
type IMMsgNode struct { type IMMsgNode struct {
msgId uint16 msgId uint16
params q5.Args params q5.Args
next *IMMsgNode next *IMMsgNode
} }
type Pagination struct { type Pagination struct {
Total int32 Total int32
PerPage int32 PerPage int32
CurrentPage int32 CurrentPage int32
TotalPages int32 TotalPages int32
Rows *DataSet Rows *DataSet
} }
type HandlerFunc func(*Context) type HandlerFunc func(*Context)
type QueryResultCb func (error, *DataSet); type QueryResultCb func(error, *DataSet)
type QueryOneCb func (error, *[]*string);
type PageQueryCb func (error, *Pagination); type QueryOneCb func(error, *DataSet)
type ExecResultCb func (error, int64, int64);
type PageQueryCb func(error, *Pagination)
type ExecResultCb func(error, int64, int64)
type middleware struct { type middleware struct {
middlewareType int32 middlewareType int32
handlerFunc HandlerFunc handlerFunc HandlerFunc
entry q5.ListHead entry q5.ListHead
} }