1
This commit is contained in:
parent
52d292902a
commit
f326f515dc
46
dbpool.go
46
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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user