Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce RegisterReaderHandlerWithEncoderInfo #1419

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 23 additions & 3 deletions infile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ import (
"os"
"strings"
"sync"
"time"
)

var (
fileRegister map[string]bool
fileRegisterLock sync.RWMutex
readerRegister map[string]func() io.Reader
readerRegister map[string]func(EncoderInfo) io.Reader
readerRegisterLock sync.RWMutex
)

// EncoderInfo contains the settings needed to encode a TSV file for LOAD DATA INFILE.
type EncoderInfo struct {
Location *time.Location
}

func (c *Config) encoderInfo() EncoderInfo {
return EncoderInfo{
Location: c.Loc,
}
}

// RegisterLocalFile adds the given file to the file allowlist,
// so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
// Alternatively you can allow the use of all local files with
Expand Down Expand Up @@ -66,10 +78,18 @@ func DeregisterLocalFile(filePath string) {
// if err != nil {
// ...
func RegisterReaderHandler(name string, handler func() io.Reader) {
RegisterReaderHandlerWithEncoderInfo(name, func(EncoderInfo) io.Reader {
return handler()
})
}

// RegisterReaderHandlerWithEncoderInfo is like RegisterReaderHandler but the
// callback receives the information needed to correctly encode a TSV file.
func RegisterReaderHandlerWithEncoderInfo(name string, handler func(EncoderInfo) io.Reader) {
readerRegisterLock.Lock()
// lazy map init
if readerRegister == nil {
readerRegister = make(map[string]func() io.Reader)
readerRegister = make(map[string]func(EncoderInfo) io.Reader)
}

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

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