Skip to content

Commit 988de6d

Browse files
committed
Rename sql3util.
1 parent b9b489a commit 988de6d

File tree

29 files changed

+125
-100
lines changed

29 files changed

+125
-100
lines changed

ext/closure/closure.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/ncruces/go-sqlite3"
1414
"github.com/ncruces/go-sqlite3/internal/util"
15-
"github.com/ncruces/go-sqlite3/util/vtabutil"
15+
"github.com/ncruces/go-sqlite3/util/sql3util"
1616
)
1717

1818
const (
@@ -39,17 +39,17 @@ func Register(db *sqlite3.Conn) error {
3939
)
4040

4141
for _, arg := range arg {
42-
key, val := vtabutil.NamedArg(arg)
42+
key, val := sql3util.NamedArg(arg)
4343
if done.Contains(key) {
4444
return nil, fmt.Errorf("transitive_closure: more than one %q parameter", key)
4545
}
4646
switch key {
4747
case "tablename":
48-
table = vtabutil.Unquote(val)
48+
table = sql3util.Unquote(val)
4949
case "idcolumn":
50-
column = vtabutil.Unquote(val)
50+
column = sql3util.Unquote(val)
5151
case "parentcolumn":
52-
parent = vtabutil.Unquote(val)
52+
parent = sql3util.Unquote(val)
5353
default:
5454
return nil, fmt.Errorf("transitive_closure: unknown %q parameter", key)
5555
}

ext/csv/arg.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"fmt"
55
"strconv"
66

7-
"github.com/ncruces/go-sqlite3/internal/util"
8-
"github.com/ncruces/go-sqlite3/util/vtabutil"
7+
"github.com/ncruces/go-sqlite3/util/sql3util"
98
)
109

1110
func uintArg(key, val string) (int, error) {
@@ -20,15 +19,15 @@ func boolArg(key, val string) (bool, error) {
2019
if val == "" {
2120
return true, nil
2221
}
23-
b, ok := util.ParseBool(val)
22+
b, ok := sql3util.ParseBool(val)
2423
if ok {
2524
return b, nil
2625
}
2726
return false, fmt.Errorf("csv: invalid %q parameter: %s", key, val)
2827
}
2928

3029
func runeArg(key, val string) (rune, error) {
31-
r, _, tail, err := strconv.UnquoteChar(vtabutil.Unquote(val), 0)
30+
r, _, tail, err := strconv.UnquoteChar(sql3util.Unquote(val), 0)
3231
if tail != "" || err != nil {
3332
return 0, fmt.Errorf("csv: invalid %q parameter: %s", key, val)
3433
}

ext/csv/arg_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package csv
33
import (
44
"testing"
55

6-
"github.com/ncruces/go-sqlite3/util/vtabutil"
6+
"github.com/ncruces/go-sqlite3/util/sql3util"
77
)
88

99
func Test_uintArg(t *testing.T) {
@@ -24,7 +24,7 @@ func Test_uintArg(t *testing.T) {
2424
}
2525
for _, tt := range tests {
2626
t.Run(tt.arg, func(t *testing.T) {
27-
key, val := vtabutil.NamedArg(tt.arg)
27+
key, val := sql3util.NamedArg(tt.arg)
2828
if key != tt.key {
2929
t.Errorf("NamedArg() %v, want err %v", key, tt.key)
3030
}
@@ -62,7 +62,7 @@ func Test_boolArg(t *testing.T) {
6262
}
6363
for _, tt := range tests {
6464
t.Run(tt.arg, func(t *testing.T) {
65-
key, val := vtabutil.NamedArg(tt.arg)
65+
key, val := sql3util.NamedArg(tt.arg)
6666
if key != tt.key {
6767
t.Errorf("NamedArg() %v, want err %v", key, tt.key)
6868
}
@@ -96,7 +96,7 @@ func Test_runeArg(t *testing.T) {
9696
}
9797
for _, tt := range tests {
9898
t.Run(tt.arg, func(t *testing.T) {
99-
key, val := vtabutil.NamedArg(tt.arg)
99+
key, val := sql3util.NamedArg(tt.arg)
100100
if key != tt.key {
101101
t.Errorf("NamedArg() %v, want err %v", key, tt.key)
102102
}

ext/csv/csv.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/ncruces/go-sqlite3"
1919
"github.com/ncruces/go-sqlite3/internal/util"
2020
"github.com/ncruces/go-sqlite3/util/osutil"
21-
"github.com/ncruces/go-sqlite3/util/vtabutil"
21+
"github.com/ncruces/go-sqlite3/util/sql3util"
2222
)
2323

2424
// Register registers the CSV virtual table.
@@ -44,17 +44,17 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
4444
)
4545

4646
for _, arg := range arg {
47-
key, val := vtabutil.NamedArg(arg)
47+
key, val := sql3util.NamedArg(arg)
4848
if done.Contains(key) {
4949
return nil, fmt.Errorf("csv: more than one %q parameter", key)
5050
}
5151
switch key {
5252
case "filename":
53-
filename = vtabutil.Unquote(val)
53+
filename = sql3util.Unquote(val)
5454
case "data":
55-
data = vtabutil.Unquote(val)
55+
data = sql3util.Unquote(val)
5656
case "schema":
57-
schema = vtabutil.Unquote(val)
57+
schema = sql3util.Unquote(val)
5858
case "header":
5959
header, err = boolArg(key, val)
6060
case "columns":

ext/csv/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package csv
33
import (
44
"strings"
55

6-
"github.com/ncruces/go-sqlite3/util/vtabutil"
6+
"github.com/ncruces/go-sqlite3/util/sql3util"
77
)
88

99
type affinity byte
@@ -17,7 +17,7 @@ const (
1717
)
1818

1919
func getColumnAffinities(schema string) ([]affinity, error) {
20-
tab, err := vtabutil.Parse(schema)
20+
tab, err := sql3util.ParseTable(schema)
2121
if err != nil {
2222
return nil, err
2323
}

internal/util/bool.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

internal/util/bool_test.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/db_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package tests
22

33
import (
4+
_ "embed"
45
"os"
56
"path/filepath"
67
"testing"
78

8-
_ "embed"
9-
109
"github.com/ncruces/go-sqlite3"
1110
_ "github.com/ncruces/go-sqlite3/embed"
1211
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
1312
"github.com/ncruces/go-sqlite3/vfs"
1413
_ "github.com/ncruces/go-sqlite3/vfs/adiantum"
1514
"github.com/ncruces/go-sqlite3/vfs/memdb"
16-
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
1715
_ "github.com/ncruces/go-sqlite3/vfs/xts"
1816
)
1917

File renamed without changes.

util/vtabutil/arg.go renamed to util/sql3util/arg.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package vtabutil
1+
package sql3util
22

33
import "strings"
44

@@ -17,19 +17,44 @@ func Unquote(val string) string {
1717
if len(val) < 2 {
1818
return val
1919
}
20-
if val[0] != val[len(val)-1] {
20+
fst := val[0]
21+
lst := val[len(val)-1]
22+
rst := val[1 : len(val)-1]
23+
if fst == '[' && lst == ']' {
24+
return rst
25+
}
26+
if fst != lst {
2127
return val
2228
}
2329
var old, new string
24-
switch val[0] {
30+
switch fst {
2531
default:
2632
return val
27-
case '"':
28-
old, new = `""`, `"`
2933
case '`':
3034
old, new = "``", "`"
35+
case '"':
36+
old, new = `""`, `"`
3137
case '\'':
3238
old, new = `''`, `'`
3339
}
34-
return strings.ReplaceAll(val[1:len(val)-1], old, new)
40+
return strings.ReplaceAll(rst, old, new)
41+
}
42+
43+
func ParseBool(s string) (b, ok bool) {
44+
if len(s) == 0 {
45+
return false, false
46+
}
47+
if s[0] == '0' {
48+
return false, true
49+
}
50+
if '1' <= s[0] && s[0] <= '9' {
51+
return true, true
52+
}
53+
switch strings.ToLower(s) {
54+
case "true", "yes", "on":
55+
return true, true
56+
case "false", "no", "off":
57+
return false, true
58+
}
59+
return false, false
3560
}

0 commit comments

Comments
 (0)