Browse Source

db.QueryRow -> db.QueryRowContext

Vova Tkach 5 years ago
parent
commit
5c3b5f3a74

+ 3 - 1
engine/basket/session.go

@@ -248,7 +248,9 @@ func (this *session) Plus(p *SBParam, product_id int) {
 	roc := &utils.MySql_shop_currency{}
 	var img_product_id string
 	var img_filename string
-	if err := p.DB.QueryRow(`
+	if err := p.DB.QueryRow(
+		p.R.Context(),
+		`
 		SELECT
 			shop_products.id,
 			shop_products.name,

+ 1 - 1
engine/builder/data_table.go

@@ -40,7 +40,7 @@ func DataTable(
 			num, err = custom_sql_count()
 			wrap.LogCpError(&err)
 		} else {
-			err = wrap.DB.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
+			err = wrap.DB.QueryRow(wrap.R.Context(), "SELECT COUNT(*) FROM `"+table+"`;").Scan(&num)
 			if *wrap.LogCpError(&err) != nil {
 				return ""
 			}

+ 1 - 1
engine/engine.go

@@ -69,7 +69,7 @@ func (this *Engine) Process() bool {
 	}
 
 	// Backend must use MySQL anyway, so, check and connect
-	err := this.Wrap.UseDatabase(this.Wrap.R.Context())
+	err := this.Wrap.UseDatabase()
 	if err != nil {
 		utils.SystemErrorPageEngine(this.Wrap.W, err)
 		return true

+ 1 - 1
engine/fetdata/blog.go

@@ -282,7 +282,7 @@ func (this *Blog) load() *Blog {
 		`
 	}
 
-	if err := this.wrap.DB.QueryRow(sql_nums).Scan(&this.postsCount); *this.wrap.LogCpError(&err) == nil {
+	if err := this.wrap.DB.QueryRow(this.wrap.R.Context(), sql_nums).Scan(&this.postsCount); *this.wrap.LogCpError(&err) == nil {
 		if this.category == nil {
 			this.postsPerPage = (*this.wrap.Config).Blog.Pagination.Index
 		} else {

+ 3 - 2
engine/fetdata/blog_category.go

@@ -114,8 +114,9 @@ func (this *BlogCategory) loadById(id int) {
 		return
 	}
 	this.object = &utils.MySql_blog_category{}
-	if err := this.wrap.DB.QueryRow(`
-		SELECT
+	if err := this.wrap.DB.QueryRow(
+		this.wrap.R.Context(),
+		`SELECT
 			main.id,
 			main.user,
 			main.name,

+ 1 - 1
engine/fetdata/shop.go

@@ -314,7 +314,7 @@ func (this *Shop) load() *Shop {
 
 	product_ids := []string{}
 
-	if err := this.wrap.DB.QueryRow(sql_nums).Scan(&this.productsCount); *this.wrap.LogCpError(&err) == nil {
+	if err := this.wrap.DB.QueryRow(this.wrap.R.Context(), sql_nums).Scan(&this.productsCount); *this.wrap.LogCpError(&err) == nil {
 		if this.category == nil {
 			this.productsPerPage = (*this.wrap.Config).Shop.Pagination.Index
 		} else {

+ 3 - 2
engine/fetdata/shop_category.go

@@ -114,8 +114,9 @@ func (this *ShopCategory) loadById(id int) {
 		return
 	}
 	this.object = &utils.MySql_shop_category{}
-	if err := this.wrap.DB.QueryRow(`
-		SELECT
+	if err := this.wrap.DB.QueryRow(
+		this.wrap.R.Context(),
+		`SELECT
 			main.id,
 			main.user,
 			main.name,

+ 3 - 2
engine/fetdata/shop_currency.go

@@ -22,8 +22,9 @@ func (this *ShopCurrency) loadById(id int) {
 		return
 	}
 	this.object = &utils.MySql_shop_currency{}
-	if err := this.wrap.DB.QueryRow(`
-		SELECT
+	if err := this.wrap.DB.QueryRow(
+		this.wrap.R.Context(),
+		`SELECT
 			id,
 			name,
 			coefficient,

+ 3 - 2
engine/fetdata/user.go

@@ -22,8 +22,9 @@ func (this *User) loadById(id int) {
 		return
 	}
 	this.object = &utils.MySql_user{}
-	if err := this.wrap.DB.QueryRow(`
-		SELECT
+	if err := this.wrap.DB.QueryRow(
+		this.wrap.R.Context(),
+		`SELECT
 			id,
 			first_name,
 			last_name,

+ 2 - 2
engine/sqlw/sqlw.go

@@ -66,10 +66,10 @@ func (this *DB) SetMaxOpenConns(n int) {
 	this.db.SetMaxOpenConns(n)
 }
 
-func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
+func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
 	if consts.ParamDebug {
 		s := time.Now()
-		r := this.db.QueryRow(query, args...)
+		r := this.db.QueryRowContext(ctx, query, args...)
 		log(query, s, nil, false)
 		return r
 	}

+ 6 - 6
engine/wrapper/wrapper.go

@@ -2,7 +2,6 @@ package wrapper
 
 import (
 	"bytes"
-	"context"
 	"errors"
 	"fmt"
 	"html/template"
@@ -121,7 +120,7 @@ func (this *Wrapper) dbReconnect() error {
 	return nil
 }
 
-func (this *Wrapper) UseDatabase(ctx context.Context) error {
+func (this *Wrapper) UseDatabase() error {
 	this.DB = this.MSPool.Get(this.CurrHost)
 	if this.DB == nil {
 		if err := this.dbReconnect(); err != nil {
@@ -129,12 +128,12 @@ func (this *Wrapper) UseDatabase(ctx context.Context) error {
 		}
 	}
 
-	if err := this.DB.Ping(ctx); err != nil {
+	if err := this.DB.Ping(this.R.Context()); err != nil {
 		this.DB.Close()
 		if err := this.dbReconnect(); err != nil {
 			return err
 		}
-		if err := this.DB.Ping(ctx); err != nil {
+		if err := this.DB.Ping(this.R.Context()); err != nil {
 			this.DB.Close()
 			return err
 		}
@@ -156,8 +155,9 @@ func (this *Wrapper) LoadSessionUser() bool {
 		return false
 	}
 	user := &utils.MySql_user{}
-	err := this.DB.QueryRow(`
-		SELECT
+	err := this.DB.QueryRow(
+		this.R.Context(),
+		`SELECT
 			id,
 			first_name,
 			last_name,

+ 12 - 8
modules/module_blog.go

@@ -36,8 +36,9 @@ func (this *Modules) RegisterModule_Blog() *Module {
 			// Blog category
 			row := &utils.MySql_blog_category{}
 			rou := &utils.MySql_user{}
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					main.id,
 					main.user,
 					main.name,
@@ -143,8 +144,9 @@ func (this *Modules) RegisterModule_Blog() *Module {
 			// Blog post
 			row := &utils.MySql_blog_post{}
 			rou := &utils.MySql_user{}
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					blog_posts.id,
 					blog_posts.user,
 					blog_posts.name,
@@ -414,8 +416,9 @@ func (this *Modules) RegisterModule_Blog() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						user,
 						name,
@@ -604,8 +607,9 @@ func (this *Modules) RegisterModule_Blog() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						user,
 						name,

+ 3 - 2
modules/module_blog_categories.go

@@ -66,8 +66,9 @@ func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int
 
 func (this *Modules) blog_GetCategoryParentId(wrap *wrapper.Wrapper, id int) int {
 	var parentId int
-	err := wrap.DB.QueryRow(`
-		SELECT
+	err := wrap.DB.QueryRow(
+		wrap.R.Context(),
+		`SELECT
 			parent.id
 		FROM
 			blog_cats AS node,

+ 3 - 2
modules/module_blog_categories_act_modify.go

@@ -199,8 +199,9 @@ func (this *Modules) RegisterAction_BlogCategoriesModify() *Action {
 		} else {
 			// Check if parent category exists
 			var parentId int
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					id
 				FROM
 					blog_cats

+ 6 - 4
modules/module_index.go

@@ -28,8 +28,9 @@ func (this *Modules) RegisterModule_Index() *Module {
 		// Front-end
 		row := &utils.MySql_page{}
 		rou := &utils.MySql_user{}
-		err := wrap.DB.QueryRow(`
-			SELECT
+		err := wrap.DB.QueryRow(
+			wrap.R.Context(),
+			`SELECT
 				pages.id,
 				pages.user,
 				pages.name,
@@ -197,8 +198,9 @@ func (this *Modules) RegisterModule_Index() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						user,
 						name,

+ 3 - 2
modules/module_index_act_first_user.go

@@ -32,8 +32,9 @@ func (this *Modules) RegisterAction_IndexFirstUser() *Action {
 
 		// Security, check if still need to run this action
 		var count int
-		err := wrap.DB.QueryRow(`
-			SELECT
+		err := wrap.DB.QueryRow(
+			wrap.R.Context(),
+			`SELECT
 				COUNT(*)
 			FROM
 				users

+ 1 - 0
modules/module_index_act_signin.go

@@ -36,6 +36,7 @@ func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
 
 		var user_id int
 		err := wrap.DB.QueryRow(
+			wrap.R.Context(),
 			`SELECT
 				id
 			FROM

+ 2 - 1
modules/module_notify_mail.go

@@ -105,7 +105,8 @@ func (this *Modules) RegisterModule_NotifyMail() *Module {
 				func() (int, error) {
 					var count int
 					return count, wrap.DB.QueryRow(
-						"SELECT COUNT(*) FROM `notify_mail`" + ModuleSqlWhere + ";",
+						wrap.R.Context(),
+						"SELECT COUNT(*) FROM `notify_mail`"+ModuleSqlWhere+";",
 					).Scan(&count)
 				},
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {

+ 22 - 14
modules/module_shop.go

@@ -361,8 +361,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			// Shop category
 			row := &utils.MySql_shop_category{}
 			rou := &utils.MySql_user{}
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					main.id,
 					main.user,
 					main.name,
@@ -520,8 +521,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			// Shop product
 			row := &utils.MySql_shop_product{}
 			rou := &utils.MySql_user{}
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					shop_products.id,
 					shop_products.parent_id,
 					shop_products.user,
@@ -739,6 +741,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				func() (int, error) {
 					var count int
 					return count, wrap.DB.QueryRow(
+						wrap.R.Context(),
 						"SELECT COUNT(*) FROM `shop_products`;",
 					).Scan(&count)
 				},
@@ -1159,8 +1162,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						parent_id,
 						user,
@@ -1631,8 +1635,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						user,
 						name,
@@ -1757,8 +1762,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						name,
 						filter
@@ -1875,8 +1881,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						name,
 						coefficient,
@@ -2004,8 +2011,9 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			var curr_order_status int
 			var curr_order_total float64
 
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					shop_orders.id,
 					shop_orders.client_phone,
 					shop_orders.update_datetime,

+ 3 - 2
modules/module_shop_categories.go

@@ -66,8 +66,9 @@ func (this *Modules) shop_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int
 
 func (this *Modules) shop_GetCategoryParentId(wrap *wrapper.Wrapper, id int) int {
 	var parentId int
-	err := wrap.DB.QueryRow(`
-		SELECT
+	err := wrap.DB.QueryRow(
+		wrap.R.Context(),
+		`SELECT
 			parent.id
 		FROM
 			shop_cats AS node,

+ 3 - 2
modules/module_shop_categories_act_modify.go

@@ -199,8 +199,9 @@ func (this *Modules) RegisterAction_ShopCategoriesModify() *Action {
 		} else {
 			// Check if parent category exists
 			var parentId int
-			err := wrap.DB.QueryRow(`
-				SELECT
+			err := wrap.DB.QueryRow(
+				wrap.R.Context(),
+				`SELECT
 					id
 				FROM
 					shop_cats

+ 3 - 2
modules/module_users.go

@@ -125,8 +125,9 @@ func (this *Modules) RegisterModule_Users() *Module {
 				if !utils.IsNumeric(wrap.UrlArgs[2]) {
 					return "", "", ""
 				}
-				err := wrap.DB.QueryRow(`
-					SELECT
+				err := wrap.DB.QueryRow(
+					wrap.R.Context(),
+					`SELECT
 						id,
 						first_name,
 						last_name,

+ 2 - 2
modules/modules.go

@@ -297,7 +297,7 @@ func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
 			if name != "" {
 				if act, ok := this.acts[name]; ok {
 					if act.Info.WantDB {
-						err := wrap.UseDatabase(wrap.R.Context())
+						err := wrap.UseDatabase()
 						if err != nil {
 							this.XXXActionHeaders(wrap, http.StatusNotFound)
 							wrap.MsgError(err.Error())
@@ -345,7 +345,7 @@ func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
 		wrap.CurrModule = cm
 		if mod.Front != nil {
 			if mod.Info.WantDB {
-				err := wrap.UseDatabase(wrap.R.Context())
+				err := wrap.UseDatabase()
 				if err != nil {
 					utils.SystemErrorPageEngine(wrap.W, err)
 					return true

+ 1 - 1
support/support.go

@@ -63,7 +63,7 @@ func (this *Support) Migrate(ctx context.Context, host string) error {
 		}
 		defer db.Close()
 		var version string
-		if err := db.QueryRow(`SELECT value FROM settings WHERE name = 'database_version' LIMIT 1;`).Scan(&version); err != nil {
+		if err := db.QueryRow(ctx, `SELECT value FROM settings WHERE name = 'database_version' LIMIT 1;`).Scan(&version); err != nil {
 			if this.isSettingsTableDoesntExist(err) {
 				if _, err := db.Exec(
 					`CREATE TABLE settings (