This commit is contained in:
azw 2023-08-20 10:58:44 +08:00
parent 1f205705b3
commit 1301cb260f
4 changed files with 162 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package f5
import ( import (
"q5" "q5"
"fmt"
"strings" "strings"
) )
@ -14,7 +15,7 @@ type dbPool struct {
style int32 style int32
dataSourceHash map[string]*q5.Mysql dataSourceHash map[string]*q5.Mysql
} }
n
func (this *dbPool) init() { func (this *dbPool) init() {
} }
@ -28,7 +29,7 @@ func (this *dbPool) Select(
whereKv [][]string, whereKv [][]string,
cb QueryResultCb) { cb QueryResultCb) {
params := []string{} params := []string{}
sql := "SELECT " + this.joinSelectFields(fields) + " FROM " + tblName + " WHERE 1=1" sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
this.joinWhere(&sql, &params, whereKv) this.joinWhere(&sql, &params, whereKv)
this.internalQuery(dataSource, sql, params, cb) this.internalQuery(dataSource, sql, params, cb)
} }
@ -39,7 +40,7 @@ func (this *dbPool) OrmSelect(
whereKv [][]string, whereKv [][]string,
cb QueryResultCb) { cb QueryResultCb) {
params := []string{} params := []string{}
sql := "SELECT * FROM " + tblName + " WHERE 1=1" sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
this.joinWhere(&sql, &params, whereKv) this.joinWhere(&sql, &params, whereKv)
this.internalQuery(dataSource, sql, params, cb) this.internalQuery(dataSource, sql, params, cb)
} }
@ -51,7 +52,7 @@ func (this *dbPool) SelectOne(
whereKv [][]string, whereKv [][]string,
cb QueryOneCb) { cb QueryOneCb) {
params := []string{} params := []string{}
sql := "SELECT " + this.joinSelectFields(fields) + " FROM " + tblName + " WHERE 1=1" sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
this.joinWhere(&sql, &params, whereKv) this.joinWhere(&sql, &params, whereKv)
this.internalQueryOne(dataSource, sql, params, cb) this.internalQueryOne(dataSource, sql, params, cb)
} }
@ -63,7 +64,7 @@ func (this *dbPool) OrmSelectOne(
whereKv [][]string, whereKv [][]string,
cb QueryOneCb) { cb QueryOneCb) {
params := []string{} params := []string{}
sql := "SELECT * FROM " + tblName + " WHERE 1=1" sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
this.joinWhere(&sql, &params, whereKv) this.joinWhere(&sql, &params, whereKv)
this.internalQueryOne(dataSource, sql, params, cb) this.internalQueryOne(dataSource, sql, params, cb)
} }
@ -102,10 +103,15 @@ func (this *dbPool) Upsert(
func (this *dbPool) PageQuery( func (this *dbPool) PageQuery(
dataSource string, dataSource string,
tblName string, perPage int32,
whereKv [][]string, page int32,
fieldsKv [][]string, sql string,
cb QueryResultCb) { 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 { func (this *dbPool) borrowConn(dataSource string) *q5.Mysql {

View File

@ -5,6 +5,7 @@ var _timer *Timer
var _sysLog *SysLog_ var _sysLog *SysLog_
var _tgLog *TGLog_ var _tgLog *TGLog_
var _httpCliMgr *HttpCliMgr var _httpCliMgr *HttpCliMgr
var _pageQuery *pageQuery
var _goStyleDb *dbPool var _goStyleDb *dbPool
var _jsStyleDb *dbPool var _jsStyleDb *dbPool
@ -28,6 +29,10 @@ func GetHttpCliMgr() *HttpCliMgr {
return _httpCliMgr return _httpCliMgr
} }
func GetPageQuery() *pageQuery {
return _pageQuery
}
func GetGoStyleDb() *dbPool { func GetGoStyleDb() *dbPool {
return _goStyleDb return _goStyleDb
} }

132
pagequery.go Normal file
View File

@ -0,0 +1,132 @@
package f5
/*
import (
"fmt"
)
*/
type LinkOp int32
const (
LK_AND LinkOp = iota
LK_OR
)
type QueryCond int32
const (
QC_LIKE QueryCond = iota
QC_EQ
QC_LT
QC_LE
QC_GT
QC_GE
QC_CUSTOM
)
type pageQuery struct {
}
type PageQueryFilter interface {
And() PageQueryFilter
Or() PageQueryFilter
Not() PageQueryFilter
GenSql() string
}
type PageQueryOne struct {
linkOp LinkOp
not bool
fieldName string
cond QueryCond
ignoreEmpty bool
val string
}
type PageQueryComp struct {
linkOp LinkOp
not bool
subFilters []*PageQueryOne
}
type PageQueryParam struct {
Filter []PageQueryFilter
OrderBy string
}
func (this *PageQueryParam) genSql() string {
sql := " "
/*
for _, field := range this.Filter {
}*/
sql += " " + this.OrderBy + " "
return sql
}
func (this *pageQuery) Like(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_LIKE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) EQ(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_EQ,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) LT(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_LT,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) LE(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_LE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) GT(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_GT,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) GE(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_GE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) Custom(fieldName string, val string) *PageQueryOne {
f := &PageQueryOne{
cond: QC_CUSTOM,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) Comp(subFilters []*PageQueryOne) *PageQueryComp {
f := &PageQueryComp{
subFilters: subFilters}
return f
}
func (this *PageQueryOne) IgnoreEmpy() *PageQueryOne {
this.ignoreEmpty = true
return this
}

View File

@ -20,10 +20,20 @@ type IMMsgNode struct {
next *IMMsgNode next *IMMsgNode
} }
type Pagination struct {
Total int32
Count int32
PerPage int32
CurrentPage int32
TotalPages int32
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 QueryOneCb func (error, *[]*string);
type PageQueryCb func (error, Pagination);
type ExecResultCb func (error, int64, int64); type ExecResultCb func (error, int64, int64);
type middleware struct { type middleware struct {