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 GET Procedures #2

Merged
merged 33 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
169e9fd
Update create_tables.sql to enforce uniqueness on player and world na…
Vianpyro Nov 12, 2024
38f8afe
Add stored procedures for player, world, island, city, building, unit…
Vianpyro Nov 12, 2024
0b9a89f
Merge branch 'main' into feature/get_procedures
Vianpyro Nov 12, 2024
5580d3a
Merge branch 'main' into feature/get_procedures
Vianpyro Nov 12, 2024
b7117d0
Enhance player table schema by making player_name and salt fields NOT…
Vianpyro Nov 12, 2024
fd42439
Refactor stored procedures to use consistent naming conventions for p…
Vianpyro Nov 12, 2024
edbd6c5
Exclude PRS rule from SQLFluff configuration
Vianpyro Nov 12, 2024
ea37297
Replace CREATE PROCEDURE with CREATE OR REPLACE PROCEDURE for all sto…
Vianpyro Nov 12, 2024
8df5335
Weird test to revert if not working
Vianpyro Nov 12, 2024
5c83f3c
Revert "Weird test to revert if not working"
Vianpyro Nov 12, 2024
ce0316e
Fix SQLFluff configuration and standardize procedure delimiters in cr…
Vianpyro Nov 12, 2024
8062541
Revert "Fix SQLFluff configuration and standardize procedure delimite…
Vianpyro Nov 13, 2024
5e4d6b5
Update SQLFluff configuration to ignore parsing errors
Vianpyro Nov 13, 2024
b75f255
Update Dockerfile to change ownership to mysql user and modify unit_i…
Vianpyro Nov 13, 2024
35d9cf2
Update Dockerfile to set ownership for SQL files and adjust permissio…
Vianpyro Nov 13, 2024
10c884a
Switch to the limited user at the end of the Dockerfile
Vianpyro Nov 13, 2024
730211e
Refactor create_tables.sql to remove NOT NULL constraints from option…
Vianpyro Nov 13, 2024
2c591aa
Fix parameter naming in get_player_by_id procedure for clarity
Vianpyro Nov 13, 2024
2ae6124
Update Dockerfile to simplify user management and ensure proper permi…
Vianpyro Nov 13, 2024
e28e5c7
Remove alliance_cap selection
Vianpyro Nov 13, 2024
b2fd067
Fix parameter naming in get_player_by_name procedure for consistency
Vianpyro Nov 13, 2024
ffa3bd2
Fix parameter naming in player retrieval procedures for consistency
Vianpyro Nov 13, 2024
d7f4701
Update Dockerfile to create a non-root user and adjust file ownership…
Vianpyro Nov 13, 2024
553781f
Add world_id parameter to get_player_cities procedure and update city…
Vianpyro Nov 13, 2024
1ad69df
Fix column name in get_active_worlds procedure for consistency
Vianpyro Nov 13, 2024
851317d
Update world table schema to enforce uniqueness on world_name and all…
Vianpyro Nov 13, 2024
6f38b58
Add procedure to check for existing player email in the database
Vianpyro Nov 13, 2024
ff143bf
Rename get_world_islands procedure to get_islands_in_world and add ge…
Vianpyro Nov 13, 2024
c944b7d
Add get_city_buildings procedure to retrieve buildings by city_id
Vianpyro Nov 14, 2024
f1bae04
Add get_city_units procedure to retrieve units by city_id
Vianpyro Nov 14, 2024
8f1d76b
Refactor procedure parameters to improve clarity by prefixing with 'p_'
Vianpyro Nov 14, 2024
59086c8
Add get_player_battles procedure to retrieve battle details for a player
Vianpyro Nov 14, 2024
da6b41e
Fix SQL query in get_battle_details procedure to use correct column n…
Vianpyro Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[sqlfluff]
dialect = mariadb
max_line_length = 100
exlude_rules = PRS
ignore = parsing
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ FROM mariadb:11.5.2
# Set environment variables
ENV TZ=America/Montreal

# Create a new user and group with limited privileges
RUN groupadd -r mariadbuser && useradd -r -g mariadbuser mariadbuser
# Create a non-root user and group
RUN groupadd -r dbuser && useradd -r -g dbuser dbuser

