Browse Source

Query -> QueryContext, QueryRow -> QueryRowContext in transactions

Vova Tkach 5 years ago
parent
commit
d303a8cb32

+ 6 - 6
engine/sqlw/txw.go

@@ -34,24 +34,24 @@ func (this *Tx) Exec(ctx context.Context, query string, args ...interface{}) (sq
 	return this.tx.ExecContext(ctx, query, args...)
 }
 
-func (this *Tx) Query(query string, args ...interface{}) (*sql.Rows, error) {
+func (this *Tx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
 	if consts.ParamDebug {
 		s := time.Now()
-		r, e := this.tx.Query(query, args...)
+		r, e := this.tx.QueryContext(ctx, query, args...)
 		log("[TX] "+query, s, e, true)
 		return r, e
 	}
-	return this.tx.Query(query, args...)
+	return this.tx.QueryContext(ctx, query, args...)
 }
 
-func (this *Tx) QueryRow(query string, args ...interface{}) *sql.Row {
+func (this *Tx) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
 	if consts.ParamDebug {
 		s := time.Now()
-		r := this.tx.QueryRow(query, args...)
+		r := this.tx.QueryRowContext(ctx, query, args...)
 		log("[TX] "+query, s, nil, true)
 		return r
 	}
-	return this.tx.QueryRow(query, args...)
+	return this.tx.QueryRowContext(ctx, query, args...)
 }
 
 func (this *Tx) Rollback() error {

+ 8 - 6
modules/module_blog_act_modify.go

@@ -100,13 +100,14 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 				catids := utils.GetPostArrayInt("cats[]", wrap.R)
 				if len(catids) > 0 {
 					var catsCount int
-					err = tx.QueryRow(`
-						SELECT
+					err = tx.QueryRow(
+						ctx,
+						`SELECT
 							COUNT(*)
 						FROM
 							blog_cats
 						WHERE
-							id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
+							id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
 						FOR UPDATE;`,
 					).Scan(
 						&catsCount,
@@ -178,13 +179,14 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 				catids := utils.GetPostArrayInt("cats[]", wrap.R)
 				if len(catids) > 0 {
 					var catsCount int
-					err := tx.QueryRow(`
-						SELECT
+					err := tx.QueryRow(
+						ctx,
+						`SELECT
 							COUNT(*)
 						FROM
 							blog_cats
 						WHERE
-							id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
+							id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
 						FOR UPDATE;`,
 					).Scan(
 						&catsCount,

+ 3 - 3
modules/module_blog_categories_act_modify.go

@@ -78,19 +78,19 @@ func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 
 		var parentL int
 		var parentR int
-		if err := tx.QueryRow(`SELECT lft, rgt FROM blog_cats WHERE id = ?;`, utils.StrToInt(pf_parent)).Scan(&parentL, &parentR); *wrap.LogCpError(&err) != nil {
+		if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM blog_cats WHERE id = ?;`, utils.StrToInt(pf_parent)).Scan(&parentL, &parentR); *wrap.LogCpError(&err) != nil {
 			return err
 		}
 
 		var targetL int
 		var targetR int
-		if err := tx.QueryRow(`SELECT lft, rgt FROM blog_cats WHERE id = ?;`, utils.StrToInt(pf_id)).Scan(&targetL, &targetR); *wrap.LogCpError(&err) != nil {
+		if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM blog_cats WHERE id = ?;`, utils.StrToInt(pf_id)).Scan(&targetL, &targetR); *wrap.LogCpError(&err) != nil {
 			return err
 		}
 
 		if !(targetL < parentL && targetR > parentR) {
 			// Select data
-			rows, err := tx.Query("SELECT id, lft, rgt FROM blog_cats WHERE lft >= ? and rgt <= ? ORDER BY lft ASC", targetL, targetR)
+			rows, err := tx.Query(ctx, "SELECT id, lft, rgt FROM blog_cats WHERE lft >= ? and rgt <= ? ORDER BY lft ASC", targetL, targetR)
 			if err != nil {
 				return err
 			}

+ 2 - 0
modules/module_shop_act_attach_product_to.go

@@ -26,6 +26,7 @@ func (this *Modules) RegisterAction_ShopAttachProductTo() *Action {
 			// Check parent
 			var count int
 			if err := tx.QueryRow(
+				ctx,
 				"SELECT COUNT(*) FROM `shop_products` WHERE `id` = ? AND `parent_id` IS NULL;",
 				utils.StrToInt(pf_parent_id),
 			).Scan(&count); err != nil {
@@ -37,6 +38,7 @@ func (this *Modules) RegisterAction_ShopAttachProductTo() *Action {
 
 			// Check child
 			if err := tx.QueryRow(
+				ctx,
 				"SELECT COUNT(*) FROM `shop_products` WHERE `parent_id` = ?;",
 				utils.StrToInt(pf_product_id),
 			).Scan(&count); err != nil {

+ 2 - 0
modules/module_shop_act_duplicate.go

@@ -98,6 +98,7 @@ func (this *Modules) RegisterAction_ShopDuplicate() *Action {
 			// Duplicate categories
 			cat_sqls := []string{}
 			if cat_rows, err := tx.Query(
+				ctx,
 				`SELECT
 					product_id,
 					category_id
@@ -129,6 +130,7 @@ func (this *Modules) RegisterAction_ShopDuplicate() *Action {
 			// Duplicate attributes
 			attributes_sqls := []string{}
 			if attributes_rows, err := tx.Query(
+				ctx,
 				`SELECT
 					product_id,
 					filter_value_id

+ 8 - 6
modules/module_shop_act_modify.go

@@ -164,13 +164,14 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 				catids := utils.GetPostArrayInt("cats[]", wrap.R)
 				if len(catids) > 0 {
 					var catsCount int
-					err = tx.QueryRow(`
-						SELECT
+					err = tx.QueryRow(
+						ctx,
+						`SELECT
 							COUNT(*)
 						FROM
 							shop_cats
 						WHERE
-							id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
+							id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
 						FOR UPDATE;`,
 					).Scan(
 						&catsCount,
@@ -313,13 +314,14 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 				catids := utils.GetPostArrayInt("cats[]", wrap.R)
 				if len(catids) > 0 {
 					var catsCount int
-					err := tx.QueryRow(`
-						SELECT
+					err := tx.QueryRow(
+						ctx,
+						`SELECT
 							COUNT(*)
 						FROM
 							shop_cats
 						WHERE
-							id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
+							id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
 						FOR UPDATE;`,
 					).Scan(
 						&catsCount,

+ 3 - 3
modules/module_shop_categories_act_modify.go

@@ -78,19 +78,19 @@ func (this *Modules) shop_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 
 		var parentL int
 		var parentR int
-		if err := tx.QueryRow(`SELECT lft, rgt FROM shop_cats WHERE id = ?;`, utils.StrToInt(pf_parent)).Scan(&parentL, &parentR); *wrap.LogCpError(&err) != nil {
+		if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM shop_cats WHERE id = ?;`, utils.StrToInt(pf_parent)).Scan(&parentL, &parentR); *wrap.LogCpError(&err) != nil {
 			return err
 		}
 
 		var targetL int
 		var targetR int
-		if err := tx.QueryRow(`SELECT lft, rgt FROM shop_cats WHERE id = ?;`, utils.StrToInt(pf_id)).Scan(&targetL, &targetR); *wrap.LogCpError(&err) != nil {
+		if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM shop_cats WHERE id = ?;`, utils.StrToInt(pf_id)).Scan(&targetL, &targetR); *wrap.LogCpError(&err) != nil {
 			return err
 		}
 
 		if !(targetL < parentL && targetR > parentR) {
 			// Select data
-			rows, err := tx.Query("SELECT id, lft, rgt FROM shop_cats WHERE lft >= ? and rgt <= ? ORDER BY lft ASC", targetL, targetR)
+			rows, err := tx.Query(ctx, "SELECT id, lft, rgt FROM shop_cats WHERE lft >= ? and rgt <= ? ORDER BY lft ASC", targetL, targetR)
 			if err != nil {
 				return err
 			}