Skip to content

Commit ce79958

Browse files
committed
ClickHouse#128 Add slice as IN arg
1 parent 0d0f71e commit ce79958

File tree

3 files changed

+94
-38
lines changed

3 files changed

+94
-38
lines changed

clickhouse_test.go

+84-38
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func Test_Insert(t *testing.T) {
6464
`
6565
dml = `
6666
INSERT INTO clickhouse_test_insert (
67-
int8,
68-
int16,
67+
int8,
68+
int16,
6969
int32,
7070
int64,
71-
uint8,
72-
uint16,
71+
uint8,
72+
uint16,
7373
uint32,
7474
uint64,
7575
float32,
@@ -79,8 +79,8 @@ func Test_Insert(t *testing.T) {
7979
date,
8080
datetime
8181
) VALUES (
82-
?,
83-
?,
82+
?,
83+
?,
8484
?,
8585
?,
8686
?,
@@ -96,13 +96,13 @@ func Test_Insert(t *testing.T) {
9696
)
9797
`
9898
query = `
99-
SELECT
100-
int8,
101-
int16,
99+
SELECT
100+
int8,
101+
int16,
102102
int32,
103103
int64,
104-
uint8,
105-
uint16,
104+
uint8,
105+
uint16,
106106
uint32,
107107
uint64,
108108
float32,
@@ -125,9 +125,9 @@ func Test_Insert(t *testing.T) {
125125
uint8(1*i), uint16(2*i), uint32(4*i), uint64(8*i), // uint
126126
1.32*float32(i), 1.64*float64(i), //float
127127
fmt.Sprintf("string %d", i), // string
128-
"RU", //fixedstring,
129-
time.Now(), //date
130-
time.Now(), //datetime
128+
"RU", //fixedstring,
129+
time.Now(), //date
130+
time.Now(), //datetime
131131
)
132132
if !assert.NoError(t, err) {
133133
return
@@ -209,12 +209,12 @@ func Test_InsertBatch(t *testing.T) {
209209
`
210210
dml = `
211211
INSERT INTO clickhouse_test_insert_batch (
212-
int8,
213-
int16,
212+
int8,
213+
int16,
214214
int32,
215215
int64,
216-
uint8,
217-
uint16,
216+
uint8,
217+
uint16,
218218
uint32,
219219
uint64,
220220
float32,
@@ -225,8 +225,8 @@ func Test_InsertBatch(t *testing.T) {
225225
datetime,
226226
arrayString
227227
) VALUES (
228-
?,
229-
?,
228+
?,
229+
?,
230230
?,
231231
?,
232232
?,
@@ -255,9 +255,9 @@ func Test_InsertBatch(t *testing.T) {
255255
uint8(1*i), uint16(2*i), uint32(4*i), uint64(8*i), // uint
256256
1.32*float32(i), 1.64*float64(i), //float
257257
fmt.Sprintf("string %d ", i), // string
258-
"RU", //fixedstring,
259-
time.Now(), //date
260-
time.Now(), //datetime
258+
"RU", //fixedstring,
259+
time.Now(), //date
260+
time.Now(), //datetime
261261
[]string{"A", "B", "C"},
262262
)
263263
if !assert.NoError(t, err) {
@@ -475,11 +475,11 @@ func Test_ArrayT(t *testing.T) {
475475
dml = `
476476
INSERT INTO clickhouse_test_array (
477477
int8,
478-
int16,
478+
int16,
479479
int32,
480480
int64,
481-
uint8,
482-
uint16,
481+
uint8,
482+
uint16,
483483
uint32,
484484
uint64,
485485
float32,
@@ -491,8 +491,8 @@ func Test_ArrayT(t *testing.T) {
491491
enum8,
492492
enum16
493493
) VALUES (
494-
?,
495-
?,
494+
?,
495+
?,
496496
?,
497497
?,
498498
?,
@@ -510,13 +510,13 @@ func Test_ArrayT(t *testing.T) {
510510
)
511511
`
512512
query = `
513-
SELECT
514-
int8,
515-
int16,
513+
SELECT
514+
int8,
515+
int16,
516516
int32,
517517
int64,
518-
uint8,
519-
uint16,
518+
uint8,
519+
uint16,
520520
uint32,
521521
uint64,
522522
float32,
@@ -690,7 +690,7 @@ func Test_With_Totals(t *testing.T) {
690690
`
691691
dml = `INSERT INTO clickhouse_test_with_totals (country) VALUES (?)`
692692
query = `
693-
SELECT
693+
SELECT
694694
country,
695695
COUNT(*)
696696
FROM clickhouse_test_with_totals
@@ -924,12 +924,12 @@ func Test_Ternary_Operator(t *testing.T) {
924924
}
925925
}
926926
if rows, err := connect.Query(`
927-
SELECT
928-
a ?
929-
'+' : '-',
927+
SELECT
928+
a ?
929+
'+' : '-',
930930
b ? '+' : '-' ,
931931
a, b
932-
FROM clickhouse_ternary_operator
932+
FROM clickhouse_ternary_operator
933933
WHERE a = ? AND b < ? AND a IN(?,
934934
?
935935
) OR b = 0 OR b > ?`, 1, 2, 1, 100, -1); assert.NoError(t, err) {
@@ -1108,3 +1108,49 @@ func Test_Timeout(t *testing.T) {
11081108
}
11091109
}
11101110
}
1111+
1112+
func Test_InArray(t *testing.T) {
1113+
const (
1114+
ddl = `
1115+
CREATE TABLE clickhouse_test_in_array (
1116+
Value String
1117+
) Engine=Memory
1118+
`
1119+
dml = `
1120+
INSERT INTO clickhouse_test_in_array (Value) VALUES (?)
1121+
`
1122+
query = `
1123+
SELECT
1124+
groupArray(Value)
1125+
FROM clickhouse_test_in_array WHERE Value IN(?)
1126+
`
1127+
)
1128+
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
1129+
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_in_array"); assert.NoError(t, err) {
1130+
if _, err := connect.Exec(ddl); assert.NoError(t, err) {
1131+
if tx, err := connect.Begin(); assert.NoError(t, err) {
1132+
if stmt, err := tx.Prepare(dml); assert.NoError(t, err) {
1133+
for _, v := range []string{"A", "B", "C"} {
1134+
_, err = stmt.Exec(v)
1135+
if !assert.NoError(t, err) {
1136+
return
1137+
}
1138+
}
1139+
} else {
1140+
return
1141+
}
1142+
if assert.NoError(t, tx.Commit()) {
1143+
var value []string
1144+
if err := connect.QueryRow(query, []string{"A", "C"}).Scan(&value); assert.NoError(t, err) {
1145+
if !assert.NoError(t, err) {
1146+
return
1147+
}
1148+
}
1149+
assert.Equal(t, []string{"A", "C"}, value)
1150+
1151+
}
1152+
}
1153+
}
1154+
}
1155+
}
1156+
}

helpers.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"regexp"
99
"strings"
1010
"time"
11+
12+
"github.com/kshvakov/clickhouse/lib/types"
1113
)
1214

1315
func numInput(query string) int {
@@ -88,6 +90,10 @@ func isInsert(query string) bool {
8890
}
8991

9092
func quote(v driver.Value) string {
93+
switch value := v.(type) {
94+
case *types.Array:
95+
v = value.Values()
96+
}
9197
switch v := reflect.ValueOf(v); v.Kind() {
9298
case reflect.Slice:
9399
values := make([]string, 0, v.Len())

lib/types/array.go

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func (array *Array) Value() (driver.Value, error) {
6262
return buff.Bytes(), nil
6363
}
6464

65+
func (array *Array) Values() interface{} {
66+
return array.values
67+
}
68+
6569
func (array *Array) WriteArray(encoder *binary.Encoder, column column.Column) (uint64, error) {
6670
if array.err != nil {
6771
return 0, array.err

0 commit comments

Comments
 (0)