Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension to help use LOAD DATA LOCAL INFILE #1060

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Maciej Zimnoch <maciej.zimnoch at codilime.com>
Michael Woolnough <michael.woolnough at gmail.com>
Nathanial Murphy <nathanial.murphy at gmail.com>
Nicola Peduzzi <thenikso at gmail.com>
Noboru Saito <noborusai at gmail.com>
Olivier Mengué <dolmen at cpan.org>
oscarzhao <oscarzhaosl at gmail.com>
Paul Bonser <misterpib at gmail.com>
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,32 @@ To use a `io.Reader` a handler function must be registered with `mysql.RegisterR

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

### Execute `LOAD DATA LOCAL INFILE` instead of `INSERT INTO`

Enables `LOAD DATA LOCAL INFILE` without the need to call a special function.
Using `LOAD DATA LOCAL INFILE` instead of `INSERT INTO` is available with the filepath `Data::Data`.
Create a statement by executing `LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE table name` as a query to the prepare function.
Execute the returned statement with a value in Exec.
Exec is imported as LOAD DATA until the statement is closed.

```go
//stmt, _ = db.Prepare("INSERT INTO test (id, value1, value2 ) VALUES (?, ?, ?);")
stmt, _ = db.Prepare("LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE test (id, value1, value2);")
stmt.Exec(1, "test11", "test12")
stmt.Exec(2, "test21", "test22")
stmt.Close()
```

It is also possible to perform a `LOAD DATA LOCAL INFILE Data::Data' INTO TABLE table name` Query with Exec.
In that case, import the following Exec parameters as LOAD DATA.
To finish importing LOAD data, run the parameter with nil.

```go
db.Exec("LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE test (id, value1, value2);")
db.Exec("", 1, "test11", "test12")
db.Exec("", 2, "test21", "test22")
db.Exec("")
```

### `time.Time` support
The default internal output type of MySQL `DATE` and `DATETIME` values is `[]byte` which allows you to scan the value into a `[]byte`, `string` or `sql.RawBytes` variable in your program.
Expand Down
25 changes: 25 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type mysqlConn struct {
sequence uint8
parseTime bool
reset bool // set when the Go SQL package calls ResetSession
inLoadData bool
loadData []byte
maxLoadDataSize int

// for context support (Go 1.8+)
watching bool
Expand Down Expand Up @@ -151,6 +154,18 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if len(query) > 4 && strings.EqualFold(query[:4], "LOAD") {
err := mc.exec(query)
if err != nil {
return nil, err
}
mc.inLoadData = true
stmt := &mysqlStmt{
mc: mc,
paramCount: -1,
}
return stmt, err
}
// Send command
err := mc.writeCommandPacketStr(comStmtPrepare, query)
if err != nil {
Expand Down Expand Up @@ -310,6 +325,9 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if mc.inLoadData {
return nil, mc.loadDataWrite(args)
}
if len(args) != 0 {
if !mc.cfg.InterpolateParams {
return nil, driver.ErrSkip
Expand Down Expand Up @@ -524,6 +542,10 @@ func (mc *mysqlConn) ExecContext(ctx context.Context, query string, args []drive
return nil, err
}

if mc.inLoadData {
return nil, mc.loadDataWrite(dargs)
}

if err := mc.watchCancel(ctx); err != nil {
return nil, err
}
Expand Down Expand Up @@ -577,6 +599,9 @@ func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue
return nil, err
}

if stmt.mc.inLoadData {
return nil, stmt.mc.loadDataWrite(dargs)
}
if err := stmt.mc.watchCancel(ctx); err != nil {
return nil, err
}
Expand Down
173 changes: 173 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,179 @@ func TestLoadData(t *testing.T) {
})
}

func TestExecLoadData(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
in := [][]driver.Value{{"tt1", "tt1"}, {"tt2", "tt2"}, {"tt3", nil}}
v := [][]string{{"tt1", "tt1"}, {"tt2", "tt2"}, {"tt3", ""}}
dbt.db.Exec("DROP TABLE IF EXISTS test")

dbt.mustExec("CREATE TABLE test (value text, value2 text)")
dbt.mustExec("LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE test")

for _, v := range in {
dbt.mustExec("", v[0], v[1])
}
dbt.mustExec("")

rows := dbt.mustQuery("SELECT * FROM test")
for n := 0; rows.Next(); n++ {
var out, out2 sql.RawBytes
rows.Scan(&out, &out2)
if !bytes.Equal([]byte(v[n][0]), out) {
dbt.Errorf("expected %v, got %v", v[n][0], out)
}
if !bytes.Equal([]byte(v[n][1]), out2) {
dbt.Errorf("expected %v, got %v", v[n][1], out2)
}
rows.Close()
}

dbt.mustExec("DROP TABLE IF EXISTS test")
})
}

