@@ -4,17 +4,13 @@ use std::{
44} ;
55mod base_parser;
66mod grant_parser;
7- mod policy_parser;
87mod revoke_parser;
98
109use crate :: queries:: { self , QueryResult , TreeSitterQueriesExecutor } ;
1110use pgt_text_size:: { TextRange , TextSize } ;
1211
1312use crate :: context:: {
14- base_parser:: CompletionStatementParser ,
15- grant_parser:: GrantParser ,
16- policy_parser:: { PolicyParser , PolicyStmtKind } ,
17- revoke_parser:: RevokeParser ,
13+ base_parser:: CompletionStatementParser , grant_parser:: GrantParser , revoke_parser:: RevokeParser ,
1814} ;
1915
2016#[ derive( Debug , PartialEq , Eq , Hash , Clone ) ]
@@ -34,20 +30,15 @@ pub enum WrappingClause<'a> {
3430 DropColumn ,
3531 AlterColumn ,
3632 RenameColumn ,
37- PolicyName ,
3833 ToRoleAssignment ,
3934 SetStatement ,
4035 AlterRole ,
4136 DropRole ,
4237
43- /// `PolicyCheck` refers to either the `WITH CHECK` or the `USING` clause
44- /// in a policy statement.
45- /// ```sql
46- /// CREATE POLICY "my pol" ON PUBLIC.USERS
47- /// FOR SELECT
48- /// USING (...) -- this one!
49- /// ```
50- PolicyCheck ,
38+ CreatePolicy ,
39+ AlterPolicy ,
40+ DropPolicy ,
41+ CheckOrUsingClause ,
5142}
5243
5344#[ derive( PartialEq , Eq , Hash , Debug , Clone ) ]
@@ -205,12 +196,7 @@ impl<'a> TreesitterContext<'a> {
205196 mentioned_columns : HashMap :: new ( ) ,
206197 } ;
207198
208- // policy handling is important to Supabase, but they are a PostgreSQL specific extension,
209- // so the pgt_treesitter_grammar language does not support it.
210- // We infer the context manually.
211- if PolicyParser :: looks_like_matching_stmt ( params. text ) {
212- ctx. gather_policy_context ( ) ;
213- } else if GrantParser :: looks_like_matching_stmt ( params. text ) {
199+ if GrantParser :: looks_like_matching_stmt ( params. text ) {
214200 ctx. gather_grant_context ( ) ;
215201 } else if RevokeParser :: looks_like_matching_stmt ( params. text ) {
216202 ctx. gather_revoke_context ( ) ;
@@ -278,43 +264,6 @@ impl<'a> TreesitterContext<'a> {
278264 } ;
279265 }
280266
281- fn gather_policy_context ( & mut self ) {
282- let policy_context = PolicyParser :: get_context ( self . text , self . position ) ;
283-
284- self . node_under_cursor = Some ( NodeUnderCursor :: CustomNode {
285- text : policy_context. node_text ,
286- range : policy_context. node_range ,
287- kind : policy_context. node_kind . clone ( ) ,
288- previous_node_kind : Some ( policy_context. previous_node_kind ) ,
289- } ) ;
290-
291- if policy_context. node_kind == "policy_table" {
292- self . schema_or_alias_name = policy_context. schema_name . clone ( ) ;
293- }
294-
295- if policy_context. table_name . is_some ( ) {
296- let mut new = HashSet :: new ( ) ;
297- new. insert ( policy_context. table_name . unwrap ( ) ) ;
298- self . mentioned_relations
299- . insert ( policy_context. schema_name , new) ;
300- }
301-
302- self . wrapping_clause_type = match policy_context. node_kind . as_str ( ) {
303- "policy_name" if policy_context. statement_kind != PolicyStmtKind :: Create => {
304- Some ( WrappingClause :: PolicyName )
305- }
306- "policy_role" => Some ( WrappingClause :: ToRoleAssignment ) ,
307- "policy_table" => Some ( WrappingClause :: From ) ,
308- _ => {
309- if policy_context. in_check_or_using_clause {
310- Some ( WrappingClause :: PolicyCheck )
311- } else {
312- None
313- }
314- }
315- } ;
316- }
317-
318267 fn gather_info_from_ts_queries ( & mut self ) {
319268 let stmt_range = self . wrapping_statement_range . as_ref ( ) ;
320269 let sql = self . text ;
@@ -498,13 +447,6 @@ impl<'a> TreesitterContext<'a> {
498447 }
499448 }
500449
501- "where" | "update" | "select" | "delete" | "from" | "join" | "column_definitions"
502- | "alter_role" | "drop_role" | "set_statement" | "drop_table" | "alter_table"
503- | "drop_column" | "alter_column" | "rename_column" => {
504- self . wrapping_clause_type =
505- self . get_wrapping_clause_from_current_node ( current_node, & mut cursor) ;
506- }
507-
508450 "relation" | "binary_expression" | "assignment" => {
509451 self . wrapping_node_kind = current_node_kind. try_into ( ) . ok ( ) ;
510452 }
@@ -518,7 +460,13 @@ impl<'a> TreesitterContext<'a> {
518460 }
519461 }
520462
521- _ => { }
463+ _ => {
464+ if let Some ( clause_type) =
465+ self . get_wrapping_clause_from_current_node ( current_node, & mut cursor)
466+ {
467+ self . wrapping_clause_type = Some ( clause_type) ;
468+ }
469+ }
522470 }
523471
524472 // We have arrived at the leaf node
@@ -734,6 +682,10 @@ impl<'a> TreesitterContext<'a> {
734682 "alter_table" => Some ( WrappingClause :: AlterTable ) ,
735683 "set_statement" => Some ( WrappingClause :: SetStatement ) ,
736684 "column_definitions" => Some ( WrappingClause :: ColumnDefinitions ) ,
685+ "create_policy" => Some ( WrappingClause :: CreatePolicy ) ,
686+ "alter_policy" => Some ( WrappingClause :: AlterPolicy ) ,
687+ "drop_policy" => Some ( WrappingClause :: DropPolicy ) ,
688+ "check_or_using_clause" => Some ( WrappingClause :: CheckOrUsingClause ) ,
737689 "insert" => Some ( WrappingClause :: Insert ) ,
738690 "join" => {
739691 // sadly, we need to manually iterate over the children –
0 commit comments