good morning!!!!

Skip to content
Snippets Groups Projects
Commit 481481a5 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Adding benchmarks and prototyping cache functions.

parent 4d8c2a2e
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,11 @@ type Column struct { ...@@ -14,7 +14,11 @@ type Column struct {
Value interface{} Value interface{}
} }
func (self Column) Compile(layout *Template) string { func (self Column) Compile(layout *Template) (compiled string) {
if s, ok := layout.Cache(self); ok {
return s
}
switch value := self.Value.(type) { switch value := self.Value.(type) {
case string: case string:
...@@ -44,10 +48,14 @@ func (self Column) Compile(layout *Template) string { ...@@ -44,10 +48,14 @@ func (self Column) Compile(layout *Template) string {
alias = mustParse(layout.IdentifierQuote, Raw{alias}) alias = mustParse(layout.IdentifierQuote, Raw{alias})
} }
return mustParse(layout.ColumnAliasLayout, column_t{name, alias}) compiled = mustParse(layout.ColumnAliasLayout, column_t{name, alias})
case Raw: case Raw:
return value.String() compiled = value.String()
default:
compiled = fmt.Sprintf("%v", self.Value)
} }
return fmt.Sprintf("%v", self.Value) layout.SetCache(self, compiled)
return compiled
} }
...@@ -16,18 +16,33 @@ type columnValue_s struct { ...@@ -16,18 +16,33 @@ type columnValue_s struct {
Value string Value string
} }
func (self ColumnValue) Compile(layout *Template) string { func (self ColumnValue) Compile(layout *Template) (compiled string) {
if s, ok := layout.Cache(self); ok {
return s
}
data := columnValue_s{ data := columnValue_s{
self.Column.Compile(layout), self.Column.Compile(layout),
self.Operator, self.Operator,
self.Value.Compile(layout), self.Value.Compile(layout),
} }
return mustParse(layout.ColumnValue, data)
compiled = mustParse(layout.ColumnValue, data)
return
} }
type ColumnValues []ColumnValue type ColumnValues []ColumnValue
func (self ColumnValues) Compile(layout *Template) string { func (self ColumnValues) Compile(layout *Template) (compiled string) {
/*
if s, ok := layout.Cache(self); ok {
return s
}
*/
l := len(self) l := len(self)
out := make([]string, l) out := make([]string, l)
...@@ -36,5 +51,7 @@ func (self ColumnValues) Compile(layout *Template) string { ...@@ -36,5 +51,7 @@ func (self ColumnValues) Compile(layout *Template) string {
out[i] = self[i].Compile(layout) out[i] = self[i].Compile(layout)
} }
return strings.Join(out, layout.IdentifierSeparator) compiled = strings.Join(out, layout.IdentifierSeparator)
return
} }
...@@ -141,4 +141,5 @@ var defaultTemplate = &Template{ ...@@ -141,4 +141,5 @@ var defaultTemplate = &Template{
defaultDropTableLayout, defaultDropTableLayout,
defaultSelectCountLayout, defaultSelectCountLayout,
defaultGroupByLayout, defaultGroupByLayout,
nil,
} }
...@@ -24,14 +24,21 @@ type ( ...@@ -24,14 +24,21 @@ type (
Extra string Extra string
) )
func mustParse(text string, data interface{}) string { var parsedTemplates = make(map[string]*template.Template)
func mustParse(text string, data interface{}) (compiled string) {
var b bytes.Buffer var b bytes.Buffer
var ok bool
t := template.Must(template.New("").Parse(text)) if _, ok = parsedTemplates[text]; ok == false {
parsedTemplates[text] = template.Must(template.New("").Parse(text))
}
if err := t.Execute(&b, data); err != nil { if err := parsedTemplates[text].Execute(&b, data); err != nil {
panic("t.Execute: " + err.Error()) panic("There was an error compiling the following template:\n" + text + "\nError was: " + err.Error())
} }
return b.String() compiled = b.String()
return
} }
...@@ -29,7 +29,7 @@ type statement_s struct { ...@@ -29,7 +29,7 @@ type statement_s struct {
Where string Where string
} }
func (self *Statement) Compile(layout *Template) string { func (self *Statement) Compile(layout *Template) (compiled string) {
data := statement_s{ data := statement_s{
Table: self.Table.Compile(layout), Table: self.Table.Compile(layout),
...@@ -47,21 +47,24 @@ func (self *Statement) Compile(layout *Template) string { ...@@ -47,21 +47,24 @@ func (self *Statement) Compile(layout *Template) string {
switch self.Type { switch self.Type {
case SqlTruncate: case SqlTruncate:
return mustParse(layout.TruncateLayout, data) compiled = mustParse(layout.TruncateLayout, data)
case SqlDropTable: case SqlDropTable:
return mustParse(layout.DropTableLayout, data) compiled = mustParse(layout.DropTableLayout, data)
case SqlDropDatabase: case SqlDropDatabase:
return mustParse(layout.DropDatabaseLayout, data) compiled = mustParse(layout.DropDatabaseLayout, data)
case SqlSelectCount: case SqlSelectCount:
return mustParse(layout.SelectCountLayout, data) compiled = mustParse(layout.SelectCountLayout, data)
case SqlSelect: case SqlSelect:
return mustParse(layout.SelectLayout, data) compiled = mustParse(layout.SelectLayout, data)
case SqlDelete: case SqlDelete:
return mustParse(layout.DeleteLayout, data) compiled = mustParse(layout.DeleteLayout, data)
case SqlUpdate: case SqlUpdate:
return mustParse(layout.UpdateLayout, data) compiled = mustParse(layout.UpdateLayout, data)
case SqlInsert: case SqlInsert:
return mustParse(layout.InsertLayout, data) compiled = mustParse(layout.InsertLayout, data)
default:
compiled = ""
} }
return ""
return compiled
} }
...@@ -50,8 +50,13 @@ func quotedTableName(layout *Template, input string) string { ...@@ -50,8 +50,13 @@ func quotedTableName(layout *Template, input string) string {
return mustParse(layout.TableAliasLayout, table_t{name, alias}) return mustParse(layout.TableAliasLayout, table_t{name, alias})
} }
func (self Table) Compile(layout *Template) string { func (self Table) Hash() string {
return self.Name
}
func (self Table) Compile(layout *Template) (compiled string) {
// Splitting tables by a comma
parts := reTableSeparator.Split(self.Name, -1) parts := reTableSeparator.Split(self.Name, -1)
l := len(parts) l := len(parts)
...@@ -60,5 +65,7 @@ func (self Table) Compile(layout *Template) string { ...@@ -60,5 +65,7 @@ func (self Table) Compile(layout *Template) string {
parts[i] = quotedTableName(layout, parts[i]) parts[i] = quotedTableName(layout, parts[i])
} }
return strings.Join(parts, layout.IdentifierSeparator) compiled = strings.Join(parts, layout.IdentifierSeparator)
return compiled
} }
...@@ -29,4 +29,21 @@ type Template struct { ...@@ -29,4 +29,21 @@ type Template struct {
DropTableLayout string DropTableLayout string
SelectCountLayout string SelectCountLayout string
GroupByLayout string GroupByLayout string
cache map[interface{}]string
}
func (self *Template) SetCache(key interface{}, value string) {
if self.cache == nil {
self.cache = make(map[interface{}]string)
}
self.cache[key] = value
}
func (self *Template) Cache(key interface{}) (string, bool) {
if self.cache != nil {
if s, ok := self.cache[key]; ok {
return s, true
}
}
return "", false
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment