Selaa lähdekoodia

Merge pull request #105 from go-sql-driver/infile

Improved LOAD INFILE documentation
Julien Schmidt 12 vuotta sitten
vanhempi
commit
9bf94d495e
2 muutettua tiedostoa jossa 24 lisäystä ja 7 poistoa
  1. 3 3
      README.md
  2. 21 4
      infile.go

+ 3 - 3
README.md

@@ -106,7 +106,7 @@ For Unix domain sockets the address is the absolute path to the MySQL-Server-soc
 ***Parameters are case-sensitive!***
 
 Possible Parameters are:
-  * `allowAllFiles`: `allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files. *Might be insecure!*
+  * `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)
   * `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`).
   * `clientFoundRows`: `clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
   * `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
 import "github.com/go-sql-driver/mysql"
 ```
 
-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).
+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)).
 
 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.
 
-See also the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation")
+See the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
 
 
 ### `time.Time` support

+ 21 - 4
infile.go

@@ -31,13 +31,20 @@ func init() {
 // so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
 // Alternatively you can allow the use of all local files with
 // the DSN parameter 'allowAllFiles=true'
-func RegisterLocalFile(filepath string) {
-	fileRegister[strings.Trim(filepath, `"`)] = true
+//
+//  filePath := "/home/gopher/data.csv"
+//  mysql.RegisterLocalFile(filePath)
+//  err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
+//  if err != nil {
+//  ...
+//
+func RegisterLocalFile(filePath string) {
+	fileRegister[strings.Trim(filePath, `"`)] = true
 }
 
 // DeregisterLocalFile removes the given filepath from the whitelist.
-func DeregisterLocalFile(filepath string) {
-	delete(fileRegister, strings.Trim(filepath, `"`))
+func DeregisterLocalFile(filePath string) {
+	delete(fileRegister, strings.Trim(filePath, `"`))
 }
 
 // RegisterReaderHandler registers a handler function which is used
@@ -45,6 +52,16 @@ func DeregisterLocalFile(filepath string) {
 // The Reader can be used by "LOAD DATA LOCAL INFILE Reader::<name>".
 // If the handler returns a io.ReadCloser Close() is called when the
 // request is finished.
+//
+//  mysql.RegisterReaderHandler("data", func() io.Reader {
+//  	var csvReader io.Reader // Some Reader that returns CSV data
+//  	... // Open Reader here
+//  	return csvReader
+//  })
+//  err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
+//  if err != nil {
+//  ...
+//
 func RegisterReaderHandler(name string, handler func() io.Reader) {
 	readerRegister[name] = handler
 }