Browse Source

MySQL wrapper, optimization

Vova Tkach 6 years ago
parent
commit
a1ecdc2b20

+ 3 - 5
engine/builder/data_table.go

@@ -1,14 +1,12 @@
 package builder
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
 	"fmt"
 	"html"
 	"math"
 	"strconv"
 
+	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper"
 )
 
@@ -29,7 +27,7 @@ func DataTable(
 	action func(values *[]string) string,
 	pagination_url string,
 	custom_sql_count func() (int, error),
-	custom_sql_data func(limit_offset int, pear_page int) (*sql.Rows, error),
+	custom_sql_data func(limit_offset int, pear_page int) (*sqlw.Rows, error),
 	pagination_enabled bool,
 ) string {
 	var num int
@@ -104,7 +102,7 @@ func DataTable(
 	result += `<tbody>`
 	if num > 0 || !pagination_enabled {
 		have_records := false
-		var rows *sql.Rows
+		var rows *sqlw.Rows
 		var err error
 		if custom_sql_data == nil {
 			rows, err = wrap.DB.Query(qsql, limit_offset, pear_page)

+ 6 - 5
engine/mysqlpool/mysqlpool.go

@@ -1,22 +1,23 @@
 package mysqlpool
 
 import (
-	"database/sql"
 	"sync"
+
+	"golang-fave/engine/sqlw"
 )
 
 type MySqlPool struct {
 	sync.RWMutex
-	connections map[string]*sql.DB
+	connections map[string]*sqlw.DB
 }
 
 func New() *MySqlPool {
 	r := MySqlPool{}
-	r.connections = map[string]*sql.DB{}
+	r.connections = map[string]*sqlw.DB{}
 	return &r
 }
 
-func (this *MySqlPool) Get(key string) *sql.DB {
+func (this *MySqlPool) Get(key string) *sqlw.DB {
 	this.Lock()
 	defer this.Unlock()
 	if value, ok := this.connections[key]; ok == true {
@@ -25,7 +26,7 @@ func (this *MySqlPool) Get(key string) *sql.DB {
 	return nil
 }
 
-func (this *MySqlPool) Set(key string, value *sql.DB) {
+func (this *MySqlPool) Set(key string, value *sqlw.DB) {
 	this.Lock()
 	defer this.Unlock()
 	this.connections[key] = value

+ 61 - 0
engine/sqlw/sqlw.go

@@ -0,0 +1,61 @@
+package sqlw
+
+import (
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+
+	"time"
+)
+
+type Tx = sql.Tx
+type Rows = sql.Rows
+
+type DB struct {
+	db *sql.DB
+}
+
+var ErrNoRows = sql.ErrNoRows
+
+func Open(driverName, dataSourceName string) (*DB, error) {
+	db, err := sql.Open(driverName, dataSourceName)
+	if err != nil {
+		return nil, err
+	}
+	return &DB{db: db}, nil
+}
+
+func (this *DB) Close() error {
+	return this.db.Close()
+}
+
+func (this *DB) Ping() error {
+	return this.db.Ping()
+}
+
+func (this *DB) SetConnMaxLifetime(d time.Duration) {
+	this.db.SetConnMaxLifetime(d)
+}
+
+func (this *DB) SetMaxIdleConns(n int) {
+	this.db.SetMaxIdleConns(n)
+}
+
+func (this *DB) SetMaxOpenConns(n int) {
+	this.db.SetMaxOpenConns(n)
+}
+
+func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
+	return this.db.QueryRow(query, args...)
+}
+
+func (this *DB) Begin() (*sql.Tx, error) {
+	return this.db.Begin()
+}
+
+func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
+	return this.db.Query(query, args...)
+}
+
+func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
+	return this.db.Exec(query, args...)
+}

+ 5 - 7
engine/wrapper/wrapper.go

@@ -1,9 +1,6 @@
 package wrapper
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
 	"bytes"
 	"errors"
 	"fmt"
@@ -14,15 +11,16 @@ import (
 
 	"golang-fave/consts"
 	"golang-fave/engine/mysqlpool"
+	"golang-fave/engine/sqlw"
 	"golang-fave/logger"
 	"golang-fave/utils"
 
 	"github.com/vladimirok5959/golang-server-sessions/session"
 )
 
-type Tx = sql.Tx
+type Tx = sqlw.Tx
 
-var ErrNoRows = sql.ErrNoRows
+var ErrNoRows = sqlw.ErrNoRows
 
 type Wrapper struct {
 	l *logger.Logger
@@ -47,7 +45,7 @@ type Wrapper struct {
 	CurrSubModule   string
 	MSPool          *mysqlpool.MySqlPool
 
-	DB   *sql.DB
+	DB   *sqlw.DB
 	User *utils.MySql_user
 }
 
@@ -88,7 +86,7 @@ func (this *Wrapper) dbReconnect() error {
 	if err != nil {
 		return err
 	}
-	this.DB, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
+	this.DB, err = sqlw.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
 	if err != nil {
 		return err
 	}

+ 2 - 4
modules/module_blog.go

@@ -1,15 +1,13 @@
 package modules
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
 	"html"
 	"strings"
 
 	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/engine/builder"
+	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -161,7 +159,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 				},
 				"/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
 				nil,
-				func(limit_offset int, pear_page int) (*sql.Rows, error) {
+				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
 						`SELECT
 							node.id,

+ 2 - 4
modules/module_index_act_cypress.go

@@ -1,12 +1,10 @@
 package modules
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
 	"os"
 
 	"golang-fave/consts"
+	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper"
 )
 
@@ -20,7 +18,7 @@ func (this *Modules) RegisterAction_IndexCypressReset() *Action {
 			return
 		}
 
-		db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/fave")
+		db, err := sqlw.Open("mysql", "root:root@tcp(localhost:3306)/fave")
 		if err != nil {
 			wrap.Write(err.Error())
 			return

+ 2 - 4
modules/module_index_act_mysql_setup.go

@@ -1,12 +1,10 @@
 package modules
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
 	"os"
 	"strconv"
 
+	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -54,7 +52,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 		}
 
 		// Try connect to mysql
-		db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
+		db, err := sqlw.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
 		if err != nil {
 			wrap.MsgError(err.Error())
 			return

+ 3 - 5
modules/module_index_act_signin.go

@@ -1,9 +1,7 @@
 package modules
 
 import (
-	"database/sql"
-	_ "github.com/go-sql-driver/mysql"
-
+	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -54,12 +52,12 @@ func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
 			&user_id,
 		)
 
-		if err != nil && err != sql.ErrNoRows {
+		if err != nil && err != sqlw.ErrNoRows {
 			wrap.MsgError(err.Error())
 			return
 		}
 
-		if err == sql.ErrNoRows {
+		if err == sqlw.ErrNoRows {
 			wrap.MsgError(`Incorrect email or password`)
 			return
 		}