Merge branch 'master' into master

This commit is contained in:
getsu 2020-05-27 04:51:02 +00:00
commit 1c620a905a
3 changed files with 44 additions and 16 deletions

View File

@ -500,8 +500,8 @@ var (
} }
oracleQuoter = schemas.Quoter{ oracleQuoter = schemas.Quoter{
Prefix: '[', Prefix: '"',
Suffix: ']', Suffix: '"',
IsReserved: schemas.AlwaysReserve, IsReserved: schemas.AlwaysReserve,
} }
) )

View File

@ -7,6 +7,7 @@ package names
import ( import (
"strings" "strings"
"sync" "sync"
"unsafe"
) )
// Mapper represents a name convertation between struct's fields name and table's column name // Mapper represents a name convertation between struct's fields name and table's column name
@ -77,19 +78,24 @@ func (m SameMapper) Table2Obj(t string) string {
type SnakeMapper struct { type SnakeMapper struct {
} }
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func snakeCasedName(name string) string { func snakeCasedName(name string) string {
newstr := make([]rune, 0) newstr := make([]byte, 0, len(name)+1)
for idx, chr := range name { for i := 0; i < len(name); i++ {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper { c := name[i]
if idx > 0 { if isUpper := 'A' <= c && c <= 'Z'; isUpper {
if i > 0 {
newstr = append(newstr, '_') newstr = append(newstr, '_')
} }
chr -= ('A' - 'a') c += 'a' - 'A'
} }
newstr = append(newstr, chr) newstr = append(newstr, c)
} }
return string(newstr) return b2s(newstr)
} }
func (mapper SnakeMapper) Obj2Table(name string) string { func (mapper SnakeMapper) Obj2Table(name string) string {
@ -97,27 +103,28 @@ func (mapper SnakeMapper) Obj2Table(name string) string {
} }
func titleCasedName(name string) string { func titleCasedName(name string) string {
newstr := make([]rune, 0) newstr := make([]byte, 0, len(name))
upNextChar := true upNextChar := true
name = strings.ToLower(name) name = strings.ToLower(name)
for _, chr := range name { for i := 0; i < len(name); i++ {
c := name[i]
switch { switch {
case upNextChar: case upNextChar:
upNextChar = false upNextChar = false
if 'a' <= chr && chr <= 'z' { if 'a' <= c && c <= 'z' {
chr -= ('a' - 'A') c -= 'a' - 'A'
} }
case chr == '_': case c == '_':
upNextChar = true upNextChar = true
continue continue
} }
newstr = append(newstr, chr) newstr = append(newstr, c)
} }
return string(newstr) return b2s(newstr)
} }
func (mapper SnakeMapper) Table2Obj(name string) string { func (mapper SnakeMapper) Table2Obj(name string) string {

View File

@ -5,6 +5,7 @@
package names package names
import ( import (
"strings"
"testing" "testing"
) )
@ -47,3 +48,23 @@ func TestGonicMapperToObj(t *testing.T) {
} }
} }
} }
func BenchmarkSnakeCasedName(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
s := strings.Repeat("FooBar", 32)
for i := 0; i < b.N; i++ {
_ = snakeCasedName(s)
}
}
func BenchmarkTitleCasedName(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
s := strings.Repeat("foo_bar", 32)
for i := 0; i < b.N; i++ {
_ = titleCasedName(s)
}
}