mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-19 23:57:28 +08:00
优化接口设计
This commit is contained in:
@@ -47,7 +47,6 @@ type DBResult struct {
|
|||||||
|
|
||||||
// DBResult ...
|
// DBResult ...
|
||||||
type DBResultEx struct {
|
type DBResultEx struct {
|
||||||
Err error
|
|
||||||
LastInsertID int64
|
LastInsertID int64
|
||||||
RowsAffected int64
|
RowsAffected int64
|
||||||
|
|
||||||
@@ -62,6 +61,21 @@ type DataSetList struct {
|
|||||||
blur bool
|
blur bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SyncDBResult ...
|
||||||
|
type SyncDBResult struct {
|
||||||
|
sres chan DBResult
|
||||||
|
}
|
||||||
|
|
||||||
|
type SyncQueryDBResultEx struct {
|
||||||
|
sres chan *DataSetList
|
||||||
|
err chan error
|
||||||
|
}
|
||||||
|
|
||||||
|
type SyncExecuteDBResult struct {
|
||||||
|
sres chan *DBResultEx
|
||||||
|
err chan error
|
||||||
|
}
|
||||||
|
|
||||||
func (slf *DBModule) Init(maxConn int, url string, userName string, password string, dbname string) error {
|
func (slf *DBModule) Init(maxConn int, url string, userName string, password string, dbname string) error {
|
||||||
slf.url = url
|
slf.url = url
|
||||||
slf.maxconn = maxConn
|
slf.maxconn = maxConn
|
||||||
@@ -283,11 +297,6 @@ func (slf *DBModule) Connect(maxConn int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncDBResult ...
|
|
||||||
type SyncDBResult struct {
|
|
||||||
sres chan DBResult
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get ...
|
// Get ...
|
||||||
func (slf *SyncDBResult) Get(timeoutMs int) DBResult {
|
func (slf *SyncDBResult) Get(timeoutMs int) DBResult {
|
||||||
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
||||||
@@ -302,6 +311,19 @@ func (slf *SyncDBResult) Get(timeoutMs int) DBResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *SyncQueryDBResultEx) Get(timeoutMs int) (*DataSetList, error) {
|
||||||
|
timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C
|
||||||
|
select {
|
||||||
|
case <-timerC:
|
||||||
|
break
|
||||||
|
case err := <-slf.err:
|
||||||
|
dataset := <-slf.sres
|
||||||
|
return dataset, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs)
|
||||||
|
}
|
||||||
|
|
||||||
// Query ...
|
// Query ...
|
||||||
func (slf *DBModule) Query(query string, args ...interface{}) DBResult {
|
func (slf *DBModule) Query(query string, args ...interface{}) DBResult {
|
||||||
if slf.db == nil {
|
if slf.db == nil {
|
||||||
@@ -323,11 +345,11 @@ func (slf *DBModule) Query(query string, args ...interface{}) DBResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *DBModule) QueryEx(query string, args ...interface{}) (DataSetList, error) {
|
func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, error) {
|
||||||
datasetList := DataSetList{}
|
datasetList := DataSetList{}
|
||||||
if slf.db == nil {
|
if slf.db == nil {
|
||||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||||
return datasetList, fmt.Errorf("cannot connect database!")
|
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||||
}
|
}
|
||||||
rows, err := slf.db.Query(query, args...)
|
rows, err := slf.db.Query(query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -335,7 +357,7 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (DataSetList, er
|
|||||||
if rows != nil {
|
if rows != nil {
|
||||||
rows.Close()
|
rows.Close()
|
||||||
}
|
}
|
||||||
return datasetList, err
|
return &datasetList, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
@@ -349,7 +371,7 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (DataSetList, er
|
|||||||
//RowInfo map[string][][]sql.NullString //map[fieldname][row][column]sql.NullString
|
//RowInfo map[string][][]sql.NullString //map[fieldname][row][column]sql.NullString
|
||||||
colField, err := rows.Columns()
|
colField, err := rows.Columns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return datasetList, err
|
return &datasetList, err
|
||||||
}
|
}
|
||||||
count := len(colField)
|
count := len(colField)
|
||||||
valuePtrs := make([]interface{}, count)
|
valuePtrs := make([]interface{}, count)
|
||||||
@@ -374,25 +396,27 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (DataSetList, er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return datasetList, nil
|
return &datasetList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncQuery ...
|
// SyncQuery ...
|
||||||
func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncDBResult {
|
func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncQueryDBResultEx {
|
||||||
ret := SyncDBResult{
|
ret := SyncQueryDBResultEx{
|
||||||
sres: make(chan DBResult, 1),
|
sres: make(chan *DataSetList, 1),
|
||||||
|
err: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||||
dbret := DBResult{}
|
dbret := DataSetList{}
|
||||||
dbret.Err = fmt.Errorf("chan is full,sql:%s", query)
|
ret.err <- fmt.Errorf("chan is full,sql:%s", query)
|
||||||
ret.sres <- dbret
|
ret.sres <- &dbret
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
slf.syncExecuteFun <- func() {
|
slf.syncExecuteFun <- func() {
|
||||||
rsp := slf.Query(query, args...)
|
rsp, err := slf.QueryEx(query, args...)
|
||||||
|
ret.err <- err
|
||||||
ret.sres <- rsp
|
ret.sres <- rsp
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -400,43 +424,46 @@ func (slf *DBModule) SyncQuery(query string, args ...interface{}) SyncDBResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exec ...
|
// Exec ...
|
||||||
func (slf *DBModule) Exec(query string, args ...interface{}) DBResult {
|
func (slf *DBModule) Exec(query string, args ...interface{}) (*DBResultEx, error) {
|
||||||
ret := DBResult{}
|
ret := &DBResultEx{}
|
||||||
if slf.db == nil {
|
if slf.db == nil {
|
||||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||||
ret.Err = fmt.Errorf("cannot connect database!")
|
return ret, fmt.Errorf("cannot connect database!")
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := slf.db.Exec(query, args...)
|
res, err := slf.db.Exec(query, args...)
|
||||||
ret.Err = err
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err)
|
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err)
|
||||||
return ret
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.LastInsertID, _ = res.LastInsertId()
|
ret.LastInsertID, _ = res.LastInsertId()
|
||||||
ret.RowsAffected, _ = res.RowsAffected()
|
ret.RowsAffected, _ = res.RowsAffected()
|
||||||
return ret
|
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncExec ...
|
// SyncExec ...
|
||||||
func (slf *DBModule) SyncExec(query string, args ...interface{}) SyncDBResult {
|
func (slf *DBModule) SyncExec(query string, args ...interface{}) *SyncExecuteDBResult {
|
||||||
ret := SyncDBResult{
|
ret := &SyncExecuteDBResult{
|
||||||
sres: make(chan DBResult, 1),
|
sres: make(chan *DBResultEx, 1),
|
||||||
|
err: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
if len(slf.syncExecuteFun) >= MAX_EXECUTE_FUN {
|
||||||
dbret := DBResult{}
|
ret.err <- fmt.Errorf("chan is full,sql:%s", query)
|
||||||
dbret.Err = fmt.Errorf("chan is full,sql:%s", query)
|
|
||||||
ret.sres <- dbret
|
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
slf.syncExecuteFun <- func() {
|
slf.syncExecuteFun <- func() {
|
||||||
rsp := slf.Exec(query, args...)
|
rsp, err := slf.Exec(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
ret.err <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ret.sres <- rsp
|
ret.sres <- rsp
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
Reference in New Issue
Block a user