mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 10:47:38 +08:00
example: password encrypt-decrypt for database using custom implement driver (#3610)
This commit is contained in:
parent
ee211dbdac
commit
753965b9a9
@ -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
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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 (
|
||||
|
@ -0,0 +1,3 @@
|
||||
database:
|
||||
default:
|
||||
- link: "hellosql:root:cm9vdDEyMw==@tcp(127.0.0.1:3306)/focus"
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user