add comment

This commit is contained in:
zhengguanghao
2019-09-17 10:06:33 +08:00
parent ed78b8985d
commit 834f3ff062

View File

@@ -735,24 +735,29 @@ func (slf *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{}
return nil return nil
} }
func (slf *DBModule) GetTx() (*Tx, error) { // Begin starts a transaction.
func (slf *DBModule) Begin() (*Tx, error) {
var txDBMoudule Tx var txDBMoudule Tx
txdb, err := slf.db.Begin() txdb, err := slf.db.Begin()
if err != nil { if err != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "Begin error:%s", err.Error())
return &txDBMoudule, err return &txDBMoudule, err
} }
txDBMoudule.tx = txdb txDBMoudule.tx = txdb
return &txDBMoudule, nil return &txDBMoudule, nil
} }
// Rollback aborts the transaction.
func (slf *Tx) Rollback() error { func (slf *Tx) Rollback() error {
return slf.tx.Rollback() return slf.tx.Rollback()
} }
// Commit commits the transaction.
func (slf *Tx) Commit() error { func (slf *Tx) Commit() error {
return slf.tx.Commit() return slf.tx.Commit()
} }
// CheckArgs...
func (slf *Tx) CheckArgs(args ...interface{}) error { func (slf *Tx) CheckArgs(args ...interface{}) error {
for _, val := range args { for _, val := range args {
if reflect.TypeOf(val).Kind() == reflect.String { if reflect.TypeOf(val).Kind() == reflect.String {
@@ -796,6 +801,7 @@ func (slf *Tx) CheckArgs(args ...interface{}) error {
return nil return nil
} }
// Query executes a query that returns rows, typically a SELECT.
func (slf *Tx) Query(query string, args ...interface{}) DBResult { func (slf *Tx) Query(query string, args ...interface{}) DBResult {
if slf.CheckArgs(args) != nil { if slf.CheckArgs(args) != nil {
ret := DBResult{} ret := DBResult{}
@@ -813,7 +819,7 @@ func (slf *Tx) Query(query string, args ...interface{}) DBResult {
rows, err := slf.tx.Query(query, args...) rows, err := slf.tx.Query(query, args...)
if err != nil { if err != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%v)", query, err) service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%v)", query, err)
} }
return DBResult{ return DBResult{
@@ -824,6 +830,7 @@ func (slf *Tx) Query(query string, args ...interface{}) DBResult {
} }
} }
// IsPrintTimeLog...
func (slf *Tx) IsPrintTimeLog(Time time.Duration) bool { func (slf *Tx) IsPrintTimeLog(Time time.Duration) bool {
if slf.PrintTime != 0 && Time >= slf.PrintTime { if slf.PrintTime != 0 && Time >= slf.PrintTime {
return true return true
@@ -831,6 +838,7 @@ func (slf *Tx) IsPrintTimeLog(Time time.Duration) bool {
return false return false
} }
// QueryEx executes a query that return rows.
func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error) { func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error) {
datasetList := DataSetList{} datasetList := DataSetList{}
datasetList.tag = "json" datasetList.tag = "json"
@@ -851,7 +859,7 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
TimeFuncPass := time.Since(TimeFuncStart) TimeFuncPass := time.Since(TimeFuncStart)
if slf.IsPrintTimeLog(TimeFuncPass) { if slf.IsPrintTimeLog(TimeFuncPass) {
service.GetLogger().Printf(service.LEVER_INFO, "DBModule Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args) service.GetLogger().Printf(service.LEVER_INFO, "Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
} }
if err != nil { if err != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%v)", query, err) service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%v)", query, err)
@@ -895,7 +903,7 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
if hasRet == false { if hasRet == false {
if rows.Err() != nil { if rows.Err() != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%+v)", query, rows) service.GetLogger().Printf(service.LEVER_ERROR, "Tx Query:%s(%+v)", query, rows)
} }
break break
} }
@@ -904,7 +912,7 @@ func (slf *Tx) QueryEx(query string, args ...interface{}) (*DataSetList, error)
return &datasetList, nil return &datasetList, nil
} }
// Exec ... // Exec executes a query that doesn't return rows.
func (slf *Tx) Exec(query string, args ...interface{}) (*DBResultEx, error) { func (slf *Tx) Exec(query string, args ...interface{}) (*DBResultEx, error) {
ret := &DBResultEx{} ret := &DBResultEx{}
if slf.tx == nil { if slf.tx == nil {
@@ -922,10 +930,10 @@ func (slf *Tx) Exec(query string, args ...interface{}) (*DBResultEx, error) {
res, err := slf.tx.Exec(query, args...) res, err := slf.tx.Exec(query, args...)
TimeFuncPass := time.Since(TimeFuncStart) TimeFuncPass := time.Since(TimeFuncStart)
if slf.IsPrintTimeLog(TimeFuncPass) { if slf.IsPrintTimeLog(TimeFuncPass) {
service.GetLogger().Printf(service.LEVER_INFO, "DBModule QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args) service.GetLogger().Printf(service.LEVER_INFO, "Tx QueryEx Time %s , Query :%s , args :%+v", TimeFuncPass, query, args)
} }
if err != nil { if err != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err) service.GetLogger().Printf(service.LEVER_ERROR, "Tx Exec:%s(%v)", query, err)
return nil, err return nil, err
} }