Skip to content

Commit

Permalink
Fixed the constuction of query for tables with schema
Browse files Browse the repository at this point in the history
  • Loading branch information
talkanbaev-artur committed Apr 9, 2022
1 parent 8603fe0 commit 56e8dba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
25 changes: 20 additions & 5 deletions builder/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,25 @@ func (b *Buffer) WriteEscape(value string) {
}

func (b Buffer) escape(table, value string) string {
var escaped_table string
if table != "" {
parts := strings.Split(table, ".")
if len(parts) != 1 {
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 == "*" {
if table == "" {
return value
}
return b.Quoter.ID(table) + ".*"
return escaped_table + ".*"
}

key := escapeCacheKey{table: table, value: value, quoter: b.Quoter}
Expand All @@ -138,17 +152,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 56e8dba

Please sign in to comment.