Skip to content

Commit

Permalink
remove FilteredLink function for DB and all driver implements and i…
Browse files Browse the repository at this point in the history
…mprove details for package gdb (#2142)

* fix: pgsql DoExec Transaction checks (#2101)

Co-authored-by: John Guo <john@johng.cn>

* improve package gdb

* up

* up

* up

* up

* up

* add DriverWrapper and DriverWarapperDB for package gdb

* add DriverWrapper and DriverWarapperDB for package gdb

* up

Co-authored-by: HaiLaz <739476267@qq.com>
  • Loading branch information
gqcn and hailaz authored Sep 24, 2022
1 parent 28a329e commit 399ca23
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 408 deletions.
1 change: 1 addition & 0 deletions .github/workflows/gf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
- 3306:3306

# PostgreSQL backend server.
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=12345678 -e POSTGRES_USER=postgres -e POSTGRES_DB=test -v postgres:/Users/john/Temp/postgresql/data loads/postgres:13
postgres:
image: loads/postgres:13
env:
Expand Down
49 changes: 14 additions & 35 deletions contrib/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
"time"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gutil"
"github.com/google/uuid"
"github.com/shopspring/decimal"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)

Expand Down Expand Up @@ -67,14 +67,14 @@ func New() gdb.Driver {

// New creates and returns a database object for clickhouse.
// It implements the interface of gdb.Driver for extra database driver installation.
func (d *Driver) New(core *gdb.Core, node gdb.ConfigNode) (gdb.DB, error) {
func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
return &Driver{
Core: core,
}, nil
}

// Open creates and returns an underlying sql.DB object for clickhouse.
func (d *Driver) Open(config gdb.ConfigNode) (*sql.DB, error) {
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source := config.Link
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
if config.Link != "" {
Expand Down Expand Up @@ -108,12 +108,14 @@ func (d *Driver) Open(config gdb.ConfigNode) (*sql.DB, error) {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
}
db, err := sql.Open(driverName, source)
if err != nil {
if db, err = sql.Open(driverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`sql.Open failed for driver "%s" by source "%s"`, driverName, source,
)
return nil, err
}

return db, nil
return
}

// Tables retrieves and returns the tables of current schema.
Expand All @@ -140,18 +142,10 @@ func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string,
func (d *Driver) TableFields(
ctx context.Context, table string, schema ...string,
) (fields map[string]*gdb.TableField, err error) {
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "function TableFields supports only single table operations")
}
useSchema := d.GetSchema()
if len(schema) > 0 && schema[0] != "" {
useSchema = schema[0]
}
var (
result gdb.Result
link gdb.Link
result gdb.Result
link gdb.Link
useSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
if link, err = d.SlaveLink(useSchema); err != nil {
return nil, err
Expand Down Expand Up @@ -192,21 +186,6 @@ func (d *Driver) TableFields(
return fields, nil
}

// FilteredLink retrieves and returns filtered `linkInfo` that can be using for
// logging or tracing purpose.
func (d *Driver) FilteredLink() string {
linkInfo := d.GetConfig().Link
if linkInfo == "" {
return ""
}
s, _ := gregex.ReplaceString(
`(.+?):(.+)@tcp(.+)`,
`$1:xxx@tcp$3`,
linkInfo,
)
return s
}

// PingMaster pings the master node to check authentication or keeps the connection alive.
func (d *Driver) PingMaster() error {
conn, err := d.Master()
Expand Down
41 changes: 8 additions & 33 deletions contrib/drivers/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package mssql implements gdb.Driver, which supports operations for MSSql.
//
// Note:
// 1. It needs manually import: _ "github.com/denisenkom/go-mssqldb"
// 2. It does not support Save/Replace features.
// 3. It does not support LastInsertId.

// Package mssql implements gdb.Driver, which supports operations for MSSql.
package mssql

import (
Expand All @@ -20,6 +20,7 @@ import (
"strings"

_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/v2/util/gutil"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
Expand All @@ -46,14 +47,14 @@ func New() gdb.Driver {

// New creates and returns a database object for SQL server.
// It implements the interface of gdb.Driver for extra database driver installation.
func (d *Driver) New(core *gdb.Core, node gdb.ConfigNode) (gdb.DB, error) {
func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
return &Driver{
Core: core,
}, nil
}

// Open creates and returns an underlying sql.DB object for mssql.
func (d *Driver) Open(config gdb.ConfigNode) (db *sql.DB, err error) {
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "sqlserver"
Expand Down Expand Up @@ -93,21 +94,6 @@ func (d *Driver) Open(config gdb.ConfigNode) (db *sql.DB, err error) {
return
}

// FilteredLink retrieves and returns filtered `linkInfo` that can be using for
// logging or tracing purpose.
func (d *Driver) FilteredLink() string {
linkInfo := d.GetConfig().Link
if linkInfo == "" {
return ""
}
s, _ := gregex.ReplaceString(
`(.+);\s*password=(.+);\s*server=(.+)`,
`$1;password=xxx;server=$3`,
d.GetConfig().Link,
)
return s
}

// GetChars returns the security char for this type of database.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return `"`, `"`
Expand Down Expand Up @@ -251,21 +237,10 @@ func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string,
//
// Also see DriverMysql.TableFields.
func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, gerror.NewCode(
gcode.CodeInvalidParameter, "function TableFields supports only single table operations",
)
}
useSchema := d.GetSchema()
if len(schema) > 0 && schema[0] != "" {
useSchema = schema[0]
}

var (
result gdb.Result
link gdb.Link
result gdb.Result
link gdb.Link
useSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
if link, err = d.SlaveLink(useSchema); err != nil {
return nil, err
Expand Down
24 changes: 3 additions & 21 deletions contrib/drivers/mssql/mssql_z_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ package mssql_test
import (
"context"
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/encoding/gxml"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"testing"
"time"
)

func TestTables(t *testing.T) {
Expand Down Expand Up @@ -108,25 +109,6 @@ func TestTableFields(t *testing.T) {
})
}

func TestFilteredLink(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := db.FilteredLink()
gtest.AssertEQ(s, "")
})

gtest.C(t, func(t *gtest.T) {
_, err := dblink.Query(ctx, "select 1")
gtest.Assert(err, nil)

s := dblink.FilteredLink()
gtest.AssertNE(s, nil)
})

gtest.C(t, func(t *gtest.T) {
_, err := dbErr.Query(ctx, "select 1")
gtest.AssertNE(err, nil)
})
}
func TestDoInsert(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
createTable("t_user")
Expand Down
43 changes: 8 additions & 35 deletions contrib/drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (

_ "github.com/go-sql-driver/mysql"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gutil"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)

// Driver is the driver for mysql database.
Expand Down Expand Up @@ -48,15 +48,15 @@ func New() gdb.Driver {

// New creates and returns a database object for mysql.
// It implements the interface of gdb.Driver for extra database driver installation.
func (d *Driver) New(core *gdb.Core, node gdb.ConfigNode) (gdb.DB, error) {
func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
return &Driver{
Core: core,
}, nil
}

// Open creates and returns an underlying sql.DB object for mysql.
// Note that it converts time.Time argument to local timezone in default.
func (d *Driver) Open(config gdb.ConfigNode) (db *sql.DB, err error) {
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "mysql"
Expand All @@ -73,8 +73,8 @@ func (d *Driver) Open(config gdb.ConfigNode) (db *sql.DB, err error) {
}
} else {
source = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=%s",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
"%s:%s@%s(%s:%s)/%s?charset=%s",
config.User, config.Pass, config.Protocol, config.Host, config.Port, config.Name, config.Charset,
)
if config.Timezone != "" {
source = fmt.Sprintf("%s&loc=%s", source, url.QueryEscape(config.Timezone))
Expand All @@ -93,21 +93,6 @@ func (d *Driver) Open(config gdb.ConfigNode) (db *sql.DB, err error) {
return
}

// FilteredLink retrieves and returns filtered `linkInfo` that can be using for
// logging or tracing purpose.
func (d *Driver) FilteredLink() string {
linkInfo := d.GetConfig().Link
if linkInfo == "" {
return ""
}
s, _ := gregex.ReplaceString(
`(.+?):(.+)@tcp(.+)`,
`$1:xxx@tcp$3`,
linkInfo,
)
return s
}

// GetChars returns the security char for this type of database.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return "`", "`"
Expand Down Expand Up @@ -153,22 +138,10 @@ func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string,
func (d *Driver) TableFields(
ctx context.Context, table string, schema ...string,
) (fields map[string]*gdb.TableField, err error) {
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, gerror.NewCode(
gcode.CodeInvalidParameter,
"function TableFields supports only single table operations",
)
}
useSchema := d.GetSchema()
if len(schema) > 0 && schema[0] != "" {
useSchema = schema[0]
}

var (
result gdb.Result
link gdb.Link
result gdb.Result
link gdb.Link
useSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
)
if link, err = d.SlaveLink(useSchema); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/mysql/mysql_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ func Test_Core_ClearTableFields(t *testing.T) {
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
fields, err := db.GetCore().TableFields(ctx, table)
fields, err := db.TableFields(ctx, table)
t.AssertNil(err)
t.Assert(len(fields), 5)
})
Expand Down
Loading

0 comments on commit 399ca23

Please sign in to comment.