From 208d7701377912cb944708935aca176f8d6c0651 Mon Sep 17 00:00:00 2001 From: Christian De la Hoz Date: Wed, 1 Sep 2021 19:15:10 +0200 Subject: [PATCH] update --- corpus/insert_statement/insert.txt | 4 +- corpus/update_statement.txt | 168 +++++++++++++++++++++++++++++ grammar.js | 17 ++- 3 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 corpus/update_statement.txt diff --git a/corpus/insert_statement/insert.txt b/corpus/insert_statement/insert.txt index 156b1e2..547b90e 100644 --- a/corpus/insert_statement/insert.txt +++ b/corpus/insert_statement/insert.txt @@ -103,7 +103,7 @@ insert into foo values(1) returning *, 1; (insert_items (insert_item (number))) - (insert_returning + (returning (select_item (star)) (select_item @@ -153,7 +153,7 @@ returning id into _var; (from_item (from_table (identifier)))))) - (insert_returning + (returning (select_item (identifier))) (into diff --git a/corpus/update_statement.txt b/corpus/update_statement.txt new file mode 100644 index 0000000..0fe633d --- /dev/null +++ b/corpus/update_statement.txt @@ -0,0 +1,168 @@ +================================================================================ +update one field +================================================================================ +update items set foo = 'bar'; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (update_value + (string))))) + +================================================================================ +alias +================================================================================ +update items as t set foo = 'bar'; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (identifier) + (update_set + (identifier) + (update_value + (string))))) + +================================================================================ +update many fields +================================================================================ +update t set col1 = val1, col2 = default returning foo, *; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (update_value + (identifier))) + (update_set + (identifier) + (update_value)) + (returning + (select_item + (identifier)) + (select_item + (star))))) + +================================================================================ +update many fields(1) +================================================================================ +update t set (col1, col2) = (val1, val2); +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (identifier) + (update_value + (identifier)) + (update_value + (identifier))))) + +================================================================================ +update where +================================================================================ +update t set foo = bar where column1 = 'magic'; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (update_value + (identifier))) + (where_filter + (op_expression + (identifier) + (comparison_op) + (string))))) + +================================================================================ +update where subselect +================================================================================ +UPDATE t SET a = b + 1 WHERE id = (SELECT foo FROM bar WHERE c = 'd'); +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (update_value + (op_expression + (identifier) + (number)))) + (where_filter + (op_expression + (identifier) + (comparison_op) + (select_statement + (select_item + (identifier)) + (select_from + (from_item + (from_table + (identifier)))) + (select_where + (where_filter + (op_expression + (identifier) + (comparison_op) + (string))))))))) + +================================================================================ +update from +================================================================================ +UPDATE foo SET bar = bar + 1 FROM t WHERE b = 'z'; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (identifier) + (update_set + (identifier) + (update_value + (op_expression + (identifier) + (number)))) + (from_item + (from_table + (identifier))) + (where_filter + (op_expression + (identifier) + (comparison_op) + (string))))) + +================================================================================ +with cte +================================================================================ +with foo as (select * from bar) +update foo set bar = 1; +-------------------------------------------------------------------------------- + +(source_file + (update_statement + (with_query + (with_query_item + (identifier) + (select_statement + (select_item + (star)) + (select_from + (from_item + (from_table + (identifier))))))) + (identifier) + (update_set + (identifier) + (update_value + (number))))) diff --git a/grammar.js b/grammar.js index 5ee2b23..f53209f 100644 --- a/grammar.js +++ b/grammar.js @@ -52,6 +52,7 @@ module.exports = grammar({ $.select_statement, $.insert_statement, $.delete_statement, + $.update_statement, $.grant_statement, $.create_trigger_statement, $.create_sequence_statement, @@ -60,6 +61,15 @@ module.exports = grammar({ $.do_block, ), + update_statement: $ => seq( + optional($.with_query), + kw("update"), $.identifier, optional(seq(optional(kw("as")), $.identifier)), + kw("set"), commaSep1($.update_set), + optional(seq(kw("from"), commaSep1($.from_item))), + optional($.where_filter), + optional($.returning), + ), + drop_function_statement: $ => seq( kw("drop"), kw("function"), commaSep1($.drop_function_item), @@ -85,11 +95,12 @@ module.exports = grammar({ )) ), - // TODO(chrde): update, values + // TODO(chrde): values _with_query_statement: $ => choice( $.select_statement, $.insert_statement, $.delete_statement, + $.update_statement, ), insert_statement: $ => seq( @@ -97,7 +108,7 @@ module.exports = grammar({ kw("insert"), kw("into"), $.identifier, optional($.as), optional($._list_of_identifiers), $.insert_items, optional($.insert_conflict), - optional($.insert_returning), + optional($.returning), optional($.into), ), insert_items: $ => choice( @@ -125,7 +136,7 @@ module.exports = grammar({ seq($._list_of_identifiers, "=", optional(kw("row")), "(", commaSep1($.update_value), ")"), ), update_value: $ => choice(kw("default"), $._value_expression), - insert_returning: $ => seq(kw("returning"), commaSep1($.select_item)), + returning: $ => seq(kw("returning"), commaSep1($.select_item)), create_table_statement: $ => seq( kw("create"), optional($.temporary), optional(kw("unlogged")), kw("table"),