Skip to content

Commit

Permalink
IP: support for direct interface
Browse files Browse the repository at this point in the history
* add support IP addresses for direct interface
* add test
  • Loading branch information
kshvakov committed Jul 5, 2017
1 parent 824b33b commit 4bdcb0a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 30 deletions.
9 changes: 7 additions & 2 deletions clickhouse_direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/kshvakov/clickhouse"
"github.com/stretchr/testify/assert"
"net"
)

func Test_DirectInsert(t *testing.T) {
Expand All @@ -30,7 +31,8 @@ func Test_DirectInsert(t *testing.T) {
datetime DateTime,
enum8 Enum8 ('a' = 1, 'b' = 2),
enum16 Enum16('c' = 1, 'd' = 2),
uuid FixedString(16)
uuid FixedString(16),
ip FixedString(16)
) Engine=Memory
`
dml = `
Expand All @@ -51,7 +53,8 @@ func Test_DirectInsert(t *testing.T) {
datetime,
enum8,
enum16,
uuid
uuid,
ip
) VALUES (
?,
?,
Expand All @@ -69,6 +72,7 @@ func Test_DirectInsert(t *testing.T) {
?,
?,
?,
?,
?
)
`
Expand Down Expand Up @@ -118,6 +122,7 @@ func Test_DirectInsert(t *testing.T) {
"d",

clickhouse.UUID("123e4567-e89b-12d3-a456-426655440000"),
net.ParseIP("127.0.0.1"),
})
if !assert.NoError(t, err) {
return
Expand Down
56 changes: 48 additions & 8 deletions clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kshvakov/clickhouse"
"github.com/stretchr/testify/assert"
"net"
)

func Test_OpenConnectAndPing(t *testing.T) {
Expand Down Expand Up @@ -889,20 +890,59 @@ func Test_UUID(t *testing.T) {
}
}

if rows, err := connect.Query("SELECT UUIDNumToString(UUID) FROM clickhouse_test_uuid"); assert.NoError(t, err) {
if rows, err := connect.Query("SELECT UUID, UUIDNumToString(UUID) FROM clickhouse_test_uuid"); assert.NoError(t, err) {
if assert.True(t, rows.Next()) {
var uuid string
if err := rows.Scan(&uuid); assert.NoError(t, err) {
assert.Equal(t, "123e4567-e89b-12d3-a456-426655440000", uuid)
var (
uuid clickhouse.UUID
uuidStr string
)
if err := rows.Scan(&uuid, &uuidStr); assert.NoError(t, err) {
if assert.Equal(t, "123e4567-e89b-12d3-a456-426655440000", uuidStr) {
assert.Equal(t, clickhouse.UUID("123e4567-e89b-12d3-a456-426655440000"), uuid)
}
}
}
}
}
}
}
}
}

if rows, err := connect.Query("SELECT UUID FROM clickhouse_test_uuid"); assert.NoError(t, err) {
func Test_IP(t *testing.T) {
const (
ddl = `
CREATE TABLE clickhouse_test_ip (
IPv4 FixedString(16),
IPv6 FixedString(16)
) Engine=Memory;
`
)
var (
ipv4 = net.ParseIP("127.0.0.1")
ipv6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
)
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_ip"); assert.NoError(t, err) {
if _, err := tx.Exec(ddl); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if stmt, err := tx.Prepare("INSERT INTO clickhouse_test_ip VALUES(?, ?)"); assert.NoError(t, err) {
if _, err := stmt.Exec(clickhouse.IP(ipv4), clickhouse.IP(ipv6)); !assert.NoError(t, err) {
t.Fatal(err)
}
}
if err := tx.Commit(); !assert.NoError(t, err) {
t.Fatal(err)
}
}
if rows, err := connect.Query("SELECT IPv4, IPv6 FROM clickhouse_test_ip"); assert.NoError(t, err) {
if assert.True(t, rows.Next()) {
var uuid clickhouse.UUID
if err := rows.Scan(&uuid); assert.NoError(t, err) {
assert.Equal(t, clickhouse.UUID("123e4567-e89b-12d3-a456-426655440000"), uuid)
var v4, v6 clickhouse.IP
if err := rows.Scan(&v4, &v6); assert.NoError(t, err) {
if assert.Equal(t, ipv4, net.IP(v4)) {
assert.Equal(t, ipv6, net.IP(v6))
}
}
}
}
Expand Down
35 changes: 15 additions & 20 deletions ip_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
package clickhouse

import (
"fmt"
"net"
"strings"
"testing"
)

var ipAddresses = []string{
"127.0.0.1",
"99.67.1.100",
"::1",
"2001:0db8:0a0b:12f0:0000:0000:0000:0001",
"2001:0db8::0001",
"3731:54:65fe:2::a7",
}
"github.com/stretchr/testify/assert"
)

func Test_IPConverter(t *testing.T) {
var ipAddresses = []string{
"127.0.0.1",
"99.67.1.100",
"::1",
"2001:0db8:0a0b:12f0:0000:0000:0000:0001",
"2001:0db8::0001",
"3731:54:65fe:2::a7",
}
for _, ips := range ipAddresses {
var (
ip2 IP
ip = net.ParseIP(ips)
value, err = IP(ip).Value()
)

if err != nil {
t.Error(err)
return
}

if err == nil {
if assert.NoError(t, err) {
if !strings.Contains(ips, ":") {
vl := value.([]byte)
err = ip2.Scan(vl[len(vl)-4:])
} else {
err = ip2.Scan(value)
}
}

if !ip.Equal(net.IP(ip2)) {
t.Errorf("Invalid ip restore: %s != %s", ip, ip2)
if assert.NoError(t, err) {
assert.True(t, ip.Equal(net.IP(ip2)), fmt.Sprintf("Invalid ip restore: %s != %s", ip, ip2))
}
}
}
}
17 changes: 17 additions & 0 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math"
"net"
"time"
)

Expand Down Expand Up @@ -140,6 +141,22 @@ func write(buffer *writeBuffer, columnInfo interface{}, v driver.Value) error {
if str, err = uuid2bytes(string(v)); err != nil {
return err
}
case IP, net.IP:
var ip IP
switch v := v.(type) {
case IP:
ip = v
case net.IP:
ip = IP(v)
}
value, err := ip.Value()
if err != nil {
return err
}
var ok bool
if str, ok = value.([]byte); !ok {
return fmt.Errorf("unexpected type %T for IP", value)
}
case []byte:
str = v
case string:
Expand Down

0 comments on commit 4bdcb0a

Please sign in to comment.