func TestPrepareLoadData(t *testing.T) {
count := 1000
runTests(t, dsn, func(dbt *DBTest) {
in := []driver.Value{"test1", "test2"}
v := []string{"test1", "test2"}
dbt.db.Exec("DROP TABLE IF EXISTS test")
dbt.mustExec("CREATE TABLE test (id INT, value1 text, value2 text)")

// Uncomment the next 'INSERT' line and comment out 'LOAD DATA' so it works.
//stmt, err := dbt.db.Prepare("INSERT INTO test (id, value1, value2) VALUES(?, ?, ?);")
stmt, err := dbt.db.Prepare("LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE test (id, value1, value2)")
if err != nil {
t.Fatalf("error preparing statement: %s", err.Error())
}

for i := 0; i < count; i++ {
_, err = stmt.Exec(i, in[0], in[1])
if err != nil {
t.Fatalf("error executing statement: %s", err.Error())
}
}
err = stmt.Close()
if err != nil {
t.Fatalf("error close statement: %s", err.Error())
}

rows := dbt.mustQuery("SELECT COUNT(*) FROM test")
if !rows.Next() {
dbt.Fatalf("error rows: %s", rows.Err())
}

c := 0
if err := rows.Scan(&c); err != nil {
dbt.Fatal(err.Error())
}
if count != c {
dbt.Errorf("Load Data Error:%v != %v", c, count)
}
rows = dbt.mustQuery("SELECT id, value1, value2 FROM test")
for rows.Next() {
var id int
var out, out2 sql.RawBytes
if err := rows.Scan(&id, &out, &out2); err != nil {
dbt.Fatal(err.Error())
}
if !bytes.Equal([]byte(v[0]), out) {
dbt.Errorf("expected %v, got %v", v[0], out)
}
if !bytes.Equal([]byte(v[1]), out2) {
dbt.Errorf("expected %v, got %v", v[1], out2)
}
}
rows.Close()

dbt.mustExec("DROP TABLE IF EXISTS test")
})
}

func TestExecLoadDataType(t *testing.T) {
type tType struct {
dbType string
value driver.Value
result sql.RawBytes
}
tTypes := []tType{
{
dbType: "varchar(10)",
value: "ttt",
result: []byte("ttt"),
},
{
dbType: "text",
value: "ttt",
result: []byte("ttt"),
},
{
dbType: "text",
value: nil,
result: []byte(""),
},
{
dbType: "text",
value: []byte(nil),
result: []byte(""),
},
{
dbType: "int",
value: 42,
result: []byte("42"),
},
{
dbType: "float",
value: 42.23,
result: []byte("42.23"),
},
{
dbType: "datetime",
value: time.Date(2001, 5, 20, 23, 59, 59, 0, time.UTC),
result: []byte("2001-05-20 23:59:59"),
},
{
dbType: "datetime",
value: time.Time{},
result: []byte("0000-00-00 00:00:00"),
},
{
dbType: "bool",
value: true,
result: []byte("1"),
},
{
dbType: "bool",
value: false,
result: []byte("0"),
},
}
runTests(t, dsn, func(dbt *DBTest) {
var rows *sql.Rows
for _, tType := range tTypes {
dbt.db.Exec("DROP TABLE IF EXISTS test")
dbt.mustExec("CREATE TABLE test (value " + tType.dbType + ")")

dbt.mustExec("LOAD DATA LOCAL INFILE 'Data::Data' INTO TABLE test")
dbt.mustExec("", tType.value)
dbt.mustExec("")
rows = dbt.mustQuery("SELECT value FROM test")
if rows.Next() {
var out sql.RawBytes
rows.Scan(&out)
if !bytes.Equal(tType.result, out) {
dbt.Errorf("%s: expected %v, got %v(%s)", tType.dbType, tType.result, out, out)
}
} else {
dbt.Errorf("%s: no data", tType.dbType)
}
rows.Close()

dbt.mustExec("DROP TABLE IF EXISTS test")
}
})
}

func TestFoundRows(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
dbt.mustExec("CREATE TABLE test (id INT NOT NULL ,data INT NOT NULL)")
Expand Down
Loading