Skip to content

Commit

Permalink
Merge pull request #16 from mailru/develop
Browse files Browse the repository at this point in the history
add type UInt64 to avoid database/sql limitations
  • Loading branch information
bgaifullin committed Feb 3, 2018
2 parents 2d81d88 + 632671c commit e270604
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ http://user:password@host:8123/clicks?read_timeout=10&write_timeout=20
* Enum
* [Array(T) (one-dimensional)](https://clickhouse.yandex/reference_en.html#Array(T))

Note:
database/sql does not allow to use big uint64 values.
It is recommended use type `UInt64` which is provided by driver for such kind of values.

## Install
```
Expand Down
5 changes: 5 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (s *connSuite) TestExec() {
"",
[]interface{}{int64(3), Array([]int16{1, 2})},
},
{
"INSERT INTO data (u64) VALUES (?)",
"",
[]interface{}{UInt64(uint64(1) << 63)},
},
{
"INSERT INTO data (d, t) VALUES (?, ?)",
"",
Expand Down
13 changes: 13 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse

import (
"database/sql/driver"
"strconv"
"time"
)

Expand All @@ -15,6 +16,11 @@ func Date(t time.Time) driver.Valuer {
return date(t)
}

// UInt64 returns date for t
func UInt64(u uint64) driver.Valuer {
return bigUint64(u)
}

type array struct {
v interface{}
}
Expand All @@ -30,3 +36,10 @@ type date time.Time
func (d date) Value() (driver.Value, error) {
return []byte(formatDate(time.Time(d))), nil
}

type bigUint64 uint64

// Value implements driver.Valuer
func (u bigUint64) Value() (driver.Value, error) {
return []byte(strconv.FormatUint(uint64(u), 10)), nil
}
8 changes: 8 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ func TestDate(t *testing.T) {
assert.Equal(t, []byte("'2016-04-04'"), dv)
}
}

func TestUInt64(t *testing.T) {
u := uint64(1) << 63
dv, err := UInt64(u).Value()
if assert.NoError(t, err) {
assert.Equal(t, []byte("9223372036854775808"), dv)
}
}

0 comments on commit e270604

Please sign in to comment.