Skip to content
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

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Conversation

EduardoLR10
Copy link
Member

@EduardoLR10 EduardoLR10 commented Dec 16, 2024

@EduardoLR10 EduardoLR10 marked this pull request as draft December 16, 2024 23:07
@@ -1,5 +1,5 @@
CREATE TABLE player.record(
username VARCHAR(32) NOT NULL,
username TEXT NOT NULL,
Copy link
Collaborator

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 CHECKs 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,
Copy link
Collaborator

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,
Copy link
Collaborator

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.

Comment on lines +132 to +149
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;
Copy link
Collaborator

@schonfinkel schonfinkel Mar 18, 2025

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

@EduardoLR10 EduardoLR10 marked this pull request as ready for review March 18, 2025 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make resources
3 participants