Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #159 from askuy/feature/dm
Browse files Browse the repository at this point in the history
support dm
  • Loading branch information
askuy authored Nov 24, 2021
2 parents 675b09e + e8d3f7c commit 5c7746a
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 10 deletions.
6 changes: 3 additions & 3 deletions egorm/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package egorm
import (
"context"
"errors"
"fmt"

"github.com/gotomicro/ego-component/egorm/dsn"
"github.com/gotomicro/ego/core/elog"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"

"github.com/gotomicro/ego-component/egorm/dsn"
)

// PackageName ...
Expand Down Expand Up @@ -52,6 +52,7 @@ func WithContext(ctx context.Context, db *Component) *Component {
// newComponent ...
func newComponent(compName string, dsnParser dsn.DSNParser, config *config, elogger *elog.Component) (*Component, error) {
db, err := gorm.Open(dsnParser.GetDialector(config.DSN), &gorm.Config{})
fmt.Printf("err--------------->"+"%+v\n", err)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,6 +89,5 @@ func newComponent(compName string, dsnParser dsn.DSNParser, config *config, elog
replace(db.Callback().Query(), "gorm:query", config.interceptors...)
// replace(db.Callback().Row(), "gorm:row", config.interceptors...)
replace(db.Callback().Raw(), "gorm:raw", config.interceptors...)

return db, nil
}
1 change: 1 addition & 0 deletions egorm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
dialectMysql = "mysql"
dialectPostgres = "postgres"
dialectDm = "dm"
)

var errSupportDialect = errors.New("invalid support Dialect")
Expand Down
2 changes: 2 additions & 0 deletions egorm/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (c *Container) setDSNParserIfNotExists(dialect string) error {
c.dsnParser = dsn.DefaultMysqlDSNParser
case dialectPostgres:
c.dsnParser = dsn.DefaultPostgresDSNParser
case dialectDm:
c.dsnParser = dsn.DefaultDmDSNParser
default:
return errSupportDialect
}
Expand Down
69 changes: 69 additions & 0 deletions egorm/dsn/dm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dsn

import (
"github.com/gotomicro/gorm-driver-dm"
"gorm.io/gorm"
)

var (
DefaultDmDSNParser = &DmDSNParser{}
_ DSNParser = (*DmDSNParser)(nil)
)

type DmDSNParser struct {
}

func (m *DmDSNParser) GetDialector(dsn string) gorm.Dialector {
return dm.Open(dsn)
}

func (m *DmDSNParser) ParseDSN(dsn string) (cfg *DSN, err error) {
// New config with some default values
cfg = new(DSN)

// [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]
// Find the last '/' (since the password or the net addr might contain a '/')
//foundSlash := false
for i := len(dsn) - 1; i >= 0; i-- {
if dsn[i] == '/' {
//foundSlash = true
var j int

// left part is empty if i <= 0
if i > 0 {
// [username[:password]@][protocol[(address)]]
// Find the last '@' in dsn[:i]
for j = i; j >= 0; j-- {
if dsn[j] == '@' {
parseUsernamePassword(cfg, dsn[:j])
break
}
}

// [protocol[(address)]]
// Find the first '(' in dsn[j+1:i]
if err = parseAddrNet(cfg, dsn[j:i]); err != nil {
return
}
}

// dbname[?param1=value1&...&paramN=valueN]
// Find the first '?' in dsn[i+1:]
for j = i + 1; j < len(dsn); j++ {
if dsn[j] == '?' {
if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
return
}
break
}
}
//cfg.DBName = dsn[i+1 : j]

break
}
}
//if !foundSlash && len(dsn) > 0 {
// return nil, errInvalidDSNNoSlash
//}
return
}
9 changes: 6 additions & 3 deletions egorm/examples/commonmysql/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# enableDetailSQL = false
# enableAccessInterceptor = true
# enableAccessInterceptorReq = true
# enableAccessInterceptorReply = true
# enableAccessInterceptorRes = true


