Browse Source

Reduce MySQL queries for disabled modules

Vova Tkach 5 years ago
parent
commit
55604baecc
4 changed files with 64 additions and 39 deletions
  1. 25 17
      engine/fetdata/blog.go
  2. 3 0
      engine/fetdata/blog_category.go
  3. 33 22
      engine/fetdata/shop.go
  4. 3 0
      engine/fetdata/shop_category.go

+ 25 - 17
engine/fetdata/blog.go

@@ -37,6 +37,9 @@ func (this *Blog) load() *Blog {
 	if this == nil {
 	if this == nil {
 		return this
 		return this
 	}
 	}
+	if (*this.wrap.Config).Modules.Enabled.Blog == 0 {
+		return this
+	}
 	sql_nums := `
 	sql_nums := `
 		SELECT
 		SELECT
 			COUNT(*)
 			COUNT(*)
@@ -427,6 +430,9 @@ func (this *Blog) load() *Blog {
 }
 }
 
 
 func (this *Blog) preload_cats() {
 func (this *Blog) preload_cats() {
+	if (*this.wrap.Config).Modules.Enabled.Blog == 0 {
+		return
+	}
 	if this.bufferCats == nil {
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_blog_category{}
 		this.bufferCats = map[int]*utils.MySql_blog_category{}
 		if rows, err := this.wrap.DB.Query(
 		if rows, err := this.wrap.DB.Query(
@@ -597,32 +603,34 @@ func (this *Blog) Categories(parent, depth int) []*BlogCategory {
 	depth_tmp := 0
 	depth_tmp := 0
 	result := []*BlogCategory{}
 	result := []*BlogCategory{}
 
 
-	for _, cat := range this.bufferCats {
-		if parent <= 1 {
-			if depth <= 0 {
-				result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
-			} else {
-				if cat.A_depth <= depth {
-					result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
-				}
-			}
-		} else {
-			if cat.A_parent == parent {
-				if depth_tmp == 0 {
-					depth_tmp = cat.A_depth
-				}
+	if (*this.wrap.Config).Modules.Enabled.Blog != 0 {
+		for _, cat := range this.bufferCats {
+			if parent <= 1 {
 				if depth <= 0 {
 				if depth <= 0 {
 					result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 					result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 				} else {
 				} else {
-					if (cat.A_depth - depth_tmp + 1) <= depth {
+					if cat.A_depth <= depth {
+						result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+					}
+				}
+			} else {
+				if cat.A_parent == parent {
+					if depth_tmp == 0 {
+						depth_tmp = cat.A_depth
+					}
+					if depth <= 0 {
 						result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 						result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+					} else {
+						if (cat.A_depth - depth_tmp + 1) <= depth {
+							result = append(result, (&BlogCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+						}
 					}
 					}
 				}
 				}
 			}
 			}
 		}
 		}
-	}
 
 
-	sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
+		sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
+	}
 
 
 	return result
 	return result
 }
 }

+ 3 - 0
engine/fetdata/blog_category.go

@@ -18,6 +18,9 @@ func (this *BlogCategory) load(cache *map[int]*utils.MySql_blog_category) *BlogC
 	if this == nil {
 	if this == nil {
 		return this
 		return this
 	}
 	}
+	if (*this.wrap.Config).Modules.Enabled.Blog == 0 {
+		return this
+	}
 	if cache != nil {
 	if cache != nil {
 		this.bufferCats = (*cache)
 		this.bufferCats = (*cache)
 		return this
 		return this

+ 33 - 22
engine/fetdata/shop.go

@@ -37,6 +37,9 @@ func (this *Shop) load() *Shop {
 	if this == nil {
 	if this == nil {
 		return this
 		return this
 	}
 	}
+	if (*this.wrap.Config).Modules.Enabled.Shop == 0 {
+		return this
+	}
 	sql_nums := `
 	sql_nums := `
 		SELECT
 		SELECT
 			COUNT(*)
 			COUNT(*)
@@ -507,6 +510,9 @@ func (this *Shop) load() *Shop {
 }
 }
 
 
 func (this *Shop) preload_cats() {
 func (this *Shop) preload_cats() {
+	if (*this.wrap.Config).Modules.Enabled.Shop == 0 {
+		return
+	}
 	if this.bufferCats == nil {
 	if this.bufferCats == nil {
 		this.bufferCats = map[int]*utils.MySql_shop_category{}
 		this.bufferCats = map[int]*utils.MySql_shop_category{}
 		if rows, err := this.wrap.DB.Query(
 		if rows, err := this.wrap.DB.Query(
@@ -673,12 +679,15 @@ func (this *Shop) PaginationNext() *ShopPagination {
 
 
 func (this *Shop) Currencies() []*ShopCurrency {
 func (this *Shop) Currencies() []*ShopCurrency {
 	result := []*ShopCurrency{}
 	result := []*ShopCurrency{}
-	for _, currency := range *this.wrap.ShopGetAllCurrencies() {
-		obj := currency
-		result = append(result, (&ShopCurrency{wrap: this.wrap, object: &obj}).load())
-	}
 
 
-	sort.Slice(result, func(i, j int) bool { return result[i].Id() < result[j].Id() })
+	if (*this.wrap.Config).Modules.Enabled.Shop != 0 {
+		for _, currency := range *this.wrap.ShopGetAllCurrencies() {
+			obj := currency
+			result = append(result, (&ShopCurrency{wrap: this.wrap, object: &obj}).load())
+		}
+
+		sort.Slice(result, func(i, j int) bool { return result[i].Id() < result[j].Id() })
+	}
 
 
 	return result
 	return result
 }
 }
@@ -694,32 +703,34 @@ func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 	depth_tmp := 0
 	depth_tmp := 0
 	result := []*ShopCategory{}
 	result := []*ShopCategory{}
 
 
-	for _, cat := range this.bufferCats {
-		if parent <= 1 {
-			if depth <= 0 {
-				result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
-			} else {
-				if cat.A_depth <= depth {
-					result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
-				}
-			}
-		} else {
-			if cat.A_parent == parent {
-				if depth_tmp == 0 {
-					depth_tmp = cat.A_depth
-				}
+	if (*this.wrap.Config).Modules.Enabled.Shop != 0 {
+		for _, cat := range this.bufferCats {
+			if parent <= 1 {
 				if depth <= 0 {
 				if depth <= 0 {
 					result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 					result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 				} else {
 				} else {
-					if (cat.A_depth - depth_tmp + 1) <= depth {
+					if cat.A_depth <= depth {
+						result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+					}
+				}
+			} else {
+				if cat.A_parent == parent {
+					if depth_tmp == 0 {
+						depth_tmp = cat.A_depth
+					}
+					if depth <= 0 {
 						result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
 						result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+					} else {
+						if (cat.A_depth - depth_tmp + 1) <= depth {
+							result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
+						}
 					}
 					}
 				}
 				}
 			}
 			}
 		}
 		}
-	}
 
 
-	sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
+		sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
+	}
 
 
 	return result
 	return result
 }
 }

+ 3 - 0
engine/fetdata/shop_category.go

@@ -18,6 +18,9 @@ func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopC
 	if this == nil {
 	if this == nil {
 		return this
 		return this
 	}
 	}
+	if (*this.wrap.Config).Modules.Enabled.Shop == 0 {
+		return this
+	}
 	if cache != nil {
 	if cache != nil {
 		this.bufferCats = (*cache)
 		this.bufferCats = (*cache)
 		return this
 		return this