Skip to content

Commit 649219c

Browse files
committed
Merge pull request #80 from MJKWoolnough/master
Added clientFoundRows support.
2 parents d22091e + 38bc8ca commit 649219c

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Possible Parameters are:
111111
* `parseTime`: `parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
112112
* `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.
113113
* `strict`: Enable strict mode. MySQL warnings are treated as errors.
114+
* `clientFoundRows`: `clientFoundRows=true` causes causes an UPDATE to return the number of matching rows instead of the number of rows changed.
114115

115116
All other parameters are interpreted as system variables:
116117
* `autocommit`: *"SET autocommit=`value`"*

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (mc *mysqlConn) handleParams() (err error) {
6363
}
6464

6565
// handled elsewhere
66-
case "timeout", "allowAllFiles", "loc":
66+
case "timeout", "allowAllFiles", "loc", "clientFoundRows":
6767
continue
6868

6969
// time.Time parsing

driver_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,51 @@ func TestConcurrent(t *testing.T) {
10361036
})
10371037
}
10381038

1039+
func TestFoundRows(t *testing.T) {
1040+
runTests(t, "TestFoundRows1", dsn, func(dbt *DBTest) {
1041+
dbt.mustExec("CREATE TABLE test (id INT NOT NULL ,data INT NOT NULL)")
1042+
dbt.mustExec("INSERT INTO test (id, data) VALUES (0, 0),(0, 0),(1, 0),(1, 0),(1, 1)")
1043+
1044+
res := dbt.mustExec("UPDATE test SET data = 1 WHERE id = 0")
1045+
count, err := res.RowsAffected()
1046+
if err != nil {
1047+
dbt.Fatalf("res.RowsAffected() returned error: %v", err)
1048+
}
1049+
if count != 2 {
1050+
dbt.Fatalf("Expected 2 affected rows, got %d", count)
1051+
}
1052+
res = dbt.mustExec("UPDATE test SET data = 1 WHERE id = 1")
1053+
count, err = res.RowsAffected()
1054+
if err != nil {
1055+
dbt.Fatalf("res.RowsAffected() returned error: %v", err)
1056+
}
1057+
if count != 2 {
1058+
dbt.Fatalf("Expected 2 affected rows, got %d", count)
1059+
}
1060+
})
1061+
runTests(t, "TestFoundRows2", dsn + "&clientFoundRows=true", func(dbt *DBTest) {
1062+
dbt.mustExec("CREATE TABLE test (id INT NOT NULL ,data INT NOT NULL)")
1063+
dbt.mustExec("INSERT INTO test (id, data) VALUES (0, 0),(0, 0),(1, 0),(1, 0),(1, 1)")
1064+
1065+
res := dbt.mustExec("UPDATE test SET data = 1 WHERE id = 0")
1066+
count, err := res.RowsAffected()
1067+
if err != nil {
1068+
dbt.Fatalf("res.RowsAffected() returned error: %v", err)
1069+
}
1070+
if count != 2 {
1071+
dbt.Fatalf("Expected 2 matched rows, got %d", count)
1072+
}
1073+
res = dbt.mustExec("UPDATE test SET data = 1 WHERE id = 1")
1074+
count, err = res.RowsAffected()
1075+
if err != nil {
1076+
dbt.Fatalf("res.RowsAffected() returned error: %v", err)
1077+
}
1078+
if count != 3 {
1079+
dbt.Fatalf("Expected 3 matched rows, got %d", count)
1080+
}
1081+
})
1082+
}
1083+
10391084
// BENCHMARKS
10401085
var sample []byte
10411086

packets.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ func (mc *mysqlConn) writeAuthPacket() error {
215215
if mc.flags&clientLongFlag > 0 {
216216
clientFlags |= uint32(clientLongFlag)
217217
}
218-
218+
if _, ok := mc.cfg.params["clientFoundRows"]; ok {
219+
clientFlags |= uint32(clientFoundRows)
220+
}
219221
// User Password
220222
scrambleBuff := scramblePassword(mc.cipher, []byte(mc.cfg.passwd))
221223
mc.cipher = nil

0 commit comments

Comments
 (0)