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

Added execution of multiple statement at once with two tests #296

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 23 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,31 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
return nil, driver.ErrBadConn
}
if len(args) == 0 { // no args, fastpath
mc.affectedRows = 0
mc.insertId = 0
totalAffectedRows := int64(0)
lastInsertId := int64(0)

queries := strings.Split(query, ";")
for _, singleQuery := range queries {
singleQuery := strings.TrimSpace(singleQuery)

if len(singleQuery) > 0 {
mc.affectedRows = 0
mc.insertId = 0

err := mc.exec(singleQuery)
if err != nil {
return nil, err
} else {
totalAffectedRows += int64(mc.affectedRows)
lastInsertId = int64(mc.insertId)
}
}

err := mc.exec(query)
if err == nil {
return &mysqlResult{
affectedRows: int64(mc.affectedRows),
insertId: int64(mc.insertId),
}, err
}
return nil, err
return &mysqlResult{
affectedRows: totalAffectedRows,
insertId: lastInsertId,
}, nil
}

// with args, must use prepared stmt
Expand Down
59 changes: 59 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,60 @@ func TestEmptyQuery(t *testing.T) {
})
}

func TestSimpleMultipleStatement(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// Create Table
dbt.mustExec("CREATE TABLE test (value int(11))")

// Multiple Statement
query := "INSERT INTO test VALUES(1);INSERT INTO test VALUES(2);"
res := dbt.mustExec(query)
count, err := res.RowsAffected()
if err != nil {
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 2 {
dbt.Fatalf("Expected 2 affected rows, got %d", count)
}

id, err := res.LastInsertId()
if err != nil {
dbt.Fatalf("res.LastInsertId() returned error: %s", err.Error())
}
if id != 0 {
dbt.Fatalf("Expected InsertID 0, got %d", id)
}

})
}
func TestComplexMultipleStatement(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// Multiple Statement
query, err := readFromExternalFile("complex_statement.sql")
if err != nil {
dbt.Fatalf("Unable to read external file, returned error : %s", err.Error())
}

res := dbt.mustExec(query)
count, err := res.RowsAffected()
if err != nil {
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 2 {
dbt.Fatalf("Expected 2 affected rows, got %d", count)
}

id, err := res.LastInsertId()
if err != nil {
dbt.Fatalf("res.LastInsertId() returned error: %s", err.Error())
}
if id != 0 {
dbt.Fatalf("Expected InsertID 0, got %d", id)
}

})
}

func TestCRUD(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// Create Table
Expand Down Expand Up @@ -1538,3 +1592,8 @@ func TestCustomDial(t *testing.T) {
t.Fatalf("Connection failed: %s", err.Error())
}
}

func readFromExternalFile(filename string) (string, error) {
content, err := ioutil.ReadFile("./tests/" + filename)
return string(content), err
}
8 changes: 8 additions & 0 deletions tests/complex_statement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11),
`value` int(11),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO test VALUES(1,1);
INSERT INTO test VALUES(2,1);