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 #1416
  • Loading branch information
Jille committed Apr 25, 2023
1 parent 8503110 commit 39a9638
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Jeffrey Charles <jeffreycharles at gmail.com>
Jerome Meyer <jxmeyer at gmail.com>
Jiajia Zhong <zhong2plus at gmail.com>
Jian Zhen <zhenjl at gmail.com>
Jille Timmermans <jille at quis.cx>
Joshua Prunier <joshua.prunier at gmail.com>
Julien Lefevre <julien.lefevr at gmail.com>
Julien Schmidt <go-sql-driver at julienschmidt.com>
Expand Down
17 changes: 14 additions & 3 deletions infile.go
Original file line number Diff line number Diff line change
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 39a9638

Please sign in to comment.