Skip to content

Commit 2c74313

Browse files
committed
simplified engine API
1 parent 7609ebc commit 2c74313

File tree

9 files changed

+1233
-1755
lines changed

9 files changed

+1233
-1755
lines changed

examples/plugin-based-codegen/plugin_test.go

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestEnginePlugin(t *testing.T) {
2727
}
2828
defer os.Remove(pluginBin)
2929

30-
// Test Parse
30+
// Test Parse without schema
3131
t.Run("Parse", func(t *testing.T) {
3232
req := &engine.ParseRequest{
3333
Sql: "SELECT * FROM users WHERE id = ?;",
@@ -37,79 +37,53 @@ func TestEnginePlugin(t *testing.T) {
3737
t.Fatal(err)
3838
}
3939

40-
if len(resp.Statements) != 1 {
41-
t.Fatalf("expected 1 statement, got %d", len(resp.Statements))
40+
if resp.Sql == "" {
41+
t.Fatal("expected non-empty sql in response")
4242
}
43-
t.Logf("✓ Parse: %s", resp.Statements[0].RawSql)
44-
})
45-
46-
// Test GetCatalog
47-
t.Run("GetCatalog", func(t *testing.T) {
48-
req := &engine.GetCatalogRequest{}
49-
resp := &engine.GetCatalogResponse{}
50-
if err := invokePlugin(ctx, pluginBin, "get_catalog", req, resp); err != nil {
51-
t.Fatal(err)
52-
}
53-
54-
if resp.Catalog == nil || resp.Catalog.Name != "sqlite3" {
55-
t.Fatalf("expected catalog 'sqlite3', got %v", resp.Catalog)
43+
if len(resp.Parameters) != 1 {
44+
t.Fatalf("expected 1 parameter, got %d", len(resp.Parameters))
5645
}
57-
t.Logf("✓ GetCatalog: %s (schema: %s)", resp.Catalog.Name, resp.Catalog.DefaultSchema)
46+
t.Logf("✓ Parse: sql=%q params=%d columns=%d", resp.Sql, len(resp.Parameters), len(resp.Columns))
5847
})
5948

60-
// Test IsReservedKeyword
61-
t.Run("IsReservedKeyword", func(t *testing.T) {
62-
tests := []struct {
63-
keyword string
64-
expected bool
65-
}{
66-
{"SELECT", true},
67-
{"PRAGMA", true},
68-
{"users", false},
69-
}
70-
71-
for _, tc := range tests {
72-
req := &engine.IsReservedKeywordRequest{Keyword: tc.keyword}
73-
resp := &engine.IsReservedKeywordResponse{}
74-
if err := invokePlugin(ctx, pluginBin, "is_reserved_keyword", req, resp); err != nil {
75-
t.Fatal(err)
76-
}
77-
if resp.IsReserved != tc.expected {
78-
t.Errorf("IsReservedKeyword(%q) = %v, want %v", tc.keyword, resp.IsReserved, tc.expected)
79-
}
49+
// Test Parse with schema (wildcard expansion)
50+
t.Run("ParseWithSchema", func(t *testing.T) {
51+
schemaSQL := `CREATE TABLE users (
52+
id INTEGER PRIMARY KEY,
53+
name TEXT NOT NULL,
54+
email TEXT NOT NULL
55+
);`
56+
req := &engine.ParseRequest{
57+
Sql: "SELECT * FROM users WHERE id = ?;",
58+
SchemaSource: &engine.ParseRequest_SchemaSql{SchemaSql: schemaSQL},
8059
}
81-
t.Log("✓ IsReservedKeyword")
82-
})
83-
84-
// Test GetDialect
85-
t.Run("GetDialect", func(t *testing.T) {
86-
req := &engine.GetDialectRequest{}
87-
resp := &engine.GetDialectResponse{}
88-
if err := invokePlugin(ctx, pluginBin, "get_dialect", req, resp); err != nil {
60+
resp := &engine.ParseResponse{}
61+
if err := invokePlugin(ctx, pluginBin, "parse", req, resp); err != nil {
8962
t.Fatal(err)
9063
}
9164

92-
if resp.ParamStyle != "question" {
93-
t.Errorf("expected param_style 'question', got '%s'", resp.ParamStyle)
65+
if resp.Sql == "" {
66+
t.Fatal("expected non-empty sql in response")
9467
}
95-
t.Logf("✓ GetDialect: quote=%s param=%s", resp.QuoteChar, resp.ParamStyle)
96-
})
97-
98-
// Test GetCommentSyntax
99-
t.Run("GetCommentSyntax", func(t *testing.T) {
100-
req := &engine.GetCommentSyntaxRequest{}
101-
resp := &engine.GetCommentSyntaxResponse{}
102-
if err := invokePlugin(ctx, pluginBin, "get_comment_syntax", req, resp); err != nil {
103-
t.Fatal(err)
68+
// With schema, wildcard should be expanded to explicit columns
69+
if len(resp.Columns) != 3 {
70+
t.Fatalf("expected 3 columns (id, name, email), got %d", len(resp.Columns))
10471
}
105-
106-
if !resp.Dash || !resp.SlashStar {
107-
t.Errorf("expected dash and slash_star comments")
72+
if len(resp.Parameters) != 1 {
73+
t.Fatalf("expected 1 parameter, got %d", len(resp.Parameters))
10874
}
109-
t.Logf("✓ GetCommentSyntax: dash=%v slash_star=%v", resp.Dash, resp.SlashStar)
75+
t.Logf("✓ ParseWithSchema: sql=%q params=%d columns=%v", resp.Sql, len(resp.Parameters), columnNames(resp.Columns))
11076
})
11177
}
11278

79+
func columnNames(cols []*engine.Column) []string {
80+
names := make([]string, len(cols))
81+
for i, c := range cols {
82+
names[i] = c.Name
83+
}
84+
return names
85+
}
86+
11387
func invokePlugin(ctx context.Context, bin, method string, req, resp proto.Message) error {
11488
reqData, err := proto.Marshal(req)
11589
if err != nil {

0 commit comments

Comments
 (0)