Skip to content

Commit

Permalink
Fixed the construction of query for tables with schema (#26)
Browse files Browse the repository at this point in the history
* Fixed the constuction of query for tables with schema

* Minor fixes to the cache calls of sql query buffer

* Prioritised checks for simple queries in buffer

* Added select all stmts to chaching in buffer
  • Loading branch information
talkanbaev-artur committed Apr 11, 2022
1 parent 8603fe0 commit 4c61d06
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
34 changes: 24 additions & 10 deletions builder/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ func (b *Buffer) WriteEscape(value string) {
}

func (b Buffer) escape(table, value string) string {
if value == "*" {
if table == "" {
return value
}
return b.Quoter.ID(table) + ".*"
if table == "" && value == "*" {
return value
}

key := escapeCacheKey{table: table, value: value, quoter: b.Quoter}
Expand All @@ -128,7 +125,23 @@ func (b Buffer) escape(table, value string) string {
return escapedValue.(string)
}

if len(value) > 0 && value[0] == UnescapeCharacter {
var escaped_table string
if table != "" {
if strings.IndexByte(table, '.') >= 0 {
parts := strings.Split(table, ".")
for i, part := range parts {
part = strings.TrimSpace(part)
parts[i] = b.Quoter.ID(part)
}
escaped_table = strings.Join(parts, ".")
} else {
escaped_table = b.Quoter.ID(table)
}
}

if value == "*" {
escapedValue = escaped_table + ".*"
} else if len(value) > 0 && value[0] == UnescapeCharacter {
escapedValue = value[1:]
} else if _, err := strconv.Atoi(value); err == nil {
escapedValue = value
Expand All @@ -138,17 +151,18 @@ func (b Buffer) escape(table, value string) string {
escapedValue = value[:start+1] + b.escape(table, value[start+1:end]) + value[end:]
} else {
parts := strings.Split(value, ".")
if len(parts) == 1 && table != "" {
parts = []string{table, parts[0]}
}
for i, part := range parts {
part = strings.TrimSpace(part)
if part == "*" && i == len(parts)-1 {
break
}
parts[i] = b.Quoter.ID(part)
}
escapedValue = strings.Join(parts, ".")
result := strings.Join(parts, ".")
if len(parts) == 1 && table != "" {
result = escaped_table + "." + result
}
escapedValue = result
}

escapeCache.Store(key, escapedValue)
Expand Down
5 changes: 5 additions & 0 deletions builder/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func TestBuffer_escape(t *testing.T) {
field: "*",
result: "[user].*",
},
{
table: "user.user",
field: "*",
result: "[user].[user].*",
},
{
table: "user",
field: "address as home_address",
Expand Down

0 comments on commit 4c61d06

Please sign in to comment.