1
This commit is contained in:
parent
a149beea06
commit
35f839e5d4
57
dbpool.go
57
dbpool.go
@ -70,7 +70,7 @@ func (this *dbPool) Select(
|
||||
params := []string{}
|
||||
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
|
||||
this.joinWhere(&sql, ¶ms, whereKv)
|
||||
this.internalQuery(dataSource, sql, params, cb)
|
||||
go this.internalQuery(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) OrmSelect(
|
||||
@ -81,34 +81,30 @@ func (this *dbPool) OrmSelect(
|
||||
params := []string{}
|
||||
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
|
||||
this.joinWhere(&sql, ¶ms, whereKv)
|
||||
this.internalQuery(dataSource, sql, params, cb)
|
||||
go this.internalQuery(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) SelectCustomQuery(dataSource string, sql string, cb QueryResultCb) {
|
||||
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) {
|
||||
chDone := make(chan bool)
|
||||
params := []string{}
|
||||
if this.style == GO_STYLE_DB {
|
||||
go this.internalQuery(dataSource, sql, params,
|
||||
func(err error, ds *DataSet) {
|
||||
cb(err, ds)
|
||||
chDone <- true
|
||||
})
|
||||
} else {
|
||||
this.internalQuery(dataSource, sql, params,
|
||||
func(err error, ds *DataSet) {
|
||||
cb(err, ds)
|
||||
chDone <- true
|
||||
})
|
||||
}
|
||||
var e error
|
||||
var d *DataSet
|
||||
go this.internalQueryNoMainThread(dataSource, sql, params,
|
||||
func(err error, ds *DataSet) {
|
||||
e = err
|
||||
d = ds
|
||||
chDone <- true
|
||||
})
|
||||
for {
|
||||
select {
|
||||
case <-chDone:
|
||||
close(chDone)
|
||||
cb(e, d)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -129,7 +125,7 @@ func (this *dbPool) SelectLike(
|
||||
this.joinWhereLike(&sql, ¶ms, likeWhere)
|
||||
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(
|
||||
@ -141,7 +137,7 @@ func (this *dbPool) SelectOne(
|
||||
params := []string{}
|
||||
sql := fmt.Sprintf("SELECT %s FROM %s WHERE 1=1 ", this.joinSelectFields(fields), tblName)
|
||||
this.joinWhere(&sql, ¶ms, whereKv)
|
||||
this.internalQueryOne(dataSource, sql, params, cb)
|
||||
go this.internalQueryOne(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) OrmSelectOne(
|
||||
@ -152,7 +148,7 @@ func (this *dbPool) OrmSelectOne(
|
||||
params := []string{}
|
||||
sql := fmt.Sprintf("SELECT * FROM %s WHERE 1=1 ", tblName)
|
||||
this.joinWhere(&sql, ¶ms, whereKv)
|
||||
this.internalQueryOne(dataSource, sql, params, cb)
|
||||
go this.internalQueryOne(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) Update(
|
||||
@ -165,7 +161,7 @@ func (this *dbPool) Update(
|
||||
sql := "UPDATE `" + tblName + "` SET " + this.joinUpdateFields(fieldsKv, ¶ms) +
|
||||
" WHERE 1=1"
|
||||
this.joinWhere(&sql, ¶ms, whereKv)
|
||||
this.internalExec(dataSource, sql, params, cb)
|
||||
go this.internalExec(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) Insert(
|
||||
@ -175,7 +171,7 @@ func (this *dbPool) Insert(
|
||||
cb ExecResultCb) {
|
||||
params := []string{}
|
||||
sql := "INSERT INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, ¶ms)
|
||||
this.internalExec(dataSource, sql, params, cb)
|
||||
go this.internalExec(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) Replace(
|
||||
@ -185,7 +181,7 @@ func (this *dbPool) Replace(
|
||||
cb ExecResultCb) {
|
||||
params := []string{}
|
||||
sql := "REPLACE INTO `" + tblName + "` " + this.joinInsertFields(fieldsKv, ¶ms)
|
||||
this.internalExec(dataSource, sql, params, cb)
|
||||
go this.internalExec(dataSource, sql, params, cb)
|
||||
}
|
||||
|
||||
func (this *dbPool) Upsert(
|
||||
@ -228,7 +224,7 @@ func (this *dbPool) PageQuery(
|
||||
if orderBy != "" {
|
||||
finalySql += " " + orderBy + " "
|
||||
}
|
||||
this.internalQueryOne(
|
||||
go this.internalQueryOne(
|
||||
dataSource,
|
||||
fmt.Sprintf("SELECT COUNT(*) FROM (%s) as t", finalySql),
|
||||
params,
|
||||
@ -244,7 +240,7 @@ func (this *dbPool) PageQuery(
|
||||
}
|
||||
start := pagination.PerPage * (pagination.CurrentPage - 1)
|
||||
limit := pagination.PerPage
|
||||
this.internalQuery(
|
||||
go this.internalQuery(
|
||||
dataSource,
|
||||
fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit),
|
||||
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,
|
||||
cb QueryOneCb) {
|
||||
ds := this.borrowConn(dataSource)
|
||||
|
Loading…
x
Reference in New Issue
Block a user