From f326f515dc536494256f4cafd5381b820bc082c1 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 20 Aug 2023 14:29:13 +0800 Subject: [PATCH] 1 --- dbpool.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- types.go | 1 - 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/dbpool.go b/dbpool.go index 0d02170..509a9e2 100644 --- a/dbpool.go +++ b/dbpool.go @@ -5,6 +5,7 @@ import ( "fmt" "math" "strings" + "sync" ) type DBStyle int32 @@ -14,9 +15,15 @@ const ( JS_STYLE_DB ) +type dataSource struct { + conn *q5.Mysql + entry q5.ListHead +} + type dbPool struct { style DBStyle - dataSourceHash map[string]*q5.Mysql + lock sync.Mutex + dataSourceHash map[string]*q5.ListHead } func (this *dbPool) init() { @@ -25,6 +32,29 @@ func (this *dbPool) init() { func (this *dbPool) unInit() { } +func (this *dbPool) RegisterDataSource(name string, host string, port int32, + user string, passwd string, dataBase string, size int32) { + this.lock.Lock() + defer this.lock.Unlock() + var head *q5.ListHead + if val, ok := this.dataSourceHash[name]; ok { + head = val + } else { + head = q5.NewListHead() + this.dataSourceHash[name] = head + } + for i := int32(0); i < size; i++ { + ds := dataSource{} + ds.conn = q5.NewMysql(host, port, user, passwd, dataBase) + ds.entry.Init(&ds) + head.AddTail(&ds.entry) + err := ds.conn.Open() + if err != nil { + panic(fmt.Sprintf("RegisterDataSource err:%s", err)) + } + } +} + func (this *dbPool) Select( dataSource string, tblName string, @@ -135,6 +165,20 @@ func (this *dbPool) PageQuery( pagination.Total = q5.ToInt32(*(*row)[0]) pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*(*row)[0]) / float64(pagination.PerPage))) + start := pagination.PerPage * (pagination.CurrentPage - 1) + limit := pagination.PerPage + this.internalQuery( + dataSource, + fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit), + params, + func (err error, rows *DataSet) { + if err != nil { + cb(err, &pagination) + return + } + pagination.Rows = rows + cb(nil, &pagination) + }) }) } diff --git a/types.go b/types.go index bacf7ae..43e4b59 100644 --- a/types.go +++ b/types.go @@ -22,7 +22,6 @@ type IMMsgNode struct { type Pagination struct { Total int32 - Count int32 PerPage int32 CurrentPage int32 TotalPages int32