@@ -8,12 +8,47 @@ use serde::Deserialize;
88pub use sqlx:: postgres:: PgSeverity ;
99use sqlx:: { Acquire , PgPool , Postgres , Transaction } ;
1010
11+ /// Settings that control which plpgsql_check_function() arguments are passed.
12+ #[ derive( Clone , Debug ) ]
13+ pub struct PlPgSqlCheckOptions {
14+ pub fatal_errors : bool ,
15+ pub other_warnings : bool ,
16+ pub extra_warnings : bool ,
17+ pub performance_warnings : bool ,
18+ pub security_warnings : bool ,
19+ pub compatibility_warnings : bool ,
20+ pub without_warnings : bool ,
21+ pub all_warnings : bool ,
22+ pub use_incomment_options : bool ,
23+ pub incomment_options_usage_warning : bool ,
24+ pub constant_tracing : bool ,
25+ }
26+
27+ impl Default for PlPgSqlCheckOptions {
28+ fn default ( ) -> Self {
29+ Self {
30+ fatal_errors : true ,
31+ other_warnings : true ,
32+ extra_warnings : true ,
33+ performance_warnings : false ,
34+ security_warnings : false ,
35+ compatibility_warnings : false ,
36+ without_warnings : false ,
37+ all_warnings : false ,
38+ use_incomment_options : true ,
39+ incomment_options_usage_warning : false ,
40+ constant_tracing : true ,
41+ }
42+ }
43+ }
44+
1145#[ derive( Debug ) ]
1246pub struct PlPgSqlCheckParams < ' a > {
1347 pub conn : & ' a PgPool ,
1448 pub sql : & ' a str ,
1549 pub ast : & ' a pgls_query:: NodeEnum ,
1650 pub schema_cache : & ' a pgls_schema_cache:: SchemaCache ,
51+ pub options : & ' a PlPgSqlCheckOptions ,
1752}
1853
1954#[ derive( Debug , Deserialize ) ]
@@ -129,6 +164,52 @@ fn build_function_identifier(
129164 }
130165}
131166
167+ /// Build extra named parameters for plpgsql_check_function().
168+ /// Only includes parameters that differ from plpgsql_check defaults.
169+ fn build_extra_params ( options : & PlPgSqlCheckOptions ) -> String {
170+ let mut params = Vec :: new ( ) ;
171+
172+ if !options. fatal_errors {
173+ params. push ( "fatal_errors := false" . to_string ( ) ) ;
174+ }
175+ if !options. other_warnings {
176+ params. push ( "other_warnings := false" . to_string ( ) ) ;
177+ }
178+ if !options. extra_warnings {
179+ params. push ( "extra_warnings := false" . to_string ( ) ) ;
180+ }
181+ if options. performance_warnings {
182+ params. push ( "performance_warnings := true" . to_string ( ) ) ;
183+ }
184+ if options. security_warnings {
185+ params. push ( "security_warnings := true" . to_string ( ) ) ;
186+ }
187+ if options. compatibility_warnings {
188+ params. push ( "compatibility_warnings := true" . to_string ( ) ) ;
189+ }
190+ if options. without_warnings {
191+ params. push ( "without_warnings := true" . to_string ( ) ) ;
192+ }
193+ if options. all_warnings {
194+ params. push ( "all_warnings := true" . to_string ( ) ) ;
195+ }
196+ if !options. use_incomment_options {
197+ params. push ( "use_incomment_options := false" . to_string ( ) ) ;
198+ }
199+ if options. incomment_options_usage_warning {
200+ params. push ( "incomment_options_usage_warning := true" . to_string ( ) ) ;
201+ }
202+ if !options. constant_tracing {
203+ params. push ( "constant_tracing := false" . to_string ( ) ) ;
204+ }
205+
206+ if params. is_empty ( ) {
207+ String :: new ( )
208+ } else {
209+ format ! ( ", {}" , params. join( ", " ) )
210+ }
211+ }
212+
132213pub async fn check_plpgsql (
133214 params : PlPgSqlCheckParams < ' _ > ,
134215) -> Result < Vec < PlPgSqlCheckDiagnostic > , sqlx:: Error > {
@@ -175,6 +256,7 @@ pub async fn check_plpgsql(
175256 sqlx:: query ( & sql_with_replace) . execute ( & mut * tx) . await ?;
176257
177258 // run plpgsql_check and collect results with their relations
259+ let extra = build_extra_params ( params. options ) ;
178260 let results_with_relations: Vec < ( String , Option < String > ) > = if is_trigger {
179261 let mut results = Vec :: new ( ) ;
180262
@@ -185,7 +267,7 @@ pub async fn check_plpgsql(
185267 let relation = format ! ( "{}.{}" , trigger. table_schema, trigger. table_name) ;
186268
187269 let result: Option < String > = sqlx:: query_scalar ( & format ! (
188- "select plpgsql_check_function('{fn_identifier}', '{relation}', format := 'json')"
270+ "select plpgsql_check_function('{fn_identifier}', '{relation}', format := 'json'{extra} )"
189271 ) )
190272 . fetch_optional ( & mut * tx)
191273 . await ?
@@ -200,7 +282,7 @@ pub async fn check_plpgsql(
200282 results
201283 } else {
202284 let result: Option < String > = sqlx:: query_scalar ( & format ! (
203- "select plpgsql_check_function('{fn_identifier}', format := 'json')"
285+ "select plpgsql_check_function('{fn_identifier}', format := 'json'{extra} )"
204286 ) )
205287 . fetch_optional ( & mut * tx)
206288 . await ?
@@ -248,11 +330,13 @@ mod tests {
248330 . ok_or ( "Failed to parse SQL root" ) ?;
249331 let schema_cache = pgls_schema_cache:: SchemaCache :: load ( test_db) . await ?;
250332
333+ let options = super :: PlPgSqlCheckOptions :: default ( ) ;
251334 let diagnostics = super :: check_plpgsql ( super :: PlPgSqlCheckParams {
252335 conn : test_db,
253336 sql : create_fn_sql,
254337 ast : & ast,
255338 schema_cache : & schema_cache,
339+ options : & options,
256340 } )
257341 . await ?;
258342
0 commit comments