Skip to content

Commit 31c5d5a

Browse files
committed
Merge branch 'master' of https://github.com/gogf/gf into develop
2 parents 6abbc57 + ef04c8a commit 31c5d5a

File tree

9 files changed

+2052
-56
lines changed

9 files changed

+2052
-56
lines changed

.github/workflows/gf.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,23 @@ jobs:
9797
- 8090:8090
9898
- 8091:8091
9999

100+
#oracle 11g server
101+
oracle-server:
102+
image: loads/oracle-xe-11g-r2:latest
103+
env:
104+
ORACLE_ALLOW_REMOTE: true
105+
ORACLE_SID: XE
106+
ORACLE_DB_USER_NAME: system
107+
ORACLE_DB_PASSWORD: oracle
108+
ports:
109+
- 1521:1521
110+
111+
112+
100113
strategy:
101114
matrix:
102-
go: ["1.15", "1.16", "1.17"]
103-
goarch: ["386", "amd64"]
115+
go: [ "1.15", "1.16", "1.17" ]
116+
goarch: [ "386", "amd64" ]
104117

105118

106119
steps:
@@ -131,7 +144,9 @@ jobs:
131144
dirpath=$(dirname $file)
132145
133146
if [ "oracle" = $(basename $dirpath) ]; then
134-
continue 1
147+
if ! go version|grep -q "1.17"; then
148+
continue 1
149+
fi
135150
fi
136151
137152
cd $dirpath

