Skip to content

Commit cb008b5

Browse files
mrmekuclaude
andcommitted
Expose parameter clause context to sqlc plugins
Surface the AST clause context (SET, WHERE, LIMIT, OFFSET, etc.) for each query parameter through the plugin proto. The compiler already dispatches on parent node type to resolve parameters — this commit tags each Parameter with that context so plugins can distinguish e.g. WHERE params from SET params without reparsing query text. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1d5923c commit cb008b5

File tree

6 files changed

+232
-94
lines changed

6 files changed

+232
-94
lines changed

internal/cmd/shim.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
236236

237237
func pluginQueryParam(p compiler.Parameter) *plugin.Parameter {
238238
return &plugin.Parameter{
239-
Number: int32(p.Number),
240-
Column: pluginQueryColumn(p.Column),
239+
Number: int32(p.Number),
240+
Column: pluginQueryColumn(p.Column),
241+
Context: plugin.ParameterContext(p.Context),
241242
}
242243
}
243244

internal/compiler/query.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,21 @@ type Query struct {
6161
RawStmt *ast.RawStmt
6262
}
6363

64+
type ParameterContext int
65+
66+
const (
67+
ParameterContextUnspecified ParameterContext = iota
68+
ParameterContextSet
69+
ParameterContextValues
70+
ParameterContextWhere
71+
ParameterContextHaving
72+
ParameterContextFunctionArg
73+
ParameterContextLimit
74+
ParameterContextOffset
75+
)
76+
6477
type Parameter struct {
65-
Number int
66-
Column *Column
78+
Number int
79+
Column *Column
80+
Context ParameterContext
6781
}

internal/compiler/resolve.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
126126
NotNull: p.NotNull(),
127127
IsNamedParam: isNamed,
128128
},
129+
Context: ParameterContextOffset,
129130
})
130131

131132
case *limitCount:
@@ -139,6 +140,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
139140
NotNull: p.NotNull(),
140141
IsNamedParam: isNamed,
141142
},
143+
Context: ParameterContextLimit,
142144
})
143145

144146
case *ast.A_Expr:
@@ -173,6 +175,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
173175
NotNull: p.NotNull(),
174176
IsSqlcSlice: p.IsSqlcSlice(),
175177
},
178+
Context: ParameterContextWhere,
176179
})
177180
continue
178181
}
@@ -246,6 +249,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
246249
IsNamedParam: isNamed,
247250
IsSqlcSlice: p.IsSqlcSlice(),
248251
},
252+
Context: ParameterContextWhere,
249253
})
250254
}
251255
}
@@ -311,6 +315,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
311315
IsNamedParam: isNamed,
312316
IsSqlcSlice: p.IsSqlcSlice(),
313317
},
318+
Context: ParameterContextWhere,
314319
})
315320
}
316321
}
@@ -383,6 +388,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
383388
NotNull: p.NotNull(),
384389
IsSqlcSlice: p.IsSqlcSlice(),
385390
},
391+
Context: ParameterContextFunctionArg,
386392
})
387393
continue
388394
}
@@ -425,6 +431,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
425431
IsNamedParam: isNamed,
426432
IsSqlcSlice: p.IsSqlcSlice(),
427433
},
434+
Context: ParameterContextFunctionArg,
428435
})
429436
}
430437

@@ -498,6 +505,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
498505
IsNamedParam: isNamed,
499506
IsSqlcSlice: p.IsSqlcSlice(),
500507
},
508+
Context: ParameterContextSet,
501509
})
502510
} else {
503511
return nil, &sqlerr.Error{
@@ -608,6 +616,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
608616
IsNamedParam: isNamed,
609617
IsSqlcSlice: p.IsSqlcSlice(),
610618
},
619+
Context: ParameterContextWhere,
611620
})
612621
}
613622
}

internal/endtoend/testdata/codegen_json/gen/codegen.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65074,7 +65074,8 @@
6507465074
"original_name": "id",
6507565075
"unsigned": false,
6507665076
"array_dims": 0
65077-
}
65077+
},
65078+
"context": "PARAMETER_CONTEXT_WHERE"
6507865079
}
6507965080
],
6508065081
"comments": [],
@@ -65286,7 +65287,8 @@
6528665287
"original_name": "name",
6528765288
"unsigned": false,
6528865289
"array_dims": 0
65289-
}
65290+
},
65291+
"context": "PARAMETER_CONTEXT_SET"
6529065292
},
6529165293
{
6529265294
"number": 2,
@@ -65315,7 +65317,8 @@
6531565317
"original_name": "bio",
6531665318
"unsigned": false,
6531765319
"array_dims": 0
65318-
}
65320+
},
65321+
"context": "PARAMETER_CONTEXT_SET"
6531965322
}
6532065323
],
6532165324
"comments": [],
@@ -65361,7 +65364,8 @@
6536165364
"original_name": "bio",
6536265365
"unsigned": false,
6536365366
"array_dims": 0
65364-
}
65367+
},
65368+
"context": "PARAMETER_CONTEXT_SET"
6536565369
},
6536665370
{
6536765371
"number": 2,
@@ -65390,7 +65394,8 @@
6539065394
"original_name": "id",
6539165395
"unsigned": false,
6539265396
"array_dims": 0
65393-
}
65397+
},
65398+
"context": "PARAMETER_CONTEXT_WHERE"
6539465399
}
6539565400
],
6539665401
"comments": [],
@@ -65436,7 +65441,8 @@
6543665441
"original_name": "id",
6543765442
"unsigned": false,
6543865443
"array_dims": 0
65439-
}
65444+
},
65445+
"context": "PARAMETER_CONTEXT_WHERE"
6544065446
}
6544165447
],
6544265448
"comments": [],

0 commit comments

Comments
 (0)