Browse Source

Optimization

Vova Tkach 6 years ago
parent
commit
8f1cfe6fda
3 changed files with 61 additions and 36 deletions
  1. 4 7
      engine/sqlw/log.go
  2. 29 15
      engine/sqlw/sqlw.go
  3. 28 14
      engine/sqlw/txw.go

+ 4 - 7
engine/sqlw/log.go

@@ -22,12 +22,9 @@ func log(query string, s time.Time, transaction bool) {
 	if reg, err := regexp.Compile("[\\s\\t]+;$"); err == nil {
 		msg = reg.ReplaceAllString(msg, ";")
 	}
-	if consts.ParamDebug {
-		t := time.Now().Sub(s).Seconds()
-		if consts.IS_WIN {
-			fmt.Fprintln(os.Stdout, "[SQL] "+msg+fmt.Sprintf(" %.3f ms", t))
-		} else {
-			fmt.Fprintln(os.Stdout, "\033["+color+"m[SQL] "+msg+fmt.Sprintf(" %.3f ms", t)+"\033[0m")
-		}
+	if consts.IS_WIN {
+		fmt.Fprintln(os.Stdout, "[SQL] "+msg+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds()))
+	} else {
+		fmt.Fprintln(os.Stdout, "\033["+color+"m[SQL] "+msg+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds())+"\033[0m")
 	}
 }

+ 29 - 15
engine/sqlw/sqlw.go

@@ -6,6 +6,8 @@ import (
 
 	"errors"
 	"time"
+
+	"golang-fave/consts"
 )
 
 type Rows = sql.Rows
@@ -45,10 +47,13 @@ func (this *DB) SetMaxOpenConns(n int) {
 }
 
 func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
-	s := time.Now()
-	r := this.db.QueryRow(query, args...)
-	log(query, s, false)
-	return r
+	if consts.ParamDebug {
+		s := time.Now()
+		r := this.db.QueryRow(query, args...)
+		log(query, s, false)
+		return r
+	}
+	return this.db.QueryRow(query, args...)
 }
 
 func (this *DB) Begin() (*Tx, error) {
@@ -56,23 +61,32 @@ func (this *DB) Begin() (*Tx, error) {
 	if err != nil {
 		return nil, err
 	}
-	s := time.Now()
-	log("[TX] TRANSACTION START", s, true)
-	return &Tx{tx, s}, err
+	if consts.ParamDebug {
+		s := time.Now()
+		log("[TX] TRANSACTION START", s, true)
+		return &Tx{tx, s}, err
+	}
+	return &Tx{tx, time.Now()}, err
 }
 
 func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
-	s := time.Now()
-	r, e := this.db.Query(query, args...)
-	log(query, s, false)
-	return r, e
+	if consts.ParamDebug {
+		s := time.Now()
+		r, e := this.db.Query(query, args...)
+		log(query, s, false)
+		return r, e
+	}
+	return this.db.Query(query, args...)
 }
 
 func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
-	s := time.Now()
-	r, e := this.db.Exec(query, args...)
-	log(query, s, false)
-	return r, e
+	if consts.ParamDebug {
+		s := time.Now()
+		r, e := this.db.Exec(query, args...)
+		log(query, s, false)
+		return r, e
+	}
+	return this.db.Exec(query, args...)
 }
 
 func (this *DB) Transaction(queries func(tx *Tx) error) error {

+ 28 - 14
engine/sqlw/txw.go

@@ -5,6 +5,8 @@ import (
 	_ "github.com/go-sql-driver/mysql"
 
 	"time"
+
+	"golang-fave/consts"
 )
 
 type Tx struct {
@@ -13,27 +15,39 @@ type Tx struct {
 }
 
 func (this *Tx) Rollback() error {
-	err := this.tx.Rollback()
-	log("[TX] TRANSACTION END (Rollback)", this.s, true)
-	return err
+	if consts.ParamDebug {
+		err := this.tx.Rollback()
+		log("[TX] TRANSACTION END (Rollback)", this.s, true)
+		return err
+	}
+	return this.tx.Rollback()
 }
 
 func (this *Tx) Commit() error {
-	err := this.tx.Commit()
-	log("[TX] TRANSACTION END (Commit)", this.s, true)
-	return err
+	if consts.ParamDebug {
+		err := this.tx.Commit()
+		log("[TX] TRANSACTION END (Commit)", this.s, true)
+		return err
+	}
+	return this.tx.Commit()
 }
 
 func (this *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
-	s := time.Now()
-	r, e := this.tx.Exec(query, args...)
-	log("[TX] "+query, s, true)
-	return r, e
+	if consts.ParamDebug {
+		s := time.Now()
+		r, e := this.tx.Exec(query, args...)
+		log("[TX] "+query, s, true)
+		return r, e
+	}
+	return this.tx.Exec(query, args...)
 }
 
 func (this *Tx) QueryRow(query string, args ...interface{}) *sql.Row {
-	s := time.Now()
-	r := this.tx.QueryRow(query, args...)
-	log("[TX] "+query, s, true)
-	return r
+	if consts.ParamDebug {
+		s := time.Now()
+		r := this.tx.QueryRow(query, args...)
+		log("[TX] "+query, s, true)
+		return r
+	}
+	return this.tx.QueryRow(query, args...)
 }