diff --git a/database/gdb/gdb_core_structure.go b/database/gdb/gdb_core_structure.go index 452de2345..852c7aa58 100644 --- a/database/gdb/gdb_core_structure.go +++ b/database/gdb/gdb_core_structure.go @@ -86,9 +86,7 @@ func (c *Core) ConvertValueForField(ctx context.Context, fieldType string, field // If `value` implements interface `driver.Valuer`, it then uses the interface for value converting. if valuer, ok := fieldValue.(driver.Valuer); ok { if convertedValue, err = valuer.Value(); err != nil { - if err != nil { - return nil, err - } + return nil, err } return convertedValue, nil } diff --git a/database/gdb/gdb_model_hook.go b/database/gdb/gdb_model_hook.go index fec30f759..7dd007405 100644 --- a/database/gdb/gdb_model_hook.go +++ b/database/gdb/gdb_model_hook.go @@ -137,6 +137,9 @@ func (h *HookSelectInput) Next(ctx context.Context) (result Result, err error) { return fmt.Sprintf(` FROM %s%s%s`, charL, h.Table, charR) }, ) + if err != nil { + return + } } // Schema change. if h.Schema != "" && h.Schema != h.originalSchemaName.String() { diff --git a/database/gdb/gdb_z_mysql_internal_test.go b/database/gdb/gdb_z_mysql_internal_test.go index 538ab967d..abf82b6a1 100644 --- a/database/gdb/gdb_z_mysql_internal_test.go +++ b/database/gdb/gdb_z_mysql_internal_test.go @@ -7,7 +7,6 @@ package gdb import ( - "context" "fmt" "testing" @@ -15,11 +14,6 @@ import ( "github.com/gogf/gf/v2/text/gregex" ) -var ( - db DB - ctx = context.TODO() -) - func Test_HookSelect_Regex(t *testing.T) { gtest.C(t, func(t *gtest.T) { var ( diff --git a/example/database/mysql/driver-encrypt-decrypt-password/config.yaml b/example/database/mysql/driver-encrypt-decrypt-password/config.yaml new file mode 100644 index 000000000..ab56e68eb --- /dev/null +++ b/example/database/mysql/driver-encrypt-decrypt-password/config.yaml @@ -0,0 +1,3 @@ +database: + default: + - link: "hellosql:root:cm9vdDEyMw==@tcp(127.0.0.1:3306)/focus" diff --git a/example/database/mysql/driver-encrypt-decrypt-password/main.go b/example/database/mysql/driver-encrypt-decrypt-password/main.go new file mode 100644 index 000000000..421372d5f --- /dev/null +++ b/example/database/mysql/driver-encrypt-decrypt-password/main.go @@ -0,0 +1,82 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// 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 main + +import ( + "context" + "database/sql" + "fmt" + + "github.com/gogf/gf/contrib/drivers/mysql/v2" + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/encoding/gbase64" + "github.com/gogf/gf/v2/frame/g" +) + +const ( + mysqlDriverName = "hellosql" + quoteChar = "`" +) + +func init() { + var ( + err error + driverObj = &DriverMysql{ + Driver: mysql.Driver{}, + } + ) + if err = gdb.Register(mysqlDriverName, driverObj); err != nil { + panic(err) + } +} + +// New creates and returns a database object for mysql. +// It implements the interface of gdb.Driver for extra database driver installation. +func (d *DriverMysql) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) { + return &DriverMysql{ + Driver: mysql.Driver{Core: core}, + }, nil +} + +// GetChars returns the security char for this type of database. +func (d *DriverMysql) GetChars() (charLeft string, charRight string) { + return quoteChar, quoteChar +} + +func (d *DriverMysql) Open(config *gdb.ConfigNode) (db *sql.DB, err error) { + fmt.Println("DriverMysql.Open") + fmt.Println("config.Pass(encode):" + config.Pass) + // Decrypt the password if it is encrypted. + config.Pass, err = gbase64.DecodeToString(config.Pass) + if err != nil { + return nil, err + } + fmt.Println("config.Pass(decode):" + config.Pass) + return d.Driver.Open(config) +} + +func (d *DriverMysql) Tables(ctx context.Context, schema ...string) (tables []string, err error) { + return d.Driver.Tables(ctx, schema...) +} + +func (d *DriverMysql) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) { + return d.Driver.TableFields(ctx, table, schema...) +} + +// DriverMysql is the driver for mysql database. +type DriverMysql struct { + mysql.Driver +} + +func main() { + list, err := g.DB().Tables(context.Background()) + if err != nil { + panic(err) + } + fmt.Println(list) +}