Skip to content

Commit 77acdcc

Browse files
committed
Fix DATE values
Fixes #39
1 parent 2c36e71 commit 77acdcc

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

driver_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,45 @@ func TestString(t *testing.T) {
335335
return
336336
}
337337

338+
func TestDateTime(t *testing.T) {
339+
if !getEnv() {
340+
t.Logf("MySQL-Server not running on %s. Skipping TestString", netAddr)
341+
return
342+
}
343+
344+
db, err := sql.Open("mysql", dsn)
345+
if err != nil {
346+
t.Fatalf("Error connecting: %v", err)
347+
}
348+
349+
defer db.Close()
350+
351+
mustExec(t, db, "DROP TABLE IF EXISTS test")
352+
353+
types := [...]string{"DATE", "DATETIME"}
354+
in := [...]string{"2012-06-14", "2011-11-20 21:27:37"}
355+
var out string
356+
var rows *sql.Rows
357+
358+
for i, v := range types {
359+
mustExec(t, db, "CREATE TABLE test (value "+v+") CHARACTER SET utf8 COLLATE utf8_unicode_ci")
360+
361+
mustExec(t, db, ("INSERT INTO test VALUES (?)"), in[i])
362+
363+
rows = mustQuery(t, db, ("SELECT value FROM test"))
364+
if rows.Next() {
365+
rows.Scan(&out)
366+
if in[i] != out {
367+
t.Errorf("%s: %s != %s", v, in[i], out)
368+
}
369+
} else {
370+
t.Errorf("%s: no data", v)
371+
}
372+
373+
mustExec(t, db, "DROP TABLE IF EXISTS test")
374+
}
375+
}
376+
338377
func TestNULL(t *testing.T) {
339378
if !getEnv() {
340379
t.Logf("MySQL-Server not running on %s. Skipping TestNULL", netAddr)

packets.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -808,22 +808,22 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
808808
var isNull bool
809809
num, isNull, n = readLengthEncodedInteger(data[pos:])
810810

811+
pos += n
812+
811813
if num == 0 {
812814
if isNull {
813815
dest[i] = nil
814-
pos++ // always n=1
815816
continue
816817
} else {
817818
dest[i] = []byte("0000-00-00")
818-
pos += n
819819
continue
820820
}
821821
} else {
822822
dest[i] = []byte(fmt.Sprintf("%04d-%02d-%02d",
823823
binary.LittleEndian.Uint16(data[pos:pos+2]),
824824
data[pos+2],
825825
data[pos+3]))
826-
pos += n + int(num)
826+
pos += int(num)
827827
continue
828828
}
829829

@@ -833,20 +833,18 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
833833
var isNull bool
834834
num, isNull, n = readLengthEncodedInteger(data[pos:])
835835

836+
pos += n
837+
836838
if num == 0 {
837839
if isNull {
838840
dest[i] = nil
839-
pos++ // always n=1
840841
continue
841842
} else {
842843
dest[i] = []byte("00:00:00")
843-
pos += n
844844
continue
845845
}
846846
}
847847

848-
pos += n
849-
850848
var sign byte
851849
if data[pos] == 1 {
852850
sign = byte('-')
@@ -884,20 +882,18 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
884882
var isNull bool
885883
num, isNull, n = readLengthEncodedInteger(data[pos:])
886884

885+
pos += n
886+
887887
if num == 0 {
888888
if isNull {
889889
dest[i] = nil
890-
pos++ // always n=1
891890
continue
892891
} else {
893892
dest[i] = []byte("0000-00-00 00:00:00")
894-
pos += n
895893
continue
896894
}
897895
}
898896

899-
pos += n
900-
901897
switch num {
902898
case 4:
903899
dest[i] = []byte(fmt.Sprintf(

0 commit comments

Comments
 (0)