Browse Source

Code optimization

Volodymyr Tkach 2 years ago
parent
commit
0562ad7e2f
4 changed files with 20 additions and 20 deletions
  1. 2 2
      gosql/common/common.go
  2. 6 6
      gosql/common/common_test.go
  3. 7 7
      gosql/common/dbmethods.go
  4. 5 5
      gosql/common/tx.go

+ 2 - 2
gosql/common/common.go

@@ -50,7 +50,7 @@ func log(w io.Writer, fname string, start time.Time, err error, tx bool, query s
 
 
 	// Function name
 	// Function name
 	if fname != "" {
 	if fname != "" {
-		values = append(values, fname)
+		values = append(values, "[func "+fname+"]")
 	}
 	}
 
 
 	// SQL query
 	// SQL query
@@ -131,7 +131,7 @@ func OpenDB(databaseURL *url.URL, migrationsDir string, debug bool) (*sql.DB, er
 	if debug {
 	if debug {
 		t := time.Now()
 		t := time.Now()
 		db, err = driver.Open()
 		db, err = driver.Open()
-		log(os.Stdout, "[func Open]", t, err, false, "")
+		log(os.Stdout, "Open", t, err, false, "")
 		if err != nil {
 		if err != nil {
 			return nil, fmt.Errorf("DB open error: %w", err)
 			return nil, fmt.Errorf("DB open error: %w", err)
 		}
 		}

+ 6 - 6
gosql/common/common_test.go

@@ -18,34 +18,34 @@ var _ = Describe("common", func() {
 	Context("log", func() {
 	Context("log", func() {
 		Context("time", func() {
 		Context("time", func() {
 			It("calculate one second", func() {
 			It("calculate one second", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now().Add(time.Second*-1), nil, false, "")
+				str := common.Log(io.Discard, "Exec", time.Now().Add(time.Second*-1), nil, false, "")
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 1.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 1.000 ms\x1b[0m\n"))
 			})
 			})
 		})
 		})
 
 
 		Context("format", func() {
 		Context("format", func() {
 			It("with func name", func() {
 			It("with func name", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now(), nil, false, "")
+				str := common.Log(io.Discard, "Exec", time.Now(), nil, false, "")
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 0.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 0.000 ms\x1b[0m\n"))
 			})
 			})
 
 
 			It("with sql query", func() {
 			It("with sql query", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now(), nil, false, "select * from users")
+				str := common.Log(io.Discard, "Exec", time.Now(), nil, false, "select * from users")
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] select * from users (empty) (nil) 0.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] select * from users (empty) (nil) 0.000 ms\x1b[0m\n"))
 			})
 			})
 
 
 			It("with error message", func() {
 			It("with error message", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now(), fmt.Errorf("Exec error"), false, "select * from users")
+				str := common.Log(io.Discard, "Exec", time.Now(), fmt.Errorf("Exec error"), false, "select * from users")
 				Expect(str).To(Equal("\x1b[0;31m[SQL] [func Exec] select * from users (empty) (Exec error) 0.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[0;31m[SQL] [func Exec] select * from users (empty) (Exec error) 0.000 ms\x1b[0m\n"))
 			})
 			})
 
 
 			It("with transaction flag", func() {
 			It("with transaction flag", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now(), fmt.Errorf("Exec error"), true, "select * from users")
+				str := common.Log(io.Discard, "Exec", time.Now(), fmt.Errorf("Exec error"), true, "select * from users")
 				Expect(str).To(Equal("\x1b[1;31m[SQL] [TX] [func Exec] select * from users (empty) (Exec error) 0.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[1;31m[SQL] [TX] [func Exec] select * from users (empty) (Exec error) 0.000 ms\x1b[0m\n"))
 			})
 			})
 
 
 			It("with sql query arguments", func() {
 			It("with sql query arguments", func() {
-				str := common.Log(io.Discard, "[func Exec]", time.Now(), fmt.Errorf("Exec error"), true, "select * from users where id=$1", 100)
+				str := common.Log(io.Discard, "Exec", time.Now(), fmt.Errorf("Exec error"), true, "select * from users where id=$1", 100)
 				Expect(str).To(Equal("\x1b[1;31m[SQL] [TX] [func Exec] select * from users where id=$1 ([100]) (Exec error) 0.000 ms\x1b[0m\n"))
 				Expect(str).To(Equal("\x1b[1;31m[SQL] [TX] [func Exec] select * from users where id=$1 ([100]) (Exec error) 0.000 ms\x1b[0m\n"))
 			})
 			})
 		})
 		})

+ 7 - 7
gosql/common/dbmethods.go

@@ -26,7 +26,7 @@ func (db *DBMethods) Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		tx, err := db.DB.BeginTx(ctx, opts)
 		tx, err := db.DB.BeginTx(ctx, opts)
-		log(os.Stdout, "[func Begin]", t, err, true, "")
+		log(os.Stdout, "Begin", t, err, true, "")
 		return &Tx{tx, db.Debug, db.Driver, t}, err
 		return &Tx{tx, db.Debug, db.Driver, t}, err
 	}
 	}
 
 
@@ -41,7 +41,7 @@ func (db *DBMethods) Close() error {
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		err := db.DB.Close()
 		err := db.DB.Close()
-		log(os.Stdout, "[func Close]", t, err, false, "")
+		log(os.Stdout, "Close", t, err, false, "")
 		return err
 		return err
 	}
 	}
 	return db.DB.Close()
 	return db.DB.Close()
@@ -51,7 +51,7 @@ func (db *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.R
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		res, err := db.DB.ExecContext(ctx, db.fixQuery(query), args...)
 		res, err := db.DB.ExecContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func Exec]", t, err, false, db.fixQuery(query), args...)
