Browse Source

db.Query -> db.QueryContext, more contexts

Vova Tkach 5 years ago
parent
commit
15b6f5675d

+ 1 - 1
Makefile

@@ -125,4 +125,4 @@ ab:
 	ab -kc 10 -t 60 http://localhost:8080/blog/the-best-juices-for-a-child/
 	ab -kc 10 -t 60 http://localhost:8080/shop/
 	ab -kc 10 -t 60 http://localhost:8080/shop/category/electronics/
-	ab -kc 10 -t 60 http://localhost:8080/shop/samsung-galaxy-s10/
+	ab -kc 10 -t 60 http://localhost:8080/shop/samsung-galaxy-s10-128-gb/

+ 11 - 8
engine/basket/session.go

@@ -1,6 +1,7 @@
 package basket
 
 import (
+	"context"
 	"encoding/json"
 	"html"
 	"strings"
@@ -37,13 +38,14 @@ func (this *session) makePrice(product_price float64, product_currency_id int) f
 	}
 }
 
-func (this *session) updateProducts(db *sqlw.DB) {
+func (this *session) updateProducts(ctx context.Context, db *sqlw.DB) {
 	products_ids := []int{}
 	for _, product := range this.Products {
 		products_ids = append(products_ids, product.Id)
 	}
 	if len(products_ids) > 0 {
 		if rows, err := db.Query(
+			ctx,
 			`SELECT
 				shop_products.id,
 				shop_products.name,
@@ -98,7 +100,7 @@ func (this *session) updateProducts(db *sqlw.DB) {
 				) as image_parent ON image_parent.product_id = shop_products.parent_id
 			WHERE
 				shop_products.active = 1 AND
-				shop_products.id IN (` + strings.Join(utils.ArrayOfIntToArrayOfString(products_ids), ",") + `)
+				shop_products.id IN (`+strings.Join(utils.ArrayOfIntToArrayOfString(products_ids), ",")+`)
 			;`,
 		); err == nil {
 			defer rows.Close()
@@ -175,6 +177,7 @@ func (this *session) Preload(p *SBParam) {
 
 	// Load currencies from database
 	if rows, err := p.DB.Query(
+		p.R.Context(),
 		`SELECT
 			id,
 			name,
@@ -226,7 +229,7 @@ func (this *session) Preload(p *SBParam) {
 }
 
 func (this *session) String(p *SBParam) string {
-	this.updateProducts(p.DB)
+	this.updateProducts(p.R.Context(), p.DB)
 	this.updateTotals(p)
 
 	json, err := json.Marshal(this)
@@ -240,7 +243,7 @@ func (this *session) String(p *SBParam) string {
 func (this *session) Plus(p *SBParam, product_id int) {
 	if prod, ok := this.Products[product_id]; ok == true {
 		prod.Quantity++
-		this.updateProducts(p.DB)
+		this.updateProducts(p.R.Context(), p.DB)
 		this.updateTotals(p)
 		return
 	}
@@ -337,7 +340,7 @@ func (this *session) Plus(p *SBParam, product_id int) {
 			price:    row.A_price,
 			Quantity: 1,
 		}
-		this.updateProducts(p.DB)
+		this.updateProducts(p.R.Context(), p.DB)
 		this.updateTotals(p)
 	}
 }
@@ -349,7 +352,7 @@ func (this *session) Minus(p *SBParam, product_id int) {
 		} else {
 			delete(this.Products, product_id)
 		}
-		this.updateProducts(p.DB)
+		this.updateProducts(p.R.Context(), p.DB)
 		this.updateTotals(p)
 	}
 }
@@ -357,14 +360,14 @@ func (this *session) Minus(p *SBParam, product_id int) {
 func (this *session) Remove(p *SBParam, product_id int) {
 	if _, ok := this.Products[product_id]; ok == true {
 		delete(this.Products, product_id)
-		this.updateProducts(p.DB)
+		this.updateProducts(p.R.Context(), p.DB)
 		this.updateTotals(p)
 	}
 }
 
 func (this *session) ClearBasket(p *SBParam) {
 	this.Products = map[int]*product{}
-	this.updateProducts(p.DB)
+	this.updateProducts(p.R.Context(), p.DB)
 	this.updateTotals(p)
 }
 

+ 1 - 1
engine/builder/data_table.go

@@ -108,7 +108,7 @@ func DataTable(
 		var rows *sqlw.Rows
 		var err error
 		if custom_sql_data == nil {
-			rows, err = wrap.DB.Query(qsql, limit_offset, pear_page)
+			rows, err = wrap.DB.Query(wrap.R.Context(), qsql, limit_offset, pear_page)
 		} else {
 			rows, err = custom_sql_data(limit_offset, pear_page)
 		}

+ 5 - 3
engine/fetdata/blog.go

@@ -142,6 +142,7 @@ func (this *Blog) load() *Blog {
 	if this.category != nil {
 		var cat_ids []string
 		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
 			`SELECT
 				node.id
 			FROM
@@ -291,7 +292,7 @@ func (this *Blog) load() *Blog {
 		this.postsMaxPage = int(math.Ceil(float64(this.postsCount) / float64(this.postsPerPage)))
 		this.postsCurrPage = this.wrap.GetCurrentPage(this.postsMaxPage)
 		offset := this.postsCurrPage*this.postsPerPage - this.postsPerPage
-		if rows, err := this.wrap.DB.Query(sql_rows, offset, this.postsPerPage); err == nil {
+		if rows, err := this.wrap.DB.Query(this.wrap.R.Context(), sql_rows, offset, this.postsPerPage); err == nil {
 			defer rows.Close()
 			for rows.Next() {
 				rp := utils.MySql_blog_post{}
@@ -428,8 +429,9 @@ func (this *Blog) load() *Blog {
 func (this *Blog) preload_cats() {
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_blog_category{}
-		if rows, err := this.wrap.DB.Query(`
-			SELECT
+		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
+			`SELECT
 				main.id,
 				main.user,
 				main.name,

+ 3 - 2
engine/fetdata/blog_category.go

@@ -25,8 +25,9 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_blog_category{}
 	}
-	if rows, err := this.wrap.DB.Query(`
-		SELECT
+	if rows, err := this.wrap.DB.Query(
+		this.wrap.R.Context(),
+		`SELECT
 			main.id,
 			main.user,
 			main.name,

+ 7 - 4
engine/fetdata/shop.go

@@ -157,6 +157,7 @@ func (this *Shop) load() *Shop {
 	if this.category != nil {
 		var cat_ids []string
 		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
 			`SELECT
 				node.id
 			FROM
@@ -323,7 +324,7 @@ func (this *Shop) load() *Shop {
 		this.productsMaxPage = int(math.Ceil(float64(this.productsCount) / float64(this.productsPerPage)))
 		this.productsCurrPage = this.wrap.GetCurrentPage(this.productsMaxPage)
 		offset := this.productsCurrPage*this.productsPerPage - this.productsPerPage
-		if rows, err := this.wrap.DB.Query(sql_rows, offset, this.productsPerPage); err == nil {
+		if rows, err := this.wrap.DB.Query(this.wrap.R.Context(), sql_rows, offset, this.productsPerPage); err == nil {
 			defer rows.Close()
 			for rows.Next() {
 				rp := utils.MySql_shop_product{}
@@ -383,13 +384,14 @@ func (this *Shop) load() *Shop {
 	product_images := map[int][]*ShopProductImage{}
 	if len(product_ids) > 0 {
 		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
 			`SELECT
 				shop_product_images.product_id,
 				shop_product_images.filename
 			FROM
 				shop_product_images
 			WHERE
-				shop_product_images.product_id IN (` + strings.Join(product_ids, ", ") + `)
+				shop_product_images.product_id IN (`+strings.Join(product_ids, ", ")+`)
 			ORDER BY
 				shop_product_images.ord ASC
 			;`,
@@ -507,8 +509,9 @@ func (this *Shop) load() *Shop {
 func (this *Shop) preload_cats() {
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_shop_category{}
-		if rows, err := this.wrap.DB.Query(`
-			SELECT
+		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
+			`SELECT
 				main.id,
 				main.user,
 				main.name,

+ 3 - 2
engine/fetdata/shop_category.go

@@ -25,8 +25,9 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_shop_category{}
 	}
-	if rows, err := this.wrap.DB.Query(`
-		SELECT
+	if rows, err := this.wrap.DB.Query(
+		this.wrap.R.Context(),
+		`SELECT
 			main.id,
 			main.user,
 			main.name,

+ 4 - 0
engine/fetdata/shop_product.go

@@ -33,6 +33,7 @@ func (this *ShopProduct) load() *ShopProduct {
 		return this
 	}
 	if rows, err := this.wrap.DB.Query(
+		this.wrap.R.Context(),
 		`SELECT
 			shop_product_images.product_id,
 			shop_product_images.filename
@@ -60,6 +61,7 @@ func (this *ShopProduct) load() *ShopProduct {
 	// Get images from parent
 	if len(this.images) <= 0 && this.object.A_parent_id() > 0 {
 		if rows, err := this.wrap.DB.Query(
+			this.wrap.R.Context(),
 			`SELECT
 				shop_product_images.product_id,
 				shop_product_images.filename
@@ -89,6 +91,7 @@ func (this *ShopProduct) load() *ShopProduct {
 	filter_names := map[int]string{}
 	filter_values := map[int][]string{}
 	if rows, err := this.wrap.DB.Query(
+		this.wrap.R.Context(),
 		`SELECT
 			shop_filters.id,
 			shop_filters.filter,
@@ -133,6 +136,7 @@ func (this *ShopProduct) load() *ShopProduct {
 
 	// Variations
 	if rows, err := this.wrap.DB.Query(
+		this.wrap.R.Context(),
 		`SELECT
 			shop_products.id,
 			shop_products.name,

+ 2 - 4
engine/sqlw/sqlw.go

@@ -92,12 +92,10 @@ func (this *DB) Begin() (*Tx, error) {
 	return &Tx{tx, time.Now()}, err
 }
 
-// TODO: func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
-// https://golang.org/pkg/database/sql/
-func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
+func (this *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
 	if consts.ParamDebug {
 		s := time.Now()
-		r, e := this.db.Query(query, args...)
+		r, e := this.db.QueryContext(ctx, query, args...)
 		log(query, s, e, false)
 		return r, e
 	}

+ 1 - 0
engine/wrapper/wrapper.go

@@ -429,6 +429,7 @@ func (this *Wrapper) ShopGetAllCurrencies() *map[int]utils.MySql_shop_currency {
 	if this.ShopAllCurrencies == nil {
 		this.ShopAllCurrencies = &map[int]utils.MySql_shop_currency{}
 		if rows, err := this.DB.Query(
+			this.R.Context(),
 			`SELECT
 				id,
 				name,

+ 2 - 0
logger/handler.go

@@ -42,6 +42,8 @@ func (this handler) log(w *writer, r *http.Request) {
 	}, " "))
 
 	select {
+	case <-r.Context().Done():
+		return
 	case this.c <- logMsg{r.Host, msg, w.status >= 400}:
 		return
 	case <-time.After(1 * time.Second):

+ 2 - 0
logger/logger.go

@@ -147,6 +147,8 @@ func (this *Logger) Log(msg string, r *http.Request, isError bool, vars ...inter
 	}
 
 	select {
+	case <-r.Context().Done():
+		return
 	case this.cdata <- logMsg{host, msg, isError}:
 		return
 	case <-time.After(1 * time.Second):

+ 2 - 1
modules/module_blog.go

@@ -366,6 +366,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 				nil,
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
+						wrap.R.Context(),
 						`SELECT
 							node.id,
 							node.user,
@@ -451,7 +452,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 			// All post current categories
 			var selids []int
 			if data.A_id > 0 {
-				rows, err := wrap.DB.Query("SELECT category_id FROM blog_cat_post_rel WHERE post_id = ?;", data.A_id)
+				rows, err := wrap.DB.Query(wrap.R.Context(), "SELECT category_id FROM blog_cat_post_rel WHERE post_id = ?;", data.A_id)
 				if err == nil {
 					defer rows.Close()
 					values := make([]int, 1)

+ 1 - 0
modules/module_blog_categories.go

@@ -11,6 +11,7 @@ import (
 func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int, selids []int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			node.id,
 			node.user,

+ 1 - 0
modules/module_notify_mail.go

@@ -111,6 +111,7 @@ func (this *Modules) RegisterModule_NotifyMail() *Module {
 				},
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
+						wrap.R.Context(),
 						`SELECT
 							notify_mail.id,
 							notify_mail.email,

+ 15 - 3
modules/module_shop.go

@@ -19,6 +19,7 @@ import (
 func (this *Modules) shop_GetCurrencySelectOptions(wrap *wrapper.Wrapper, id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			code
@@ -53,6 +54,7 @@ func (this *Modules) shop_GetCurrencySelectOptions(wrap *wrapper.Wrapper, id int
 func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			shop_filters.id,
 			shop_filters.name,
@@ -64,7 +66,7 @@ func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_
 			LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
 			LEFT JOIN shop_filter_product_values ON
 				shop_filter_product_values.filter_value_id = shop_filters_values.id AND
-				shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
+				shop_filter_product_values.product_id = `+utils.IntToStr(product_id)+`
 			LEFT JOIN (
 				SELECT
 					shop_filters_values.filter_id,
@@ -73,7 +75,7 @@ func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_
 					shop_filter_product_values
 					LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id 
 				WHERE
-					shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
+					shop_filter_product_values.product_id = `+utils.IntToStr(product_id)+`
 				GROUP BY
 					shop_filters_values.filter_id
 			) as filter_used ON filter_used.filter_id = shop_filters.id
@@ -129,6 +131,7 @@ func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_
 func (this *Modules) shop_GetFilterValuesInputs(wrap *wrapper.Wrapper, filter_id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			name
@@ -161,6 +164,7 @@ func (this *Modules) shop_GetFilterValuesInputs(wrap *wrapper.Wrapper, filter_id
 func (this *Modules) shop_GetAllAttributesSelectOptions(wrap *wrapper.Wrapper) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			name,
@@ -192,6 +196,7 @@ func (this *Modules) shop_GetAllAttributesSelectOptions(wrap *wrapper.Wrapper) s
 func (this *Modules) shop_GetAllCurrencies(wrap *wrapper.Wrapper) map[int]string {
 	result := map[int]string{}
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			code
@@ -221,6 +226,7 @@ func (this *Modules) shop_GetAllCurrencies(wrap *wrapper.Wrapper) map[int]string
 func (this *Modules) shop_GetAllProductImages(wrap *wrapper.Wrapper, product_id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			product_id,
@@ -254,6 +260,7 @@ func (this *Modules) shop_GetAllProductImages(wrap *wrapper.Wrapper, product_id
 func (this *Modules) shop_GetSubProducts(wrap *wrapper.Wrapper, id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			name
@@ -286,6 +293,7 @@ func (this *Modules) shop_GetSubProducts(wrap *wrapper.Wrapper, id int) string {
 func (this *Modules) shop_GetParentProduct(wrap *wrapper.Wrapper, id int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			name
@@ -747,6 +755,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				},
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
+						wrap.R.Context(),
 						`SELECT
 							shop_products.id,
 							shop_products.name,
@@ -835,6 +844,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				nil,
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
+						wrap.R.Context(),
 						`SELECT
 							node.id,
 							node.user,
@@ -1081,6 +1091,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				nil,
 				func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
 					return wrap.DB.Query(
+						wrap.R.Context(),
 						`SELECT
 							shop_orders.id,
 							shop_orders.client_phone,
@@ -1221,7 +1232,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 			// All product current categories
 			var selids []int
 			if data.A_id > 0 {
-				rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
+				rows, err := wrap.DB.Query(wrap.R.Context(), "SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
 				if err == nil {
 					defer rows.Close()
 					values := make([]int, 1)
@@ -2144,6 +2155,7 @@ func (this *Modules) RegisterModule_Shop() *Module {
 				<tbody>
 			`
 			rows, err := wrap.DB.Query(
+				wrap.R.Context(),
 				`SELECT
 					shop_order_products.product_id,
 					shop_products.name,

+ 1 - 0
modules/module_shop_act_attach_product_search.go

@@ -29,6 +29,7 @@ func (this *Modules) shop_GetProductsListForAttaching(wrap *wrapper.Wrapper, nam
 	}
 
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			id,
 			name

+ 1 - 0
modules/module_shop_act_get_attribute_values.go

@@ -22,6 +22,7 @@ func (this *Modules) RegisterAction_ShopGetAttributeValues() *Action {
 
 		options := ``
 		rows, err := wrap.DB.Query(
+			wrap.R.Context(),
 			`SELECT
 				id,
 				name

+ 1 - 0
modules/module_shop_categories.go

@@ -11,6 +11,7 @@ import (
 func (this *Modules) shop_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int, selids []int) string {
 	result := ``
 	rows, err := wrap.DB.Query(
+		wrap.R.Context(),
 		`SELECT
 			node.id,
 			node.user,

+ 1 - 0
smtp.go

@@ -72,6 +72,7 @@ func smtp_process(ctx context.Context, dir, host string, mp *mysqlpool.MySqlPool
 
 func smtp_prepare(ctx context.Context, db *sqlw.DB, conf *config.Config) {
 	rows, err := db.Query(
+		ctx,
 		`SELECT
 			id,
 			email,

+ 6 - 0
xml.go

@@ -124,6 +124,7 @@ func xml_generate(ctx context.Context, db *sqlw.DB, conf *config.Config) (string
 func xml_gen_currencies(ctx context.Context, db *sqlw.DB, conf *config.Config) (string, error) {
 	result := ``
 	rows, err := db.Query(
+		ctx,
 		`SELECT
 			code,
 			coefficient
@@ -153,6 +154,7 @@ func xml_gen_currencies(ctx context.Context, db *sqlw.DB, conf *config.Config) (
 func xml_gen_categories(ctx context.Context, db *sqlw.DB, conf *config.Config) (string, error) {
 	result := ``
 	rows, err := db.Query(
+		ctx,
 		`SELECT
 			data.id,
 			data.user,
@@ -212,6 +214,7 @@ func xml_gen_categories(ctx context.Context, db *sqlw.DB, conf *config.Config) (
 func xml_gen_offers(ctx context.Context, db *sqlw.DB, conf *config.Config) (string, error) {
 	result := ``
 	rows, err := db.Query(
+		ctx,
 		`SELECT
 			shop_products.id,
 			shop_currencies.code,
@@ -272,6 +275,7 @@ func xml_gen_offers(ctx context.Context, db *sqlw.DB, conf *config.Config) (stri
 func xml_gen_offer_pictures(ctx context.Context, db *sqlw.DB, conf *config.Config, product_id, parent_id int) string {
 	result := ``
 	if rows, err := db.Query(
+		ctx,
 		`SELECT
 			shop_product_images.product_id,
 			shop_product_images.filename
@@ -301,6 +305,7 @@ func xml_gen_offer_pictures(ctx context.Context, db *sqlw.DB, conf *config.Confi
 	// Get images from parent
 	if result == "" && parent_id > 0 {
 		if rows, err := db.Query(
+			ctx,
 			`SELECT
 				shop_product_images.product_id,
 				shop_product_images.filename
@@ -337,6 +342,7 @@ func xml_gen_offer_attributes(ctx context.Context, db *sqlw.DB, conf *config.Con
 	filter_names := map[int]string{}
 	filter_values := map[int][]string{}
 	rows, err := db.Query(
+		ctx,
 		`SELECT
 			shop_filters.id,
 			shop_filters.filter,