@@ -185,14 +185,14 @@ func TestDbWithConnection(t *testing.T) {
185
185
186
186
connection .Query ("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);" )
187
187
188
- ret , err := connection .Query ("SELECT * FROM testtable;" )
188
+ ret , err := connection .Query ("SELECT * FROM testdb. testtable;" )
189
189
if err != nil {
190
190
t .Fatalf ("Query fail, err: %s" , err )
191
191
}
192
192
if string (ret .Buf ()) != "1\n 2\n 3\n " {
193
193
t .Errorf ("Query result should be 1\n 2\n 3\n , got %s" , string (ret .Buf ()))
194
194
}
195
- db , err := sql .Open ("chdb" , fmt .Sprintf ("connection=%s " , connectionDir ))
195
+ db , err := sql .Open ("chdb" , fmt .Sprintf ("connection=file:%s/chdb.db " , connectionDir ))
196
196
if err != nil {
197
197
t .Fatalf ("open db fail, err: %s" , err )
198
198
}
@@ -225,6 +225,56 @@ func TestDbWithConnection(t *testing.T) {
225
225
}
226
226
}
227
227
228
+ func TestDbWithConnectionSqlDriverOnly (t * testing.T ) {
229
+ connectionDir , err := os .MkdirTemp ("" , "unittest-connectiondata" )
230
+ if err != nil {
231
+ t .Fatalf ("create temp directory fail, err: %s" , err )
232
+ }
233
+ defer os .RemoveAll (connectionDir )
234
+ db , err := sql .Open ("chdb" , fmt .Sprintf ("connection=file:%s/chdb.db" , connectionDir ))
235
+ if err != nil {
236
+ t .Fatalf ("open db fail, err: %s" , err )
237
+ }
238
+ if db .Ping () != nil {
239
+ t .Fatalf ("ping db fail, err: %s" , err )
240
+ }
241
+
242
+ _ , err = db .Exec ("CREATE DATABASE IF NOT EXISTS testdb; " +
243
+ "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;" )
244
+ if err != nil {
245
+ t .Fatalf ("could not create database & table: %s" , err )
246
+ }
247
+ _ , err = db .Exec ("INSERT INTO testdb.testtable VALUES (1), (2), (3);" )
248
+ if err != nil {
249
+ t .Fatalf ("could not insert rows in the table: %s" , err )
250
+ }
251
+
252
+ rows , err := db .Query ("select * from testdb.testtable;" )
253
+ if err != nil {
254
+ t .Fatalf ("exec create function fail, err: %s" , err )
255
+ }
256
+ defer rows .Close ()
257
+ cols , err := rows .Columns ()
258
+ if err != nil {
259
+ t .Fatalf ("get result columns fail, err: %s" , err )
260
+ }
261
+ if len (cols ) != 1 {
262
+ t .Fatalf ("result columns length shoule be 3, actual: %d" , len (cols ))
263
+ }
264
+ var bar = 0
265
+ var count = 1
266
+ for rows .Next () {
267
+ err = rows .Scan (& bar )
268
+ if err != nil {
269
+ t .Fatalf ("scan fail, err: %s" , err )
270
+ }
271
+ if bar != count {
272
+ t .Fatalf ("result is not match, want: %d actual: %d" , count , bar )
273
+ }
274
+ count ++
275
+ }
276
+ }
277
+
228
278
func TestQueryRow (t * testing.T ) {
229
279
sessionDir , err := os .MkdirTemp ("" , "unittest-sessiondata" )
230
280
if err != nil {
0 commit comments