-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: password encrypt-decrypt for database using custom implement…
… driver (#3610)
- Loading branch information
Showing
5 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
example/database/mysql/driver-encrypt-decrypt-password/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
example/database/mysql/driver-encrypt-decrypt-password/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |