Skip to content

Commit 3e4d413

Browse files
committed
NULL string fix
1 parent a19c218 commit 3e4d413

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

packets.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (mc *mysqlConn) readColumns(count int) (columns []mysqlField, err error) {
416416
pos += n
417417

418418
// Name [len coded string]
419-
name, n, err = readLengthEnodedString(data[pos:])
419+
name, _, n, err = readLengthEnodedString(data[pos:])
420420
if err != nil {
421421
return
422422
}
@@ -471,14 +471,19 @@ func (rows *mysqlRows) readRow(dest []driver.Value) (err error) {
471471

472472
// RowSet Packet
473473
var n int
474+
var isNull bool
474475
pos := 0
475476

476477
for i := range dest {
477478
// Read bytes and convert to string
478-
dest[i], n, err = readLengthEnodedString(data[pos:])
479+
dest[i], isNull, n, err = readLengthEnodedString(data[pos:])
479480
pos += n
480481
if err == nil {
481-
continue
482+
if !isNull {
483+
continue
484+
} else {
485+
dest[i] = nil
486+
}
482487
}
483488
return // err
484489
}
@@ -700,6 +705,7 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
700705
// values [rest]
701706
var n int
702707
var unsigned bool
708+
703709
for i := range dest {
704710
// Field is NULL
705711
// (byte >> bit-pos) % 2 == 1
@@ -774,10 +780,15 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
774780
FIELD_TYPE_TINY_BLOB, FIELD_TYPE_MEDIUM_BLOB, FIELD_TYPE_LONG_BLOB,
775781
FIELD_TYPE_BLOB, FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING,
776782
FIELD_TYPE_GEOMETRY:
777-
dest[i], n, err = readLengthEnodedString(data[pos:])
783+
var isNull bool
784+
dest[i], isNull, n, err = readLengthEnodedString(data[pos:])
778785
pos += n
779786
if err == nil {
780-
continue
787+
if !isNull {
788+
continue
789+
} else {
790+
dest[i] = nil
791+
}
781792
}
782793
return // err
783794

utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,20 @@ func uint64ToString(n uint64) []byte {
150150
return a[i:]
151151
}
152152

153-
func readLengthEnodedString(b []byte) ([]byte, int, error) {
153+
func readLengthEnodedString(b []byte) ([]byte, bool, int, error) {
154154
// Get length
155-
num, _, n := readLengthEncodedInteger(b)
155+
num, isNull, n := readLengthEncodedInteger(b)
156156
if num < 1 {
157-
return nil, n, nil
157+
return nil, isNull, n, nil
158158
}
159159

160160
n += int(num)
161161

162162
// Check data length
163163
if len(b) >= n {
164-
return b[n-int(num) : n], n, nil
164+
return b[n-int(num) : n], false, n, nil
165165
}
166-
return nil, n, io.EOF
166+
return nil, false, n, io.EOF
167167
}
168168

169169
func skipLengthEnodedString(b []byte) (int, error) {

0 commit comments

Comments
 (0)