Skip to content

Commit

Permalink
Add basic update_statement (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ce11an authored Feb 12, 2025
1 parent 796f35e commit 1d87f84
Show file tree
Hide file tree
Showing 7 changed files with 61,999 additions and 60,910 deletions.
99 changes: 99 additions & 0 deletions examples/update.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- Create a Schemaless person table with a random id
CREATE person CONTENT {
name: 'John',
company: 'Surrealist',
skills: ['JavaScript', 'Go' , 'SurrealQL']
};

-- Create another person with a specific id
CREATE person:tobie CONTENT {
name: 'Tobie',
company: 'SurrealDB',
skills: ['JavaScript', 'Go' , 'SurrealQL']
};

-- Update all records in a table
-- The `enjoys` field will also be an array.
-- The += operator alone is enough to infer the type
UPDATE person SET
dollars = 50,
skills += 'breathing',
enjoys += 'reading',
full_name = name + ' Mc' + name + 'erson';

-- Update a record with a specific string id to add a new skill: 'Rust'
UPDATE person:tobie SET skills += 'Rust';

-- The -= operator can be used to remove an item from an array or reduce a numeric value by a certain value.

UPDATE person:tobie SET
skills -= 'Go',
dollars -= 1;

-- Remove the company field by setting it to NONE or using the UNSET keyword
UPDATE person:tobie SET company = NONE;

UPDATE person:tobie UNSET company;

-- Update all records which match the condition that `company` is not equal to "SurrealDB"
UPDATE person SET skills += "System design" WHERE company != "SurrealDB";

-- Update all records with the same content
UPDATE person CONTENT {
name: 'John',
company: 'SurrealDB',
skills: ['Rust', 'Go', 'JavaScript']
};

-- Oops, now they are both named John.
-- Update a specific record with some content
UPDATE person:tobie CONTENT {
name: 'Tobie',
company: 'SurrealDB',
skills: ['Rust', 'Go', 'JavaScript']
};

-- Update certain fields on all records
UPDATE person MERGE {
settings: {
marketing: true
}
};

-- Update certain fields on a specific record
UPDATE person:tobie MERGE {
settings: {
marketing: true
}
};

-- Patch the JSON response
UPDATE person:tobie PATCH [
{
"op": "add",
"path": "Engineering",
"value": "true"
}
]

-- Don't return any result
UPDATE person SET skills += 'reading' RETURN NONE;

-- Return the changeset diff
UPDATE person SET skills += 'reading' RETURN DIFF;

-- Return the record before changes were applied
UPDATE person SET skills += 'reading' RETURN BEFORE;

-- Return the record after changes were applied (the default)
UPDATE person SET skills += 'reading' RETURN AFTER;

-- Return the value of the 'skills' field without the field name
UPDATE person SET skills += 'reading' RETURN VALUE skills;

-- Using a timeout
UPDATE person
SET important = true
WHERE ->knows->person->(knows WHERE influencer = true)
TIMEOUT 5s;

5 changes: 4 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ module.exports = grammar({
keyword_password_hash: _ => token("PASSHASH"),
keyword_on_duplicate_key_update: _ => token("ON DUPLICATE KEY UPDATE"),
keyword_count: _ => token("COUNT"),
keyword_set: _ => token("SET"),
keyword_unset: _ => token("UNSET"),
keyword_set: _ => token("SET"),

// Expressions
expressions: $ =>
Expand Down Expand Up @@ -533,6 +533,7 @@ module.exports = grammar({
$.merge_clause,
$.patch_clause,
$.set_clause,
$.unset_clause,
),
),
optional($.where_clause),
Expand Down Expand Up @@ -887,6 +888,8 @@ module.exports = grammar({

set_clause: $ => seq($.keyword_set, commaSeparated($.field_assignment)),

unset_clause: $ => seq($.keyword_unset, commaSeparated($.identifier)),

return_clause: $ =>
seq(
$.keyword_return,
Expand Down
1 change: 1 addition & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
(keyword_return)
(keyword_overwrite)
(keyword_throw)
(keyword_unset)
] @keyword

; Operators
Expand Down
46 changes: 42 additions & 4 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d87f84

Please sign in to comment.