Browse Source

Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476)

lunny/test_oracle2
Guillermo Prandi Lunny Xiao <xiaolunwen@gmail.com> 4 weeks ago
parent
commit
29b162a3ab
2 changed files with 74 additions and 12 deletions
  1. +33
    -12
      engine.go
  2. +41
    -0
      engine_test.go

+ 33
- 12
engine.go View File

@@ -207,25 +207,46 @@ func (engine *Engine) QuoteTo(buf *strings.Builder, value string) {
return
}

quotePair := engine.dialect.Quote("")
quoteTo(buf, engine.dialect.Quote(""), value)
}

if value[0] == '`' || len(quotePair) < 2 || value[0] == quotePair[0] { // no quote
func quoteTo(buf *strings.Builder, quotePair string, value string) {
if len(quotePair) < 2 { // no quote
_, _ = buf.WriteString(value)
return
} else {
prefix, suffix := quotePair[0], quotePair[1]

_ = buf.WriteByte(prefix)
for i := 0; i < len(value); i++ {
if value[i] == '.' {
_ = buf.WriteByte(suffix)
_ = buf.WriteByte('.')
_ = buf.WriteByte(prefix)
}
prefix, suffix := quotePair[0], quotePair[1]

i := 0
for i < len(value) {
// start of a token; might be already quoted
if value[i] == '.' {
_ = buf.WriteByte('.')
i++
} else if value[i] == prefix || value[i] == '`' {
// Has quotes; skip/normalize `name` to prefix+name+sufix
var ch byte
if value[i] == prefix {
ch = suffix
} else {
ch = '`'
}
i++
_ = buf.WriteByte(prefix)
for ; i < len(value) && value[i] != ch; i++ {
_ = buf.WriteByte(value[i])
}
_ = buf.WriteByte(suffix)
i++
} else {
// Requires quotes
_ = buf.WriteByte(prefix)
for ; i < len(value) && value[i] != '.'; i++ {
_ = buf.WriteByte(value[i])
}
_ = buf.WriteByte(suffix)
}
_ = buf.WriteByte(suffix)
}
}



+ 41
- 0
engine_test.go View File

@@ -0,0 +1,41 @@
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestQuoteTo(t *testing.T) {

test := func(t *testing.T, expected string, value string) {
buf := &strings.Builder{}
quoteTo(buf, "[]", value)
assert.EqualValues(t, expected, buf.String())
}

test(t, "[mytable]", "mytable")
test(t, "[mytable]", "`mytable`")
test(t, "[mytable]", `[mytable]`)

test(t, `["mytable"]`, `"mytable"`)

test(t, "[myschema].[mytable]", "myschema.mytable")
test(t, "[myschema].[mytable]", "`myschema`.mytable")
test(t, "[myschema].[mytable]", "myschema.`mytable`")
test(t, "[myschema].[mytable]", "`myschema`.`mytable`")
test(t, "[myschema].[mytable]", `[myschema].mytable`)
test(t, "[myschema].[mytable]", `myschema.[mytable]`)
test(t, "[myschema].[mytable]", `[myschema].[mytable]`)

test(t, `["myschema].[mytable"]`, `"myschema.mytable"`)

buf := &strings.Builder{}
quoteTo(buf, "", "noquote")
assert.EqualValues(t, "noquote", buf.String())
}

Loading…
Cancel
Save