|
6 | 6 | package sqlite3
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "bytes" |
9 | 10 | "database/sql"
|
10 | 11 | "database/sql/driver"
|
11 | 12 | "errors"
|
@@ -1343,6 +1344,61 @@ func TestUpdateAndTransactionHooks(t *testing.T) {
|
1343 | 1344 | }
|
1344 | 1345 | }
|
1345 | 1346 |
|
| 1347 | +func TestNilAndEmptyBytes(t *testing.T) { |
| 1348 | + db, err := sql.Open("sqlite3", ":memory:") |
| 1349 | + if err != nil { |
| 1350 | + t.Fatal(err) |
| 1351 | + } |
| 1352 | + defer db.Close() |
| 1353 | + actualNil := []byte("use this to use an actual nil not a reference to nil") |
| 1354 | + emptyBytes := []byte{} |
| 1355 | + for tsti, tst := range []struct { |
| 1356 | + name string |
| 1357 | + columnType string |
| 1358 | + insertBytes []byte |
| 1359 | + expectedBytes []byte |
| 1360 | + }{ |
| 1361 | + {"actual nil blob", "blob", actualNil, nil}, |
| 1362 | + {"referenced nil blob", "blob", nil, nil}, |
| 1363 | + {"empty blob", "blob", emptyBytes, emptyBytes}, |
| 1364 | + {"actual nil text", "text", actualNil, nil}, |
| 1365 | + {"referenced nil text", "text", nil, nil}, |
| 1366 | + {"empty text", "text", emptyBytes, emptyBytes}, |
| 1367 | + } { |
| 1368 | + if _, err = db.Exec(fmt.Sprintf("create table tbl%d (txt %s)", tsti, tst.columnType)); err != nil { |
| 1369 | + t.Fatal(tst.name, err) |
| 1370 | + } |
| 1371 | + if bytes.Equal(tst.insertBytes, actualNil) { |
| 1372 | + if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), nil); err != nil { |
| 1373 | + t.Fatal(tst.name, err) |
| 1374 | + } |
| 1375 | + } else { |
| 1376 | + if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), &tst.insertBytes); err != nil { |
| 1377 | + t.Fatal(tst.name, err) |
| 1378 | + } |
| 1379 | + } |
| 1380 | + rows, err := db.Query(fmt.Sprintf("select txt from tbl%d", tsti)) |
| 1381 | + if err != nil { |
| 1382 | + t.Fatal(tst.name, err) |
| 1383 | + } |
| 1384 | + if !rows.Next() { |
| 1385 | + t.Fatal(tst.name, "no rows") |
| 1386 | + } |
| 1387 | + var scanBytes []byte |
| 1388 | + if err = rows.Scan(&scanBytes); err != nil { |
| 1389 | + t.Fatal(tst.name, err) |
| 1390 | + } |
| 1391 | + if err = rows.Err(); err != nil { |
| 1392 | + t.Fatal(tst.name, err) |
| 1393 | + } |
| 1394 | + if tst.expectedBytes == nil && scanBytes != nil { |
| 1395 | + t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes) |
| 1396 | + } else if !bytes.Equal(scanBytes, tst.expectedBytes) { |
| 1397 | + t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes) |
| 1398 | + } |
| 1399 | + } |
| 1400 | +} |
| 1401 | + |
1346 | 1402 | var customFunctionOnce sync.Once
|
1347 | 1403 |
|
1348 | 1404 | func BenchmarkCustomFunctions(b *testing.B) {
|
|
0 commit comments