From 28b37642c9a5b34c0a3c083fc994f2d95bcdf09d Mon Sep 17 00:00:00 2001 From: DemonHa Date: Fri, 12 Apr 2024 18:48:19 +0200 Subject: [PATCH 1/2] fix: auto generated types --- ast/postgresql.ts | 115 ++++++++++++++++++--------------------- pegjs/postgresql.pegjs | 118 ++++++++++++++++++++++------------------- 2 files changed, 115 insertions(+), 118 deletions(-) diff --git a/ast/postgresql.ts b/ast/postgresql.ts index 8bf4a0c6..6dfe0e7a 100644 --- a/ast/postgresql.ts +++ b/ast/postgresql.ts @@ -18,9 +18,9 @@ export type crud_stmt = union_stmt | update_stmt | replace_insert_stmt | insert_ // is in reality: { tableList: any[]; columnList: any[]; ast: T; } export type AstStatement = T; -export type multiple_stmt = AstStatement; +export type multiple_stmt = AstStatement; -export type set_op = 'union' | 'union all' | 'union distinct' | 'intersect | 'except'; +export type set_op = 'union' | 'union all' | 'union distinct' | 'intersect' | 'except'; export interface union_stmt_node extends select_stmt_node { _next: union_stmt_node; @@ -46,7 +46,7 @@ export type create_extension_stmt = { export type create_db_definition = create_option_character_set[]; -export type create_db_stmt = { +export type create_db_stmt_t = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', @@ -54,7 +54,7 @@ export type create_db_stmt = { create_definitions?: create_db_definition } -export type create_db_stmt = AstStatement; +export type create_db_stmt = AstStatement; export type view_with = string; @@ -62,7 +62,7 @@ export type with_view_option = {type: string; value: string; symbol: string; }; export type with_view_options = with_view_option[]; -export type create_view_stmt = { +export type create_view_stmt_t = { type: 'create', keyword: 'view', replace?: 'or replace', @@ -71,11 +71,11 @@ export type create_view_stmt = { view: table_name, columns?: column_list, select: select_stmt_nake, - with_options?: with_options, + with_options?: with_view_options, with?: string, } -export type create_view_stmt = AstStatement; +export type create_view_stmt = AstStatement; export type create_aggregate_opt_required = { type: string; symbol: '='; value: expr; }[]; @@ -83,7 +83,7 @@ export type create_aggregate_opt_optional = { type: string; symbol: '='; value: export type create_aggregate_opts = create_aggregate_opt_optional[]; -export type create_aggregate_stmt = { +export type create_aggregate_stmt_t = { type: 'create', keyword: 'aggregate', replace?: 'or replace', @@ -92,7 +92,7 @@ export type create_aggregate_stmt = { options: create_aggregate_opt_optional[] } -export type create_aggregate_stmt = AstStatement; +export type create_aggregate_stmt = AstStatement; export type column_data_type = { column: column_ref; definition: data_type; }; @@ -100,17 +100,17 @@ export type column_data_type_list = column_data_type[]; export type func_returns = { type: "returns"; keyword?: "setof"; expr: data_type; } | { type: "returns"; keyword?: "table"; expr: column_data_type_list; }; -export type declare_variable_item = { keyword: 'variable'; name: string, constant?: string; datatype: data_type; collate?: collate; not_null?: string; default?: { type: 'default'; keyword: string; value: literal | expr; }; }; +export type declare_variable_item = { keyword: 'variable'; name: string, constant?: string; datatype: data_type; collate?: collate_expr; not_null?: string; default?: { type: 'default'; keyword: string; value: literal | expr; }; }; export type declare_variables = declare_variable_item[]; -export type declare_stmt = { type: 'declare'; declare: declare_variable_item[]; } +export type declare_stmt_t = { type: 'declare'; declare: declare_variable_item[]; } -export type declare_stmt = AstStatement; +export type declare_stmt = AstStatement; export type create_func_opt = literal_string | { type: 'as'; begin?: string; declare?: declare_stmt; expr: multiple_stmt; end?: string; symbol: string; } | literal_numeric | { type: "set"; parameter: ident_name; value?: { prefix: string; expr: expr }}; -export type create_function_stmt = { +export type create_function_stmt_t = { type: 'create'; replace?: string; name: { schema?: string; name: string }; @@ -120,9 +120,9 @@ export type create_function_stmt = { options?: create_func_opt[]; } -export type create_function_stmt = AstStatement; +export type create_function_stmt = AstStatement; -export type create_type_stmt = { +export type create_type_stmt_t = { type: 'create', keyword: 'type', name: { schema: string; name: string }, @@ -131,9 +131,9 @@ export type create_type_stmt = { create_definitions?: any } -export type create_type_stmt = AstStatement; +export type create_type_stmt = AstStatement; -export type create_domain_stmt = { +export type create_domain_stmt_t = { type: 'create', keyword: 'domain', domain: { schema: string; name: string }, @@ -142,7 +142,7 @@ export type create_domain_stmt = { create_definitions?: any[] } -export type create_domain_stmt = AstStatement; +export type create_domain_stmt = AstStatement; export type create_table_stmt_node = create_table_stmt_node_simple | create_table_stmt_node_like; export interface create_table_stmt_node_base { @@ -320,9 +320,9 @@ export interface use_stmt_node { export type use_stmt = AstStatement; -export type aggregate_signature = { name: ”*“ } | alter_func_args; +export type aggregate_signature = { name: "*" } | alter_func_args; -export type alter_func_argmode = ignore; +export type alter_func_argmode = "IN" | "OUT" | "VARIADIC"; export type alter_func_arg_item = { mode?: string; name?: string; type: data_type; default: default_arg_expr; }; @@ -396,10 +396,10 @@ export interface alter_rename_owner { type: 'alter'; resource: string; keyword?: 'to' | 'as'; - [key: string]: ident; + [key: string]: ident | undefined; } -export type ALTER_RENAME = AstStatement; +export type ALTER_RENAME = AstStatement; export type ALTER_OWNER_TO = AstStatement; @@ -657,7 +657,7 @@ export type with_admin_option = origin_str_stmt; export type grant_revoke_keyword = { type: 'grant' } | { type: 'revoke'; grant_option_for?: origin_str_stmt; }; -export interface grant_revoke_stmt { +export interface grant_revoke_stmt_t { type: string; grant_option_for?: origin_str_stmt; keyword: 'priv'; @@ -671,25 +671,25 @@ export interface grant_revoke_stmt { with?: with_grant_option; } -export type grant_revoke_stmt = AstStatement | => AstStatement; +export type grant_revoke_stmt = AstStatement; -export type elseif_stmt = { type: 'elseif'; boolean_expr: expr; then: curd_stmt; semicolon?: string; }; +export type elseif_stmt = { type: 'elseif'; boolean_expr: expr; then: crud_stmt; semicolon?: string; }; export type elseif_stmt_list = elseif_stmt[]; -export interface if_else_stmt { +export interface if_else_stmt_t { type: 'if'; keyword: 'if'; boolean_expr: expr; semicolons: string[]; if_expr: crud_stmt; elseif_expr: elseif_stmt[]; - else_expr: curd_stmt; + else_expr: crud_stmt; prefix: literal_string; suffix: literal_string; } -export type if_else_stmt = AstStatement; +export type if_else_stmt = AstStatement; export type raise_level = "DEBUG" | "LOG" | "INFO" | "NOTICE" | "WARNING" | "EXCEPTION"; @@ -697,26 +697,26 @@ export type raise_opt = { type: 'using'; option: string; symbol: '='; expr: expr type raise_item = never; -export interface raise_stmt { +export interface raise_stmt_t { type: 'raise'; level?: string; raise?: raise_item; using?: raise_opt; } -export type raise_stmt = AstStatement; +export type raise_stmt = AstStatement; -export interface execute_stmt { +export interface execute_stmt_t { type: 'execute'; name: string; args?: { type: expr_list; value: proc_primary_list; } } -export type execute_stmt = AstStatement; +export type execute_stmt = AstStatement; export type for_label = { label?: string; keyword: 'for'; }; -export interface for_loop_stmt { +export interface for_loop_stmt_t { type: 'for'; label?: string target: string; @@ -724,9 +724,9 @@ export interface for_loop_stmt { stmts: multiple_stmt; } -export type for_loop_stmt = AstStatement; +export type for_loop_stmt = AstStatement; -export interface transaction_stmt { +export interface transaction_stmt_t { type: 'transaction'; expr: { type: 'origin', @@ -734,7 +734,7 @@ export interface transaction_stmt { } } -export type transaction_stmt = AstStatement; +export type transaction_stmt = AstStatement; export interface select_stmt_node extends select_stmt_nake { parentheses: true; @@ -778,7 +778,7 @@ export type array_index = { brackets: boolean, number: number }; export type array_index_list = array_index[]; -export type expr_item = binary_expr & { array_index: array_index }; +export type expr_item = binary_column_expr & { array_index: array_index }; export type cast_data_type = data_type & { quoted?: string }; @@ -823,7 +823,7 @@ export type table_ref = table_base | table_join; export type table_join = table_base & {join: join_op; using: ident_name[]; } | table_base & {join: join_op; on?: on_clause; } | { - expr: (union_stmt || table_ref_list) & { parentheses: true; }; + expr: (union_stmt | table_ref_list) & { parentheses: true; }; as?: alias_clause; join: join_op; on?: on_clause; @@ -845,6 +845,13 @@ export type join_op = 'LEFT JOIN' | 'RIGHT JOIN' | 'FULL JOIN' | 'CROSS JOIN' | export type table_name = { db?: ident; schema?: ident, table: ident | '*'; }; +export type binary_expr = { + type: 'binary_expr'; + operator: string; + left: string; + right: string; + } + export type or_and_expr = binary_expr; @@ -993,7 +1000,7 @@ export type case_expr = { export type case_when_then_list = case_when_then[]; -export type case_when_then = { type: 'when'; cond: binary_expr; result: expr; }; +export type case_when_then = { type: 'when'; cond: or_and_where_expr; result: expr; }; export type case_else = { type: 'else'; condition?: never; result: expr; }; @@ -1001,20 +1008,6 @@ export type _expr = or_expr | unary_expr; export type expr = _expr | union_stmt; -export type BINARY_OPERATORS = LOGIC_OPERATOR | 'OR' | 'AND' | multiplicative_operator | additive_operator - | arithmetic_comparison_operator - | 'IN' | 'NOT IN' - | 'BETWEEN' | 'NOT BETWEEN' - | 'IS' | 'IS NOT' - | 'LIKE' - | '@>' | '<@' | OPERATOR_CONCATENATION | DOUBLE_WELL_ARROW | WELL_ARROW | '?' | '?|' | '?&' | '#-' - export interface binary_expr { - type: 'binary_expr', - operator: BINARY_OPERATORS, - left: expr, - right: expr - } - export type UNARY_OPERATORS = '+' | '-' | 'EXISTS' | 'NOT EXISTS' | 'NULL' export type unary_expr = { @@ -1109,7 +1102,7 @@ export type column_ref = string_constants_escape | { property?: (literal_string | literal_numeric)[]; }; -export type column_ref_quoted = IGNORE; +export type column_ref_quoted = unknown; export type column_list = column[]; @@ -1187,7 +1180,7 @@ export type aggr_fun_smma = { type: 'aggr_func'; name: 'SUM' | 'MAX' | 'MIN' | ' type KW_SUM_MAX_MIN_AVG = never; -export type aggr_fun_count = { type: 'aggr_func'; name: 'COUNT' | 'GROUP_CONCAT'; args:count_arg; over: over_partition } | { type: 'aggr_func'; name: 'PERCENTILE_CONT' | 'PERCENTILE_DISC'; args: literal_numeric / literal_array; within_group_orderby: order_by_clause; over?: over_partition } | { type: 'aggr_func'; name: 'MODE'; args: literal_numeric / literal_array; within_group_orderby: order_by_clause; over?: over_partition }; +export type aggr_fun_count = { type: 'aggr_func'; name: 'COUNT' | 'GROUP_CONCAT'; args:count_arg; over: over_partition } | { type: 'aggr_func'; name: 'PERCENTILE_CONT' | 'PERCENTILE_DISC'; args: literal_numeric | literal_array; within_group_orderby: order_by_clause; over?: over_partition } | { type: 'aggr_func'; name: 'MODE'; args: literal_numeric | literal_array; within_group_orderby: order_by_clause; over?: over_partition }; export type concat_separator = { keyword: string | null; value: literal_string; }; @@ -1244,14 +1237,12 @@ export type cast_expr = { expr: or_expr | column_ref | param | expr; keyword: 'cast'; - ...cast_double_colon; - } | { + } & cast_double_colon | ({ type: 'cast'; expr: literal | aggr_func | func_call | case_expr | interval_expr | column_ref | param | expr; keyword: 'cast'; - ...cast_double_colon; - }; + } & cast_double_colon); export type signedness = KW_SIGNED | KW_UNSIGNED; @@ -1726,9 +1717,9 @@ type EOF = never; export type proc_stmts = (proc_stmt)[]; -export interface proc_stmt { type: 'proc'; stmt: assign_stmt | return_stmt; vars: any } +export interface proc_stmt_t { type: 'proc'; stmt: assign_stmt | return_stmt; vars: any } -export type proc_stmt = AstStatement; +export type proc_stmt = AstStatement; export type assign_stmt = { type: 'assign'; left: var_decl | without_prefix_var_decl; symbol: ':=' | '='; right: proc_expr; }; @@ -1754,7 +1745,7 @@ export type proc_array = { type: 'array'; value: proc_primary_list }; export type var_decl_list = var_decl[]; -export type var_decl = { type: 'var'; name: string; prefix: string; suffix: string; }; | without_prefix_var_decl & { type: 'var'; prefix: string; };; +export type var_decl = { type: 'var'; name: string; prefix: string; suffix: string; } | without_prefix_var_decl & { type: 'var'; prefix: string; }; export type without_prefix_var_decl = { type: 'var'; prefix: string; name: ident_name; members: mem_chain; quoted: string | null } | { type: 'var'; prefix: null; name: number; members: []; quoted: null }; diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index b51a2893..249ce1f1 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -269,7 +269,7 @@ multiple_stmt /* // is in reality: { tableList: any[]; columnList: any[]; ast: T; } export type AstStatement = T; - => AstStatement */ + => AstStatement */ const headAst = head && head.ast || head const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst for (let i = 0; i < tail.length; i++) { @@ -289,7 +289,7 @@ set_op return a ? `union ${a.toLowerCase()}` : 'union' } / KW_INTERSECT { - // => 'intersect + // => 'intersect' return 'intersect' } / KW_EXCEPT { @@ -373,14 +373,14 @@ create_db_stmt t:ident_name __ c:create_db_definition? { /* - export type create_db_stmt = { + export type create_db_stmt_t = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', database: string, create_definitions?: create_db_definition } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -423,7 +423,7 @@ create_view_stmt KW_VIEW __ v:table_name __ c:(LPAREN __ column_list __ RPAREN)? __ wo:(KW_WITH __ LPAREN __ with_view_options __ RPAREN)? __ KW_AS __ s:select_stmt_nake __ w:view_with? { /* - export type create_view_stmt = { + export type create_view_stmt_t = { type: 'create', keyword: 'view', replace?: 'or replace', @@ -432,10 +432,10 @@ create_view_stmt view: table_name, columns?: column_list, select: select_stmt_nake, - with_options?: with_options, + with_options?: with_view_options, with?: string, } - => AstStatement + => AstStatement */ v.view = v.table delete v.table @@ -492,7 +492,7 @@ create_aggregate_opts create_aggregate_stmt = a:KW_CREATE __ or:(KW_OR __ KW_REPLACE)? __ t:'AGGREGATE'i __ s:table_name __ LPAREN __ as:aggregate_signature __ RPAREN __ LPAREN __ opts:create_aggregate_opts __ RPAREN { /* - export type create_aggregate_stmt = { + export type create_aggregate_stmt_t = { type: 'create', keyword: 'aggregate', replace?: 'or replace', @@ -500,7 +500,7 @@ create_aggregate_stmt args?: aggregate_signature, options: create_aggregate_opt_optional[] } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -551,7 +551,7 @@ func_returns declare_variable_item = n:ident_name &{ return n.toLowerCase() !== 'begin' } __ c:'CONSTANT'i? __ d:data_type __ collate:collate_expr? __ nu:(KW_NOT __ KW_NULL)? __ expr:((KW_DEFAULT / ':=')? __ (&'BEGIN'i / literal / expr))? __ s:SEMICOLON? { - // => { keyword: 'variable'; name: string, constant?: string; datatype: data_type; collate?: collate; not_null?: string; default?: { type: 'default'; keyword: string; value: literal | expr; }; } + // => { keyword: 'variable'; name: string, constant?: string; datatype: data_type; collate?: collate_expr; not_null?: string; default?: { type: 'default'; keyword: string; value: literal | expr; }; } return { keyword: 'variable', name: n, @@ -574,8 +574,8 @@ declare_variables declare_stmt = 'DECLARE'i __ vars:declare_variables { /* - export type declare_stmt = { type: 'declare'; declare: declare_variable_item[]; } - => AstStatement + export type declare_stmt_t = { type: 'declare'; declare: declare_variable_item[]; } + => AstStatement */ return { tableList: Array.from(tableList), @@ -694,7 +694,7 @@ create_function_stmt r:func_returns? __ fo:create_func_opt* __ SEMICOLON? __ { /* - export type create_function_stmt = { + export type create_function_stmt_t = { type: 'create'; replace?: string; name: { schema?: string; name: string }; @@ -703,7 +703,7 @@ create_function_stmt keyword: 'function'; options?: create_func_opt[]; } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -723,7 +723,7 @@ create_function_stmt create_type_stmt = a:KW_CREATE __ k:'TYPE'i __ s:table_name __ as:KW_AS __ r:KW_ENUM __ LPAREN __ e:expr_list? __ RPAREN { /* - export type create_type_stmt = { + export type create_type_stmt_t = { type: 'create', keyword: 'type', name: { schema: string; name: string }, @@ -731,7 +731,7 @@ create_type_stmt resource?: string, create_definitions?: any } - => AstStatement + => AstStatement */ e.parentheses = true customTypes.add([s.db, s.table].filter(v => v).join('.')) @@ -749,7 +749,7 @@ create_type_stmt } } / a:KW_CREATE __ k:'TYPE'i __ s:table_name { - // => AstStatement + // => AstStatement customTypes.add([s.db, s.table].filter(v => v).join('.')) return { tableList: Array.from(tableList), @@ -764,7 +764,7 @@ create_type_stmt create_domain_stmt = a:KW_CREATE __ k:'DOMAIN'i __ s:table_name __ as:KW_AS? __ d:data_type __ ce:collate_expr? __ de:default_expr? __ ccc: create_constraint_check? { /* - export type create_domain_stmt = { + export type create_domain_stmt_t = { type: 'create', keyword: 'domain', domain: { schema: string; name: string }, @@ -772,7 +772,7 @@ create_domain_stmt target: data_type, create_definitions?: any[] } - => AstStatement + => AstStatement */ if (ccc) ccc.type = 'constraint' const definitions = [ce, de, ccc].filter(v => v) @@ -1405,7 +1405,7 @@ use_stmt aggregate_signature = STAR { - // => { name: ”*“ } + // => { name: "*" } return [ { name: '*' @@ -1421,8 +1421,8 @@ aggregate_signature / alter_func_args alter_func_argmode - = t:(KW_IN / 'OUT'i / 'VARIADIC'i / 'INOUT'i) { - // => ignore + = t:(KW_IN / 'OUT'i / 'VARIADIC'i) { + // => "IN" | "OUT" | "VARIADIC" return t.toUpperCase() } @@ -1662,9 +1662,9 @@ ALTER_RENAME type: 'alter'; resource: string; keyword?: 'to' | 'as'; - [key: string]: ident; + [key: string]: ident | undefined; } - => AstStatement + => AstStatement */ return { action: 'rename', @@ -2458,7 +2458,7 @@ grant_revoke_stmt const obj = { revoke: 'from', grant: 'to' } return obj[g.type].toLowerCase() === t[0].toLowerCase() } __ to:user_or_role_list __ wo:with_grant_option? { - /* export interface grant_revoke_stmt { + /* export interface grant_revoke_stmt_t { type: string; grant_option_for?: origin_str_stmt; keyword: 'priv'; @@ -2471,7 +2471,7 @@ grant_revoke_stmt user_or_roles?: user_or_role_list; with?: with_grant_option; } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -2494,7 +2494,7 @@ grant_revoke_stmt const obj = { revoke: 'from', grant: 'to' } return obj[g.type].toLowerCase() === t[0].toLowerCase() } __ to:user_or_role_list __ wo:with_admin_option? { - // => => AstStatement + // => AstStatement return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), @@ -2510,7 +2510,7 @@ grant_revoke_stmt } elseif_stmt = 'ELSEIF'i __ e:expr __ 'THEN'i __ ia:crud_stmt __ s:SEMICOLON? { - // => { type: 'elseif'; boolean_expr: expr; then: curd_stmt; semicolon?: string; } + // => { type: 'elseif'; boolean_expr: expr; then: crud_stmt; semicolon?: string; } return { type: 'elseif', boolean_expr: e, @@ -2526,18 +2526,18 @@ elseif_stmt_list } if_else_stmt = 'IF'i __ ie:expr __ 'THEN'i __ ia:crud_stmt __ s:SEMICOLON? __ ei:elseif_stmt_list? __ el:(KW_ELSE __ crud_stmt)? __ es:SEMICOLON? __ 'END'i __ 'IF'i { - /* export interface if_else_stmt { + /* export interface if_else_stmt_t { type: 'if'; keyword: 'if'; boolean_expr: expr; semicolons: string[]; if_expr: crud_stmt; elseif_expr: elseif_stmt[]; - else_expr: curd_stmt; + else_expr: crud_stmt; prefix: literal_string; suffix: literal_string; } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -2602,13 +2602,13 @@ raise_item } raise_stmt = 'RAISE'i __ l:raise_level? __ r:raise_item? __ using:raise_opt? { - /* export interface raise_stmt { + /* export interface raise_stmt_t { type: 'raise'; level?: string; raise?: raise_item; using?: raise_opt; } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -2623,12 +2623,12 @@ raise_stmt } execute_stmt = 'EXECUTE'i __ name:ident __ a:(LPAREN __ proc_primary_list __ RPAREN)? { - /* export interface execute_stmt { + /* export interface execute_stmt_t { type: 'execute'; name: string; args?: { type: expr_list; value: proc_primary_list; } } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -2661,14 +2661,14 @@ for_loop_stmt if (!f.label && !label) return true return false } { - /* export interface for_loop_stmt { + /* export interface for_loop_stmt_t { type: 'for'; label?: string target: string; query: select_stmt; stmts: multiple_stmt; } - => AstStatement + => AstStatement */ return { tableList: Array.from(tableList), @@ -2684,14 +2684,14 @@ for_loop_stmt } transaction_stmt = k:('begin'i / 'commit'i / 'rollback'i) { - /* export interface transaction_stmt { + /* export interface transaction_stmt_t { type: 'transaction'; expr: { type: 'origin', value: string } } - => AstStatement + => AstStatement */ return { type: 'transaction', @@ -2876,7 +2876,7 @@ array_index_list expr_item = e:binary_column_expr __ a:array_index_list? { - // => binary_expr & { array_index: array_index } + // => binary_column_expr & { array_index: array_index } if (a) e.array_index = a return e } @@ -3077,7 +3077,7 @@ table_join } / op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? { /* => { - expr: (union_stmt || table_ref_list) & { parentheses: true; }; + expr: (union_stmt | table_ref_list) & { parentheses: true; }; as?: alias_clause; join: join_op; on?: on_clause; @@ -3187,7 +3187,15 @@ table_name or_and_expr = head:expr tail:(__ (KW_AND / KW_OR) __ expr)* { - // => binary_expr + /* + export type binary_expr = { + type: 'binary_expr'; + operator: string; + left: string; + right: string; + } + => binary_expr + */ const len = tail.length let result = head for (let i = 0; i < len; ++i) { @@ -3704,7 +3712,7 @@ case_when_then_list case_when_then = KW_WHEN __ condition:or_and_where_expr __ KW_THEN __ result:expr { - // => { type: 'when'; cond: binary_expr; result: expr; } + // => { type: 'when'; cond: or_and_where_expr; result: expr; } return { type: 'when', cond: condition, @@ -4067,7 +4075,7 @@ column_ref column_ref_quoted = col:literal_double_quoted_string { - // => IGNORE + // => unknown columnList.add(`select::null::${col.value}`); return { type: 'column_ref', @@ -4335,7 +4343,7 @@ aggr_fun_count }; } / name:('percentile_cont'i / 'percentile_disc'i) __ LPAREN __ arg:(literal_numeric / literal_array) __ RPAREN __ 'within'i __ KW_GROUP __ LPAREN __ or:order_by_clause __ RPAREN __ bc:over_partition? { - // => { type: 'aggr_func'; name: 'PERCENTILE_CONT' | 'PERCENTILE_DISC'; args: literal_numeric / literal_array; within_group_orderby: order_by_clause; over?: over_partition } + // => { type: 'aggr_func'; name: 'PERCENTILE_CONT' | 'PERCENTILE_DISC'; args: literal_numeric | literal_array; within_group_orderby: order_by_clause; over?: over_partition } return { type: 'aggr_func', name: name.toUpperCase(), @@ -4347,7 +4355,7 @@ aggr_fun_count }; } / name:('mode'i) __ LPAREN __ RPAREN __ 'within'i __ KW_GROUP __ LPAREN __ or:order_by_clause __ RPAREN __ bc:over_partition? { - // => { type: 'aggr_func'; name: 'MODE'; args: literal_numeric / literal_array; within_group_orderby: order_by_clause; over?: over_partition } + // => { type: 'aggr_func'; name: 'MODE'; args: literal_numeric | literal_array; within_group_orderby: order_by_clause; over?: over_partition } return { type: 'aggr_func', name: name.toUpperCase(), @@ -4602,8 +4610,7 @@ cast_expr expr: or_expr | column_ref | param | expr; keyword: 'cast'; - ...cast_double_colon; - } + } & cast_double_colon */ e.parentheses = true if (!c) return e @@ -4615,13 +4622,12 @@ cast_expr } } / e:(column_ref_quoted / literal / aggr_func / window_func / func_call / case_expr / interval_expr / column_ref_array_index / param) __ c:cast_double_colon? { - /* => { + /* => ({ type: 'cast'; expr: literal | aggr_func | func_call | case_expr | interval_expr | column_ref | param | expr; keyword: 'cast'; - ...cast_double_colon; - } + } & cast_double_colon) */ if (!c) return e return { @@ -5115,8 +5121,8 @@ proc_stmts proc_stmt = &{ varList = []; return true; } __ s:(assign_stmt / return_stmt) { - /* export interface proc_stmt { type: 'proc'; stmt: assign_stmt | return_stmt; vars: any } - => AstStatement + /* export interface proc_stmt_t { type: 'proc'; stmt: assign_stmt | return_stmt; vars: any } + => AstStatement */ return { type: 'proc', stmt: s, vars: varList } } @@ -5240,7 +5246,7 @@ var_decl_list var_decl = p:KW_VAR_PRE_DOLLAR_DOUBLE d:[^$]* s:KW_VAR_PRE_DOLLAR_DOUBLE { - // => { type: 'var'; name: string; prefix: string; suffix: string; }; + // => { type: 'var'; name: string; prefix: string; suffix: string; } return { type: 'var', name: d.join(''), @@ -5249,7 +5255,7 @@ var_decl }; } / KW_VAR_PRE_DOLLAR f:column KW_VAR_PRE_DOLLAR d:[^$]* KW_VAR_PRE_DOLLAR s:column !{ if (f !== s) return true } KW_VAR_PRE_DOLLAR { - // => { type: 'var'; name: string; prefix: string; suffix: string; }; + // => { type: 'var'; name: string; prefix: string; suffix: string; } return { type: 'var', name: d.join(''), @@ -5258,7 +5264,7 @@ var_decl }; } / p:KW_VAR_PRE d: without_prefix_var_decl { - // => without_prefix_var_decl & { type: 'var'; prefix: string; }; + // => without_prefix_var_decl & { type: 'var'; prefix: string; } // push for analysis return { type: 'var', From 3bb0f641bfd6d3528c7884fb28a71bd4c2b0fbac Mon Sep 17 00:00:00 2001 From: DemonHa Date: Tue, 16 Apr 2024 10:01:39 +0200 Subject: [PATCH 2/2] fix: binary_expr type --- ast/postgresql.ts | 36 ++++++++++++++++++++++++++++++------ pegjs/postgresql.pegjs | 34 +++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/ast/postgresql.ts b/ast/postgresql.ts index 6dfe0e7a..6f44172c 100644 --- a/ast/postgresql.ts +++ b/ast/postgresql.ts @@ -845,12 +845,36 @@ export type join_op = 'LEFT JOIN' | 'RIGHT JOIN' | 'FULL JOIN' | 'CROSS JOIN' | export type table_name = { db?: ident; schema?: ident, table: ident | '*'; }; -export type binary_expr = { - type: 'binary_expr'; - operator: string; - left: string; - right: string; - } +export type BINARY_OPERATORS = + | LOGIC_OPERATOR + | "OR" + | "AND" + | multiplicative_operator + | additive_operator + | arithmetic_comparison_operator + | "IN" + | "NOT IN" + | "BETWEEN" + | "NOT BETWEEN" + | "IS" + | "IS NOT" + | "LIKE" + | "@>" + | "<@" + | OPERATOR_CONCATENATION + | DOUBLE_WELL_ARROW + | WELL_ARROW + | "?" + | "?|" + | "?&" + | "#-"; + + export type binary_expr = { + type: "binary_expr"; + operator: BINARY_OPERATORS; + left: expr; + right: expr; + }; export type or_and_expr = binary_expr; diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 249ce1f1..a637ae8b 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -3188,12 +3188,36 @@ table_name or_and_expr = head:expr tail:(__ (KW_AND / KW_OR) __ expr)* { /* + export type BINARY_OPERATORS = + | LOGIC_OPERATOR + | "OR" + | "AND" + | multiplicative_operator + | additive_operator + | arithmetic_comparison_operator + | "IN" + | "NOT IN" + | "BETWEEN" + | "NOT BETWEEN" + | "IS" + | "IS NOT" + | "LIKE" + | "@>" + | "<@" + | OPERATOR_CONCATENATION + | DOUBLE_WELL_ARROW + | WELL_ARROW + | "?" + | "?|" + | "?&" + | "#-"; + export type binary_expr = { - type: 'binary_expr'; - operator: string; - left: string; - right: string; - } + type: "binary_expr"; + operator: BINARY_OPERATORS; + left: expr; + right: expr; + }; => binary_expr */ const len = tail.length