[pg.test]
dialect = "postgres"
Expand All @@ -27,7 +28,8 @@
# enableDetailSQL = false
# enableAccessInterceptor = true
# enableAccessInterceptorReq = true
# enableAccessInterceptorReply = true
# enableAccessInterceptorRes = true


[other.test]
dialect = "other"
Expand All @@ -43,4 +45,5 @@
# enableDetailSQL = false
# enableAccessInterceptor = true
# enableAccessInterceptorReq = true
# enableAccessInterceptorReply = true
# enableAccessInterceptorRes = true

15 changes: 15 additions & 0 deletions egorm/examples/dm/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[dm.test]
dialect = "dm"
debug = true # ego重写gorm debug,打开后可以看到,配置名、地址、耗时、请求数据、响应数据
# rawDebug = true 原生gorm debug
dsn = "sysdba:shimo2021@192.168.242.136:5236/SYSDBA"
onFail = "panic" # 失败后,直接fail fast,panic操作
# connMaxLifetime = "300s"
# maxIdleConns = 50
# maxOpenConns = 100
# disableTrace = false
# disableMetric = false
# enableDetailSQL = false
# enableAccessInterceptor = true
# enableAccessInterceptorReq = true
# enableAccessInterceptorRes = true
34 changes: 34 additions & 0 deletions egorm/examples/dm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"

"github.com/gotomicro/ego"
"github.com/gotomicro/ego-component/egorm"
)

func main() {
err := ego.New().Invoker(
openDB,
).Run()
fmt.Printf("err--------------->"+"%+v\n", err)
}

func openDB() error {
db := egorm.Load("dm.test").Build()
fmt.Printf("db--------------->"+"%+v\n", db)
//1.查询多少张表
tables := make([]UserTabComments, 0)
db.Find(&tables)
// select TABLE_NAME,comments TABLE_COMMENT from user_tab_comments
fmt.Printf("tables--------------->"+"%+v\n", tables)
for _, value := range tables {
fmt.Printf("value--------------->"+"%+v\n", value)
}
return nil
}

type UserTabComments struct {
TableName string `gorm:"column:TABLE_NAME"` //表名
TableComment string `gorm:"column:TABLE_COMMENT"` //表注释
}
2 changes: 1 addition & 1 deletion egorm/examples/mysqltrace/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# enableDetailSQL = false
# enableAccessInterceptor = true
# enableAccessInterceptorReq = true
# enableAccessInterceptorReply = true
# enableAccessInterceptorRes = true
27 changes: 27 additions & 0 deletions egorm/examples/origindm/origindm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"database/sql"
"fmt"

_ "github.com/gotomicro/dmgo"
)

func main() {
obj, err := sql.Open("dm", "dm://sysdba:shimo2021@192.168.242.136:5236")
if err != nil {
panic(err)
return
}
rows, err := obj.Query("select TABLE_NAME,comments TABLE_COMMENT from user_tab_comments")
for rows.Next() {
a := TableStruct{}
rows.Scan(&a.TableName, &a.TableComment)
fmt.Printf("a--------------->"+"%+v\n", a)
}
}

type TableStruct struct {
TableName string //表名
TableComment string //表注释
}
11 changes: 8 additions & 3 deletions egorm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ go 1.15

require (
github.com/davecgh/go-spew v1.1.1
github.com/gotomicro/ego v0.6.14
github.com/json-iterator/go v1.1.11
github.com/emirpasic/gods v1.12.0 // indirect
github.com/gotomicro/dmgo v1.8.4 // indirect
github.com/gotomicro/ego v0.7.0
github.com/gotomicro/gorm-driver-dm v0.0.0-20211124105326-5891acf63020 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/json-iterator/go v1.1.12
github.com/opentracing/opentracing-go v1.1.0
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.0
google.golang.org/grpc v1.39.0
github.com/thoas/go-funk v0.9.1 // indirect
google.golang.org/grpc v1.42.0
gorm.io/driver/mysql v1.0.5
gorm.io/driver/postgres v1.0.8
gorm.io/gorm v1.21.3
Expand Down
Loading

0 comments on commit 5c7746a

Please sign in to comment.