This commit is contained in:
aozhiwei 2024-03-24 09:23:17 +08:00
parent a149beea06
commit 35f839e5d4

View File

@ -70,7 +70,7 @@ func (this *dbPool) Select(
params := []string{} params := []string{}
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName) 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) go this.internalQuery(dataSource, sql, params, cb)
} }
func (this *dbPool) OrmSelect( func (this *dbPool) OrmSelect(
@ -81,34 +81,30 @@ func (this *dbPool) OrmSelect(
params := []string{} params := []string{}
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName) 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) go this.internalQuery(dataSource, sql, params, cb)
} }
func (this *dbPool) SelectCustomQuery(dataSource string, sql string, cb QueryResultCb) { func (this *dbPool) SelectCustomQuery(dataSource string, sql string, cb QueryResultCb) {
params := []string{} params := []string{}
this.internalQuery(dataSource, sql, params, cb) go this.internalQuery(dataSource, sql, params, cb)
} }
func (this *dbPool) SyncSelectCustomQuery(dataSource string, sql string, cb QueryResultCb) { func (this *dbPool) SyncSelectCustomQuery(dataSource string, sql string, cb QueryResultCb) {
chDone := make(chan bool) chDone := make(chan bool)
params := []string{} params := []string{}
if this.style == GO_STYLE_DB { var e error
go this.internalQuery(dataSource, sql, params, var d *DataSet
func(err error, ds *DataSet) { go this.internalQueryNoMainThread(dataSource, sql, params,
cb(err, ds) func(err error, ds *DataSet) {
chDone <- true e = err
}) d = ds
} else { chDone <- true
this.internalQuery(dataSource, sql, params, })
func(err error, ds *DataSet) {
cb(err, ds)
chDone <- true
})
}
for { for {
select { select {
case <-chDone: case <-chDone:
close(chDone) close(chDone)
cb(e, d)
return return
} }
} }
@ -129,7 +125,7 @@ func (this *dbPool) SelectLike(
this.joinWhereLike(&sql, &params, likeWhere) this.joinWhereLike(&sql, &params, likeWhere)
sql = fmt.Sprintf("%s ORDER BY idx ASC LIMIT %d", sql, limit) sql = fmt.Sprintf("%s ORDER BY idx ASC LIMIT %d", sql, limit)
this.internalQuery(dataSource, sql, params, cb) go this.internalQuery(dataSource, sql, params, cb)
} }
func (this *dbPool) SelectOne( func (this *dbPool) SelectOne(
@ -141,7 +137,7 @@ func (this *dbPool) SelectOne(
params := []string{} params := []string{}
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName) 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) go this.internalQueryOne(dataSource, sql, params, cb)
} }
func (this *dbPool) OrmSelectOne( func (this *dbPool) OrmSelectOne(
@ -152,7 +148,7 @@ func (this *dbPool) OrmSelectOne(
params := []string{} params := []string{}
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName) 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) go this.internalQueryOne(dataSource, sql, params, cb)
} }
func (this *dbPool) Update( func (this *dbPool) Update(
@ -165,7 +161,7 @@ func (this *dbPool) Update(
sql := "UPDATE `" + tblName + "` SET " + this.joinUpdateFields(fieldsKv, &params) + sql := "UPDATE `" + tblName + "` SET " + this.joinUpdateFields(fieldsKv, &params) +
" WHERE 1=1" " WHERE 1=1"
this.joinWhere(&sql, &params, whereKv) this.joinWhere(&sql, &params, whereKv)
this.internalExec(dataSource, sql, params, cb) go this.internalExec(dataSource, sql, params, cb)
} }
func (this *dbPool) Insert( func (this *dbPool) Insert(
@ -175,7 +171,7 @@ func (this *dbPool) Insert(
cb ExecResultCb) { cb ExecResultCb) {
params := []string{} params := []string{}
sql := "INSERT INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params) sql := "INSERT INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params)
this.internalExec(dataSource, sql, params, cb) go this.internalExec(dataSource, sql, params, cb)
} }
func (this *dbPool) Replace( func (this *dbPool) Replace(
@ -185,7 +181,7 @@ func (this *dbPool) Replace(
cb ExecResultCb) { cb ExecResultCb) {
params := []string{} params := []string{}
sql := "REPLACE INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params) sql := "REPLACE INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, &params)
this.internalExec(dataSource, sql, params, cb) go this.internalExec(dataSource, sql, params, cb)
} }
func (this *dbPool) Upsert( func (this *dbPool) Upsert(
@ -228,7 +224,7 @@ func (this *dbPool) PageQuery(
if orderBy != "" { if orderBy != "" {
finalySql += " " + orderBy + " " finalySql += " " + orderBy + " "
} }
this.internalQueryOne( go this.internalQueryOne(
dataSource, dataSource,
fmt.Sprintf("SELECT COUNT(*) FROM (%s) as t", finalySql), fmt.Sprintf("SELECT COUNT(*) FROM (%s) as t", finalySql),
params, params,
@ -244,7 +240,7 @@ func (this *dbPool) PageQuery(
} }
start := pagination.PerPage * (pagination.CurrentPage - 1) start := pagination.PerPage * (pagination.CurrentPage - 1)
limit := pagination.PerPage limit := pagination.PerPage
this.internalQuery( go this.internalQuery(
dataSource, dataSource,
fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit), fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit),
params, params,
@ -404,6 +400,19 @@ func (this *dbPool) internalQuery(dataSource string, sql string, params []string
} }
} }
func (this *dbPool) internalQueryNoMainThread(dataSource string, sql string, params []string,
cb QueryResultCb) {
ds := this.borrowConn(dataSource)
if ds == nil {
cb(errors.New("borrowConn error"), nil)
return
}
rows, err := ds.conn.Query(sql, q5.ToInterfaces(params)...)
this.returnConn(ds)
cb(err, NewDataSet(rows))
}
func (this *dbPool) internalQueryOne(dataSource string, sql string, params []string, func (this *dbPool) internalQueryOne(dataSource string, sql string, params []string,
cb QueryOneCb) { cb QueryOneCb) {
ds := this.borrowConn(dataSource) ds := this.borrowConn(dataSource)