contrib/drivers/mssql/mssql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ ORDER BY a.id,a.colorder`,
299299
}
300300
fields = make(map[string]*gdb.TableField)
301301
for i, m := range result {
302+
302303
fields[m["Field"].String()] = &gdb.TableField{
303304
Index: i,
304305
Name: m["Field"].String(),

contrib/drivers/mssql/mssql_z_basic_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,42 @@ func TestTableFields(t *testing.T) {
6363
gtest.C(t, func(t *gtest.T) {
6464
createTable("t_user")
6565
defer dropTable("t_user")
66-
var expect map[string]string = map[string]string{
67-
"ID": "numeric(10,0)",
68-
"PASSPORT": "VARCHAR(45)",
69-
"PASSWORD": "CHAR(32)",
70-
"NICKNAME": "VARCHAR(45)",
71-
"CREATE_TIME": "time",
66+
var expect = map[string][]interface{}{
67+
"ID": {"numeric(10,0)", false, "PRI", "", "", ""},
68+
"PASSPORT": {"varchar(45)", true, "", "", "", ""},
69+
"PASSWORD": {"varchar(32)", true, "", "", "", ""},
70+
"NICKNAME": {"varchar(45)", true, "", "", "", ""},
71+
"CREATE_TIME": {"datetime", true, "", "", "", ""},
7272
}
7373

7474
res, err := db.TableFields(context.Background(), "t_user")
7575
gtest.Assert(err, nil)
7676

77-
for k, _ := range expect {
77+
for k, v := range expect {
7878
_, ok := res[k]
7979
gtest.AssertEQ(ok, true)
80-
8180
gtest.AssertEQ(res[k].Name, k)
81+
gtest.AssertEQ(res[k].Type, v[0])
82+
gtest.AssertEQ(res[k].Null, v[1])
83+
gtest.AssertEQ(res[k].Key, v[2])
84+
gtest.AssertEQ(res[k].Default, v[3])
85+
gtest.AssertEQ(res[k].Extra, v[4])
86+
gtest.AssertEQ(res[k].Comment, v[5])
8287
}
8388

8489
res, err = db.TableFields(context.Background(), "t_user", "test")
8590
gtest.Assert(err, nil)
8691

87-
for k, _ := range expect {
92+
for k, v := range expect {
8893
_, ok := res[k]
8994
gtest.AssertEQ(ok, true)
90-
9195
gtest.AssertEQ(res[k].Name, k)
96+
gtest.AssertEQ(res[k].Type, v[0])
97+
gtest.AssertEQ(res[k].Null, v[1])
98+
gtest.AssertEQ(res[k].Key, v[2])
99+
gtest.AssertEQ(res[k].Default, v[3])
100+
gtest.AssertEQ(res[k].Extra, v[4])
101+
gtest.AssertEQ(res[k].Comment, v[5])
92102
}
93103
})
94104

contrib/drivers/oracle/go.mod

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
module github.com/gogf/gf/contrib/drivers/oracle/v2
22

3-
go 1.15
3+
go 1.17
44

55
require (
66
github.com/gogf/gf/v2 v2.0.0
7-
github.com/mattn/go-oci8 v0.1.1
7+
github.com/sijms/go-ora/v2 v2.4.20
8+
)
9+
10+
require (
11+
github.com/BurntSushi/toml v1.1.0 // indirect
12+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
13+
github.com/clbanning/mxj/v2 v2.5.5 // indirect
14+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
15+
github.com/fatih/color v1.13.0 // indirect
16+
github.com/fsnotify/fsnotify v1.5.4 // indirect
17+
github.com/go-logr/logr v1.2.3 // indirect
18+
github.com/go-logr/stdr v1.2.2 // indirect
19+
github.com/go-redis/redis/v8 v8.11.5 // indirect
20+
github.com/gorilla/websocket v1.5.0 // indirect
21+
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
22+
github.com/magiconair/properties v1.8.6 // indirect
23+
github.com/mattn/go-colorable v0.1.9 // indirect
24+
github.com/mattn/go-isatty v0.0.14 // indirect
25+
github.com/mattn/go-runewidth v0.0.9 // indirect
26+
github.com/olekukonko/tablewriter v0.0.5 // indirect
27+
go.opentelemetry.io/otel v1.7.0 // indirect
28+
go.opentelemetry.io/otel/sdk v1.7.0 // indirect
29+
go.opentelemetry.io/otel/trace v1.7.0 // indirect
30+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
31+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
32+
golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2 // indirect
33+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
834
)
935

1036
replace github.com/gogf/gf/v2 => ../../../

contrib/drivers/oracle/go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
3434
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
3535
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
3636
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
37-
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
3837
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
3938
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
4039
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
@@ -56,8 +55,6 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
5655
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
5756
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
5857
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
59-
github.com/mattn/go-oci8 v0.1.1 h1:aEUDxNAyDG0tv8CA3TArnDQNyc4EhnWlsfxRgDHABHM=
60-
github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI=
6158
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
6259
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
6360
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
@@ -70,7 +67,6 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
7067
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
7168
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
7269
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
73-
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
7470
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
7571
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
7672
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
@@ -79,6 +75,8 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
7975
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
8076
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8177
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
78+
github.com/sijms/go-ora/v2 v2.4.20 h1:9e3z7VLBQXRAHGiIda1GEFtRhfxata0LghyMZqvLKew=
79+
github.com/sijms/go-ora/v2 v2.4.20/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
8280
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8381
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
8482
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
@@ -142,7 +140,6 @@ golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
142140
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
143141
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
144142
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
145-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
146143
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
147144
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
148145
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
@@ -151,7 +148,6 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
151148
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
152149
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
153150
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
154-
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
155151
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
156152
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
157153
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

contrib/drivers/oracle/oracle.go

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// You can obtain one at https://github.com/gogf/gf.
66
//
77
// Note:
8-
// 1. It needs manually import: _ "github.com/mattn/go-oci8"
8+
// 1. It needs manually import: _ "github.com/sijms/go-ora/v2"
99
// 2. It does not support Save/Replace features.
1010
// 3. It does not support LastInsertId.
1111

@@ -16,12 +16,7 @@ import (
1616
"context"
1717
"database/sql"
1818
"fmt"
19-
"reflect"
20-
"strconv"
21-
"strings"
22-
"time"
23-
24-
_ "github.com/mattn/go-oci8"
19+
gora "github.com/sijms/go-ora/v2"
2520

2621
"github.com/gogf/gf/v2/container/gmap"
2722
"github.com/gogf/gf/v2/database/gdb"
@@ -30,6 +25,8 @@ import (
3025
"github.com/gogf/gf/v2/text/gregex"
3126
"github.com/gogf/gf/v2/text/gstr"
3227
"github.com/gogf/gf/v2/util/gconv"
28+
"strconv"
29+
"strings"
3330
)
3431

3532
// Driver is the driver for oracle database.
@@ -65,20 +62,26 @@ func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
6562
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
6663
var (
6764
source string
68-
underlyingDriverName = "oci8"
65+
underlyingDriverName = "oracle"
6966
)
70-
// [username/[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
67+
68+
options := map[string]string{
69+
"CONNECTION TIMEOUT": "60",
70+
"PREFETCH_ROWS": "25",
71+
}
72+
73+
if config.Debug {
74+
options["TRACE FILE"] = "oracle_trace.log"
75+
}
76+
// [username:[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
7177
if config.Link != "" {
7278
source = config.Link
7379
// Custom changing the schema in runtime.
7480
if config.Name != "" {
7581
source, _ = gregex.ReplaceString(`@(.+?)/([\w\.\-]+)+`, "@$1/"+config.Name, source)
7682
}
7783
} else {
78-
source = fmt.Sprintf(
79-
"%s/%s@%s:%s/%s",
80-
config.User, config.Pass, config.Host, config.Port, config.Name,
81-
)
84+
source = gora.BuildUrl(config.Host, gconv.Int(config.Port), config.Name, config.User, config.Pass, options)
8285
}
8386

8487
if db, err = sql.Open(underlyingDriverName, source); err != nil {
@@ -99,8 +102,8 @@ func (d *Driver) FilteredLink() string {
99102
return ""
100103
}
101104
s, _ := gregex.ReplaceString(
102-
`(.+?)\s*/\s*(.+)\s*@\s*(.+)\s*:\s*(\d+)\s*/\s*(.+)`,
103-
`$1/xxx@$3:$4/$5`,
105+
`(.+?)\s*:\s*(.+)\s*@\s*(.+)\s*:\s*(\d+)\s*/\s*(.+)`,
106+
`$1:xxx@$3:$4/$5`,
104107
linkInfo,
105108
)
106109
return s
@@ -124,16 +127,7 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
124127
return fmt.Sprintf(":v%d", index)
125128
})
126129
newSql, _ = gregex.ReplaceString("\"", "", newSql)
127-
// Handle string datetime argument.
128-
for i, v := range args {
129-
if reflect.TypeOf(v).Kind() == reflect.String {
130-
valueStr := gconv.String(v)
131-
if gregex.IsMatchString(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, valueStr) {
132-
// args[i] = fmt.Sprintf(`TO_DATE('%s','yyyy-MM-dd HH:MI:SS')`, valueStr)
133-
args[i], _ = time.ParseInLocation("2006-01-02 15:04:05", valueStr, time.Local)
134-
}
135-
}
136-
}
130+
137131
newSql = d.parseSql(newSql)
138132
newArgs = args
139133
return
@@ -168,16 +162,24 @@ func (d *Driver) parseSql(sql string) string {
168162
strings.EqualFold(queryExpr[3], "LIMIT") == false {
169163
break
170164
}
171-
first, limit := 0, 0
165+
page, limit := 0, 0
172166
for i := 1; i < len(allMatch[index]); i++ {
173167
if len(strings.TrimSpace(allMatch[index][i])) == 0 {
174168
continue
175169
}
176170

177171
if strings.HasPrefix(allMatch[index][i], "LIMIT") {
178172
if allMatch[index][i+2] != "" {
179-
first, _ = strconv.Atoi(allMatch[index][i+1])
173+
page, _ = strconv.Atoi(allMatch[index][i+1])
180174
limit, _ = strconv.Atoi(allMatch[index][i+2])
175+
176+
if page <= 0 {
177+
page = 1
178+
}
179+
180+
limit = (page/limit + 1) * limit
181+
182+
page, _ = strconv.Atoi(allMatch[index][i+1])
181183
} else {
182184
limit, _ = strconv.Atoi(allMatch[index][i+1])
183185
}
@@ -187,8 +189,8 @@ func (d *Driver) parseSql(sql string) string {
187189
sql = fmt.Sprintf(
188190
"SELECT * FROM "+
189191
"(SELECT GFORM.*, ROWNUM ROWNUM_ FROM (%s %s) GFORM WHERE ROWNUM <= %d)"+
190-
" WHERE ROWNUM_ >= %d",
191-
queryExpr[1], queryExpr[2], limit, first,
192+
" WHERE ROWNUM_ > %d",
193+
queryExpr[1], queryExpr[2], limit, page,
192194
)
193195
}
194196
return sql
@@ -241,7 +243,7 @@ SELECT
241243
CASE DATA_TYPE
242244
WHEN 'NUMBER' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
243245
WHEN 'FLOAT' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
244-
ELSE DATA_TYPE||'('||DATA_LENGTH||')' END AS TYPE
246+
ELSE DATA_TYPE||'('||DATA_LENGTH||')' END AS TYPE,NULLABLE
245247
FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`,
246248
strings.ToUpper(table),
247249
)
@@ -256,10 +258,16 @@ FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`,
256258
}
257259
fields = make(map[string]*gdb.TableField)
258260
for i, m := range result {
259-
fields[strings.ToLower(m["FIELD"].String())] = &gdb.TableField{
261+
isNull := false
262+
if m["NULLABLE"].String() == "Y" {
263+
isNull = true
264+
}
265+
266+
fields[m["FIELD"].String()] = &gdb.TableField{
260267
Index: i,
261-
Name: strings.ToLower(m["FIELD"].String()),
262-
Type: strings.ToLower(m["TYPE"].String()),
268+
Name: m["FIELD"].String(),
269+
Type: m["TYPE"].String(),
270+
Null: isNull,
263271
}
264272
}
265273
return fields
@@ -288,10 +296,10 @@ func (d *Driver) DoInsert(
288296
) (result sql.Result, err error) {
289297
switch option.InsertOption {
290298
case gdb.InsertOptionSave:
291-
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
299+
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by oracle driver`)
292300

293301
case gdb.InsertOptionReplace:
294-
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
302+
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by oracle driver`)
295303
}
296304

297305
var (

0 commit comments

Comments
 (0)