# Copy SQL files from the repository root into the container
# Copy SQL files and set ownership
COPY create_database.sql /docker-entrypoint-initdb.d/
COPY create_tables.sql /docker-entrypoint-initdb.d/
# COPY create_get_procedures.sql /docker-entrypoint-initdb.d/
COPY create_get_procedures.sql /docker-entrypoint-initdb.d/
RUN chown -R dbuser:dbuser /docker-entrypoint-initdb.d

# Adjust permissions on database init files and directories
RUN chown -R mariadbuser:mariadbuser /docker-entrypoint-initdb.d
# Ensure proper permissions for MariaDB directories
RUN chown -R dbuser:dbuser /var/lib/mysql /etc/mysql

# Expose the default MariaDB port (3306)
EXPOSE 3306

# Switch to the new user
USER mariadbuser

# Add health check for the container
HEALTHCHECK --interval=1m --timeout=10s --start-period=30s --retries=3 \
CMD mysqladmin ping -h localhost || exit 1

# Switch to the non-root user
USER dbuser
232 changes: 232 additions & 0 deletions create_get_procedures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
-- Use the database
USE 0ce;

-- Player Procedures

DELIMITER //

CREATE OR REPLACE PROCEDURE get_all_players()
BEGIN
SELECT player_id, player_name, email, gold, created_at, last_login
FROM player;
END //

CREATE OR REPLACE PROCEDURE get_player_by_id(IN p_player_id INT)
BEGIN
SELECT player_id, player_name, email, gold, created_at, last_login
FROM player
WHERE player_id = p_player_id;
END //

-- This procedure is used to check if an email already exists in the database
CREATE OR REPLACE PROCEDURE get_player_by_email(IN p_email VARCHAR(100), OUT email_exists BOOLEAN)
BEGIN
SELECT EXISTS(SELECT 1 FROM player WHERE email = p_email) INTO email_exists;
END //

CREATE OR REPLACE PROCEDURE get_player_by_name(IN p_player_name VARCHAR(100))
BEGIN
SELECT player_id, player_name, email, gold, created_at, last_login
FROM player
WHERE player_name = p_player_name;
END //

CREATE OR REPLACE PROCEDURE get_player_worlds(IN p_player_id INT)
BEGIN
SELECT w.world_id, w.world_name, w.world_description, w.created_at
FROM world w
JOIN player_world pw ON w.world_id = pw.world_id
WHERE pw.player_id = p_player_id;
END //

CREATE OR REPLACE PROCEDURE get_player_cities(IN p_world_id INT, IN p_player_id INT)
BEGIN
SELECT c.city_id, c.city_name, c.x, c.y, c.island_id
FROM city c
JOIN world w ON c.world_id = w.world_id
WHERE c.owner_id = p_player_id
AND w.world_id = p_world_id;
END //

CREATE OR REPLACE PROCEDURE get_player_battles(IN p_player_id INT)
BEGIN
SELECT battle_id, attacker_id, defender_id, battle_time, winner_id, loser_id, loot_wood,
loot_stone, loot_silver
FROM battle
WHERE attacker_id = p_player_id OR defender_id = p_player_id;
END //

-- World Procedures

CREATE OR REPLACE PROCEDURE get_all_worlds()
BEGIN
SELECT world_id, world_name, world_description, seed, action_speed, unit_speed, trade_speed,
night_bonus, beginner_protection, morale, world_status, created_at
FROM world;
END //

CREATE OR REPLACE PROCEDURE get_world_by_id(IN p_world_id INT)
BEGIN
SELECT world_id, world_name, world_description, seed, action_speed, unit_speed, trade_speed,
night_bonus, beginner_protection, morale, world_status, created_at
FROM world
WHERE world_id = p_world_id;
END //

CREATE OR REPLACE PROCEDURE get_active_worlds()
BEGIN
SELECT world_id, world_name, world_description, created_at
FROM world
WHERE world_status = 1;
END //

CREATE OR REPLACE PROCEDURE get_players_in_world(IN p_world_id INT)
BEGIN
SELECT p.player_id, p.player_name, p.email, p.gold, p.created_at
FROM player p
JOIN p_player_world pw ON p.player_id = pw.player_id
WHERE pw.world_id = p_world_id;
END //

