ソースを参照

INFILE: add examples to the doc

Julien Schmidt 12 年 前
コミット
efe6c68235
2 ファイル変更23 行追加6 行削除
  1. 2 2
      README.md
  2. 21 4
      infile.go

+ 2 - 2
README.md

@@ -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
 }