Browse Source

Fix DATE values

Fixes #39
Julien Schmidt 13 years ago
parent
commit
77acdcc72d
2 changed files with 46 additions and 11 deletions
  1. 39 0
      driver_test.go
  2. 7 11
      packets.go

+ 39 - 0
driver_test.go

@@ -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)

+ 7 - 11
packets.go

@@ -808,14 +808,14 @@ 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 {
@@ -823,7 +823,7 @@ func (rc *mysqlRows) readBinaryRow(dest []driver.Value) (err error) {
 					binary.LittleEndian.Uint16(data[pos:pos+2]),
 					data[pos+2],
 					data[pos+3]))
-				pos += n + int(num)
+				pos += int(num)
 				continue
 			}
 
@@ -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('-')
@@ -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(