Skip to content

Commit

Permalink
Introduce RegisterReaderHandlerWithConfig
Browse files Browse the repository at this point in the history
It is the same as RegisterReaderHandler, but receives a copy of the
Config which is be needed to correctly encode the TSV. (For example
for the Location).

issue go-sql-driver#1416
  • Loading branch information
Jille committed Apr 25, 2023
1 parent 8503110 commit e08c23b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions infile.go
Expand Up @@ -19,7 +19,7 @@ import (
var (
fileRegister map[string]bool
fileRegisterLock sync.RWMutex
readerRegister map[string]func() io.Reader
readerRegister map[string]func(*Config) io.Reader
readerRegisterLock sync.RWMutex
)

Expand Down Expand Up @@ -66,10 +66,21 @@ func DeregisterLocalFile(filePath string) {
// if err != nil {
// ...
func RegisterReaderHandler(name string, handler func() io.Reader) {
RegisterReaderHandlerWithConfig(name, func(*Config) io.Reader {
return handler()
})
}

// RegisterReaderHandlerWithConfig is like RegisterReaderHandler but the
// callback receives a copy of the configuration. The configuration should not
// be modified.
// This allows the caller to receive information about the connection, like the
// timezone.
func RegisterReaderHandlerWithConfig(name string, handler func(*Config) io.Reader) {
readerRegisterLock.Lock()
// lazy map init
if readerRegister == nil {
readerRegister = make(map[string]func() io.Reader)
readerRegister = make(map[string]func(*Config) io.Reader)
}

readerRegister[name] = handler
Expand Down Expand Up @@ -110,7 +121,7 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
readerRegisterLock.RUnlock()

if inMap {
rdr = handler()
rdr = handler(mc.cfg.Clone())
if rdr != nil {
if cl, ok := rdr.(io.Closer); ok {
defer deferredClose(&err, cl)
Expand Down

0 comments on commit e08c23b

Please sign in to comment.