CREATE OR REPLACE PROCEDURE get_islands_in_world(IN p_world_id INT)
BEGIN
SELECT island_id, x, y
FROM island
WHERE world_id = p_world_id;
END //

CREATE OR REPLACE PROCEDURE get_cities_in_world(IN p_world_id INT)
BEGIN
SELECT c.city_id, c.city_name, c.x, c.y, c.owner_id, c.island_id
FROM city c
JOIN island i ON c.island_id = i.island_id
WHERE i.world_id = p_world_id;
END //

-- Island Procedures

CREATE OR REPLACE PROCEDURE get_all_islands()
BEGIN
SELECT island_id, x, y, world_id
FROM island;
END //

CREATE OR REPLACE PROCEDURE get_island_by_id(IN p_island_id INT)
BEGIN
SELECT island_id, x, y, world_id
FROM island
WHERE island_id = p_island_id;
END //

CREATE OR REPLACE PROCEDURE get_island_cities(IN p_island_id INT)
BEGIN
SELECT city_id, city_name, x, y, owner_id
FROM city
WHERE island_id = p_island_id;
END //

-- City Procedures

CREATE OR REPLACE PROCEDURE get_all_cities()
BEGIN
SELECT city_id, city_name, x, y, island_id, owner_id
FROM city;
END //

CREATE OR REPLACE PROCEDURE get_city_by_id(IN p_city_id INT)
BEGIN
SELECT city_id, city_name, x, y, island_id, owner_id
FROM city
WHERE id = p_city_id;
END //

CREATE OR REPLACE PROCEDURE get_city_buildings(IN p_city_id INT)
BEGIN
SELECT building_id, building_name, building_level, max_level
FROM building
WHERE city_id = p_city_id;
END //

CREATE OR REPLACE PROCEDURE get_city_units(IN p_city_id INT)
BEGIN
SELECT u.unit_id, u.unit_name, cu.quantity
FROM unit u
JOIN city_unit cu ON u.unit_id = cu.unit_id
WHERE cu.city_id = p_city_id;
END //

-- Building Procedures

CREATE OR REPLACE PROCEDURE get_all_buildings()
BEGIN
SELECT building_id, building_name, building_level, max_level, city_id
FROM building;
END //

CREATE OR REPLACE PROCEDURE get_building_by_id(IN p_building_id INT)
BEGIN
SELECT building_id, building_name, building_level, max_level, city_id
FROM building
WHERE id = p_building_id;
END //

CREATE OR REPLACE PROCEDURE get_building_prerequisites(IN p_building_id INT)
BEGIN
SELECT prerequisite_id
FROM building_prerequisite
WHERE building_id = p_building_id;
END //

-- Unit Procedures

CREATE OR REPLACE PROCEDURE get_all_units()
BEGIN
SELECT unit_id, unit_name, unit_description, unit_type, wood_cost, stone_cost, silver_cost,
population_cost, training_time, damage, defense_blunt, defense_distance, defense_sharp,
speed, can_fly
FROM unit;
END //

CREATE OR REPLACE PROCEDURE get_unit_by_id(IN p_unit_id INT)
BEGIN
SELECT unit_id, unit_name, unit_description, unit_type, wood_cost, stone_cost, silver_cost,
population_cost, training_time, damage, defense_blunt, defense_distance, defense_sharp,
speed, can_fly
FROM unit
WHERE id = p_unit_id;
END //

-- Battle Procedures

CREATE OR REPLACE PROCEDURE get_all_battles()
BEGIN
SELECT battle_id, attacker_id, defender_id, battle_time, winner_id, loser_id, loot_wood,
loot_stone, loot_silver
FROM battle;
END //

CREATE OR REPLACE PROCEDURE get_battle_by_id(IN p_battle_id INT)
BEGIN
SELECT battle_id, attacker_id, defender_id, battle_time, winner_id, loser_id, loot_wood,
loot_stone, loot_silver
FROM battle
WHERE battle_id = p_battle_id;
END //

CREATE OR REPLACE PROCEDURE get_battle_units(IN p_battle_id INT)
BEGIN
SELECT unit_id, quantity, side
FROM battle_unit
WHERE battle_id = p_battle_id;
END //

