Skip to content

Commit

Permalink
Fix DATE values
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
julienschmidt committed Mar 15, 2013
1 parent 2c36e71 commit 77acdcc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
39 changes: 39 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,45 @@ func TestString(t *testing.T) {
return
}

func TestDateTime(t *testing.T) {
if !getEnv() {
t.Logf("MySQL-Server not running on %s. Skipping TestString", netAddr)
return
}

db, err := sql.Open("mysql", dsn)
if err != nil {
t.Fatalf("Error connecting: %v", err)
}

defer db.Close()

mustExec(t, db, "DROP TABLE IF EXISTS test")

types := [...]string{"DATE", "DATETIME"}
in := [...]string{"2012-06-14", "2011-11-20 21:27:37"}
var out string
var rows *sql.Rows

for i, v := range types {
mustExec(t, db, "CREATE TABLE test (value "+v+") CHARACTER SET utf8 COLLATE utf8_unicode_ci")

mustExec(t, db, ("INSERT INTO test VALUES (?)"), in[i])

rows = mustQuery(t, db, ("SELECT value FROM test"))
if rows.Next() {
rows.Scan(&out)
if in[i] != out {
t.Errorf("%s: %s != %s", v, in[i], out)
}
} else {
t.Errorf("%s: no data", v)
}

mustExec(t, db, "DROP TABLE IF EXISTS test")
}
}

func TestNULL(t *testing.T) {
if !getEnv() {
t.Logf("MySQL-Server not running on %s. Skipping TestNULL", netAddr)
Expand Down
18 changes: 7 additions & 11 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,22 +808,22 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
var isNull bool
num, isNull, n = readLengthEncodedInteger(data[pos:])

pos += n

if num == 0 {
if isNull {
dest[i] = nil
pos++ // always n=1
continue
} else {
dest[i] = []byte("0000-00-00")
pos += n
continue
}
} else {
dest[i] = []byte(fmt.Sprintf("%04d-%02d-%02d",
binary.LittleEndian.Uint16(data[pos:pos+2]),
data[pos+2],
data[pos+3]))
pos += n + int(num)
pos += int(num)
continue
}

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

pos += n

if num == 0 {
if isNull {
dest[i] = nil
pos++ // always n=1
continue
} else {
dest[i] = []byte("00:00:00")
pos += n
continue
}
}

pos += n

var sign byte
if data[pos] == 1 {
sign = byte('-')
Expand Down Expand Up @@ -884,20 +882,18 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
var isNull bool
num, isNull, n = readLengthEncodedInteger(data[pos:])

pos += n

if num == 0 {
if isNull {
dest[i] = nil
pos++ // always n=1
continue
} else {
dest[i] = []byte("0000-00-00 00:00:00")
pos += n
continue
}
}

pos += n

switch num {
case 4:
dest[i] = []byte(fmt.Sprintf(
Expand Down

0 comments on commit 77acdcc

Please sign in to comment.