From 6f7eca020f4147e587aa67f1a8bc2132c6ac61ee Mon Sep 17 00:00:00 2001 From: Jille Timmermans Date: Tue, 25 Apr 2023 09:49:14 +0200 Subject: [PATCH] Introduce RegisterReaderHandlerWithEncoderInfo It is the same as RegisterReaderHandler, but receives some configuration settings which are needed to correctly encode the TSV. (For now only the Location). issue #1416 --- AUTHORS | 1 + infile.go | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index fb1478c3b..dea9f4e5c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -52,6 +52,7 @@ Jeffrey Charles Jerome Meyer Jiajia Zhong Jian Zhen +Jille Timmermans Joshua Prunier Julien Lefevre Julien Schmidt diff --git a/infile.go b/infile.go index 3279dcffd..3b88c611a 100644 --- a/infile.go +++ b/infile.go @@ -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 ". // Alternatively you can allow the use of all local files with @@ -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 @@ -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)