-- Miscellaneous Procedures

CREATE OR REPLACE PROCEDURE get_building_requirements(IN p_building_id INT)
BEGIN
SELECT required_wood, required_stone, required_silver, required_population
FROM building_requirement
WHERE building_id = p_building_id;
END //

DELIMITER ;
37 changes: 19 additions & 18 deletions create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ USE 0ce;

CREATE TABLE IF NOT EXISTS player (
player_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
player_name VARCHAR(100),
email VARCHAR(100) UNIQUE,
player_name VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
hashed_password VARBINARY(255) NOT NULL,
salt VARBINARY(16),
salt VARBINARY(16) NOT NULL,
gold INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS world (
world_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
world_name VARCHAR(100),
world_name VARCHAR(100) UNIQUE NOT NULL,
world_description TEXT,
seed INT NOT NULL,
action_speed INT DEFAULT 1 NOT NULL,
unit_speed INT DEFAULT 1 NOT NULL,
trade_speed INT DEFAULT 1 NOT NULL,
night_bonus INT DEFAULT 0 NOT NULL,
beginner_protection INT DEFAULT 0 NOT NULL,
morale BOOL DEFAULT FALSE NOT NULL,
alliance_cap INT DEFAULT 0 NOT NULL,
world_status TINYINT DEFAULT 1 NOT NULL,
seed INT,
action_speed TINYINT UNSIGNED DEFAULT 1,
unit_speed TINYINT UNSIGNED DEFAULT 1,
trade_speed TINYINT UNSIGNED DEFAULT 1,
night_bonus INT DEFAULT 0,
beginner_protection INT DEFAULT 0,
morale BOOL DEFAULT FALSE,
world_status TINYINT DEFAULT 2,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB;

Expand All @@ -51,8 +50,10 @@ CREATE TABLE IF NOT EXISTS city (
x INT NOT NULL,
y INT NOT NULL,
owner_id INT UNSIGNED NOT NULL,
world_id INT UNSIGNED NOT NULL,
FOREIGN KEY (island_id) REFERENCES island (island_id),
FOREIGN KEY (owner_id) REFERENCES player (player_id)
FOREIGN KEY (owner_id) REFERENCES player (player_id),
FOREIGN KEY (world_id) REFERENCES world (world_id)
) ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS building (
Expand Down Expand Up @@ -82,7 +83,7 @@ CREATE TABLE IF NOT EXISTS building_prerequisite (
) ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS unit (
unit_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
unit_id TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
unit_name VARCHAR(100),
unit_description TEXT,
unit_type TINYINT NOT NULL,
Expand All @@ -91,7 +92,7 @@ CREATE TABLE IF NOT EXISTS unit (
silver_cost INT DEFAULT 0 NOT NULL,
population_cost INT DEFAULT 0 NOT NULL,
training_time INT DEFAULT 0 NOT NULL,
damage INT DEFAULT 1 NOT NULL,
damage INT DEFAULT 0 NOT NULL,
defense_blunt INT DEFAULT 0 NOT NULL,
defense_distance INT DEFAULT 0 NOT NULL,
defense_sharp INT DEFAULT 0 NOT NULL,
Expand All @@ -101,7 +102,7 @@ CREATE TABLE IF NOT EXISTS unit (

CREATE TABLE IF NOT EXISTS city_unit (
city_id INT UNSIGNED NOT NULL,
unit_id INT UNSIGNED NOT NULL,
unit_id TINYINT UNSIGNED NOT NULL,
quantity INT DEFAULT 0 NOT NULL,
PRIMARY KEY (city_id, unit_id),
FOREIGN KEY (city_id) REFERENCES city (city_id),
Expand All @@ -126,7 +127,7 @@ CREATE TABLE IF NOT EXISTS battle (

CREATE TABLE IF NOT EXISTS battle_unit (
battle_id INT UNSIGNED NOT NULL,
unit_id INT UNSIGNED NOT NULL,
unit_id TINYINT UNSIGNED NOT NULL,
quantity INT DEFAULT 0 NOT NULL,
side TINYINT NOT NULL,
PRIMARY KEY (battle_id, unit_id),
Expand Down