@@ -12,8 +12,13 @@ import (
12
12
)
13
13
14
14
const (
15
- code = 4
16
- base = 8
15
+ _NONE = iota
16
+ _MEMORY
17
+ _SYNTAX
18
+ _UNSUPPORTEDSQL
19
+
20
+ codeptr = 4
21
+ baseptr = 8
17
22
)
18
23
19
24
var (
22
27
ctx context.Context
23
28
once sync.Once
24
29
runtime wazero.Runtime
30
+ module wazero.CompiledModule
25
31
)
26
32
27
33
// Table holds metadata about a table.
@@ -35,32 +41,38 @@ type Table struct {
35
41
//
36
42
// [CREATE]: https://sqlite.org/lang_createtable.html
37
43
// [ALTER TABLE]: https://sqlite.org/lang_altertable.html
38
- func Parse (sql string ) (* Table , error ) {
44
+ func Parse (sql string ) (_ * Table , err error ) {
39
45
once .Do (func () {
40
46
ctx = context .Background ()
41
47
cfg := wazero .NewRuntimeConfigInterpreter ().WithDebugInfoEnabled (false )
42
48
runtime = wazero .NewRuntimeWithConfig (ctx , cfg )
49
+ module , err = runtime .CompileModule (ctx , binary )
43
50
})
51
+ if err != nil {
52
+ return nil , err
53
+ }
44
54
45
- mod , err := runtime .InstantiateWithConfig (ctx , binary , wazero .NewModuleConfig ().WithName ("" ))
55
+ mod , err := runtime .InstantiateModule (ctx , module , wazero .NewModuleConfig ().WithName ("" ))
46
56
if err != nil {
47
57
return nil , err
48
58
}
49
59
50
- if buf , ok := mod .Memory ().Read (base , uint32 (len (sql ))); ok {
60
+ if buf , ok := mod .Memory ().Read (baseptr , uint32 (len (sql ))); ok {
51
61
copy (buf , sql )
52
62
}
53
- r , err := mod .ExportedFunction ("sql3parse_table" ).Call (ctx , base , uint64 (len (sql )), code )
63
+ r , err := mod .ExportedFunction ("sql3parse_table" ).Call (ctx , baseptr , uint64 (len (sql )), codeptr )
54
64
if err != nil {
55
65
return nil , err
56
66
}
57
67
58
- c , _ := mod .Memory ().ReadUint32Le (code )
59
- if c == uint32 (_MEMORY ) {
68
+ c , _ := mod .Memory ().ReadUint32Le (codeptr )
69
+ switch c {
70
+ case _MEMORY :
60
71
panic (util .OOMErr )
61
- }
62
- if c != uint32 (_NONE ) {
63
- return nil , ecode (c )
72
+ case _SYNTAX :
73
+ return nil , util .ErrorString ("sql3parse: invalid syntax" )
74
+ case _UNSUPPORTEDSQL :
75
+ return nil , util .ErrorString ("sql3parse: unsupported SQL" )
64
76
}
65
77
if r [0 ] == 0 {
66
78
return nil , nil
@@ -102,6 +114,15 @@ func (t *Table) Column(i int) Column {
102
114
}
103
115
}
104
116
117
+ func (t * Table ) string (ptr uint32 ) string {
118
+ if ptr == 0 {
119
+ return ""
120
+ }
121
+ off , _ := t .mod .Memory ().ReadUint32Le (ptr + 0 )
122
+ len , _ := t .mod .Memory ().ReadUint32Le (ptr + 4 )
123
+ return t .sql [off - baseptr : off + len - baseptr ]
124
+ }
125
+
105
126
// Column holds metadata about a column.
106
127
type Column struct {
107
128
tab * Table
@@ -116,30 +137,5 @@ func (c Column) Type() string {
116
137
if err != nil {
117
138
panic (err )
118
139
}
119
- if r [0 ] == 0 {
120
- return ""
121
- }
122
- off , _ := c .tab .mod .Memory ().ReadUint32Le (uint32 (r [0 ]) + 0 )
123
- len , _ := c .tab .mod .Memory ().ReadUint32Le (uint32 (r [0 ]) + 4 )
124
- return c .tab .sql [off - base : off + len - base ]
125
- }
126
-
127
- type ecode uint32
128
-
129
- const (
130
- _NONE ecode = iota
131
- _MEMORY
132
- _SYNTAX
133
- _UNSUPPORTEDSQL
134
- )
135
-
136
- func (e ecode ) Error () string {
137
- switch e {
138
- case _SYNTAX :
139
- return "sql3parse: invalid syntax"
140
- case _UNSUPPORTEDSQL :
141
- return "sql3parse: unsupported SQL"
142
- default :
143
- panic (util .AssertErr ())
144
- }
140
+ return c .tab .string (uint32 (r [0 ]))
145
141
}
0 commit comments