Rename sql3util.

This commit is contained in:
Nuno Cruces
2024-10-22 23:32:57 +01:00
parent b9b489aae9
commit c69ee0fe8d
29 changed files with 128 additions and 102 deletions

View File

@@ -1,6 +1,7 @@
# Virtual Table utility functions
# SQLite utility functions
This package implements utilities mostly useful to virtual table implementations.
This package implements assorted SQLite utilities
useful to extension writers.
It also wraps a [parser](https://github.com/marcobambini/sqlite-createtable-parser)
for the [`CREATE`](https://sqlite.org/lang_createtable.html) and

View File

@@ -1,4 +1,4 @@
package vtabutil
package sql3util
import "strings"
@@ -17,19 +17,44 @@ func Unquote(val string) string {
if len(val) < 2 {
return val
}
if val[0] != val[len(val)-1] {
fst := val[0]
lst := val[len(val)-1]
rst := val[1 : len(val)-1]
if fst == '[' && lst == ']' {
return rst
}
if fst != lst {
return val
}
var old, new string
switch val[0] {
switch fst {
default:
return val
case '"':
old, new = `""`, `"`
case '`':
old, new = "``", "`"
case '"':
old, new = `""`, `"`
case '\'':
old, new = `''`, `'`
}
return strings.ReplaceAll(val[1:len(val)-1], old, new)
return strings.ReplaceAll(rst, old, new)
}
func ParseBool(s string) (b, ok bool) {
if len(s) == 0 {
return false, false
}
if s[0] == '0' {
return false, true
}
if '1' <= s[0] && s[0] <= '9' {
return true, true
}
switch strings.ToLower(s) {
case "true", "yes", "on":
return true, true
case "false", "no", "off":
return false, true
}
return false, false
}

55
util/sql3util/arg_test.go Normal file
View File

@@ -0,0 +1,55 @@
package sql3util_test
import (
"testing"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
func TestUnquote(t *testing.T) {
tests := []struct {
val string
want string
}{
{"a", "a"},
{"abc", "abc"},
{"abba", "abba"},
{"`ab``c`", "ab`c"},
{"'ab''c'", "ab'c"},
{"'ab``c'", "ab``c"},
{"[ab``c]", "ab``c"},
{`"ab""c"`, `ab"c`},
}
for _, tt := range tests {
t.Run(tt.val, func(t *testing.T) {
if got := sql3util.Unquote(tt.val); got != tt.want {
t.Errorf("Unquote(%s) = %s, want %s", tt.val, got, tt.want)
}
})
}
}
func TestParseBool(t *testing.T) {
tests := []struct {
str string
val bool
ok bool
}{
{"", false, false},
{"0", false, true},
{"1", true, true},
{"9", true, true},
{"T", false, false},
{"true", true, true},
{"FALSE", false, true},
{"false?", false, false},
}
for _, tt := range tests {
t.Run(tt.str, func(t *testing.T) {
gotVal, gotOK := sql3util.ParseBool(tt.str)
if gotVal != tt.val || gotOK != tt.ok {
t.Errorf("ParseBool(%q) = (%v, %v) want (%v, %v)", tt.str, gotVal, gotOK, tt.val, tt.ok)
}
})
}
}

View File

@@ -1,4 +1,4 @@
package vtabutil
package sql3util
const (
_NONE = iota

View File

@@ -1,10 +1,9 @@
package vtabutil
package sql3util
import (
"context"
"sync"
_ "embed"
"sync"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
@@ -25,11 +24,11 @@ var (
compiled wazero.CompiledModule
)
// Parse parses a [CREATE] or [ALTER TABLE] command.
// ParseTable parses a [CREATE] or [ALTER TABLE] command.
//
// [CREATE]: https://sqlite.org/lang_createtable.html
// [ALTER TABLE]: https://sqlite.org/lang_altertable.html
func Parse(sql string) (_ *Table, err error) {
func ParseTable(sql string) (_ *Table, err error) {
once.Do(func() {
ctx := context.Background()
cfg := wazero.NewRuntimeConfigInterpreter()

View File

@@ -1,13 +1,13 @@
package vtabutil_test
package sql3util_test
import (
"testing"
"github.com/ncruces/go-sqlite3/util/vtabutil"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
func TestParse(t *testing.T) {
tab, err := vtabutil.Parse(`CREATE TABLE child(x REFERENCES parent)`)
tab, err := sql3util.ParseTable(`CREATE TABLE child(x REFERENCES parent)`)
if err != nil {
t.Fatal(err)
}

View File

@@ -0,0 +1,2 @@
// Package sql3util implements SQLite utilities.
package sql3util

View File

@@ -1,4 +1,4 @@
// Package vtabutil implements virtual filesystem utilities.
// Package vfsutil implements virtual filesystem utilities.
package vfsutil
import (

View File

@@ -1,2 +0,0 @@
// Package vtabutil implements virtual table utilities.
package vtabutil