Skip to content

Commit 9bf94d4

Browse files
committed
Merge pull request #105 from go-sql-driver/infile
Improved LOAD INFILE documentation
2 parents a9ece60 + efe6c68 commit 9bf94d4

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ For Unix domain sockets the address is the absolute path to the MySQL-Server-soc
106106
***Parameters are case-sensitive!***
107107

108108
Possible Parameters are:
109-
* `allowAllFiles`: `allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files. *Might be insecure!*
109+
* `allowAllFiles`: `allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files. [*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
110110
* `charset`: Sets the charset used for client-server interaction ("SET NAMES `value`"). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
111111
* `clientFoundRows`: `clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
112112
* `loc`: Sets the location for time.Time values (when using `parseTime=true`). The default is `UTC`. *"Local"* sets the system's location. See [time.LoadLocation](http://golang.org/pkg/time/#LoadLocation) for details.
@@ -149,11 +149,11 @@ For this feature you need direct access to the package. Therefore you must chang
149149
import "github.com/go-sql-driver/mysql"
150150
```
151151

152-
Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` (might be insecure).
152+
Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
153153

154154
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then.
155155

156-
See also the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation")
156+
See the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
157157

158158

159159
### `time.Time` support

infile.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,37 @@ func init() {
3131
// so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
3232
// Alternatively you can allow the use of all local files with
3333
// the DSN parameter 'allowAllFiles=true'
34-
func RegisterLocalFile(filepath string) {
35-
fileRegister[strings.Trim(filepath, `"`)] = true
34+
//
35+
// filePath := "/home/gopher/data.csv"
36+
// mysql.RegisterLocalFile(filePath)
37+
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
38+
// if err != nil {
39+
// ...
40+
//
41+
func RegisterLocalFile(filePath string) {
42+
fileRegister[strings.Trim(filePath, `"`)] = true
3643
}
3744

3845
// DeregisterLocalFile removes the given filepath from the whitelist.
39-
func DeregisterLocalFile(filepath string) {
40-
delete(fileRegister, strings.Trim(filepath, `"`))
46+
func DeregisterLocalFile(filePath string) {
47+
delete(fileRegister, strings.Trim(filePath, `"`))
4148
}
4249

4350
// RegisterReaderHandler registers a handler function which is used
4451
// to receive a io.Reader.
4552
// The Reader can be used by "LOAD DATA LOCAL INFILE Reader::<name>".
4653
// If the handler returns a io.ReadCloser Close() is called when the
4754
// request is finished.
55+
//
56+
// mysql.RegisterReaderHandler("data", func() io.Reader {
57+
// var csvReader io.Reader // Some Reader that returns CSV data
58+
// ... // Open Reader here
59+
// return csvReader
60+
// })
61+
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
62+
// if err != nil {
63+
// ...
64+
//
4865
func RegisterReaderHandler(name string, handler func() io.Reader) {
4966
readerRegister[name] = handler
5067
}

0 commit comments

Comments
 (0)