From 1f205705b3e328718629456ca1f4e7d9c145b8ab Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 20:52:38 +0800 Subject: [PATCH] 1 --- app.go | 4 ++ dataset.go | 8 +++ dbpool.go | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++ export.go | 10 +++ types.go | 4 ++ 5 files changed, 229 insertions(+) create mode 100644 dbpool.go diff --git a/app.go b/app.go index d909898..8eacc36 100644 --- a/app.go +++ b/app.go @@ -21,6 +21,7 @@ type App interface { NotifyLoopCond() NowUnix() int64 NowUnixMilli() int64 + RegisterMainThreadCb(func ()) } type UserApp interface { @@ -159,6 +160,9 @@ func (this *app) RegisterIMMsgHandle(msgId uint16, handle func(q5.Args)) { this.imMsgHandlers[msgId] = handle } +func (this *app) RegisterMainThreadCb(func ()) { +} + func (this *app) goLoopTimer() { var waitMs int64 = 1000 * 10 for { diff --git a/dataset.go b/dataset.go index ff7027a..fd7890f 100644 --- a/dataset.go +++ b/dataset.go @@ -32,6 +32,14 @@ func (this *DataSet) GetColumns() []string { 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 { this.GetColumns() for i := 0; i < len(this.columns); i++ { diff --git a/dbpool.go b/dbpool.go new file mode 100644 index 0000000..3fd3f14 --- /dev/null +++ b/dbpool.go @@ -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) + }) + } +} diff --git a/export.go b/export.go index b5d134c..2c0b243 100644 --- a/export.go +++ b/export.go @@ -5,6 +5,8 @@ var _timer *Timer var _sysLog *SysLog_ var _tgLog *TGLog_ var _httpCliMgr *HttpCliMgr +var _goStyleDb *dbPool +var _jsStyleDb *dbPool func GetApp() App { return _app @@ -26,6 +28,14 @@ func GetHttpCliMgr() *HttpCliMgr { return _httpCliMgr } +func GetGoStyleDb() *dbPool { + return _goStyleDb +} + +func GetJsStyleDb() *dbPool { + return _jsStyleDb +} + func Run(userApp UserApp) { _app = new(app) _app.init(userApp) diff --git a/types.go b/types.go index 0df2727..8910a31 100644 --- a/types.go +++ b/types.go @@ -22,6 +22,10 @@ type IMMsgNode struct { type HandlerFunc func(*Context) +type QueryResultCb func (error, *DataSet); +type QueryOneCb func (error, *[]*string); +type ExecResultCb func (error, int64, int64); + type middleware struct { middlewareType int32 handlerFunc HandlerFunc