Skip to content

Commit

Permalink
example: password encrypt-decrypt for database using custom implement…
Browse files Browse the repository at this point in the history
… driver (gogf#3610)
  • Loading branch information
hailaz authored Jun 4, 2024
1 parent ee211db commit 753965b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
4 changes: 1 addition & 3 deletions database/gdb/gdb_core_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions database/gdb/gdb_model_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 0 additions & 6 deletions database/gdb/gdb_z_mysql_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@
package gdb

import (
"context"
"fmt"
"testing"

"github.com/gogf/gf/v2/test/gtest"
"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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
database:
default:
- link: "hellosql:root:cm9vdDEyMw==@tcp(127.0.0.1:3306)/focus"
82 changes: 82 additions & 0 deletions example/database/mysql/driver-encrypt-decrypt-password/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 753965b

Please sign in to comment.