+		log(os.Stdout, "Exec", t, err, false, db.fixQuery(query), args...)
 		return res, err
 		return res, err
 	}
 	}
 	return db.DB.ExecContext(ctx, db.fixQuery(query), args...)
 	return db.DB.ExecContext(ctx, db.fixQuery(query), args...)
@@ -61,7 +61,7 @@ func (db *DBMethods) Ping(ctx context.Context) error {
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		err := db.DB.PingContext(ctx)
 		err := db.DB.PingContext(ctx)
-		log(os.Stdout, "[func Ping]", t, err, false, "")
+		log(os.Stdout, "Ping", t, err, false, "")
 		return err
 		return err
 	}
 	}
 	return db.DB.PingContext(ctx)
 	return db.DB.PingContext(ctx)
@@ -71,7 +71,7 @@ func (db *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, erro
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		stm, err := db.DB.PrepareContext(ctx, db.fixQuery(query))
 		stm, err := db.DB.PrepareContext(ctx, db.fixQuery(query))
-		log(os.Stdout, "[func Prepare]", t, err, false, db.fixQuery(query))
+		log(os.Stdout, "Prepare", t, err, false, db.fixQuery(query))
 		return stm, err
 		return stm, err
 	}
 	}
 	return db.DB.PrepareContext(ctx, db.fixQuery(query))
 	return db.DB.PrepareContext(ctx, db.fixQuery(query))
@@ -81,7 +81,7 @@ func (db *DBMethods) Query(ctx context.Context, query string, args ...any) (*sql
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		rows, err := db.DB.QueryContext(ctx, db.fixQuery(query), args...)
 		rows, err := db.DB.QueryContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func Query]", t, err, false, db.fixQuery(query), args...)
+		log(os.Stdout, "Query", t, err, false, db.fixQuery(query), args...)
 		return rows, err
 		return rows, err
 	}
 	}
 	return db.DB.QueryContext(ctx, db.fixQuery(query), args...)
 	return db.DB.QueryContext(ctx, db.fixQuery(query), args...)
@@ -91,7 +91,7 @@ func (db *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *s
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		row := db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
 		row := db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func QueryRow]", t, nil, false, db.fixQuery(query), args...)
+		log(os.Stdout, "QueryRow", t, nil, false, db.fixQuery(query), args...)
 		return row
 		return row
 	}
 	}
 	return db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
 	return db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)

+ 5 - 5
gosql/common/tx.go

@@ -26,7 +26,7 @@ func (db *Tx) fixQuery(query string) string {
 func (db *Tx) Commit() error {
 func (db *Tx) Commit() error {
 	if db.Debug {
 	if db.Debug {
 		err := db.tx.Commit()
 		err := db.tx.Commit()
-		log(os.Stdout, "[func Commit]", db.t, err, true, "")
+		log(os.Stdout, "Commit", db.t, err, true, "")
 		return err
 		return err
 	}
 	}
 	return db.tx.Commit()
 	return db.tx.Commit()
@@ -36,7 +36,7 @@ func (db *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result,
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		res, err := db.tx.ExecContext(ctx, db.fixQuery(query), args...)
 		res, err := db.tx.ExecContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func Exec]", t, err, true, db.fixQuery(query), args...)
+		log(os.Stdout, "Exec", t, err, true, db.fixQuery(query), args...)
 		return res, err
 		return res, err
 	}
 	}
 	return db.tx.ExecContext(ctx, db.fixQuery(query), args...)
 	return db.tx.ExecContext(ctx, db.fixQuery(query), args...)
@@ -46,7 +46,7 @@ func (db *Tx) Query(ctx context.Context, query string, args ...any) (*sql.Rows,
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		rows, err := db.tx.QueryContext(ctx, db.fixQuery(query), args...)
 		rows, err := db.tx.QueryContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func Query]", t, err, true, db.fixQuery(query), args...)
+		log(os.Stdout, "Query", t, err, true, db.fixQuery(query), args...)
 		return rows, err
 		return rows, err
 	}
 	}
 	return db.tx.QueryContext(ctx, db.fixQuery(query), args...)
 	return db.tx.QueryContext(ctx, db.fixQuery(query), args...)
@@ -56,7 +56,7 @@ func (db *Tx) QueryRow(ctx context.Context, query string, args ...any) *sql.Row
 	if db.Debug {
 	if db.Debug {
 		t := time.Now()
 		t := time.Now()
 		row := db.tx.QueryRowContext(ctx, db.fixQuery(query), args...)
 		row := db.tx.QueryRowContext(ctx, db.fixQuery(query), args...)
-		log(os.Stdout, "[func QueryRow]", t, nil, true, db.fixQuery(query), args...)
+		log(os.Stdout, "QueryRow", t, nil, true, db.fixQuery(query), args...)
 		return row
 		return row
 	}
 	}
 	return db.tx.QueryRowContext(ctx, db.fixQuery(query), args...)
 	return db.tx.QueryRowContext(ctx, db.fixQuery(query), args...)
@@ -65,7 +65,7 @@ func (db *Tx) QueryRow(ctx context.Context, query string, args ...any) *sql.Row
 func (db *Tx) Rollback() error {
 func (db *Tx) Rollback() error {
 	if db.Debug {
 	if db.Debug {
 		err := db.tx.Rollback()
 		err := db.tx.Rollback()
-		log(os.Stdout, "[func Rollback]", db.t, err, true, "")
+		log(os.Stdout, "Rollback", db.t, err, true, "")
 		return err
 		return err
 	}
 	}
 	return db.tx.Rollback()
 	return db.tx.Rollback()