Skip to content

Commit

Permalink
Merge pull request #2565 from tamird/sql-dinterval
Browse files Browse the repository at this point in the history
sql/driver: send DInterval over the wire as int64
  • Loading branch information
tamird committed Sep 21, 2015
2 parents b1291ba + 8897d59 commit 24595ab
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 112 deletions.
11 changes: 4 additions & 7 deletions cli/sql_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ package cli
import (
"database/sql"
"fmt"
"time"

"github.com/olekukonko/tablewriter"

"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/sql/parser"
)

func makeSQLClient() *sql.DB {
Expand Down Expand Up @@ -159,10 +157,9 @@ func formatVal(val interface{}) string {
switch t := val.(type) {
case nil:
return "NULL"
case time.Time:
return t.Format(parser.TimestampWithOffsetZoneFormat)
case string:
// Ensure that binary protobufs print escaped.
return fmt.Sprintf("%q", t)
}
// Note that this prints a Go-syntax representation of the value.
// This is to ensure that binary protobufs print escaped.
return fmt.Sprintf("%#v", val)
return fmt.Sprint(val)
}
14 changes: 7 additions & 7 deletions sql/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ CREATE TABLE t.alltypes (
e sql.NullBool
f *time.Time
g *time.Time
h sql.NullString // TODO(tamird): h is a time.Duration; can we do better?
h *time.Duration
)

if rows, err := db.Query("SELECT * FROM t.alltypes"); err != nil {
Expand Down Expand Up @@ -149,8 +149,8 @@ CREATE TABLE t.alltypes (
}

if !(a == 123 && b.Float64 == 3.4 && c.String == "blah" && d.String == "foo" &&
e.Bool && f.Equal(timeVal) && g.Equal(dateVal) && h.String == intervalVal.String()) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g})
e.Bool && f.Equal(timeVal) && g.Equal(dateVal) && *h == intervalVal) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g, h})
}

rows.Next()
Expand All @@ -162,8 +162,8 @@ CREATE TABLE t.alltypes (
t.Fatal(err)
}

if !(a == 456 && !b.Valid && !c.Valid && !d.Valid && !e.Valid && f == nil && g == nil && !h.Valid) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g})
if !(a == 456 && !b.Valid && !c.Valid && !d.Valid && !e.Valid && f == nil && g == nil && h == nil) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g, h})
}

if rows.Next() {
Expand All @@ -188,8 +188,8 @@ CREATE TABLE t.alltypes (
t.Fatal(err)
}

if !(a == 456 && !b.Valid && !c.Valid && !d.Valid && !e.Valid && f == nil && g == nil && !h.Valid) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g})
if !(a == 456 && !b.Valid && !c.Valid && !d.Valid && !e.Valid && f == nil && g == nil && h == nil) {
t.Errorf("got unexpected results: %+v", []interface{}{a, b, c, d, e, f, g, h})
}

if rows.Next() {
Expand Down
31 changes: 9 additions & 22 deletions sql/driver/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package driver

import (
"database/sql/driver"
"fmt"
"time"

"github.com/cockroachdb/cockroach/util"
Expand Down Expand Up @@ -70,6 +69,8 @@ func (d Datum) Value() (driver.Value, error) {
var val driver.Value

switch t := d.Payload.(type) {
case nil:
val = t
case *Datum_BoolVal:
val = t.BoolVal
case *Datum_IntVal:
Expand All @@ -80,31 +81,17 @@ func (d Datum) Value() (driver.Value, error) {
val = t.BytesVal
case *Datum_StringVal:
val = t.StringVal
case *Datum_DateVal:
val = t.DateVal.GoTime().UTC()
case *Datum_TimeVal:
val = t.TimeVal.GoTime().UTC()
case *Datum_IntervalVal:
val = time.Duration(t.IntervalVal)
default:
return nil, util.Errorf("unsupported type %T", t)
}

if driver.IsValue(val) {
return val, nil
}
return nil, util.Errorf("unsupported type %T", val)
}

func (d Datum) String() string {
v, err := d.Value()
if err != nil {
panic(err)
}

if v == nil {
return "NULL"
}

if bytes, ok := v.([]byte); ok {
return string(bytes)
}

return fmt.Sprint(v)
return val, nil
}

// GoTime converts the timestamp to a time.Time.
Expand Down
Loading

0 comments on commit 24595ab

Please sign in to comment.