Browse Source

Optimization

Vova Tkach 6 years ago
parent
commit
0d0aceffb6

+ 20 - 0
engine/sqlw/sqlw.go

@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	_ "github.com/go-sql-driver/mysql"
 
+	"errors"
 	"fmt"
 	"os"
 	"regexp"
@@ -89,3 +90,22 @@ func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
 	this.logQuery(query, s)
 	return r, e
 }
+
+func (this *DB) Transaction(queries func(tx *Tx) error) error {
+	if queries == nil {
+		return errors.New("queries is not set for transaction")
+	}
+
+	tx, err := this.db.Begin()
+	if err != nil {
+		return err
+	}
+
+	err = queries(tx)
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	return tx.Commit()
+}

+ 0 - 19
engine/wrapper/wrapper.go

@@ -237,22 +237,3 @@ func (this *Wrapper) RenderBackEnd(tcont []byte, data interface{}) {
 	}
 	this.W.Write(tpl.Bytes())
 }
-
-func (this *Wrapper) DBTrans(queries func(tx *Tx) error) error {
-	if queries == nil {
-		return errors.New("queries is not set for transaction")
-	}
-
-	tx, err := this.DB.Begin()
-	if err != nil {
-		return err
-	}
-
-	err = queries(tx)
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-
-	return tx.Commit()
-}

+ 1 - 1
modules/module_blog_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_BlogDelete() *Action {
 			return
 		}
 
-		if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", pf_id); err != nil {
 				return err

+ 2 - 2
modules/module_blog_act_modify.go

@@ -44,7 +44,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 		}
 
 		if pf_id == "0" {
-			if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
 					`INSERT INTO blog_posts SET
@@ -116,7 +116,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 
 			wrap.Write(`window.location='/cp/blog/';`)
 		} else {
-			if err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Block rows
 				if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", pf_id); err != nil {
 					return err

+ 1 - 1
modules/module_blog_categories_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_BlogCategoriesDelete() *Action {
 			return
 		}
 
-		err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 				return err

+ 3 - 3
modules/module_blog_categories_act_modify.go

@@ -6,7 +6,7 @@ import (
 )
 
 func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
-	return wrap.DBTrans(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 			return err
@@ -37,7 +37,7 @@ func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 
 	if utils.StrToInt(pf_parent) == parentId {
 		// If parent not changed, just update category data
-		return wrap.DBTrans(func(tx *wrapper.Tx) error {
+		return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec(`
 				UPDATE blog_cats SET
@@ -58,7 +58,7 @@ func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 	}
 
 	// Parent is changed, move category to new parent
-	return wrap.DBTrans(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 			return err

+ 1 - 1
modules/module_index.go

@@ -412,7 +412,7 @@ func (this *Modules) RegisterAction_IndexDelete() *Action {
 			return
 		}
 
-		err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec("DELETE FROM pages WHERE id = ?;", pf_id); err != nil {
 				return err

+ 1 - 1
modules/module_users_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_UsersDelete() *Action {
 			return
 		}
 
-		err := wrap.DBTrans(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_cats WHERE user = ? FOR UPDATE;", pf_id); err != nil {
 				return err