Browse Source

新增SqlMap和SqlTemplate初始化容量设置

xormplus 9 years ago
parent
commit
2cecd697f0
2 changed files with 18 additions and 2 deletions
  1. 9 1
      sqlmap.go
  2. 9 1
      sqltemplate.go

+ 9 - 1
sqlmap.go

@@ -14,9 +14,11 @@ type SqlMap struct {
 	SqlMapRootDir string
 	Sql           map[string]string
 	Extension     string
+	Capacity      uint
 }
 
 type SqlMapOptions struct {
+	Capacity  uint
 	Extension string
 }
 
@@ -31,7 +33,12 @@ type Sql struct {
 
 func (sqlMap *SqlMap) checkNilAndInit() {
 	if sqlMap.Sql == nil {
-		sqlMap.Sql = make(map[string]string, 100)
+		if sqlMap.Capacity == 0 {
+			sqlMap.Sql = make(map[string]string, 100)
+		} else {
+			sqlMap.Sql = make(map[string]string, sqlMap.Capacity)
+		}
+
 	}
 }
 
@@ -47,6 +54,7 @@ func (engine *Engine) InitSqlMap(options ...SqlMapOptions) error {
 	}
 
 	engine.sqlMap.Extension = opt.Extension
+	engine.sqlMap.Capacity = opt.Capacity
 
 	var err error
 	if engine.sqlMap.SqlMapRootDir == "" {

+ 9 - 1
sqltemplate.go

@@ -13,15 +13,21 @@ type SqlTemplate struct {
 	SqlTemplateRootDir string
 	Template           map[string]*pongo2.Template
 	Extension          string
+	Capacity           uint
 }
 
 type SqlTemplateOptions struct {
+	Capacity  uint
 	Extension string
 }
 
 func (sqlTemplate *SqlTemplate) checkNilAndInit() {
 	if sqlTemplate.Template == nil {
-		sqlTemplate.Template = make(map[string]*pongo2.Template, 100)
+		if sqlTemplate.Capacity == 0 {
+			sqlTemplate.Template = make(map[string]*pongo2.Template, 100)
+		} else {
+			sqlTemplate.Template = make(map[string]*pongo2.Template, sqlTemplate.Capacity)
+		}
 	}
 }
 
@@ -35,6 +41,8 @@ func (engine *Engine) InitSqlTemplate(options ...SqlTemplateOptions) error {
 	if len(opt.Extension) == 0 {
 		opt.Extension = ".stpl"
 	}
+	engine.sqlTemplate.Extension = opt.Extension
+	engine.sqlTemplate.Capacity = opt.Capacity
 
 	var err error
 	if engine.sqlTemplate.SqlTemplateRootDir == "" {