Skip to content

Commit

Permalink
Feature/oracle (#1869)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzi1 authored May 25, 2022
1 parent 0ca81bd commit ef04c8a
Show file tree
Hide file tree
Showing 9 changed files with 2,052 additions and 56 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/gf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,23 @@ jobs:
- 8090:8090
- 8091:8091

#oracle 11g server
oracle-server:
image: loads/oracle-xe-11g-r2:latest
env:
ORACLE_ALLOW_REMOTE: true
ORACLE_SID: XE
ORACLE_DB_USER_NAME: system
ORACLE_DB_PASSWORD: oracle
ports:
- 1521:1521



strategy:
matrix:
go: ["1.15", "1.16", "1.17"]
goarch: ["386", "amd64"]
go: [ "1.15", "1.16", "1.17" ]
goarch: [ "386", "amd64" ]


steps:
Expand Down Expand Up @@ -131,7 +144,9 @@ jobs:
dirpath=$(dirname $file)
if [ "oracle" = $(basename $dirpath) ]; then
continue 1
if ! go version|grep -q "1.17"; then
continue 1
fi
fi
cd $dirpath
Expand Down
1 change: 1 addition & 0 deletions contrib/drivers/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ ORDER BY a.id,a.colorder`,
}
fields = make(map[string]*gdb.TableField)
for i, m := range result {

fields[m["Field"].String()] = &gdb.TableField{
Index: i,
Name: m["Field"].String(),
Expand Down
30 changes: 20 additions & 10 deletions contrib/drivers/mssql/mssql_z_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,42 @@ func TestTableFields(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
createTable("t_user")
defer dropTable("t_user")
var expect map[string]string = map[string]string{
"ID": "numeric(10,0)",
"PASSPORT": "VARCHAR(45)",
"PASSWORD": "CHAR(32)",
"NICKNAME": "VARCHAR(45)",
"CREATE_TIME": "time",
var expect = map[string][]interface{}{
"ID": {"numeric(10,0)", false, "PRI", "", "", ""},
"PASSPORT": {"varchar(45)", true, "", "", "", ""},
"PASSWORD": {"varchar(32)", true, "", "", "", ""},
"NICKNAME": {"varchar(45)", true, "", "", "", ""},
"CREATE_TIME": {"datetime", true, "", "", "", ""},
}

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

for k, _ := range expect {
for k, v := range expect {
_, ok := res[k]
gtest.AssertEQ(ok, true)

gtest.AssertEQ(res[k].Name, k)
gtest.AssertEQ(res[k].Type, v[0])
gtest.AssertEQ(res[k].Null, v[1])
gtest.AssertEQ(res[k].Key, v[2])
gtest.AssertEQ(res[k].Default, v[3])
gtest.AssertEQ(res[k].Extra, v[4])
gtest.AssertEQ(res[k].Comment, v[5])
}

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

for k, _ := range expect {
for k, v := range expect {
_, ok := res[k]
gtest.AssertEQ(ok, true)

gtest.AssertEQ(res[k].Name, k)
gtest.AssertEQ(res[k].Type, v[0])
gtest.AssertEQ(res[k].Null, v[1])
gtest.AssertEQ(res[k].Key, v[2])
gtest.AssertEQ(res[k].Default, v[3])
gtest.AssertEQ(res[k].Extra, v[4])
gtest.AssertEQ(res[k].Comment, v[5])
}
})

Expand Down
30 changes: 28 additions & 2 deletions contrib/drivers/oracle/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
module github.com/gogf/gf/contrib/drivers/oracle/v2

go 1.15
go 1.17

require (
github.com/gogf/gf/v2 v2.0.0
github.com/mattn/go-oci8 v0.1.1
github.com/sijms/go-ora/v2 v2.4.20
)

require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/clbanning/mxj/v2 v2.5.5 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/sdk v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace github.com/gogf/gf/v2 => ../../../
8 changes: 2 additions & 6 deletions contrib/drivers/oracle/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand All @@ -56,8 +55,6 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-oci8 v0.1.1 h1:aEUDxNAyDG0tv8CA3TArnDQNyc4EhnWlsfxRgDHABHM=
github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand All @@ -70,7 +67,6 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
Expand All @@ -79,6 +75,8 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sijms/go-ora/v2 v2.4.20 h1:9e3z7VLBQXRAHGiIda1GEFtRhfxata0LghyMZqvLKew=
github.com/sijms/go-ora/v2 v2.4.20/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
Expand Down Expand Up @@ -142,7 +140,6 @@ golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand All @@ -151,7 +148,6 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
78 changes: 43 additions & 35 deletions contrib/drivers/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// You can obtain one at https://github.com/gogf/gf.
//
// Note:
// 1. It needs manually import: _ "github.com/mattn/go-oci8"
// 1. It needs manually import: _ "github.com/sijms/go-ora/v2"
// 2. It does not support Save/Replace features.
// 3. It does not support LastInsertId.

Expand All @@ -16,12 +16,7 @@ import (
"context"
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"time"

_ "github.com/mattn/go-oci8"
gora "github.com/sijms/go-ora/v2"

"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/database/gdb"
Expand All @@ -30,6 +25,8 @@ import (
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"strconv"
"strings"
)

// Driver is the driver for oracle database.
Expand Down Expand Up @@ -65,20 +62,26 @@ func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "oci8"
underlyingDriverName = "oracle"
)
// [username/[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]

options := map[string]string{
"CONNECTION TIMEOUT": "60",
"PREFETCH_ROWS": "25",
}

if config.Debug {
options["TRACE FILE"] = "oracle_trace.log"
}
// [username:[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
if config.Link != "" {
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`@(.+?)/([\w\.\-]+)+`, "@$1/"+config.Name, source)
}
} else {
source = fmt.Sprintf(
"%s/%s@%s:%s/%s",
config.User, config.Pass, config.Host, config.Port, config.Name,
)
source = gora.BuildUrl(config.Host, gconv.Int(config.Port), config.Name, config.User, config.Pass, options)
}

if db, err = sql.Open(underlyingDriverName, source); err != nil {
Expand All @@ -99,8 +102,8 @@ func (d *Driver) FilteredLink() string {
return ""
}
s, _ := gregex.ReplaceString(
`(.+?)\s*/\s*(.+)\s*@\s*(.+)\s*:\s*(\d+)\s*/\s*(.+)`,
`$1/xxx@$3:$4/$5`,
`(.+?)\s*:\s*(.+)\s*@\s*(.+)\s*:\s*(\d+)\s*/\s*(.+)`,
`$1:xxx@$3:$4/$5`,
linkInfo,
)
return s
Expand All @@ -124,16 +127,7 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
return fmt.Sprintf(":v%d", index)
})
newSql, _ = gregex.ReplaceString("\"", "", newSql)
// Handle string datetime argument.
for i, v := range args {
if reflect.TypeOf(v).Kind() == reflect.String {
valueStr := gconv.String(v)
if gregex.IsMatchString(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, valueStr) {
// args[i] = fmt.Sprintf(`TO_DATE('%s','yyyy-MM-dd HH:MI:SS')`, valueStr)
args[i], _ = time.ParseInLocation("2006-01-02 15:04:05", valueStr, time.Local)
}
}
}

newSql = d.parseSql(newSql)
newArgs = args
return
Expand Down Expand Up @@ -168,16 +162,24 @@ func (d *Driver) parseSql(sql string) string {
strings.EqualFold(queryExpr[3], "LIMIT") == false {
break
}
first, limit := 0, 0
page, limit := 0, 0
for i := 1; i < len(allMatch[index]); i++ {
if len(strings.TrimSpace(allMatch[index][i])) == 0 {
continue
}

if strings.HasPrefix(allMatch[index][i], "LIMIT") {
if allMatch[index][i+2] != "" {
first, _ = strconv.Atoi(allMatch[index][i+1])
page, _ = strconv.Atoi(allMatch[index][i+1])
limit, _ = strconv.Atoi(allMatch[index][i+2])

if page <= 0 {
page = 1
}

limit = (page/limit + 1) * limit

page, _ = strconv.Atoi(allMatch[index][i+1])
} else {
limit, _ = strconv.Atoi(allMatch[index][i+1])
}
Expand All @@ -187,8 +189,8 @@ func (d *Driver) parseSql(sql string) string {
sql = fmt.Sprintf(
"SELECT * FROM "+
"(SELECT GFORM.*, ROWNUM ROWNUM_ FROM (%s %s) GFORM WHERE ROWNUM <= %d)"+
" WHERE ROWNUM_ >= %d",
queryExpr[1], queryExpr[2], limit, first,
" WHERE ROWNUM_ > %d",
queryExpr[1], queryExpr[2], limit, page,
)
}
return sql
Expand Down Expand Up @@ -241,7 +243,7 @@ SELECT
CASE DATA_TYPE
WHEN 'NUMBER' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
WHEN 'FLOAT' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
ELSE DATA_TYPE||'('||DATA_LENGTH||')' END AS TYPE
ELSE DATA_TYPE||'('||DATA_LENGTH||')' END AS TYPE,NULLABLE
FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`,
strings.ToUpper(table),
)
Expand All @@ -256,10 +258,16 @@ FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`,
}
fields = make(map[string]*gdb.TableField)
for i, m := range result {
fields[strings.ToLower(m["FIELD"].String())] = &gdb.TableField{
isNull := false
if m["NULLABLE"].String() == "Y" {
isNull = true
}

fields[m["FIELD"].String()] = &gdb.TableField{
Index: i,
Name: strings.ToLower(m["FIELD"].String()),
Type: strings.ToLower(m["TYPE"].String()),
Name: m["FIELD"].String(),
Type: m["TYPE"].String(),
Null: isNull,
}
}
return fields
Expand Down Expand Up @@ -288,10 +296,10 @@ func (d *Driver) DoInsert(
) (result sql.Result, err error) {
switch option.InsertOption {
case gdb.InsertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by oracle driver`)

case gdb.InsertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by oracle driver`)
}

var (
Expand Down
Loading

0 comments on commit ef04c8a

Please sign in to comment.