infile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "database/sql/driver"
  11. "fmt"
  12. "io"
  13. "os"
  14. "strings"
  15. )
  16. var (
  17. fileRegister map[string]bool
  18. readerRegister map[string]func() io.Reader
  19. )
  20. func init() {
  21. fileRegister = make(map[string]bool)
  22. readerRegister = make(map[string]func() io.Reader)
  23. }
  24. // RegisterLocalFile adds the given file to the file whitelist,
  25. // so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
  26. // Alternatively you can allow the use of all local files with
  27. // the DSN parameter 'allowAllFiles=true'
  28. //
  29. // filePath := "/home/gopher/data.csv"
  30. // mysql.RegisterLocalFile(filePath)
  31. // err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
  32. // if err != nil {
  33. // ...
  34. //
  35. func RegisterLocalFile(filePath string) {
  36. fileRegister[strings.Trim(filePath, `"`)] = true
  37. }
  38. // DeregisterLocalFile removes the given filepath from the whitelist.
  39. func DeregisterLocalFile(filePath string) {
  40. delete(fileRegister, strings.Trim(filePath, `"`))
  41. }
  42. // RegisterReaderHandler registers a handler function which is used
  43. // to receive a io.Reader.
  44. // The Reader can be used by "LOAD DATA LOCAL INFILE Reader::<name>".
  45. // If the handler returns a io.ReadCloser Close() is called when the
  46. // request is finished.
  47. //
  48. // mysql.RegisterReaderHandler("data", func() io.Reader {
  49. // var csvReader io.Reader // Some Reader that returns CSV data
  50. // ... // Open Reader here
  51. // return csvReader
  52. // })
  53. // err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
  54. // if err != nil {
  55. // ...
  56. //
  57. func RegisterReaderHandler(name string, handler func() io.Reader) {
  58. readerRegister[name] = handler
  59. }
  60. // DeregisterReaderHandler removes the ReaderHandler function with
  61. // the given name from the registry.
  62. func DeregisterReaderHandler(name string) {
  63. delete(readerRegister, name)
  64. }
  65. func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
  66. var rdr io.Reader
  67. data := make([]byte, 4+mc.maxWriteSize)
  68. if strings.HasPrefix(name, "Reader::") { // io.Reader
  69. name = name[8:]
  70. handler, inMap := readerRegister[name]
  71. if handler != nil {
  72. rdr = handler()
  73. }
  74. if rdr == nil {
  75. if !inMap {
  76. err = fmt.Errorf("Reader '%s' is not registered", name)
  77. } else {
  78. err = fmt.Errorf("Reader '%s' is <nil>", name)
  79. }
  80. }
  81. } else { // File
  82. name = strings.Trim(name, `"`)
  83. if mc.cfg.allowAllFiles || fileRegister[name] {
  84. rdr, err = os.Open(name)
  85. } else {
  86. err = fmt.Errorf("Local File '%s' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files", name)
  87. }
  88. }
  89. if rdc, ok := rdr.(io.ReadCloser); ok {
  90. defer func() {
  91. if err == nil {
  92. err = rdc.Close()
  93. } else {
  94. rdc.Close()
  95. }
  96. }()
  97. }
  98. // send content packets
  99. var ioErr error
  100. if err == nil {
  101. var n int
  102. for err == nil && ioErr == nil {
  103. n, err = rdr.Read(data[4:])
  104. if n > 0 {
  105. data[0] = byte(n)
  106. data[1] = byte(n >> 8)
  107. data[2] = byte(n >> 16)
  108. data[3] = mc.sequence
  109. ioErr = mc.writePacket(data[:4+n])
  110. }
  111. }
  112. if err == io.EOF {
  113. err = nil
  114. }
  115. if ioErr != nil {
  116. errLog.Print(ioErr.Error())
  117. return driver.ErrBadConn
  118. }
  119. }
  120. // send empty packet (termination)
  121. ioErr = mc.writePacket([]byte{
  122. 0x00,
  123. 0x00,
  124. 0x00,
  125. mc.sequence,
  126. })
  127. if ioErr != nil {
  128. errLog.Print(ioErr.Error())
  129. return driver.ErrBadConn
  130. }
  131. // read OK packet
  132. if err == nil {
  133. return mc.readResultOK()
  134. } else {
  135. mc.readPacket()
  136. }
  137. return err
  138. }