Compare commits

..

25 Commits

Author SHA1 Message Date
DacongDA 5ed154f8c5 Feat: support modify the varchar type column of postgresql database in sync function (#2414)
I'm not sure why the previous version did not support modifying the varchar length in Postgresql during sync. Perhaps it was considering compatibility issues?
As nobody reply me in #2408 I decide to make a pr to solve

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2414
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: DacongDA <dacongda1@sina.com>
Co-committed-by: DacongDA <dacongda1@sina.com>
2025-07-21 19:12:39 +00:00
maktub 8f5e437a61 fix: correct nullable field detection logic in column metadata (#2520)
fix: correct nullable field detection logic in column metadata

gbase8s metadata: N  == NOT NULL

Co-authored-by: zhangyunfei <zhangyunfei@gbase.cn>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2520
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: maktub <maktub@noreply.gitea.com>
Co-committed-by: maktub <maktub@noreply.gitea.com>
2025-07-21 18:24:41 +00:00
Lunny Xiao b72e98f60e Fix bug when json with a real SQLType in the tag (#2519)
Fix a bug when the tag have both `longtext json`. It should be `longtext` column type with JSON support.

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2519
2025-07-21 18:19:39 +00:00
maktub 89d1238248 feat: Support GBase8s Database (#2517)
## Background

Currently, XORM does not yet support the [GBase 8s database by Nanda General Data Technology]. GBase 8s is a widely used domestic database in China, commonly adopted by enterprises and government systems as a backend storage solution.

## Summary of This Contribution
	•	Added support for the gbase8s driver
	•	Implemented a basic database dialect: GBase8sDialect

## Usage Instructions

```go
import	_ "gitee.com/GBase8s/go-gci""

engine, err := xorm.NewEngine("gbase8s", "gbase8s://username:pwd@ip:port/dbname?param=xxx")

Co-authored-by: zhangyunfei <zhangyunfei@gbase.cn>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2517
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: maktub <maktub@noreply.gitea.com>
Co-committed-by: maktub <maktub@noreply.gitea.com>
2025-07-18 02:59:03 +00:00
Kos f50aacd38b fix oracle date field insert (#2420)
for #2419

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2420
Co-authored-by: Kos <kos@noreply.gitea.com>
Co-committed-by: Kos <kos@noreply.gitea.com>
2025-05-26 02:54:40 +00:00
Lunny Xiao 3b3f99f8a1 Add security scan (#2454)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2454
2025-03-14 17:04:18 +00:00
Lunny Xiao cc4be22330
Disable cockroach tempororily 2025-03-12 11:07:52 -07:00
Lunny Xiao cd0ca0bbc6
Update badges on README 2025-03-12 11:02:35 -07:00
cenxiao 844543c7da fix drop index SQL for Oracle (#2469)
Adjust drop index SQL to work with Oracle Autonomous DB

Issue:
DROP INDEX IDX_casdoor_permission_rule_PTYPE ON casdoor_permission_rule
Error report -
ORA-00933: SQL command not properly ended

Expected:
DROP INDEX IDX_casdoor_permission_rule_PTYPE

Co-authored-by: Cenxiao Zhao <cenxiao@projectboard.world>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2469
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: cenxiao <cenxiao@noreply.gitea.com>
Co-committed-by: cenxiao <cenxiao@noreply.gitea.com>
2025-02-28 06:26:20 +00:00
techknowlogick 7654b7b749 add libsql as a valid driver type (#2481)
support the libsql sqlite driver library https://github.com/tursodatabase/go-libsql

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2481
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@noreply.gitea.com>
Co-committed-by: techknowlogick <techknowlogick@noreply.gitea.com>
2024-12-06 17:26:39 +00:00
Lunny Xiao d47f35b260 Branch name change (#2459)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2459
2024-05-15 08:45:25 +00:00
Lunny Xiao cbbd1f09e1 Add more tests for extends (#2443)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2443
2024-05-09 12:47:01 +00:00
lijunshi c6d05fa553 fix: Sync2 will remove AUTO_INCREMENT unexpectly (#2444) (#2445)
fix #2444

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2445
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: lijunshi <lijunshi2015@163.com>
Co-committed-by: lijunshi <lijunshi2015@163.com>
2024-04-28 16:12:17 +00:00
Dacian S 3bc2ea24f6 suwro (#2453)
please release v1.3.10

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Dacian Stanciu <dacian.stanciu@just.ro>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2453
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Dacian S <suwro@noreply.gitea.com>
Co-committed-by: Dacian S <suwro@noreply.gitea.com>
2024-04-27 14:14:15 +00:00
Dacian Stanciu 4e74c80b67 fix: Legacy MsSQL - legacy offset select sql command bug #2446 (#2448)
fix for MsSQL legacy bug

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2448
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Dacian Stanciu <daci28@yahoo.com>
Co-committed-by: Dacian Stanciu <daci28@yahoo.com>
2024-04-24 13:47:05 +00:00
Lunny Xiao 8094e98a8f Fix ci (#2449)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2449
2024-04-24 11:37:00 +00:00
tylerthail2019 589acfff86 Add includeNil requiredField Check when custom struct field IsZero in Update method. (#2438)
Add includeNil requiredField Check when custom struct field IsZero in Update method.

Co-authored-by: tyler <tyler@mbp.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2438
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: tylerthail2019 <tylerthail2019@noreply.gitea.com>
Co-committed-by: tylerthail2019 <tylerthail2019@noreply.gitea.com>
2024-04-02 04:26:41 +00:00
tylerthail2019 34e62e9b4c add IsZero check for custom struct column in the Update method (#2417)
Co-authored-by: tyler <tyler@mbp.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2417
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: tylerthail2019 <tylerthail2019@noreply.gitea.com>
Co-committed-by: tylerthail2019 <tylerthail2019@noreply.gitea.com>
2024-03-18 06:43:33 +00:00
appleboy be77a7084b refactor: ensure non-null values in migration table creation (#2434)
- Change the migration table creation to not allow null values for the column

fix https://gitea.com/xorm/xorm/issues/2433

Signed-off-by: appleboy <appleboy.tw@gmail.com>

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2434
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: appleboy <appleboy.tw@gmail.com>
Co-committed-by: appleboy <appleboy.tw@gmail.com>
2024-03-17 03:53:16 +00:00
Lunny Xiao 7a535a7899 Update ci (#2432)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2432
2024-03-15 14:11:47 +00:00
Lunny Xiao 2e2a66c640 Support not drop index when sync (#2429)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2429
2024-03-15 03:13:09 +00:00
CyJaySong 63222312b2 Fix the issue of incorrect insertion of data in non UTC time zone zero for numeric types (#2413)
Fix the issue of incorrect insertion of data in non-UTC time zone zero for numeric types

Co-authored-by: CyJay <cyjay@MacBook-Pro.lan>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2413
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: CyJaySong <CyJaySong@gmail.com>
Co-committed-by: CyJaySong <CyJaySong@gmail.com>
2024-02-25 16:16:56 +00:00
Lunny Xiao 0c9a2f6a70 Fix migrate schema bug (#2407)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2407
2024-02-08 08:45:23 +00:00
Kos 11e96d9654 Update internal/statements/legacy_select.go (#2400)
fix #2399, When Oracle enables USE_LEGACY_LIMIT_OFFSET, the Where condition fails

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2400
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Kos <kos@noreply.gitea.com>
Co-committed-by: Kos <kos@noreply.gitea.com>
2024-02-05 11:13:17 +00:00
zzdboy 5301cc0ae9 fix: dm Database Clob field conversion error (#2405)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2405
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: zzdboy <28206697@qq.com>
Co-committed-by: zzdboy <28206697@qq.com>
2024-02-04 09:06:47 +00:00
46 changed files with 1865 additions and 418 deletions

View File

@ -9,13 +9,13 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: setup go
uses: actions/setup-go@v5
with:
go-version: '>=1.20.1'
go-version-file: 'go.mod'
- name: Use Go Action
id: use-go-action
uses: https://gitea.com/actions/release-action@main

View File

@ -1,42 +1,21 @@
name: test cockroach
on:
on:
push:
branches:
- master
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
- donttrigger # disabled for now
#- main
#- v1
#pull_request:
jobs:
test-cockroach:
name: test cockroach
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test cockroach
env:
TEST_COCKROACH_HOST: "cockroach:26257"
@ -49,8 +28,6 @@ jobs:
services:
cockroach:
image: cockroachdb/cockroach:v19.2.4
ports:
- 26257:26257
cmd:
- 'start'
- '--insecure'

View File

@ -2,41 +2,19 @@ name: test mariadb
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
lint:
name: test mariadb
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test mariadb
env:
TEST_MYSQL_HOST: mariadb
@ -51,6 +29,4 @@ jobs:
image: mariadb:10.4
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: xorm_test
ports:
- 3306:3306
MYSQL_DATABASE: xorm_test

View File

@ -2,42 +2,19 @@ name: test mssql
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
RUNNER_TOOL_CACHE: /toolcache # specify with your cache path
jobs:
test-mssql-collation:
name: test mssql with collation
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test mssql with collation
env:
TEST_MSSQL_HOST: mssql2
@ -53,6 +30,4 @@ jobs:
env:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
MSSQL_PID: Standard
ports:
- 1433:1433
MSSQL_PID: Standard

View File

@ -2,41 +2,19 @@ name: test mssql
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
test-mssql:
name: test mssql
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test mssql
env:
TEST_MSSQL_HOST: mssql
@ -51,6 +29,4 @@ jobs:
env:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
MSSQL_PID: Standard
ports:
- 1433:1433
MSSQL_PID: Standard

View File

@ -2,41 +2,19 @@ name: test mysql
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
test-mysql:
name: test mysql
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test mysql utf8mb4
env:
TEST_MYSQL_HOST: mysql
@ -51,6 +29,4 @@ jobs:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: xorm_test
ports:
- 3306:3306
MYSQL_DATABASE: xorm_test

View File

@ -2,41 +2,19 @@ name: test mysql8
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
lint:
name: test mysql8
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test mysql8
env:
TEST_MYSQL_HOST: mysql8
@ -51,6 +29,4 @@ jobs:
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: xorm_test
ports:
- 3306:3306
MYSQL_DATABASE: xorm_test

View File

@ -2,41 +2,19 @@ name: test postgres
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
lint:
name: test postgres
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test postgres
env:
TEST_PGSQL_HOST: pgsql
@ -74,6 +52,4 @@ jobs:
env:
POSTGRES_DB: xorm_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
POSTGRES_PASSWORD: postgres

View File

@ -2,41 +2,19 @@ name: test sqlite
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
test-sqlite:
name: unit test & test sqlite
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: vet
run: make vet
- name: format check
@ -46,4 +24,14 @@ jobs:
- name: test sqlite3
run: make test-sqlite3
- name: test sqlite3 with cache
run: TEST_CACHE_ENABLE=true make test-sqlite3
run: TEST_CACHE_ENABLE=true make test-sqlite3
govulncheck_job:
runs-on: ubuntu-latest
name: Run govulncheck
steps:
- id: govulncheck
uses: golang/govulncheck-action@v1
with:
go-version-file: 'go.mod'
go-package: ./...

View File

@ -2,41 +2,19 @@ name: test tidb
on:
push:
branches:
- master
- main
- v1
pull_request:
env:
GOPROXY: https://goproxy.io,direct
GOPATH: /go_path
GOCACHE: /go_cache
jobs:
test-tidb:
name: test tidb
runs-on: ubuntu-latest
steps:
# - name: cache go path
# id: cache-go-path
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_path
# key: go_path-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_path-${{ github.repository }}-
# go_path-
# - name: cache go cache
# id: cache-go-cache
# uses: https://github.com/actions/cache@v3
# with:
# path: /go_cache
# key: go_cache-${{ github.repository }}-${{ github.ref_name }}
# restore-keys: |
# go_cache-${{ github.repository }}-
# go_cache-
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.20
- uses: actions/checkout@v3
go-version-file: 'go.mod'
- name: test tidb
env:
TEST_TIDB_HOST: "tidb:4000"
@ -47,6 +25,4 @@ jobs:
services:
tidb:
image: pingcap/tidb:v3.0.3
ports:
- 4000:4000
image: pingcap/tidb:v3.0.3

View File

@ -4,7 +4,7 @@
Xorm is a simple and powerful ORM for Go.
[![Build Status](https://drone.gitea.com/api/badges/xorm/xorm/status.svg)](https://drone.gitea.com/xorm/xorm) [![](http://gocover.io/_badge/xorm.io/xorm)](https://gocover.io/xorm.io/xorm) [![](https://goreportcard.com/badge/xorm.io/xorm)](https://goreportcard.com/report/xorm.io/xorm) [![Join the chat at https://img.shields.io/discord/323460943201959939.svg](https://img.shields.io/discord/323460943201959939.svg)](https://discord.gg/HuR2CF3)
[![Build Status](https://gitea.com/xorm/xorm/actions/workflows/release-tag/badge.svg)](https://gitea.com/xorm/xorm/actions) [![](http://gocover.io/_badge/xorm.io/xorm)](https://gocover.io/xorm.io/xorm) [![](https://goreportcard.com/badge/xorm.io/xorm)](https://goreportcard.com/report/xorm.io/xorm) [![Join the chat at https://img.shields.io/discord/323460943201959939.svg](https://img.shields.io/discord/323460943201959939.svg)](https://discord.gg/HuR2CF3)
## Notice
@ -50,6 +50,9 @@ Drivers for Go's sql package which currently support database/sql includes:
* MsSql
- [github.com/microsoft/go-mssqldb](https://github.com/microsoft/go-mssqldb)
* GBase8s
- [https://gitee.com/GBase8s/go-gci](https://gitee.com/GBase8s/go-gci)
* Oracle
- [github.com/godror/godror](https://github.com/godror/godror) (experiment)
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment)
@ -465,7 +468,7 @@ res, err := engine.Transaction(func(session *xorm.Session) (interface{}, error)
## Contributing
If you want to pull request, please see [CONTRIBUTING](https://gitea.com/xorm/xorm/src/branch/master/CONTRIBUTING.md). And you can also go to [Xorm on discourse](https://xorm.discourse.group) to discuss.
If you want to pull request, please see [CONTRIBUTING](CONTRIBUTING.md). And you can also go to [Xorm on discourse](https://xorm.discourse.group) to discuss.
## Credits

View File

@ -49,6 +49,9 @@ v1.0.0 相对于 v0.8.2 有以下不兼容的变更:
* MsSql
- [github.com/microsoft/go-mssqldb](https://github.com/microsoft/go-mssqldb)
* GBase8s
- [https://gitee.com/GBase8s/go-gci](https://gitee.com/GBase8s/go-gci)
* Oracle
- [github.com/godror/godror](https://github.com/godror/godror) (试验性支持)
- [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (试验性支持)

View File

@ -28,14 +28,19 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
dt = dt.In(convertedLocation)
return &dt, nil
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
if strings.HasPrefix(s, "0000-00-00T00:00:00") || strings.HasPrefix(s, "0001-01-01T00:00:00") {
return &time.Time{}, nil
}
dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation)
if err != nil {
return nil, err
}
dt = dt.In(convertedLocation)
dt.IsZero()
return &dt, nil
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
if strings.HasPrefix(s, "0000-00-00T00:00:00") || strings.HasPrefix(s, "0001-01-01T00:00:00") {
return &time.Time{}, nil
}
dt, err := time.Parse(time.RFC3339, s)
if err != nil {
return nil, err
@ -43,6 +48,10 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
dt = dt.In(convertedLocation)
return &dt, nil
} else if len(s) >= 21 && s[10] == 'T' && s[19] == '.' {
if strings.HasPrefix(s, "0000-00-00T00:00:00."+strings.Repeat("0", len(s)-20)) ||
strings.HasPrefix(s, "0001-01-01T00:00:00."+strings.Repeat("0", len(s)-20)) {
return &time.Time{}, nil
}
dt, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return nil, err
@ -50,6 +59,10 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
dt = dt.In(convertedLocation)
return &dt, nil
} else if len(s) >= 21 && s[19] == '.' {
if strings.HasPrefix(s, "0000-00-00T00:00:00."+strings.Repeat("0", len(s)-20)) ||
strings.HasPrefix(s, "0001-01-01T00:00:00."+strings.Repeat("0", len(s)-20)) {
return &time.Time{}, nil
}
layout := "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
dt, err := time.ParseInLocation(layout, s, originalLocation)
if err != nil {
@ -68,20 +81,20 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
dt = dt.In(convertedLocation)
return &dt, nil
} else if len(s) == 8 && s[2] == ':' && s[5] == ':' {
currentDate := time.Now()
dt, err := time.ParseInLocation("15:04:05", s, originalLocation)
if err != nil {
return nil, err
}
// add current date for correct time locations
dt = dt.AddDate(currentDate.Year(), int(currentDate.Month()), currentDate.Day())
dt = dt.In(convertedLocation)
dt = dt.AddDate(2006, 01, 02).In(convertedLocation)
// back to zero year
dt = dt.AddDate(-currentDate.Year(), int(-currentDate.Month()), -currentDate.Day())
dt = dt.AddDate(-2006, -01, -02)
return &dt, nil
} else {
i, err := strconv.ParseInt(s, 10, 64)
if err == nil {
if i == 0 {
return &time.Time{}, nil
}
tm := time.Unix(i, 0).In(convertedLocation)
return &tm, nil
}
@ -108,6 +121,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
if !t.Valid {
return nil, nil
}
if utils.IsTimeZero(t.Time) {
return &time.Time{}, nil
}
z, _ := t.Time.Zone()
if len(z) == 0 || t.Time.Year() == 0 || t.Time.Location().String() != dbLoc.String() {
tm := time.Date(t.Time.Year(), t.Time.Month(), t.Time.Day(), t.Time.Hour(),
@ -117,6 +133,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
tm := t.Time.In(uiLoc)
return &tm, nil
case *time.Time:
if utils.IsTimeZero(*t) {
return &time.Time{}, nil
}
z, _ := t.Zone()
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
@ -126,6 +145,9 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
tm := t.In(uiLoc)
return &tm, nil
case time.Time:
if utils.IsTimeZero(t) {
return &time.Time{}, nil
}
z, _ := t.Zone()
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbLoc.String() {
tm := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
@ -135,12 +157,21 @@ func AsTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
tm := t.In(uiLoc)
return &tm, nil
case int:
if t == 0 {
return &time.Time{}, nil
}
tm := time.Unix(int64(t), 0).In(uiLoc)
return &tm, nil
case int64:
if t == 0 {
return &time.Time{}, nil
}
tm := time.Unix(t, 0).In(uiLoc)
return &tm, nil
case *sql.NullInt64:
if t.Int64 == 0 {
return &time.Time{}, nil
}
tm := time.Unix(t.Int64, 0).In(uiLoc)
return &tm, nil
}

View File

@ -618,8 +618,8 @@ func (db *dameng) SQLType(c *schemas.Column) string {
res = t
}
hasLen1 := (c.Length > 0)
hasLen2 := (c.Length2 > 0)
hasLen1 := c.Length > 0
hasLen2 := c.Length2 > 0
if hasLen2 {
res += "(" + strconv.FormatInt(c.Length, 10) + "," + strconv.FormatInt(c.Length2, 10) + ")"
@ -831,6 +831,13 @@ func (d *dmClobScanner) Scan(data interface{}) error {
d.data = string(t)
d.valid = true
return nil
case string:
if len(t) <= 0 {
return nil
}
d.data = string(t)
d.valid = true
return nil
default:
return fmt.Errorf("cannot convert %T as dmClobScanner", data)
}

View File

@ -279,6 +279,7 @@ func regDrvsNDialects() bool {
"pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }},
"sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }},
"sqlite": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }},
"libsql": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }},
"oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }},
"godror": {"oracle", func() Driver { return &godrorDriver{} }, func() Dialect { return &oracle{} }},
"oracle": {"oracle", func() Driver { return &oracleDriver{} }, func() Dialect { return &oracle{} }},

1089
dialects/gbase8s.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -330,15 +330,11 @@ func (db *mssql) SQLType(c *schemas.Column) string {
res += "(MAX)"
}
case schemas.TimeStamp, schemas.DateTime:
if c.Length > 3 {
res = "DATETIME2"
} else {
return schemas.DateTime
}
return "DATETIME2"
case schemas.TimeStampz:
res = "DATETIMEOFFSET"
c.Length = 7
case schemas.MediumInt, schemas.TinyInt, schemas.SmallInt, schemas.UnsignedMediumInt, schemas.UnsignedTinyInt, schemas.UnsignedSmallInt:
case schemas.MediumInt, schemas.SmallInt, schemas.UnsignedMediumInt, schemas.UnsignedTinyInt, schemas.UnsignedSmallInt:
res = schemas.Int
case schemas.Text, schemas.MediumText, schemas.TinyText, schemas.LongText, schemas.Json:
res = db.defaultVarchar + "(MAX)"
@ -381,8 +377,8 @@ func (db *mssql) SQLType(c *schemas.Column) string {
return res
}
hasLen1 := (c.Length > 0)
hasLen2 := (c.Length2 > 0)
hasLen1 := c.Length > 0
hasLen2 := c.Length2 > 0
if hasLen2 {
res += "(" + strconv.FormatInt(c.Length, 10) + "," + strconv.FormatInt(c.Length2, 10) + ")"

View File

@ -326,8 +326,8 @@ func (db *mysql) SQLType(c *schemas.Column) string {
res = t
}
hasLen1 := (c.Length > 0)
hasLen2 := (c.Length2 > 0)
hasLen1 := c.Length > 0
hasLen2 := c.Length2 > 0
if res == schemas.BigInt && !hasLen1 && !hasLen2 {
c.Length = 20
@ -402,6 +402,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
// ModifyColumnSQL returns a SQL to modify SQL
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
s, _ := ColumnString(db.dialect, col, false, true)
if col.IsAutoIncrement {
s += " " + db.AutoIncrStr()
}
if col.Comment != "" {
s += fmt.Sprintf(" COMMENT '%s'", col.Comment)
}

View File

@ -585,8 +585,8 @@ func (db *oracle) SQLType(c *schemas.Column) string {
res = t
}
hasLen1 := (c.Length > 0)
hasLen2 := (c.Length2 > 0)
hasLen1 := c.Length > 0
hasLen2 := c.Length2 > 0
if hasLen2 {
res += "(" + strconv.FormatInt(c.Length, 10) + "," + strconv.FormatInt(c.Length2, 10) + ")"
@ -684,6 +684,17 @@ func (db *oracle) IndexCheckSQL(tableName, idxName string) (string, []interface{
`WHERE TABLE_NAME = :1 AND INDEX_NAME = :2`, args
}
func (db *oracle) DropIndexSQL(tableName string, index *schemas.Index) string {
quote := db.dialect.Quoter().Quote
var name string
if index.IsRegular {
name = index.XName(tableName)
} else {
name = index.Name
}
return fmt.Sprintf("DROP INDEX %v", quote(name))
}
func (db *oracle) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) {
return db.HasRecords(queryer, ctx, `SELECT table_name FROM user_tables WHERE table_name = :1`, tableName)
}

View File

@ -957,8 +957,8 @@ func (db *postgres) SQLType(c *schemas.Column) string {
// for bool, we don't need length information
return res
}
hasLen1 := (c.Length > 0)
hasLen2 := (c.Length2 > 0)
hasLen1 := c.Length > 0
hasLen2 := c.Length2 > 0
if hasLen2 {
res += "(" + strconv.FormatInt(c.Length, 10) + "," + strconv.FormatInt(c.Length2, 10) + ")"
@ -1185,7 +1185,7 @@ WHERE n.nspname= s.table_schema AND c.relkind = 'r' AND c.relname = $1%s AND f.a
col.IsPrimaryKey = true
}
col.Nullable = (isNullable == "YES")
col.Nullable = isNullable == "YES"
switch strings.ToLower(dataType) {
case "character varying", "string":

View File

@ -7,20 +7,23 @@ package dialects
import (
"strings"
"time"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas"
)
// FormatColumnTime format column time
func FormatColumnTime(dialect Dialect, dbLocation *time.Location, col *schemas.Column, t time.Time) (interface{}, error) {
if t.IsZero() {
if utils.IsTimeZero(t) {
if col.Nullable {
return nil, nil
}
if col.SQLType.IsNumeric() {
return 0, nil
}
if col.SQLType.Name == schemas.TimeStamp || col.SQLType.Name == schemas.TimeStampz {
t = time.Unix(0, 0)
}
}
tmZone := dbLocation

View File

@ -798,6 +798,22 @@ func (engine *Engine) dumpTables(ctx context.Context, tables []*schemas.Table, w
return err
}
}
} else if sess.engine.dialect.URI().DBType == schemas.GBASE8S {
stp.Name = strings.Replace(stp.Name, "SQLT_", "", 1)
if stp.IsTime() && len(s.String) == 20 { // "2025-06-10T07:55:31Z"
t, err := time.Parse(time.RFC3339, s.String)
if err != nil {
return fmt.Errorf("failed to parse time %s: %v", s.String, err)
}
r := t.Format("2006-01-02 15:04:05")
if _, err = io.WriteString(w, "'"+r+"'"); err != nil {
return err
}
} else {
if _, err = io.WriteString(w, "'"+strings.ReplaceAll(s.String, "'", "''")+"'"); err != nil {
return err
}
}
} else {
if _, err = io.WriteString(w, "'"+strings.ReplaceAll(s.String, "'", "''")+"'"); err != nil {
return err
@ -1212,7 +1228,7 @@ func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
func (engine *Engine) InsertOne(bean interface{}) (int64, error) {
session := engine.NewSession()
defer session.Close()
return session.InsertOne(bean)
return session.Insert(bean)
}
// Update records, bean's non-empty fields are updated contents,

12
go.mod
View File

@ -6,7 +6,7 @@ require (
gitee.com/travelliu/dm v1.8.11192
github.com/go-sql-driver/mysql v1.7.0
github.com/goccy/go-json v0.8.1
github.com/jackc/pgx/v4 v4.18.0
github.com/jackc/pgx/v4 v4.18.2
github.com/json-iterator/go v1.1.12
github.com/lib/pq v1.10.7
github.com/mattn/go-sqlite3 v1.14.16
@ -27,10 +27,10 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@ -39,10 +39,10 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect

48
go.sum
View File

@ -59,8 +59,8 @@ github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsU
github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o=
github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY=
github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
github.com/jackc/pgconn v1.14.0 h1:vrbA9Ud87g6JdFWkHTJXppVce58qPIdP7N8y0Ml/A7Q=
github.com/jackc/pgconn v1.14.0/go.mod h1:9mBNlny0UvkgJdCDvdVHYSjI+8tD2rnKK69Wz8ti++E=
github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w=
github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
@ -76,8 +76,8 @@ github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvW
github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM=
github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.2 h1:7eY55bdBeCz1F2fTzSz69QC+pG46jYq9/jtSPiJ5nn0=
github.com/jackc/pgproto3/v2 v2.3.2/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
@ -91,12 +91,11 @@ github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.18.0 h1:Ltaa1ePvc7msFGALnCrqKJVEByu/qYh5jJBYcDtAno4=
github.com/jackc/pgx/v4 v4.18.0/go.mod h1:FydWkUyadDmdNH/mHnGob881GawxeEm7TcMCzkb+qQE=
github.com/jackc/pgx/v4 v4.18.2 h1:xVpYkNR5pk5bMCZGfClbO962UIqVABcAGt7ha1s/FeU=
github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@ -158,21 +157,15 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
@ -196,14 +189,11 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -212,12 +202,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -232,25 +219,18 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
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=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
@ -258,9 +238,7 @@ golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -5,10 +5,23 @@
package statements
import (
"database/sql/driver"
"fmt"
"xorm.io/builder"
"xorm.io/xorm/schemas"
)
type DateTimeString struct {
Layout string
Str string
}
// Value implements the driver Valuer interface.
func (n DateTimeString) Value() (driver.Value, error) {
return n.Str, nil
}
// WriteArg writes an arg
func (statement *Statement) WriteArg(w *builder.BytesWriter, arg interface{}) error {
switch argv := arg.(type) {
@ -22,6 +35,17 @@ func (statement *Statement) WriteArg(w *builder.BytesWriter, arg interface{}) er
if _, err := w.WriteString(")"); err != nil {
return err
}
case *DateTimeString:
if statement.dialect.URI().DBType == schemas.ORACLE {
if _, err := fmt.Fprintf(w, `TO_DATE(?,'%s')`, argv.Layout); err != nil {
return err
}
} else {
if err := w.WriteByte('?'); err != nil {
return err
}
}
w.Append(arg)
default:
if err := w.WriteByte('?'); err != nil {
return err

View File

@ -19,8 +19,9 @@ func (statement *Statement) isUsingLegacyLimitOffset() bool {
func (statement *Statement) writeMssqlLegacySelect(buf *builder.BytesWriter, columnStr string) error {
return statement.writeMultiple(buf,
statement.writeStrings("SELECT"),
statement.writeDistinct(columnStr),
statement.writeTop,
statement.writeDistinct,
statement.writeStrings(" ", columnStr),
statement.writeFrom,
statement.writeWhereWithMssqlPagination,
statement.writeGroupBy,
@ -32,10 +33,9 @@ func (statement *Statement) writeMssqlLegacySelect(buf *builder.BytesWriter, col
func (statement *Statement) writeOracleLegacySelect(buf *builder.BytesWriter, columnStr string) error {
return statement.writeMultiple(buf,
statement.writeStrings("SELECT"),
statement.writeDistinct(columnStr),
statement.writeStrings(" ", columnStr),
statement.writeSelectColumns(columnStr),
statement.writeFrom,
statement.writeWhere,
statement.writeOracleLimit(columnStr),
statement.writeGroupBy,
statement.writeHaving,

View File

@ -50,7 +50,7 @@ var ErrNoColumnName = errors.New("no column name")
func (statement *Statement) writeOrderBy(w *builder.BytesWriter, orderBy orderBy) error {
switch t := orderBy.orderStr.(type) {
case (*builder.Expression):
case *builder.Expression:
if _, err := fmt.Fprint(w.Builder, statement.dialect.Quoter().Replace(t.Content())); err != nil {
return err
}

View File

@ -141,20 +141,29 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
}
}
selectBuf := builder.NewWriter()
if err := statement.writeSelectCount(selectBuf); err != nil {
return "", nil, err
selectSQL := statement.SelectStr
if len(selectSQL) <= 0 {
if statement.IsDistinct {
selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr())
} else if statement.ColumnStr() != "" {
selectSQL = fmt.Sprintf("count(%s)", statement.ColumnStr())
} else {
selectSQL = "count(*)"
}
}
buf := builder.NewWriter()
var subQuerySelect string
if statement.GroupByStr != "" {
if err := statement.writeStrings("SELECT ", selectBuf.String(), " FROM (")(buf); err != nil {
if _, err := fmt.Fprintf(buf, "SELECT %s FROM (", selectSQL); err != nil {
return "", nil, err
}
}
var subQuerySelect string
if statement.GroupByStr != "" {
subQuerySelect = statement.GroupByStr
} else {
subQuerySelect = selectBuf.String()
subQuerySelect = selectSQL
}
if err := statement.writeSelect(buf, subQuerySelect, true); err != nil {
@ -189,27 +198,20 @@ func (statement *Statement) writeTop(w *builder.BytesWriter) error {
return err
}
func (statement *Statement) writeDistinct(selectStr string) func(w *builder.BytesWriter) error {
return func(w *builder.BytesWriter) error {
if statement.IsDistinct && !strings.HasPrefix(selectStr, "COUNT(") {
_, err := fmt.Fprint(w, " DISTINCT")
return err
}
return nil
func (statement *Statement) writeDistinct(w *builder.BytesWriter) error {
if statement.IsDistinct && !strings.HasPrefix(statement.SelectStr, "count(") {
_, err := fmt.Fprint(w, " DISTINCT")
return err
}
return nil
}
func (statement *Statement) writeSelectCount(w *builder.BytesWriter) error {
if statement.SelectStr != "" {
return statement.writeStrings(statement.SelectStr)(w)
}
if statement.IsDistinct {
return statement.writeStrings("COUNT(DISTINCT ", statement.ColumnStr(), ")")(w)
} else if statement.ColumnStr() != "" {
return statement.writeStrings("COUNT(", statement.ColumnStr(), ")")(w)
}
return statement.writeStrings("COUNT(*)")(w)
func (statement *Statement) writeSelectColumns(columnStr string) func(w *builder.BytesWriter) error {
return statement.groupWriteFns(
statement.writeStrings("SELECT"),
statement.writeDistinct,
statement.writeStrings(" ", columnStr),
)
}
func (statement *Statement) writeWhereCond(w *builder.BytesWriter, cond builder.Cond) error {
@ -251,9 +253,7 @@ func (statement *Statement) writeSelect(buf *builder.BytesWriter, columnStr stri
}
return statement.writeMultiple(buf,
statement.writeStrings("SELECT"),
statement.writeDistinct(columnStr),
statement.writeStrings(" ", columnStr),
statement.writeSelectColumns(columnStr),
statement.writeFrom,
statement.writeWhere,
statement.writeGroupBy,

View File

@ -170,7 +170,7 @@ func (statement *Statement) Reset() {
// SQL adds raw sql statement
func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement {
switch t := query.(type) {
case (*builder.Builder):
case *builder.Builder:
var err error
statement.RawSQL, statement.RawParams, err = t.ToSQL()
if err != nil {
@ -616,7 +616,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
// MergeConds merge conditions from bean and id
func (statement *Statement) MergeConds(bean interface{}) error {
if !statement.NoAutoCondition && statement.RefTable != nil {
addedTableName := (len(statement.joins) > 0)
addedTableName := len(statement.joins) > 0
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
if err != nil {
return err
@ -713,11 +713,14 @@ func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
cond := builder.NewCond()
if col.SQLType.IsNumeric() {
cond = builder.Eq{colName: 0}
} else {
// FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
if statement.dialect.URI().DBType != schemas.MSSQL {
cond = builder.Eq{colName: utils.ZeroTime1}
} else if col.SQLType.Name == schemas.TimeStamp || col.SQLType.Name == schemas.TimeStampz {
tmZone := statement.defaultTimeZone
if col.TimeZone != nil {
tmZone = col.TimeZone
}
cond = builder.Eq{colName: time.Unix(0, 0).In(tmZone).Format("2006-01-02 15:04:05.999999999")}
} else {
cond = builder.Eq{colName: utils.ZeroTime1}
}
if col.Nullable {

View File

@ -126,6 +126,9 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
if fieldValue.CanAddr() {
if structConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {
if !includeNil && !requiredField && utils.IsZero(fieldValue.Interface()) {
continue
}
data, err := structConvert.ToDB()
if err != nil {
return nil, nil, err

View File

@ -88,7 +88,20 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
if fieldType.ConvertibleTo(schemas.TimeType) {
t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time)
tf, err := dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t)
return tf, err
if val, ok := tf.(string); ok {
var layout string
switch col.SQLType.Name {
case schemas.Date:
layout = "yyyy-MM-dd"
case schemas.Time:
layout = "HH24:mi:ss"
default:
layout = "yyyy-MM-dd HH24:mi:ss"
}
return &DateTimeString{Layout: layout, Str: val}, err
} else {
return tf, err
}
} else if fieldType.ConvertibleTo(nullFloatType) {
t := fieldValue.Convert(nullFloatType).Interface().(sql.NullFloat64)
if !t.Valid {

View File

@ -146,6 +146,6 @@ const (
// IsTimeZero return true if a time is zero
func IsTimeZero(t time.Time) bool {
return t.IsZero() || t.Format("2006-01-02 15:04:05") == ZeroTime0 ||
t.Format("2006-01-02 15:04:05") == ZeroTime1
return t.IsZero() || t.Format("2006-01-02 15:04:05.999999999") == ZeroTime0 ||
t.Format("2006-01-02 15:04:05.999999999") == ZeroTime1
}

View File

@ -1,10 +1,13 @@
package migrate
import (
"context"
"errors"
"fmt"
"reflect"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
// MigrateFunc is the func signature for migrating.
@ -140,7 +143,9 @@ func (m *Migrate) RollbackMigration(mig *Migration) error {
return err
}
sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName)
tableName := m.db.TableName(m.options.TableName, true)
sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", tableName, m.options.IDColumnName)
if _, err := m.db.Exec(sql, mig.ID); err != nil {
return err
}
@ -192,7 +197,19 @@ func (m *Migrate) createMigrationTableIfNotExists() error {
return nil
}
sql := fmt.Sprintf("CREATE TABLE %s (%s VARCHAR(255) PRIMARY KEY)", m.options.TableName, m.options.IDColumnName)
idCol := schemas.NewColumn(m.options.IDColumnName, "", schemas.SQLType{
Name: "VARCHAR",
}, 255, 0, false)
idCol.IsPrimaryKey = true
table := schemas.NewTable(m.options.TableName, reflect.TypeOf(new(schemas.Table)))
table.AddColumn(idCol)
sql, _, err := m.db.Dialect().CreateTableSQL(context.Background(), m.db.DB(), table, m.options.TableName)
if err != nil {
return err
}
if _, err := m.db.Exec(sql); err != nil {
return err
}
@ -200,18 +217,21 @@ func (m *Migrate) createMigrationTableIfNotExists() error {
}
func (m *Migrate) migrationDidRun(mig *Migration) (bool, error) {
count, err := m.db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName), mig.ID).Count()
tableName := m.db.TableName(m.options.TableName, true)
count, err := m.db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", tableName, m.options.IDColumnName), mig.ID).Count()
return count > 0, err
}
func (m *Migrate) isFirstRun() (bool, error) {
var count int
_, err := m.db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM %s", m.options.TableName)).Get(&count)
tableName := m.db.TableName(m.options.TableName, true)
_, err := m.db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName)).Get(&count)
return count == 0, err
}
func (m *Migrate) insertMigration(id string) error {
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?)", m.options.TableName, m.options.IDColumnName)
tableName := m.db.TableName(m.options.TableName, true)
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?)", tableName, m.options.IDColumnName)
_, err := m.db.Exec(sql, id)
return err
}

View File

@ -149,7 +149,7 @@ func isASCIIUpper(r rune) bool {
func toASCIIUpper(r rune) rune {
if 'a' <= r && r <= 'z' {
r -= ('a' - 'A')
r -= 'a' - 'A'
}
return r
}

View File

@ -26,6 +26,7 @@ type Column struct {
FieldIndex []int // Available only when parsed from a struct
SQLType SQLType
IsJSON bool
IsJSONB bool
Length int64
Length2 int64
Nullable bool

View File

@ -23,6 +23,7 @@ const (
MSSQL DBType = "mssql"
ORACLE DBType = "oracle"
DAMENG DBType = "dameng"
GBASE8S DBType = "gbase8s"
)
// SQLType represents SQL types

View File

@ -471,7 +471,8 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
}
if col.IsDeleted {
arg, err := dialects.FormatColumnTime(session.engine.dialect, session.engine.DatabaseTZ, col, time.Time{})
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, session.engine.DatabaseTZ)
arg, err := dialects.FormatColumnTime(session.engine.dialect, session.engine.DatabaseTZ, col, zeroTime)
if err != nil {
return nil, nil, err
}

12
sync.go
View File

@ -17,6 +17,8 @@ type SyncOptions struct {
IgnoreConstrains bool
// IgnoreIndices will not add or delete indices
IgnoreIndices bool
// IgnoreDropIndices will not delete indices
IgnoreDropIndices bool
}
type SyncResult struct{}
@ -55,6 +57,7 @@ func (session *Session) Sync(beans ...interface{}) error {
WarnIfDatabaseColumnMissed: false,
IgnoreConstrains: false,
IgnoreIndices: false,
IgnoreDropIndices: false,
}, beans...)
return err
}
@ -168,7 +171,8 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
tbNameWithSchema, col.Name, curType, expectedType)
}
} else if strings.HasPrefix(curType, schemas.Varchar) && strings.HasPrefix(expectedType, schemas.Varchar) {
if engine.dialect.URI().DBType == schemas.MYSQL {
if engine.dialect.URI().DBType == schemas.POSTGRES ||
engine.dialect.URI().DBType == schemas.MYSQL {
if oriCol.Length < col.Length {
engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
tbNameWithSchema, col.Name, oriCol.Length, col.Length)
@ -184,7 +188,8 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
}
}
} else if expectedType == schemas.Varchar {
if engine.dialect.URI().DBType == schemas.MYSQL {
if engine.dialect.URI().DBType == schemas.POSTGRES ||
engine.dialect.URI().DBType == schemas.MYSQL {
if oriCol.Length < col.Length {
engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
tbNameWithSchema, col.Name, oriCol.Length, col.Length)
@ -193,6 +198,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
}
} else if col.Comment != oriCol.Comment {
if engine.dialect.URI().DBType == schemas.POSTGRES ||
engine.dialect.URI().DBType == schemas.GBASE8S ||
engine.dialect.URI().DBType == schemas.MYSQL {
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
}
@ -244,7 +250,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
for name2, index2 := range oriTable.Indexes {
if _, ok := foundIndexNames[name2]; !ok {
// ignore based on there type
if (index2.Type == schemas.IndexType && opts.IgnoreIndices) ||
if (index2.Type == schemas.IndexType && (opts.IgnoreIndices || opts.IgnoreDropIndices)) ||
(index2.Type == schemas.UniqueType && opts.IgnoreConstrains) {
// make sure we do not add a index with same name later
delete(addedNames, name2)

View File

@ -250,10 +250,16 @@ func (parser *Parser) parseFieldWithTags(table *schemas.Table, fieldIndex int, f
}
if col.SQLType.Name == "" {
var err error
col.SQLType, err = parser.getSQLTypeByType(field.Type)
if err != nil {
return nil, err
if col.IsJSONB { // check is jsonb first because it is also json
col.SQLType = schemas.SQLType{Name: schemas.Jsonb}
} else if col.IsJSON {
col.SQLType = schemas.SQLType{Name: schemas.Json}
} else {
var err error
col.SQLType, err = parser.getSQLTypeByType(field.Type)
if err != nil {
return nil, err
}
}
}
if ctx.isUnsigned && col.SQLType.IsNumeric() && !strings.HasPrefix(col.SQLType.Name, "UNSIGNED") {

View File

@ -577,7 +577,7 @@ func TestParseWithJSONB(t *testing.T) {
assert.EqualValues(t, "struct_with_jsonb", table.Name)
assert.EqualValues(t, 1, len(table.Columns()))
assert.EqualValues(t, "default1", table.Columns()[0].Name)
assert.True(t, table.Columns()[0].IsJSON)
assert.True(t, table.Columns()[0].IsJSONB)
}
func TestParseWithSQLType(t *testing.T) {
@ -617,3 +617,53 @@ func TestParseWithSQLType(t *testing.T) {
assert.EqualValues(t, "DATETIME", table.Columns()[3].SQLType.Name)
assert.EqualValues(t, "UUID", table.Columns()[4].SQLType.Name)
}
func TestParseWithJSONLongText(t *testing.T) {
parser := NewParser(
"db",
dialects.QueryDialect("mysql"),
names.GonicMapper{
"JSON": true,
},
names.GonicMapper{
"JSON": true,
},
caches.NewManager(),
)
type StructWithJSONLongText struct {
Col1 string `db:"LongText json"`
}
table, err := parser.Parse(reflect.ValueOf(new(StructWithJSONLongText)))
assert.NoError(t, err)
assert.EqualValues(t, "struct_with_json_long_text", table.Name)
assert.EqualValues(t, 1, len(table.Columns()))
assert.EqualValues(t, "col1", table.Columns()[0].Name)
assert.EqualValues(t, "LONGTEXT", table.Columns()[0].SQLType.Name)
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
type StructWithJSONLongText2 struct {
Col1 string `db:"json"`
}
table, err = parser.Parse(reflect.ValueOf(new(StructWithJSONLongText2)))
assert.NoError(t, err)
assert.EqualValues(t, "struct_with_json_long_text2", table.Name)
assert.EqualValues(t, 1, len(table.Columns()))
assert.EqualValues(t, "col1", table.Columns()[0].Name)
assert.EqualValues(t, "JSON", table.Columns()[0].SQLType.Name)
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
type StructWithJSONLongText3 struct {
Col1 string `db:"jsonb"`
}
table, err = parser.Parse(reflect.ValueOf(new(StructWithJSONLongText3)))
assert.NoError(t, err)
assert.EqualValues(t, "struct_with_json_long_text3", table.Name)
assert.EqualValues(t, 1, len(table.Columns()))
assert.EqualValues(t, "col1", table.Columns()[0].Name)
assert.EqualValues(t, "JSONB", table.Columns()[0].SQLType.Name)
assert.EqualValues(t, true, table.Columns()[0].IsJSONB)
}

View File

@ -124,10 +124,16 @@ var defaultTagHandlers = map[string]Handler{
"EXTENDS": ExtendsTagHandler,
"UNSIGNED": UnsignedTagHandler,
"COLLATE": CollateTagHandler,
"JSON": JSONTagHandler,
"JSONB": JSONBTagHandler,
}
func init() {
for k := range schemas.SqlTypes {
// don't override default tag handlers
if _, ok := defaultTagHandlers[k]; ok {
continue
}
defaultTagHandlers[k] = SQLTypeTagHandler
}
}
@ -163,7 +169,7 @@ func PKTagHandler(ctx *Context) error {
// NULLTagHandler describes null tag handler
func NULLTagHandler(ctx *Context) error {
ctx.col.Nullable = (strings.ToUpper(ctx.preTag) != "NOT")
ctx.col.Nullable = strings.ToUpper(ctx.preTag) != "NOT"
return nil
}
@ -293,12 +299,20 @@ func CollateTagHandler(ctx *Context) error {
return nil
}
func JSONTagHandler(ctx *Context) error {
ctx.col.IsJSON = true
return nil
}
func JSONBTagHandler(ctx *Context) error {
ctx.col.IsJSONB = true
ctx.col.IsJSON = true // jsonb is also json
return nil
}
// SQLTypeTagHandler describes SQL Type tag handler
func SQLTypeTagHandler(ctx *Context) error {
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagUname}
if ctx.tagUname == "JSON" || ctx.tagUname == "JSONB" {
ctx.col.IsJSON = true
}
if len(ctx.params) == 0 {
return nil
}

View File

@ -698,7 +698,7 @@ func TestSyncWithOptions(t *testing.T) {
assert.NotNil(t, result)
assert.Len(t, getIndicesOfBeanFromDB(t, &SyncWithOpts1{}), 0)
// only ignore indices
// only ignore constrains
result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreConstrains: true}, &SyncWithOpts2{})
assert.NoError(t, err)
assert.NotNil(t, result)
@ -706,7 +706,7 @@ func TestSyncWithOptions(t *testing.T) {
assert.Len(t, indices, 2)
assert.ElementsMatch(t, []string{"ttt", "index"}, getKeysFromMap(indices))
// only ignore constrains
// only ignore indices
result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreIndices: true}, &SyncWithOpts3{})
assert.NoError(t, err)
assert.NotNil(t, result)
@ -714,9 +714,16 @@ func TestSyncWithOptions(t *testing.T) {
assert.Len(t, indices, 4)
assert.ElementsMatch(t, []string{"ttt", "index", "unique", "lll"}, getKeysFromMap(indices))
// only ignore drop indices
result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, &SyncWithOpts3{})
assert.NoError(t, err)
assert.NotNil(t, result)
indices = getIndicesOfBeanFromDB(t, &SyncWithOpts1{})
assert.Len(t, indices, 4)
assert.ElementsMatch(t, []string{"ttt", "index", "unique", "lll"}, getKeysFromMap(indices))
tableInfoFromStruct, _ := testEngine.TableInfo(&SyncWithOpts1{})
assert.ElementsMatch(t, getKeysFromMap(tableInfoFromStruct.Indexes), getKeysFromMap(getIndicesOfBeanFromDB(t, &SyncWithOpts1{})))
}
func getIndicesOfBeanFromDB(t *testing.T, bean interface{}) map[string]*schemas.Index {
@ -744,3 +751,72 @@ func getKeysFromMap(m map[string]*schemas.Index) []string {
}
return ss
}
type SyncTestUser struct {
Id int64 `xorm:"pk autoincr 'id' comment('primary key 1')"`
Name string `xorm:"'name' notnull comment('nickname')" json:"name"`
}
func (m *SyncTestUser) TableName() string {
return "sync_test_user"
}
type SyncTestUser2 struct {
Id int64 `xorm:"pk autoincr 'id' comment('primary key 2')"`
Name string `xorm:"'name' notnull comment('nickname')" json:"name"`
}
func (m *SyncTestUser2) TableName() string {
return "sync_test_user"
}
func TestSync2_3(t *testing.T) {
if testEngine.Dialect().URI().DBType == schemas.MYSQL {
assert.NoError(t, PrepareEngine())
assertSync(t, new(SyncTestUser))
assert.NoError(t, testEngine.Sync2(new(SyncTestUser2)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
tableInfo, err := testEngine.TableInfo(new(SyncTestUser2))
assert.EqualValues(t, tables[0].GetColumn("id").IsAutoIncrement, tableInfo.GetColumn("id").IsAutoIncrement)
assert.EqualValues(t, tables[0].GetColumn("id").Name, tableInfo.GetColumn("id").Name)
assert.EqualValues(t, tables[0].GetColumn("id").SQLType.Name, tableInfo.GetColumn("id").SQLType.Name)
assert.EqualValues(t, tables[0].GetColumn("id").Nullable, tableInfo.GetColumn("id").Nullable)
assert.EqualValues(t, tables[0].GetColumn("id").Comment, tableInfo.GetColumn("id").Comment)
}
}
func TestSyncJSON(t *testing.T) {
type SyncTestJSON struct {
Id int64
Value string `xorm:"LONGTEXT JSON 'value' comment('json value')"`
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(SyncTestJSON))
assert.NoError(t, testEngine.Sync(new(SyncTestJSON)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
tableInfo, err := testEngine.TableInfo(new(SyncTestJSON))
assert.NoError(t, err)
assert.EqualValues(t, tables[0].GetColumn("id").IsAutoIncrement, tableInfo.GetColumn("id").IsAutoIncrement)
assert.EqualValues(t, tables[0].GetColumn("id").Name, tableInfo.GetColumn("id").Name)
if testEngine.Dialect().URI().DBType == schemas.MYSQL {
assert.EqualValues(t, tables[0].GetColumn("id").SQLType.Name, tableInfo.GetColumn("id").SQLType.Name)
}
assert.EqualValues(t, tables[0].GetColumn("id").Nullable, tableInfo.GetColumn("id").Nullable)
assert.EqualValues(t, tables[0].GetColumn("value").IsAutoIncrement, tableInfo.GetColumn("value").IsAutoIncrement)
assert.EqualValues(t, tables[0].GetColumn("value").Name, tableInfo.GetColumn("value").Name)
assert.EqualValues(t, tables[0].GetColumn("value").Nullable, tableInfo.GetColumn("value").Nullable)
if testEngine.Dialect().URI().DBType == schemas.MYSQL {
assert.EqualValues(t, tables[0].GetColumn("value").SQLType.Name, tableInfo.GetColumn("value").SQLType.Name)
}
}

View File

@ -899,6 +899,58 @@ func TestFindExtends3(t *testing.T) {
assert.EqualValues(t, 2, len(results))
}
func TestFindExtends4(t *testing.T) {
type FindExtends4A struct {
Id int64
Age int
Name string
}
type FindExtends4B struct {
Id int64
ExtId int64 `xorm:"index"`
Age int
Name string
Value int
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(FindExtends4A), new(FindExtends4B))
fe := FindExtends4A{
Age: 1,
Name: "1",
}
cnt, err := testEngine.Insert(&fe)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
cnt, err = testEngine.Insert(&FindExtends4B{
ExtId: fe.Id,
Age: 2,
Name: "2",
Value: 3,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
type FindExtends4C struct {
FindExtends4A `xorm:"extends"`
FindExtends4B `xorm:"extends"`
}
var results []FindExtends4C
err = testEngine.Table("find_extends4_a").
Join("INNER", "find_extends4_b", "`find_extends4_b`.`ext_id`=`find_extends4_a`.`id`").
Find(&results)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))
assert.EqualValues(t, 1, results[0].FindExtends4A.Age)
assert.EqualValues(t, "1", results[0].FindExtends4A.Name)
assert.EqualValues(t, 2, results[0].FindExtends4B.Age)
assert.EqualValues(t, "2", results[0].FindExtends4B.Name)
assert.EqualValues(t, 3, results[0].FindExtends4B.Value)
}
func TestFindCacheLimit(t *testing.T) {
type InviteCode struct {
ID int64 `xorm:"pk autoincr 'id'"`
@ -1254,20 +1306,3 @@ func TestFindInMaxID(t *testing.T) {
err := testEngine.In("id", builder.Select("max(id)").From(testEngine.Quote(tableName))).Find(&res)
assert.NoError(t, err)
}
func TestDistinctFindAndCount(t *testing.T) {
assert.NoError(t, PrepareEngine())
type TestDistinctFindAndCount struct {
Id int64
Name string `xorm:"index"`
Age2 int
}
assertSync(t, new(TestDistinctFindAndCount))
objects := make([]*TestDistinctFindAndCount, 0, 10)
total, err := testEngine.Distinct(testEngine.TableName(new(TestDistinctFindAndCount)) + ".*").FindAndCount(&objects)
assert.NoError(t, err)
assert.EqualValues(t, 0, total)
}

View File

@ -943,7 +943,7 @@ func TestMultipleInsertTableName(t *testing.T) {
assert.NoError(t, testEngine.Table(tableName).Sync(new(NightlyRate)))
trans := testEngine.NewSession()
defer trans.Close()
defer func(trans *xorm.Session) { _ = trans.Close() }(trans)
err := trans.Begin()
assert.NoError(t, err)
@ -1031,7 +1031,7 @@ func TestInsertTwice(t *testing.T) {
}
ssn := testEngine.NewSession()
defer ssn.Close()
defer func(ssn *xorm.Session) { _ = ssn.Close() }(ssn)
err := ssn.Begin()
assert.NoError(t, err)
@ -1209,3 +1209,166 @@ func TestInsertMultipleMap(t *testing.T) {
Name: "xiaolunwen",
}, res[1])
}
func TestInsertNotDeleted(t *testing.T) {
assert.NoError(t, PrepareEngine())
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, testEngine.GetTZDatabase())
type TestInsertNotDeletedStructNotRight struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt time.Time `xorm:"'DELETED_AT' deleted notnull"`
}
// notnull tag will be ignored
err := testEngine.Sync(new(TestInsertNotDeletedStructNotRight))
assert.NoError(t, err)
type TestInsertNotDeletedStruct struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt time.Time `xorm:"'DELETED_AT' deleted"`
}
err = testEngine.Sync(new(TestInsertNotDeletedStruct))
assert.NoError(t, err)
var v1 TestInsertNotDeletedStructNotRight
_, err = testEngine.Insert(&v1)
assert.NoError(t, err)
var v2 TestInsertNotDeletedStructNotRight
has, err := testEngine.Get(&v2)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v2.DeletedAt.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
var v3 TestInsertNotDeletedStruct
_, err = testEngine.Insert(&v3)
assert.NoError(t, err)
var v4 TestInsertNotDeletedStruct
has, err = testEngine.Get(&v4)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v4.DeletedAt.In(testEngine.GetTZDatabase()).Format("2006-01-02 15:04:05"), zeroTime.Format("2006-01-02 15:04:05"))
}
func TestInsertNotDeletedNum(t *testing.T) {
assert.NoError(t, PrepareEngine())
type TestInsertNotDeletedNumStructNotRight struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt int64 `xorm:"'DELETED_AT' deleted notnull INT(11)"`
}
// notnull tag will be ignored
err := testEngine.Sync(new(TestInsertNotDeletedNumStructNotRight))
assert.NoError(t, err)
type TestInsertNotDeletedNumStruct struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt int64 `xorm:"'DELETED_AT' deleted INT(11)"`
}
err = testEngine.Sync(new(TestInsertNotDeletedNumStruct))
assert.NoError(t, err)
var v1 TestInsertNotDeletedNumStructNotRight
_, err = testEngine.Insert(&v1)
assert.NoError(t, err)
var v2 TestInsertNotDeletedNumStructNotRight
has, err := testEngine.Get(&v2)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v2.DeletedAt, int64(0))
var v3 TestInsertNotDeletedNumStruct
_, err = testEngine.Insert(&v3)
assert.NoError(t, err)
var v4 TestInsertNotDeletedNumStruct
has, err = testEngine.Get(&v4)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v4.DeletedAt, int64(0))
}
func TestInsertNotDeletedTimeStamp(t *testing.T) {
assert.NoError(t, PrepareEngine())
// IN MYSQL DB
// The time range that timestamps can store is from '1970 01 01 00:00:01.000000' to '2038 01 19 03:14:07.999999'
// PASS notnull timestamp IN MYSQL DB
if testEngine.Dialect().URI().DBType == schemas.MSSQL ||
testEngine.Dialect().URI().DBType == schemas.SQLITE ||
testEngine.Dialect().URI().DBType == schemas.POSTGRES {
type TestInsertNotDeletedTimeStampStructNotRight struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt time.Time `xorm:"'DELETED_AT' deleted notnull TIMESTAMP"`
}
err := testEngine.Sync(new(TestInsertNotDeletedTimeStampStructNotRight))
assert.NoError(t, err)
var v1 TestInsertNotDeletedTimeStampStructNotRight
_, err = testEngine.Insert(&v1)
assert.NoError(t, err)
var v2 TestInsertNotDeletedTimeStampStructNotRight
has, err := testEngine.Get(&v2)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v2.DeletedAt, time.Unix(0, 0))
}
type TestInsertNotDeletedTimeStampStruct struct {
ID uint64 `xorm:"'ID' pk autoincr"`
DeletedAt time.Time `xorm:"'DELETED_AT' deleted TIMESTAMP"`
}
err := testEngine.Sync(new(TestInsertNotDeletedTimeStampStruct))
assert.NoError(t, err)
var v3 TestInsertNotDeletedTimeStampStruct
_, err = testEngine.Insert(&v3)
assert.NoError(t, err)
var v4 TestInsertNotDeletedTimeStampStruct
has, err := testEngine.Get(&v4)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, v4.DeletedAt, time.Time{})
}
type MyAutoTimeFields1 struct {
Id int64
Dt time.Time `xorm:"created DATETIME"`
}
func (MyAutoTimeFields1) TableName() string {
return "my_auto_time_fields"
}
type MyAutoTimeFields2 struct {
Id int64
Dt time.Time `xorm:"created"`
}
func (MyAutoTimeFields2) TableName() string {
return "my_auto_time_fields"
}
func TestAutoTimeFields(t *testing.T) {
assert.NoError(t, PrepareEngine())
assertSync(t, new(MyAutoTimeFields1))
_, err := testEngine.Insert(&MyAutoTimeFields1{})
assert.NoError(t, err)
var res []MyAutoTimeFields2
assert.NoError(t, testEngine.Find(&res))
assert.EqualValues(t, 1, len(res))
_, err = testEngine.Insert(&MyAutoTimeFields2{})
assert.NoError(t, err)
res = []MyAutoTimeFields2{}
assert.NoError(t, testEngine.Find(&res))
assert.EqualValues(t, 2, len(res))
}

View File

@ -7,11 +7,11 @@ package tests
import (
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"xorm.io/xorm/convert"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
@ -1201,8 +1201,10 @@ func TestTagTime(t *testing.T) {
has, err = testEngine.Table("tag_u_t_c_struct").Cols("created").Get(&tm)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"),
strings.ReplaceAll(strings.ReplaceAll(tm, "T", " "), "Z", ""))
tmTime, err := convert.String2Time(tm, time.UTC, time.UTC)
assert.NoError(t, err)
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"), tmTime.Format("2006-01-02 15:04:05"))
}
func TestTagAutoIncr(t *testing.T) {

View File

@ -619,3 +619,95 @@ func TestMyArray(t *testing.T) {
assert.True(t, has)
assert.EqualValues(t, v, m.Content)
}
type ZDecimal struct {
value *big.Int
}
func (d *ZDecimal) FromDB(data []byte) error {
i, _ := strconv.ParseInt(string(data), 10, 64)
*d = ZDecimal{
value: big.NewInt(i),
}
return nil
}
func (d ZDecimal) ToDB() ([]byte, error) {
if d.value == nil {
return []byte("0"), nil
}
return []byte(fmt.Sprintf("%d", (d.value).Int64())), nil
}
func (d ZDecimal) IsZero() bool {
if d.value == nil {
return true
}
return d.value.Sign() == 0
}
func (d ZDecimal) String() string {
if d.value == nil {
return "0"
}
return d.value.String()
}
func TestZDecimal(t *testing.T) {
type ZMyMoney struct {
Id int64
Account string
Amount ZDecimal
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(ZMyMoney))
_, err := testEngine.Insert(&ZMyMoney{
Account: "test",
Amount: ZDecimal{
value: big.NewInt(10000000000000000),
},
})
assert.NoError(t, err)
m := ZMyMoney{
Id: 1,
}
has, err := testEngine.Get(&m)
assert.NoError(t, err)
assert.True(t, has)
_, err = testEngine.Update(&ZMyMoney{
Id: 1,
Account: "test2",
})
assert.NoError(t, err)
m2 := ZMyMoney{
Id: 1,
}
has, err = testEngine.Get(&m2)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, "test2", "test2")
assert.False(t, m2.Amount.IsZero())
assert.Equal(t, "10000000000000000", m2.Amount.String())
_, err = testEngine.AllCols().Update(&ZMyMoney{
Id: 1,
Account: "test3",
})
assert.NoError(t, err)
var m3 = ZMyMoney{
Id: 1,
}
has, err = testEngine.Get(&m3)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, "test3", "test3")
assert.True(t, m3.Amount.IsZero())
assert.Equal(t, "0", m3.Amount.String())
}