diff --git a/go.mod b/go.mod index 683ae18cb8781..2e2c82103a94c 100644 --- a/go.mod +++ b/go.mod @@ -116,5 +116,5 @@ require ( strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 xorm.io/builder v0.3.6 xorm.io/core v0.7.3 - xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a + xorm.io/xorm v0.8.2 ) diff --git a/go.sum b/go.sum index 7522fd9c87498..2cba905b339ee 100644 --- a/go.sum +++ b/go.sum @@ -791,5 +791,5 @@ xorm.io/core v0.7.3 h1:W8ws1PlrnkS1CZU1YWaYLMQcQilwAmQXU0BJDJon+H0= xorm.io/core v0.7.3/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM= xorm.io/xorm v0.8.0 h1:iALxgJrX8O00f8Jk22GbZwPmxJNgssV5Mv4uc2HL9PM= xorm.io/xorm v0.8.0/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY= -xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a h1:hzGd080rlkZ5a7v6Tr3x8PJJnWPfKxGMMl92c8DNcww= -xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY= +xorm.io/xorm v0.8.2 h1:nbg1AyWn7iLrwp0Dqg8IrYOBkBYYJ85ry9bvZLVl4Ok= +xorm.io/xorm v0.8.2/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY= diff --git a/models/access.go b/models/access.go index c509867062686..86c0c664d7b01 100644 --- a/models/access.go +++ b/models/access.go @@ -207,7 +207,11 @@ func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]*userAcces // Delete old accesses and insert new ones for repository. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil { return fmt.Errorf("delete old accesses: %v", err) - } else if _, err = e.Insert(newAccesses); err != nil { + } + if len(newAccesses) == 0 { + return nil + } + if _, err = e.Insert(newAccesses); err != nil { return fmt.Errorf("insert new accesses: %v", err) } return nil diff --git a/models/repo.go b/models/repo.go index 672507819add7..419b68143151d 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1412,11 +1412,14 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes [ for _, u := range units { deleteUnitTypes = append(deleteUnitTypes, u.Type) } - - if _, err = sess.Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil { - return err + if len(deleteUnitTypes) != 0 { + if _, err = sess.Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil { + return err + } + } + if len(units) == 0 { + return sess.Commit() } - if _, err = sess.Insert(units); err != nil { return err } diff --git a/vendor/modules.txt b/vendor/modules.txt index 28bf533ec6090..f3a2661181081 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -642,5 +642,5 @@ strk.kbt.io/projects/go/libravatar xorm.io/builder # xorm.io/core v0.7.3 xorm.io/core -# xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a +# xorm.io/xorm v0.8.2 xorm.io/xorm diff --git a/vendor/xorm.io/xorm/.drone.yml b/vendor/xorm.io/xorm/.drone.yml index e9dae78896d82..7a273d3d00698 100644 --- a/vendor/xorm.io/xorm/.drone.yml +++ b/vendor/xorm.io/xorm/.drone.yml @@ -3,7 +3,6 @@ kind: pipeline name: testing steps: - name: test-vet - pull: default image: golang:1.12 environment: GO111MODULE: "on" @@ -16,44 +15,69 @@ steps: - pull_request - name: test-sqlite - pull: default image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" commands: - - "go test -v -race -db=\"sqlite3\" -conn_str=\"./test.db\" -coverprofile=coverage1-1.txt -covermode=atomic" - - "go test -v -race -db=\"sqlite3\" -conn_str=\"./test.db\" -cache=true -coverprofile=coverage1-2.txt -covermode=atomic" + - make test-sqlite + - TEST_CACHE_ENABLE=true make test-sqlite when: event: - push - pull_request - name: test-mysql - pull: default image: golang:1.12 environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_MYSQL_HOST: mysql + TEST_MYSQL_CHARSET: utf8 + TEST_MYSQL_DBNAME: xorm_test + TEST_MYSQL_USERNAME: root + TEST_MYSQL_PASSWORD: + commands: + - make test-mysql + - TEST_CACHE_ENABLE=true make test-mysql + when: + event: + - push + - pull_request + +- name: test-mysql8 + image: golang:1.12 + environment: + GO111MODULE: "on" + GOPROXY: "https://goproxy.cn" + TEST_MYSQL_HOST: mysql8 + TEST_MYSQL_CHARSET: utf8mb4 + TEST_MYSQL_DBNAME: xorm_test + TEST_MYSQL_USERNAME: root + TEST_MYSQL_PASSWORD: commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test\" -coverprofile=coverage2-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test\" -cache=true -coverprofile=coverage2-2.txt -covermode=atomic" + - make test-mysql + - TEST_CACHE_ENABLE=true make test-mysql when: event: - push - pull_request - name: test-mysql-utf8mb4 - pull: default image: golang:1.12 depends_on: - - test-mysql + - test-mysql environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_MYSQL_HOST: mysql + TEST_MYSQL_CHARSET: utf8mb4 + TEST_MYSQL_DBNAME: xorm_test + TEST_MYSQL_USERNAME: root + TEST_MYSQL_PASSWORD: commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test?charset=utf8mb4\" -coverprofile=coverage2.1-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(mysql)/xorm_test?charset=utf8mb4\" -cache=true -coverprofile=coverage2.1-2.txt -covermode=atomic" + - make test-mysql + - TEST_CACHE_ENABLE=true make test-mysql when: event: - push @@ -67,9 +91,13 @@ steps: environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_MYSQL_HOST: mysql:3306 + TEST_MYSQL_DBNAME: xorm_test + TEST_MYSQL_USERNAME: root + TEST_MYSQL_PASSWORD: commands: - - "go test -v -race -db=\"mymysql\" -conn_str=\"tcp:mysql:3306*xorm_test/root/\" -coverprofile=coverage3-1.txt -covermode=atomic" - - "go test -v -race -db=\"mymysql\" -conn_str=\"tcp:mysql:3306*xorm_test/root/\" -cache=true -coverprofile=coverage3-2.txt -covermode=atomic" + - make test-mymysql + - TEST_CACHE_ENABLE=true make test-mymysql when: event: - push @@ -81,9 +109,13 @@ steps: environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_PGSQL_HOST: pgsql + TEST_PGSQL_DBNAME: xorm_test + TEST_PGSQL_USERNAME: postgres + TEST_PGSQL_PASSWORD: postgres commands: - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -coverprofile=coverage4-1.txt -covermode=atomic" - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -cache=true -coverprofile=coverage4-2.txt -covermode=atomic" + - make test-postgres + - TEST_CACHE_ENABLE=true make test-postgres when: event: - push @@ -92,12 +124,19 @@ steps: - name: test-postgres-schema pull: default image: golang:1.12 + depends_on: + - test-postgres environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_PGSQL_HOST: pgsql + TEST_PGSQL_SCHEMA: xorm + TEST_PGSQL_DBNAME: xorm_test + TEST_PGSQL_USERNAME: postgres + TEST_PGSQL_PASSWORD: postgres commands: - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -schema=xorm -coverprofile=coverage5-1.txt -covermode=atomic" - - "go test -v -race -db=\"postgres\" -conn_str=\"postgres://postgres:@pgsql/xorm_test?sslmode=disable\" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic" + - make test-postgres + - TEST_CACHE_ENABLE=true make test-postgres when: event: - push @@ -109,9 +148,13 @@ steps: environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_MSSQL_HOST: mssql + TEST_MSSQL_DBNAME: xorm_test + TEST_MSSQL_USERNAME: sa + TEST_MSSQL_PASSWORD: "yourStrong(!)Password" commands: - - "go test -v -race -db=\"mssql\" -conn_str=\"server=mssql;user id=sa;password=yourStrong(!)Password;database=xorm_test\" -coverprofile=coverage6-1.txt -covermode=atomic" - - "go test -v -race -db=\"mssql\" -conn_str=\"server=mssql;user id=sa;password=yourStrong(!)Password;database=xorm_test\" -cache=true -coverprofile=coverage6-2.txt -covermode=atomic" + - make test-mssql + - TEST_CACHE_ENABLE=true make test-mssql when: event: - push @@ -123,9 +166,13 @@ steps: environment: GO111MODULE: "on" GOPROXY: "https://goproxy.cn" + TEST_TIDB_HOST: "tidb:4000" + TEST_TIDB_DBNAME: xorm_test + TEST_TIDB_USERNAME: root + TEST_TIDB_PASSWORD: commands: - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(tidb:4000)/xorm_test\" -ignore_select_update=true -coverprofile=coverage7-1.txt -covermode=atomic" - - "go test -v -race -db=\"mysql\" -conn_str=\"root:@tcp(tidb:4000)/xorm_test\" -ignore_select_update=true -cache=true -coverprofile=coverage7-2.txt -covermode=atomic" + - make test-tidb + - TEST_CACHE_ENABLE=true make test-tidb when: event: - push @@ -141,15 +188,14 @@ steps: - test-vet - test-sqlite - test-mysql - - test-mysql-utf8mb4 + - test-mysql8 - test-mymysql - test-postgres - test-postgres-schema - test-mssql - test-tidb commands: - - go get github.com/wadey/gocovmerge - - gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage2.1-1.txt coverage2.1-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt coverage6-1.txt coverage6-2.txt coverage7-1.txt coverage7-2.txt > coverage.txt + - make coverage when: event: - push @@ -169,12 +215,25 @@ services: - tag - pull_request +- name: mysql8 + pull: default + image: mysql:8.0 + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: yes + MYSQL_DATABASE: xorm_test + when: + event: + - push + - tag + - pull_request + - name: pgsql pull: default image: postgres:9.5 environment: POSTGRES_DB: xorm_test POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres when: event: - push diff --git a/vendor/xorm.io/xorm/.gitignore b/vendor/xorm.io/xorm/.gitignore index f1757b98303d8..3629e49ac8ca9 100644 --- a/vendor/xorm.io/xorm/.gitignore +++ b/vendor/xorm.io/xorm/.gitignore @@ -7,6 +7,7 @@ # Folders _obj _test +vendor/ # Architecture specific extensions/prefixes *.[568vq] diff --git a/vendor/xorm.io/xorm/.revive.toml b/vendor/xorm.io/xorm/.revive.toml new file mode 100644 index 0000000000000..64e223bbfa597 --- /dev/null +++ b/vendor/xorm.io/xorm/.revive.toml @@ -0,0 +1,25 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 1 +warningCode = 1 + +[rule.blank-imports] +[rule.context-as-argument] +[rule.context-keys-type] +[rule.dot-imports] +[rule.error-return] +[rule.error-strings] +[rule.error-naming] +[rule.exported] +[rule.if-return] +[rule.increment-decrement] +[rule.var-naming] +[rule.var-declaration] +[rule.package-comments] +[rule.range] +[rule.receiver-naming] +[rule.time-naming] +[rule.unexported-return] +[rule.indent-error-flow] +[rule.errorf] \ No newline at end of file diff --git a/vendor/xorm.io/xorm/Makefile b/vendor/xorm.io/xorm/Makefile new file mode 100644 index 0000000000000..737ca96cc99fc --- /dev/null +++ b/vendor/xorm.io/xorm/Makefile @@ -0,0 +1,190 @@ +IMPORT := xorm.io/xorm +export GO111MODULE=on + +GO ?= go +GOFMT ?= gofmt -s +TAGS ?= +SED_INPLACE := sed -i + +GOFILES := $(shell find . -name "*.go" -type f) + +PACKAGES ?= $(shell GO111MODULE=on $(GO) list ./...) + +TEST_MSSQL_HOST ?= mssql:1433 +TEST_MSSQL_DBNAME ?= gitea +TEST_MSSQL_USERNAME ?= sa +TEST_MSSQL_PASSWORD ?= MwantsaSecurePassword1 + +TEST_MYSQL_HOST ?= mysql:3306 +TEST_MYSQL_CHARSET ?= utf8 +TEST_MYSQL_DBNAME ?= xorm_test +TEST_MYSQL_USERNAME ?= root +TEST_MYSQL_PASSWORD ?= + +TEST_PGSQL_HOST ?= pgsql:5432 +TEST_PGSQL_SCHEMA ?= +TEST_PGSQL_DBNAME ?= xorm_test +TEST_PGSQL_USERNAME ?= postgres +TEST_PGSQL_PASSWORD ?= mysecretpassword + +TEST_TIDB_HOST ?= tidb:4000 +TEST_TIDB_DBNAME ?= xorm_test +TEST_TIDB_USERNAME ?= root +TEST_TIDB_PASSWORD ?= + +TEST_CACHE_ENABLE ?= false + +.PHONY: all +all: build + +.PHONY: build +build: go-check $(GO_SOURCES) + $(GO) build + +.PHONY: clean +clean: + $(GO) clean -i ./... + rm -rf *.sql *.log test.db *coverage.out coverage.all + +.PHONY: coverage +coverage: + @hash gocovmerge > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/wadey/gocovmerge; \ + fi + gocovmerge $(shell find . -type f -name "coverage.out") > coverage.all;\ + +.PHONY: fmt +fmt: + $(GOFMT) -w $(GOFILES) + +.PHONY: fmt-check +fmt-check: + # get all go files and run go fmt on them + @diff=$$($(GOFMT) -d $(GOFILES)); \ + if [ -n "$$diff" ]; then \ + echo "Please run 'make fmt' and commit the result:"; \ + echo "$${diff}"; \ + exit 1; \ + fi; + +.PHONY: go-check +go-check: + $(eval GO_VERSION := $(shell printf "%03d%03d%03d" $(shell go version | grep -Eo '[0-9]+\.?[0-9]+?\.?[0-9]?\s' | tr '.' ' ');)) + @if [ "$(GO_VERSION)" -lt "001011000" ]; then \ + echo "Gitea requires Go 1.11.0 or greater to build. You can get it at https://golang.org/dl/"; \ + exit 1; \ + fi + +.PHONY: help +help: + @echo "Make Routines:" + @echo " - equivalent to \"build\"" + @echo " - build creates the entire project" + @echo " - clean delete integration files and build files but not css and js files" + @echo " - fmt format the code" + @echo " - lint run code linter revive" + @echo " - misspell check if a word is written wrong" + @echo " - test run default unit test" + @echo " - test-sqlite run unit test for sqlite" + @echo " - vet examines Go source code and reports suspicious constructs" + +.PHONY: lint +lint: revive + +.PHONY: revive +revive: + @hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/mgechev/revive; \ + fi + revive -config .revive.toml -exclude=./vendor/... ./... || exit 1 + +.PHONY: misspell +misspell: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -w -i unknwon $(GOFILES) + +.PHONY: misspell-check +misspell-check: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -error -i unknwon,destory $(GOFILES) + +.PHONY: test +test: test-sqlite + +.PNONY: test-mssql +test-mssql: go-check + $(GO) test -v -race -db=mssql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="server=$(TEST_MSSQL_HOST);user id=$(TEST_MSSQL_USERNAME);password=$(TEST_MSSQL_PASSWORD);database=$(TEST_MSSQL_DBNAME)" \ + -coverprofile=mssql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-mssql\#% +test-mssql\#%: go-check + $(GO) test -v -race -run $* -db=mssql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="server=$(TEST_MSSQL_HOST);user id=$(TEST_MSSQL_USERNAME);password=$(TEST_MSSQL_PASSWORD);database=$(TEST_MSSQL_DBNAME)" \ + -coverprofile=mssql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-mymysql +test-mymysql: go-check + $(GO) test -v -race -db=mymysql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="tcp:$(TEST_MYSQL_HOST)*$(TEST_MYSQL_DBNAME)/$(TEST_MYSQL_USERNAME)/$(TEST_MYSQL_PASSWORD)" \ + -coverprofile=mymysql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-mymysql\#% +test-mymysql\#%: go-check + $(GO) test -v -race -run $* -db=mymysql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="tcp:$(TEST_MYSQL_HOST)*$(TEST_MYSQL_DBNAME)/$(TEST_MYSQL_USERNAME)/$(TEST_MYSQL_PASSWORD)" \ + -coverprofile=mymysql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-mysql +test-mysql: go-check + $(GO) test -v -race -db=mysql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="$(TEST_MYSQL_USERNAME):$(TEST_MYSQL_PASSWORD)@tcp($(TEST_MYSQL_HOST))/$(TEST_MYSQL_DBNAME)?charset=$(TEST_MYSQL_CHARSET)" \ + -coverprofile=mysql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: test-mysql\#% +test-mysql\#%: go-check + $(GO) test -v -race -run $* -db=mysql -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="$(TEST_MYSQL_USERNAME):$(TEST_MYSQL_PASSWORD)@tcp($(TEST_MYSQL_HOST))/$(TEST_MYSQL_DBNAME)?charset=$(TEST_MYSQL_CHARSET)" \ + -coverprofile=mysql.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-postgres +test-postgres: go-check + $(GO) test -v -race -db=postgres -schema='$(TEST_PGSQL_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="postgres://$(TEST_PGSQL_USERNAME):$(TEST_PGSQL_PASSWORD)@$(TEST_PGSQL_HOST)/$(TEST_PGSQL_DBNAME)?sslmode=disable" \ + -coverprofile=postgres.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: test-postgres\#% +test-postgres\#%: go-check + $(GO) test -v -race -run $* -db=postgres -schema='$(TEST_PGSQL_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \ + -conn_str="postgres://$(TEST_PGSQL_USERNAME):$(TEST_PGSQL_PASSWORD)@$(TEST_PGSQL_HOST)/$(TEST_PGSQL_DBNAME)?sslmode=disable" \ + -coverprofile=postgres.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: test-sqlite +test-sqlite: go-check + $(GO) test -v -race -cache=$(TEST_CACHE_ENABLE) -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \ + -coverprofile=sqlite.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: test-sqlite\#% +test-sqlite\#%: go-check + $(GO) test -v -race -run $* -cache=$(TEST_CACHE_ENABLE) -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \ + -coverprofile=sqlite.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PNONY: test-tidb +test-tidb: go-check + $(GO) test -v -race -db=mysql -cache=$(TEST_CACHE_ENABLE) -ignore_select_update=true \ + -conn_str="$(TEST_TIDB_USERNAME):$(TEST_TIDB_PASSWORD)@tcp($(TEST_TIDB_HOST))/$(TEST_TIDB_DBNAME)" \ + -coverprofile=tidb.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: test-tidb\#% +test-tidb\#%: go-check + $(GO) test -v -race -run $* -db=mysql -cache=$(TEST_CACHE_ENABLE) -ignore_select_update=true \ + -conn_str="$(TEST_TIDB_USERNAME):$(TEST_TIDB_PASSWORD)@tcp($(TEST_TIDB_HOST))/$(TEST_TIDB_DBNAME)" \ + -coverprofile=tidb.$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic + +.PHONY: vet +vet: + $(GO) vet $(PACKAGES) \ No newline at end of file diff --git a/vendor/xorm.io/xorm/engine.go b/vendor/xorm.io/xorm/engine.go index a7e52ea48497c..99e9b584cb37f 100644 --- a/vendor/xorm.io/xorm/engine.go +++ b/vendor/xorm.io/xorm/engine.go @@ -91,11 +91,22 @@ func (engine *Engine) BufferSize(size int) *Session { } // CondDeleted returns the conditions whether a record is soft deleted. -func (engine *Engine) CondDeleted(colName string) builder.Cond { - if engine.dialect.DBType() == core.MSSQL { - return builder.IsNull{colName} +func (engine *Engine) CondDeleted(col *core.Column) builder.Cond { + var cond = builder.NewCond() + if col.SQLType.IsNumeric() { + cond = builder.Eq{col.Name: 0} + } else { + // FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. + if engine.dialect.DBType() != core.MSSQL { + cond = builder.Eq{col.Name: zeroTime1} + } } - return builder.IsNull{colName}.Or(builder.Eq{colName: zeroTime1}) + + if col.Nullable { + cond = cond.Or(builder.IsNull{col.Name}) + } + + return cond } // ShowSQL show SQL statement or not on logger if log level is great than INFO @@ -215,7 +226,7 @@ func quoteTo(buf *strings.Builder, quotePair string, value string) { _, _ = buf.WriteString(value) return } - + prefix, suffix := quotePair[0], quotePair[1] i := 0 @@ -838,7 +849,7 @@ func (engine *Engine) Having(conditions string) *Session { return session.Having(conditions) } -// UnMapType removes the datbase mapper of a type +// UnMapType removes the database mapper of a type func (engine *Engine) UnMapType(t reflect.Type) { engine.mutex.Lock() defer engine.mutex.Unlock() @@ -921,7 +932,7 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) { t := v.Type() table := core.NewEmptyTable() table.Type = t - table.Name = engine.tbNameForMap(v) + table.Name = getTableName(engine.TableMapper, v) var idFieldColName string var hasCacheTag, hasNoCacheTag bool @@ -987,11 +998,11 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) { pStart := strings.Index(k, "(") if pStart == 0 { - return nil, errors.New("( could not be the first charactor") + return nil, errors.New("( could not be the first character") } if pStart > -1 { if !strings.HasSuffix(k, ")") { - return nil, fmt.Errorf("field %s tag %s cannot match ) charactor", col.FieldName, key) + return nil, fmt.Errorf("field %s tag %s cannot match ) character", col.FieldName, key) } ctx.tagName = k[:pStart] @@ -1621,7 +1632,7 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{} v = s[11:19] case core.Date: v = t.Format("2006-01-02") - case core.DateTime, core.TimeStamp: + case core.DateTime, core.TimeStamp, core.Varchar: // !DarthPestilane! format time when sqlTypeName is core.Varchar. v = t.Format("2006-01-02 15:04:05") case core.TimeStampz: if engine.dialect.DBType() == core.MSSQL { diff --git a/vendor/xorm.io/xorm/engine_cond.go b/vendor/xorm.io/xorm/engine_cond.go index 702ac8043402e..17f50bd7df188 100644 --- a/vendor/xorm.io/xorm/engine_cond.go +++ b/vendor/xorm.io/xorm/engine_cond.go @@ -58,7 +58,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{}, } if col.IsDeleted && !unscoped { // tag "deleted" is enabled - conds = append(conds, engine.CondDeleted(colName)) + conds = append(conds, engine.CondDeleted(col)) } fieldValue := *fieldValuePtr diff --git a/vendor/xorm.io/xorm/engine_table.go b/vendor/xorm.io/xorm/engine_table.go index eb5aa850af64e..001d72e7e31d7 100644 --- a/vendor/xorm.io/xorm/engine_table.go +++ b/vendor/xorm.io/xorm/engine_table.go @@ -25,13 +25,21 @@ func (engine *Engine) tbNameWithSchema(v string) string { return v } +func isSubQuery(tbName string) bool { + const selStr = "select" + if len(tbName) <= len(selStr)+1 { + return false + } + + return strings.EqualFold(tbName[:len(selStr)], selStr) || strings.EqualFold(tbName[:len(selStr)+1], "("+selStr) +} + // TableName returns table name with schema prefix if has func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string { tbName := engine.tbNameNoSchema(bean) - if len(includeSchema) > 0 && includeSchema[0] { + if len(includeSchema) > 0 && includeSchema[0] && !isSubQuery(tbName) { tbName = engine.tbNameWithSchema(tbName) } - return tbName } @@ -44,20 +52,6 @@ func (session *Session) tbNameNoSchema(table *core.Table) string { return table.Name } -func (engine *Engine) tbNameForMap(v reflect.Value) string { - if v.Type().Implements(tpTableName) { - return v.Interface().(TableName).TableName() - } - if v.Kind() == reflect.Ptr { - v = v.Elem() - if v.Type().Implements(tpTableName) { - return v.Interface().(TableName).TableName() - } - } - - return engine.TableMapper.Obj2Table(v.Type().Name()) -} - func (engine *Engine) tbNameNoSchema(tablename interface{}) string { switch tablename.(type) { case []string: @@ -82,7 +76,7 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string { v := rValue(f) t := v.Type() if t.Kind() == reflect.Struct { - table = engine.tbNameForMap(v) + table = getTableName(engine.TableMapper, v) } else { table = engine.Quote(fmt.Sprintf("%v", f)) } @@ -100,12 +94,12 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string { return tablename.(string) case reflect.Value: v := tablename.(reflect.Value) - return engine.tbNameForMap(v) + return getTableName(engine.TableMapper, v) default: v := rValue(tablename) t := v.Type() if t.Kind() == reflect.Struct { - return engine.tbNameForMap(v) + return getTableName(engine.TableMapper, v) } return engine.Quote(fmt.Sprintf("%v", tablename)) } diff --git a/vendor/xorm.io/xorm/error.go b/vendor/xorm.io/xorm/error.go index a67527acdab12..2e9cbfaaf7566 100644 --- a/vendor/xorm.io/xorm/error.go +++ b/vendor/xorm.io/xorm/error.go @@ -27,7 +27,7 @@ var ( // ErrConditionType condition type unsupported ErrConditionType = errors.New("Unsupported condition type") // ErrUnSupportedSQLType parameter of SQL is not supported - ErrUnSupportedSQLType = errors.New("unsupported sql type") + ErrUnSupportedSQLType = errors.New("Unsupported sql type") ) // ErrFieldIsNotExist columns does not exist diff --git a/vendor/xorm.io/xorm/helpers.go b/vendor/xorm.io/xorm/helpers.go index a31e922c0bc8c..aadeb6dc08d2a 100644 --- a/vendor/xorm.io/xorm/helpers.go +++ b/vendor/xorm.io/xorm/helpers.go @@ -155,6 +155,17 @@ func isZero(k interface{}) bool { return false } +func isZeroValue(v reflect.Value) bool { + if isZero(v.Interface()) { + return true + } + switch v.Kind() { + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + return v.IsNil() + } + return false +} + func isStructZero(v reflect.Value) bool { if !v.IsValid() { return true diff --git a/vendor/xorm.io/xorm/interface.go b/vendor/xorm.io/xorm/interface.go index a564db12608fe..81a4b68a31e8c 100644 --- a/vendor/xorm.io/xorm/interface.go +++ b/vendor/xorm.io/xorm/interface.go @@ -92,6 +92,7 @@ type EngineInterface interface { Quote(string) string SetCacher(string, core.Cacher) SetConnMaxLifetime(time.Duration) + SetColumnMapper(core.IMapper) SetDefaultCacher(core.Cacher) SetLogger(logger core.ILogger) SetLogLevel(core.LogLevel) @@ -99,6 +100,7 @@ type EngineInterface interface { SetMaxOpenConns(int) SetMaxIdleConns(int) SetSchema(string) + SetTableMapper(core.IMapper) SetTZDatabase(tz *time.Location) SetTZLocation(tz *time.Location) ShowExecTime(...bool) diff --git a/vendor/xorm.io/xorm/session_delete.go b/vendor/xorm.io/xorm/session_delete.go index 675d4d8c7d56e..7b0a06416ea47 100644 --- a/vendor/xorm.io/xorm/session_delete.go +++ b/vendor/xorm.io/xorm/session_delete.go @@ -101,7 +101,8 @@ func (session *Session) Delete(bean interface{}) (int64, error) { if err != nil { return 0, err } - if len(condSQL) == 0 && session.statement.LimitN == 0 { + pLimitN := session.statement.LimitN + if len(condSQL) == 0 && (pLimitN == nil || *pLimitN == 0) { return 0, ErrNeedDeletedCond } @@ -119,8 +120,9 @@ func (session *Session) Delete(bean interface{}) (int64, error) { if len(session.statement.OrderStr) > 0 { orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr) } - if session.statement.LimitN > 0 { - orderSQL += fmt.Sprintf(" LIMIT %d", session.statement.LimitN) + if pLimitN != nil && *pLimitN > 0 { + limitNValue := *pLimitN + orderSQL += fmt.Sprintf(" LIMIT %d", limitNValue) } if len(orderSQL) > 0 { @@ -139,7 +141,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } else { deleteSQL += " WHERE " + inSQL } - // TODO: how to handle delete limit on mssql? + // TODO: how to handle delete limit on mssql? case core.MSSQL: return 0, ErrNotImplemented default: @@ -180,7 +182,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } else { realSQL += " WHERE " + inSQL } - // TODO: how to handle delete limit on mssql? + // TODO: how to handle delete limit on mssql? case core.MSSQL: return 0, ErrNotImplemented default: diff --git a/vendor/xorm.io/xorm/session_exist.go b/vendor/xorm.io/xorm/session_exist.go index 660cc47e42580..bce2758d88a39 100644 --- a/vendor/xorm.io/xorm/session_exist.go +++ b/vendor/xorm.io/xorm/session_exist.go @@ -25,8 +25,8 @@ func (session *Session) Exist(bean ...interface{}) (bool, error) { var sqlStr string var args []interface{} + var joinStr string var err error - if session.statement.RawSQL == "" { if len(bean) == 0 { tableName := session.statement.TableName() @@ -35,6 +35,9 @@ func (session *Session) Exist(bean ...interface{}) (bool, error) { } tableName = session.statement.Engine.Quote(tableName) + if len(session.statement.JoinStr) > 0 { + joinStr = session.statement.JoinStr + } if session.statement.cond.IsValid() { condSQL, condArgs, err := builder.ToSQL(session.statement.cond) @@ -43,20 +46,20 @@ func (session *Session) Exist(bean ...interface{}) (bool, error) { } if session.engine.dialect.DBType() == core.MSSQL { - sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s WHERE %s", tableName, condSQL) + sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s WHERE %s", tableName, joinStr, condSQL) } else if session.engine.dialect.DBType() == core.ORACLE { - sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) AND ROWNUM=1", tableName, condSQL) + sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) %s AND ROWNUM=1", tableName, joinStr, condSQL) } else { - sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT 1", tableName, condSQL) + sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE %s LIMIT 1", tableName, joinStr, condSQL) } args = condArgs } else { if session.engine.dialect.DBType() == core.MSSQL { - sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s", tableName) + sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s", tableName, joinStr) } else if session.engine.dialect.DBType() == core.ORACLE { - sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE ROWNUM=1", tableName) + sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE ROWNUM=1", tableName, joinStr) } else { - sqlStr = fmt.Sprintf("SELECT * FROM %s LIMIT 1", tableName) + sqlStr = fmt.Sprintf("SELECT * FROM %s %s LIMIT 1", tableName, joinStr) } args = []interface{}{} } diff --git a/vendor/xorm.io/xorm/session_find.go b/vendor/xorm.io/xorm/session_find.go index e16ae54c94083..c7043ea653320 100644 --- a/vendor/xorm.io/xorm/session_find.go +++ b/vendor/xorm.io/xorm/session_find.go @@ -121,7 +121,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) colName = session.engine.Quote(nm) + "." + colName } - autoCond = session.engine.CondDeleted(colName) + autoCond = session.engine.CondDeleted(col) } } } @@ -396,7 +396,21 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in return err } bean := cacher.GetBean(tableName, sid) - if bean == nil || reflect.ValueOf(bean).Elem().Type() != t { + + // fix issue #894 + isHit := func() (ht bool) { + if bean == nil { + ht = false + return + } + ckb := reflect.ValueOf(bean).Elem().Type() + ht = ckb == t + if !ht && t.Kind() == reflect.Ptr { + ht = t.Elem() == ckb + } + return + } + if !isHit() { ides = append(ides, id) ididxes[sid] = idx } else { diff --git a/vendor/xorm.io/xorm/session_insert.go b/vendor/xorm.io/xorm/session_insert.go index 5f8f7e1ee8877..fb67db169a48a 100644 --- a/vendor/xorm.io/xorm/session_insert.go +++ b/vendor/xorm.io/xorm/session_insert.go @@ -16,6 +16,9 @@ import ( "xorm.io/core" ) +// ErrNoElementsOnSlice represents an error there is no element when insert +var ErrNoElementsOnSlice = errors.New("No element on slice when insert") + // Insert insert one or more beans func (session *Session) Insert(beans ...interface{}) (int64, error) { var affected int64 @@ -67,21 +70,23 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) { sliceValue := reflect.Indirect(reflect.ValueOf(bean)) if sliceValue.Kind() == reflect.Slice { size := sliceValue.Len() - if size > 0 { - if session.engine.SupportInsertMany() { - cnt, err := session.innerInsertMulti(bean) + if size <= 0 { + return 0, ErrNoElementsOnSlice + } + + if session.engine.SupportInsertMany() { + cnt, err := session.innerInsertMulti(bean) + if err != nil { + return affected, err + } + affected += cnt + } else { + for i := 0; i < size; i++ { + cnt, err := session.innerInsert(sliceValue.Index(i).Interface()) if err != nil { return affected, err } affected += cnt - } else { - for i := 0; i < size; i++ { - cnt, err := session.innerInsert(sliceValue.Index(i).Interface()) - if err != nil { - return affected, err - } - affected += cnt - } } } } else { @@ -674,7 +679,7 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac // !evalphobia! set fieldValue as nil when column is nullable and zero-value if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok { - if col.Nullable && isZero(fieldValue.Interface()) { + if col.Nullable && isZeroValue(fieldValue) { var nilValue *int fieldValue = reflect.ValueOf(nilValue) } diff --git a/vendor/xorm.io/xorm/session_iterate.go b/vendor/xorm.io/xorm/session_iterate.go index ca996c2884d04..4a3cc0833f067 100644 --- a/vendor/xorm.io/xorm/session_iterate.go +++ b/vendor/xorm.io/xorm/session_iterate.go @@ -4,7 +4,9 @@ package xorm -import "reflect" +import ( + "reflect" +) // IterFunc only use by Iterate type IterFunc func(idx int, bean interface{}) error @@ -60,22 +62,23 @@ func (session *Session) BufferSize(size int) *Session { } func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error { - if session.isAutoClose { - defer session.Close() - } - var bufferSize = session.statement.bufferSize - var limit = session.statement.LimitN - if limit > 0 && bufferSize > limit { - bufferSize = limit + var pLimitN = session.statement.LimitN + if pLimitN != nil && bufferSize > *pLimitN { + bufferSize = *pLimitN } var start = session.statement.Start v := rValue(bean) sliceType := reflect.SliceOf(v.Type()) var idx = 0 - for { + session.autoResetStatement = false + defer func() { + session.autoResetStatement = true + }() + + for bufferSize > 0 { slice := reflect.New(sliceType) - if err := session.Limit(bufferSize, start).find(slice.Interface(), bean); err != nil { + if err := session.NoCache().Limit(bufferSize, start).find(slice.Interface(), bean); err != nil { return err } @@ -86,13 +89,13 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error { idx++ } - start = start + slice.Elem().Len() - if limit > 0 && idx+bufferSize > limit { - bufferSize = limit - idx + if bufferSize > slice.Elem().Len() { + break } - if bufferSize <= 0 || slice.Elem().Len() < bufferSize || idx == limit { - break + start = start + slice.Elem().Len() + if pLimitN != nil && start+bufferSize > *pLimitN { + bufferSize = *pLimitN - start } } diff --git a/vendor/xorm.io/xorm/session_schema.go b/vendor/xorm.io/xorm/session_schema.go index 5e576c29aa103..d4c71374d74ec 100644 --- a/vendor/xorm.io/xorm/session_schema.go +++ b/vendor/xorm.io/xorm/session_schema.go @@ -346,10 +346,12 @@ func (session *Session) Sync2(beans ...interface{}) error { } if col.Default != oriCol.Default { - if (col.SQLType.Name == core.Bool || col.SQLType.Name == core.Boolean) && + switch { + case col.IsAutoIncrement: // For autoincrement column, don't check default + case (col.SQLType.Name == core.Bool || col.SQLType.Name == core.Boolean) && ((strings.EqualFold(col.Default, "true") && oriCol.Default == "1") || - (strings.EqualFold(col.Default, "false") && oriCol.Default == "0")) { - } else { + (strings.EqualFold(col.Default, "false") && oriCol.Default == "0")): + default: engine.logger.Warnf("Table %s Column %s db default is %s, struct default is %s", tbName, col.Name, oriCol.Default, col.Default) } diff --git a/vendor/xorm.io/xorm/session_update.go b/vendor/xorm.io/xorm/session_update.go index 47ced66d1957a..ceaade46e8e10 100644 --- a/vendor/xorm.io/xorm/session_update.go +++ b/vendor/xorm.io/xorm/session_update.go @@ -287,7 +287,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if !condBeanIsStruct && table != nil { if col := table.DeletedColumn(); col != nil && !session.statement.unscoped { // tag "deleted" is enabled - autoCond1 := session.engine.CondDeleted(session.engine.Quote(col.Name)) + autoCond1 := session.engine.CondDeleted(col) if autoCond == nil { autoCond = autoCond1 @@ -300,21 +300,25 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 st := &session.statement - var sqlStr string - var condArgs []interface{} - var condSQL string - cond := session.statement.cond.And(autoCond) + var ( + sqlStr string + condArgs []interface{} + condSQL string + cond = session.statement.cond.And(autoCond) - var doIncVer = (table != nil && table.Version != "" && session.statement.checkVersion) - var verValue *reflect.Value + doIncVer = isStruct && (table != nil && table.Version != "" && session.statement.checkVersion) + verValue *reflect.Value + ) if doIncVer { verValue, err = table.VersionColumn().ValueOf(bean) if err != nil { return 0, err } - cond = cond.And(builder.Eq{session.engine.Quote(table.Version): verValue.Interface()}) - colNames = append(colNames, session.engine.Quote(table.Version)+" = "+session.engine.Quote(table.Version)+" + 1") + if verValue != nil { + cond = cond.And(builder.Eq{session.engine.Quote(table.Version): verValue.Interface()}) + colNames = append(colNames, session.engine.Quote(table.Version)+" = "+session.engine.Quote(table.Version)+" + 1") + } } condSQL, condArgs, err = builder.ToSQL(cond) @@ -333,11 +337,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 var tableName = session.statement.TableName() // TODO: Oracle support needed var top string - if st.LimitN > 0 { + if st.LimitN != nil { + limitValue := *st.LimitN if st.Engine.dialect.DBType() == core.MYSQL { - condSQL = condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) + condSQL = condSQL + fmt.Sprintf(" LIMIT %d", limitValue) } else if st.Engine.dialect.DBType() == core.SQLITE { - tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) + tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue) cond = cond.And(builder.Expr(fmt.Sprintf("rowid IN (SELECT rowid FROM %v %v)", session.engine.Quote(tableName), tempCondSQL), condArgs...)) condSQL, condArgs, err = builder.ToSQL(cond) @@ -348,7 +353,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 condSQL = "WHERE " + condSQL } } else if st.Engine.dialect.DBType() == core.POSTGRES { - tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", st.LimitN) + tempCondSQL := condSQL + fmt.Sprintf(" LIMIT %d", limitValue) cond = cond.And(builder.Expr(fmt.Sprintf("CTID IN (SELECT CTID FROM %v %v)", session.engine.Quote(tableName), tempCondSQL), condArgs...)) condSQL, condArgs, err = builder.ToSQL(cond) @@ -363,7 +368,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL && table != nil && len(table.PrimaryKeys) == 1 { cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)", - table.PrimaryKeys[0], st.LimitN, table.PrimaryKeys[0], + table.PrimaryKeys[0], limitValue, table.PrimaryKeys[0], session.engine.Quote(tableName), condSQL), condArgs...) condSQL, condArgs, err = builder.ToSQL(cond) @@ -374,7 +379,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 condSQL = "WHERE " + condSQL } } else { - top = fmt.Sprintf("TOP (%d) ", st.LimitN) + top = fmt.Sprintf("TOP (%d) ", limitValue) } } } @@ -512,7 +517,7 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac // !evalphobia! set fieldValue as nil when column is nullable and zero-value if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok { - if col.Nullable && isZero(fieldValue.Interface()) { + if col.Nullable && isZeroValue(fieldValue) { var nilValue *int fieldValue = reflect.ValueOf(nilValue) } diff --git a/vendor/xorm.io/xorm/statement.go b/vendor/xorm.io/xorm/statement.go index 67e352136f60a..ea3ecabeaa2a7 100644 --- a/vendor/xorm.io/xorm/statement.go +++ b/vendor/xorm.io/xorm/statement.go @@ -20,7 +20,7 @@ type Statement struct { RefTable *core.Table Engine *Engine Start int - LimitN int + LimitN *int idParam *core.PK OrderStr string JoinStr string @@ -65,7 +65,7 @@ type Statement struct { func (statement *Statement) Init() { statement.RefTable = nil statement.Start = 0 - statement.LimitN = 0 + statement.LimitN = nil statement.OrderStr = "" statement.UseCascade = true statement.JoinStr = "" @@ -247,7 +247,7 @@ func (statement *Statement) buildUpdates(bean interface{}, if !includeVersion && col.IsVersion { continue } - if col.IsCreated { + if col.IsCreated && !columnMap.contain(col.Name) { continue } if !includeUpdated && col.IsUpdated { @@ -671,7 +671,7 @@ func (statement *Statement) Top(limit int) *Statement { // Limit generate LIMIT start, limit statement func (statement *Statement) Limit(limit int, start ...int) *Statement { - statement.LimitN = limit + statement.LimitN = &limit if len(start) > 0 { statement.Start = start[0] } @@ -1071,9 +1071,11 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr) } + pLimitN := statement.LimitN if dialect.DBType() == core.MSSQL { - if statement.LimitN > 0 { - top = fmt.Sprintf("TOP %d ", statement.LimitN) + if pLimitN != nil { + LimitNValue := *pLimitN + top = fmt.Sprintf("TOP %d ", LimitNValue) } if statement.Start > 0 { var column string @@ -1134,12 +1136,16 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n if needLimit { if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE { if statement.Start > 0 { - fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", statement.LimitN, statement.Start) - } else if statement.LimitN > 0 { - fmt.Fprint(&buf, " LIMIT ", statement.LimitN) + if pLimitN != nil { + fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start) + } else { + fmt.Fprintf(&buf, "LIMIT 0 OFFSET %v", statement.Start) + } + } else if pLimitN != nil { + fmt.Fprint(&buf, " LIMIT ", *pLimitN) } } else if dialect.DBType() == core.ORACLE { - if statement.Start != 0 || statement.LimitN != 0 { + if statement.Start != 0 || pLimitN != nil { oldString := buf.String() buf.Reset() rawColStr := columnStr @@ -1147,7 +1153,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n rawColStr = "at.*" } fmt.Fprintf(&buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", - columnStr, rawColStr, oldString, statement.Start+statement.LimitN, statement.Start) + columnStr, rawColStr, oldString, statement.Start+*pLimitN, statement.Start) } } } @@ -1204,8 +1210,9 @@ func (statement *Statement) convertIDSQL(sqlStr string) string { } var top string - if statement.LimitN > 0 && statement.Engine.dialect.DBType() == core.MSSQL { - top = fmt.Sprintf("TOP %d ", statement.LimitN) + pLimitN := statement.LimitN + if pLimitN != nil && statement.Engine.dialect.DBType() == core.MSSQL { + top = fmt.Sprintf("TOP %d ", *pLimitN) } newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1]) diff --git a/vendor/xorm.io/xorm/table_name.go b/vendor/xorm.io/xorm/table_name.go new file mode 100644 index 0000000000000..632c2879e141d --- /dev/null +++ b/vendor/xorm.io/xorm/table_name.go @@ -0,0 +1,31 @@ +// Copyright 2020 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 ( + "reflect" + + "xorm.io/core" +) + +func getTableName(mapper core.IMapper, v reflect.Value) string { + if t, ok := v.Interface().(TableName); ok { + return t.TableName() + } + if v.Type().Implements(tpTableName) { + return v.Interface().(TableName).TableName() + } + if v.Kind() == reflect.Ptr { + v = v.Elem() + if t, ok := v.Interface().(TableName); ok { + return t.TableName() + } + if v.Type().Implements(tpTableName) { + return v.Interface().(TableName).TableName() + } + } + + return mapper.Obj2Table(v.Type().Name()) +} diff --git a/vendor/xorm.io/xorm/tag.go b/vendor/xorm.io/xorm/tag.go index ec8d5cf05bf3c..2add6485cfcd4 100644 --- a/vendor/xorm.io/xorm/tag.go +++ b/vendor/xorm.io/xorm/tag.go @@ -81,7 +81,7 @@ func OnlyToDBTagHandler(ctx *tagContext) error { return nil } -// PKTagHandler decribes primary key tag handler +// PKTagHandler describes primary key tag handler func PKTagHandler(ctx *tagContext) error { ctx.col.IsPrimaryKey = true ctx.col.Nullable = false diff --git a/vendor/xorm.io/xorm/test_mssql.sh b/vendor/xorm.io/xorm/test_mssql.sh deleted file mode 100644 index 7f060cff32cda..0000000000000 --- a/vendor/xorm.io/xorm/test_mssql.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_mssql_cache.sh b/vendor/xorm.io/xorm/test_mssql_cache.sh deleted file mode 100644 index 76efd6ca0a518..0000000000000 --- a/vendor/xorm.io/xorm/test_mssql_cache.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mssql -conn_str="server=192.168.1.58;user id=sa;password=123456;database=xorm_test" -cache=true \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_mymysql.sh b/vendor/xorm.io/xorm/test_mymysql.sh deleted file mode 100644 index f7780d14fad3d..0000000000000 --- a/vendor/xorm.io/xorm/test_mymysql.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mymysql -conn_str="xorm_test/root/" \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_mymysql_cache.sh b/vendor/xorm.io/xorm/test_mymysql_cache.sh deleted file mode 100644 index 0100286d6585c..0000000000000 --- a/vendor/xorm.io/xorm/test_mymysql_cache.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mymysql -conn_str="xorm_test/root/" -cache=true \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_mysql.sh b/vendor/xorm.io/xorm/test_mysql.sh deleted file mode 100644 index 650e4ee170125..0000000000000 --- a/vendor/xorm.io/xorm/test_mysql.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mysql -conn_str="root:@/xorm_test" \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_mysql_cache.sh b/vendor/xorm.io/xorm/test_mysql_cache.sh deleted file mode 100644 index c542e735946c6..0000000000000 --- a/vendor/xorm.io/xorm/test_mysql_cache.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mysql -conn_str="root:@/xorm_test" -cache=true \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_postgres.sh b/vendor/xorm.io/xorm/test_postgres.sh deleted file mode 100644 index dc1152e0a667d..0000000000000 --- a/vendor/xorm.io/xorm/test_postgres.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=postgres -conn_str="dbname=xorm_test sslmode=disable" \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_postgres_cache.sh b/vendor/xorm.io/xorm/test_postgres_cache.sh deleted file mode 100644 index 462fc948cb75a..0000000000000 --- a/vendor/xorm.io/xorm/test_postgres_cache.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=postgres -conn_str="dbname=xorm_test sslmode=disable" -cache=true \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_sqlite.sh b/vendor/xorm.io/xorm/test_sqlite.sh deleted file mode 100644 index 6352b5cb5f30a..0000000000000 --- a/vendor/xorm.io/xorm/test_sqlite.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_sqlite_cache.sh b/vendor/xorm.io/xorm/test_sqlite_cache.sh deleted file mode 100644 index 75a054c3f17dd..0000000000000 --- a/vendor/xorm.io/xorm/test_sqlite_cache.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" -cache=true \ No newline at end of file diff --git a/vendor/xorm.io/xorm/test_tidb.sh b/vendor/xorm.io/xorm/test_tidb.sh deleted file mode 100644 index 03d2d6cd82b1f..0000000000000 --- a/vendor/xorm.io/xorm/test_tidb.sh +++ /dev/null @@ -1 +0,0 @@ -go test -db=mysql -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true \ No newline at end of file