From 58a32d7c9d72ddc261e6bb8939efd687d167b3d3 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 20 Aug 2023 00:52:31 +0100 Subject: [PATCH] Update GORM. --- gormlite/go.mod | 4 ++-- gormlite/go.sum | 9 ++++---- gormlite/sqlite.go | 53 ++++++++++++++++++++++++++++++++++++---------- gormlite/test.sh | 4 ++-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/gormlite/go.mod b/gormlite/go.mod index f0174dd..2b744da 100644 --- a/gormlite/go.mod +++ b/gormlite/go.mod @@ -3,8 +3,8 @@ module github.com/ncruces/go-sqlite3/gormlite go 1.21 require ( - github.com/ncruces/go-sqlite3 v0.8.4 - gorm.io/gorm v1.25.2 + github.com/ncruces/go-sqlite3 v0.8.5 + gorm.io/gorm v1.25.4 ) require ( diff --git a/gormlite/go.sum b/gormlite/go.sum index 900029a..8ca5965 100644 --- a/gormlite/go.sum +++ b/gormlite/go.sum @@ -2,8 +2,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/ncruces/go-sqlite3 v0.8.4 h1:nizhgJMMJJBrthESCwF30+oOvQkdtizgJ/v35Y0v+vg= -github.com/ncruces/go-sqlite3 v0.8.4/go.mod h1:XvDtjKk5MgwHX7L4I7BPzzKl36bTZ7+Hr6Kr2QeVkVw= +github.com/ncruces/go-sqlite3 v0.8.5 h1:JeNcbJ4rsZ07ZVyqPdnFlfmVSWDW0ONoiuZSUBC369Y= +github.com/ncruces/go-sqlite3 v0.8.5/go.mod h1:XvDtjKk5MgwHX7L4I7BPzzKl36bTZ7+Hr6Kr2QeVkVw= github.com/ncruces/julianday v0.1.5 h1:hDJ9ejiMp3DHsoZ5KW4c1lwfMjbARS7u/gbYcd0FBZk= github.com/ncruces/julianday v0.1.5/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= github.com/tetratelabs/wazero v1.4.0 h1:9/MirYvmkJ/zSUOygKY/ia3t+e+RqIZXKbylIby1WYk= @@ -11,6 +11,5 @@ github.com/tetratelabs/wazero v1.4.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kD golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= -gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= +gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= diff --git a/gormlite/sqlite.go b/gormlite/sqlite.go index 837d57c..960385c 100644 --- a/gormlite/sqlite.go +++ b/gormlite/sqlite.go @@ -5,7 +5,6 @@ import ( "context" "database/sql" "strconv" - "strings" "gorm.io/gorm" "gorm.io/gorm/callbacks" @@ -136,19 +135,51 @@ func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, } func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { - writer.WriteByte('`') - if strings.Contains(str, ".") { - for idx, str := range strings.Split(str, ".") { - if idx > 0 { - writer.WriteString(".`") + var ( + underQuoted, selfQuoted bool + continuousBacktick int8 + shiftDelimiter int8 + ) + + for _, v := range []byte(str) { + switch v { + case '`': + continuousBacktick++ + if continuousBacktick == 2 { + writer.WriteString("``") + continuousBacktick = 0 } - writer.WriteString(str) - writer.WriteByte('`') + case '.': + if continuousBacktick > 0 || !selfQuoted { + shiftDelimiter = 0 + underQuoted = false + continuousBacktick = 0 + writer.WriteString("`") + } + writer.WriteByte(v) + continue + default: + if shiftDelimiter-continuousBacktick <= 0 && !underQuoted { + writer.WriteString("`") + underQuoted = true + if selfQuoted = continuousBacktick > 0; selfQuoted { + continuousBacktick -= 1 + } + } + + for ; continuousBacktick > 0; continuousBacktick -= 1 { + writer.WriteString("``") + } + + writer.WriteByte(v) } - } else { - writer.WriteString(str) - writer.WriteByte('`') + shiftDelimiter++ } + + if continuousBacktick > 0 && !selfQuoted { + writer.WriteString("``") + } + writer.WriteString("`") } func (dialector Dialector) Explain(sql string, vars ...interface{}) string { diff --git a/gormlite/test.sh b/gormlite/test.sh index 4fc43ce..8270a93 100755 --- a/gormlite/test.sh +++ b/gormlite/test.sh @@ -3,7 +3,7 @@ set -euo pipefail cd -P -- "$(dirname -- "$0")" -rm -rf gorm/ tests/ "$(dirname $(mktemp -u))/gorm.db" +rm -rf gorm/ tests/ git clone --filter=blob:none https://github.com/go-gorm/gorm.git mv gorm/tests tests rm -rf gorm/ @@ -20,5 +20,5 @@ go mod edit \ go mod tidy && go work use . && go test cd .. -rm -rf tests/ "$(dirname $(mktemp -u))/gorm.db" +rm -rf tests/ go work use -r . \ No newline at end of file