Skip to content

Commit d90b94d

Browse files
committed
Align Scanner to match what SQLite prefers to print
1 parent 1188fb8 commit d90b94d

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

lib/scanner.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,40 @@ package sqlrunner
22

33
import (
44
"database/sql"
5+
"encoding/hex"
56
"fmt"
7+
"strconv"
8+
"time"
69
)
710

811
type StringScanner struct {
912
value string
1013
}
1114

1215
func (s *StringScanner) Scan(value any) error {
13-
s.value = fmt.Sprintf("%v", value)
16+
switch v := value.(type) {
17+
case int64:
18+
s.value = strconv.FormatInt(v, 10)
19+
case float64:
20+
s.value = strconv.FormatFloat(v, 'f', -1, 64)
21+
case bool:
22+
if v {
23+
s.value = "1"
24+
} else {
25+
s.value = "0"
26+
}
27+
case []byte:
28+
s.value = hex.EncodeToString(v)
29+
case string:
30+
s.value = v
31+
case time.Time:
32+
s.value = v.Format("2006-01-02 15:04:05")
33+
case nil:
34+
s.value = "NULL"
35+
default:
36+
s.value = fmt.Sprintf("%v", value)
37+
}
38+
1439
return nil
1540
}
1641

lib/scanner_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package sqlrunner
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestScanner(t *testing.T) {
12+
t.Parallel()
13+
14+
t.Run("int64", func(t *testing.T) {
15+
t.Parallel()
16+
17+
s := &StringScanner{}
18+
require.NoError(t, s.Scan(int64(42)))
19+
assert.Equal(t, "42", s.Value())
20+
})
21+
22+
t.Run("float64 expless", func(t *testing.T) {
23+
t.Parallel()
24+
25+
s := &StringScanner{}
26+
require.NoError(t, s.Scan(float64(42.42)))
27+
assert.Equal(t, "42.42", s.Value())
28+
})
29+
30+
t.Run("float64 exp", func(t *testing.T) {
31+
t.Parallel()
32+
33+
s := &StringScanner{}
34+
require.NoError(t, s.Scan(float64(42.42424242424242e8)))
35+
assert.Equal(t, "4242424242.424242", s.Value())
36+
})
37+
38+
t.Run("bool true", func(t *testing.T) {
39+
t.Parallel()
40+
41+
s := &StringScanner{}
42+
require.NoError(t, s.Scan(true))
43+
assert.Equal(t, "1", s.Value())
44+
})
45+
46+
t.Run("bool false", func(t *testing.T) {
47+
t.Parallel()
48+
49+
s := &StringScanner{}
50+
require.NoError(t, s.Scan(false))
51+
assert.Equal(t, "0", s.Value())
52+
})
53+
54+
t.Run("[]byte", func(t *testing.T) {
55+
t.Parallel()
56+
57+
s := &StringScanner{}
58+
require.NoError(t, s.Scan([]byte("hello")))
59+
assert.Equal(t, "68656c6c6f", s.Value())
60+
})
61+
62+
t.Run("string", func(t *testing.T) {
63+
t.Parallel()
64+
65+
s := &StringScanner{}
66+
require.NoError(t, s.Scan("hello"))
67+
assert.Equal(t, "hello", s.Value())
68+
})
69+
70+
t.Run("time.Time", func(t *testing.T) {
71+
t.Parallel()
72+
73+
s := &StringScanner{}
74+
require.NoError(t, s.Scan(time.Date(2021, 1, 2, 3, 4, 5, 6, time.UTC)))
75+
assert.Equal(t, "2021-01-02 03:04:05", s.Value())
76+
})
77+
78+
t.Run("nil", func(t *testing.T) {
79+
t.Parallel()
80+
81+
s := &StringScanner{}
82+
require.NoError(t, s.Scan(nil))
83+
assert.Equal(t, "NULL", s.Value())
84+
})
85+
}

lib/sqlrunner_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ func TestDbRunnerReadonly(t *testing.T) {
240240
require.ErrorAs(t, err, &sqlrunner.QueryError{})
241241
}
242242

243+
func TestDbRunnerNoScientificNotation(t *testing.T) {
244+
t.Parallel()
245+
246+
runner, err := sqlrunner.NewSQLRunner(`
247+
CREATE TABLE notationtest (
248+
value REAL
249+
);
250+
251+
INSERT INTO notationtest (value) VALUES (1.0);
252+
INSERT INTO notationtest (value) VALUES (1145141919.810)
253+
`)
254+
255+
require.NoError(t, err)
256+
257+
result, err := runner.Query(context.TODO(), "SELECT value FROM notationtest")
258+
require.NoError(t, err)
259+
260+
assert.Len(t, result.Rows, 2)
261+
assert.Equal(t, []string{"value"}, result.Columns)
262+
assert.Equal(t, "1", result.Rows[0][0])
263+
assert.Equal(t, "1145141919.81", result.Rows[1][0])
264+
}
265+
243266
func BenchmarkDbrunner(b *testing.B) {
244267
b.ReportAllocs()
245268

0 commit comments

Comments
 (0)