-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add draft of resources in Client and Server #85
base: master
Are you sure you want to change the base?
Conversation
EduardoLR10
commented
Dec 16, 2024
•
edited by schonfinkel
Loading
edited by schonfinkel
- Closes Make resources #60
Co-authored-by: Lemos <dudulr10@gmail.com> Co-authored-by: Magueta <maguetamarcos@gmail.com>
@@ -1,5 +1,5 @@ | |||
CREATE TABLE player.record( | |||
username VARCHAR(32) NOT NULL, | |||
username TEXT NOT NULL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MMagueta do you think it makes sense to create a domain type for usernames? we could also add a check constraint as well, but IDK if postgres likes CHECK
s in primary keys.
); | ||
END IF; | ||
|
||
-- These records are used as function arguments | ||
IF to_regtype('map.input') IS NULL THEN | ||
CREATE TYPE map.input AS ( | ||
map_name VARCHAR(18), | ||
map_name TEXT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, although we control the map names, unlike the usernames.
); | ||
|
||
CREATE TABLE character.instance( | ||
name VARCHAR(18) NOT NULL, | ||
name TEXT NOT NULL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, either a domain type or a check constraint, since this will be user input.
old_quantity := NULL; | ||
SELECT quantity INTO old_quantity FROM character.inventory | ||
WHERE name = new_name | ||
AND username = new_username | ||
AND e_mail = new_e_mail | ||
AND item_name = new_item_name; | ||
IF old_quantity IS NOT NULL | ||
THEN | ||
UPDATE character.inventory | ||
SET quantity = old_quantity + new_quantity | ||
WHERE name = new_name | ||
AND username = new_username | ||
AND e_mail = new_e_mail | ||
AND item_name = new_item_name; | ||
ELSE | ||
INSERT INTO character.inventory(name, e_mail, username, quantity, item_name) | ||
VALUES (new_name, new_e_mail, new_username, new_quantity, new_item_name); | ||
END IF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EduardoLR10 @MMagueta @z-silver Wouldn't an upsert with a where clause + EXCLUDED
replace the need for an extra SELECT
query, be easy to understand and also more performant?
INSERT INTO character.inventory(name, e_mail, username, quantity, item_name)
VALUES (new_name, new_e_mail, new_username, new_quantity, new_item_name)
ON CONFLICT (name, e_mail, username, item_name)
DO UPDATE SET
-- EXCLUDED is an alias to the row that is conflicting
-- https://www.postgresql.org/docs/17/sql-insert.html
-- so this line is basically the sum of old + new
quantity = EXCLUDED.quantity + character.inventory.quantity,
WHERE character.inventory.quantity IS NOT NULL;
here's a draft of mine