sijms / go-ora

Pure go oracle client
MIT License
795 stars 176 forks source link

Use a custom dialer to create connection with db #558

Closed XiaoYiXiaoYang closed 4 months ago

XiaoYiXiaoYang commented 4 months ago

when i use "github.com/lib/pq" as sql driver to connect to postgressql i can use func DialOpen(d Dialer, dsn string) (_ driver.Conn, err error) method to connect to db with my custom dialer

and i suggest "github.com/denisenkom/go-mssqldb" need a same one

and "github.com/go-sql-driver/mysql" have a similar method to use my own custom dialer, it is func RegisterDial(network string, dial DialFunc)

sijms commented 4 months ago

use custom configuration

config, err := go_ora.ParseConfig(`yours DSN string`)
// pass object that support dialer context
config.Dialer = new_object
go_ora.RegisterConfig(config)
db, err := sql.Open("oracle", "")

the definition of DilerContext

type DialerContext interface {
    DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
sijms commented 4 months ago

next release (v2.8.19) you can do the following

config, err := go_ora.ParseConfig(`yours DSN string`)
config.RegisterDial(func(ctx context.Context, network, address string) (net.Conn, error) {
    // your custom dial code
})
go_ora.RegisterConfig(config)
db, err := sql.Open("oracle", "")
XiaoYiXiaoYang commented 3 months ago

thanks~~

XiaoYiXiaoYang commented 3 months ago
connector := go_ora.NewConnector(connStr)
connector.(*go_ora.OracleConnector).Dialer(new_obj)
db := sql.OpenDB(connector)
defer db.Close()

this method can worked for me correctly.

XiaoYiXiaoYang commented 3 months ago

next release (v2.8.19) you can do the following

config, err := go_ora.ParseConfig(`yours DSN string`)
config.RegisterDial(func(ctx context.Context, network, address string) (net.Conn, error) {
    // your custom dial code
})
go_ora.RegisterConfig(config)
db, err := sql.Open("oracle", "")

@sijms when i use this method,it doesn't work correctly? can you help me for a review?

func (connector *OracleConnector) Connect(ctx context.Context) (driver.Conn, error) {
    // code
        conn, err := NewConnection(connector.connectString, connector.drv.connOption)
    conn.connOption.Dialer = connector.dialer

    // code
}

my register dial will be overwritten by connector's dialer

sijms commented 2 months ago

sorry for late replay I fix in next release