summaryrefslogtreecommitdiff
path: root/docs/sql-standards.md
diff options
context:
space:
mode:
authorKitzunu <24550914+Kitzunu@users.noreply.github.com>2021-05-04 13:28:13 +0200
committerGitHub <noreply@github.com>2021-05-04 13:28:13 +0200
commit8eaf79663deb9d5d5af5c34af0eadab42d2dac3c (patch)
tree080755d9f2382384e059984a70953657558b28d8 /docs/sql-standards.md
parent386d092c28b608dba8b6b3236ccd069bac06c734 (diff)
downloadwiki-8eaf79663deb9d5d5af5c34af0eadab42d2dac3c.tar.gz
wiki-8eaf79663deb9d5d5af5c34af0eadab42d2dac3c.tar.bz2
wiki-8eaf79663deb9d5d5af5c34af0eadab42d2dac3c.zip
chore(docs): SOP (#454)
* chore(docs): Code standards * Create file-headers.md * sql * SOP * issue tracker * Update issue-tracker-standards.md * Update issue-tracker-standards.md * Update sql-standards.md * Update SQL-Versioning.md
Diffstat (limited to 'docs/sql-standards.md')
-rw-r--r--docs/sql-standards.md140
1 files changed, 140 insertions, 0 deletions
diff --git a/docs/sql-standards.md b/docs/sql-standards.md
new file mode 100644
index 0000000..e2c83ba
--- /dev/null
+++ b/docs/sql-standards.md
@@ -0,0 +1,140 @@
+---
+tableofcontents: 1
+---
+
+# SQL Standards
+
+## Queries
+
+### General standards
+
+We always use backticks \` around keywords. \`creature_loot_template\`.
+
+We always use single quotes around string values ' ' but NEVER around an integer.
+
+### INSERT & DELETE
+
+We always DELETE before an INSERT to ensure we always put fields in the query and that no errors occur.
+
+Wrong:
+
+```sql
+INSERT INTO `creature_loot_template` (3, 884, 0, 40, 1, 1, 0, 1, 1, 'Comment');
+```
+
+Correct:
+
+```sql
+DELETE FROM `creature_loot_template` WHERE `entry` = 3 AND `item` = 884;
+INSERT INTO `creature_loot_template` (`Entry`, `Item`, `Reference`, `Chance`, `QuestRequired`, `LootMode`, `GroupId`, `MinCount`, `MaxCount`, `Comment`) VALUES
+(3, 884, 0, 40, 1, 1, 0, 1, 1, 'Comment');
+```
+
+### UPDATE
+
+Make sure your queries are precise so you avoid changing something you do not want to edit.
+
+Rule of thumb, always include all primary keys in your WHERE clause.
+
+Wrong:
+
+```sql
+UPDATE `creature_loot_template` SET `Chance` = 100 WHERE `item` = 884;
+```
+
+Correct:
+
+```sql
+UPDATE `creature_loot_template` SET `Chance` = 100 WHERE `entry` = 3 AND `item` = 884;
+```
+
+### Variables
+
+Variables can be good where you change the same entry in several places to avoid mistakes.
+
+Before:
+
+```sql
+UPDATE `creature_template` SET `AIName` = 'SmartAI' WHERE `entry` = 7727;
+
+DELETE FROM `smart_scripts` WHERE `entryorguid` = 7727 AND `source_type` = 0;
+INSERT INTO `smart_scripts` (`entryorguid`, `source_type`, `id`, `link`, `event_type`, `event_phase_mask`, `event_chance`, `event_flags`, `event_param1`, `event_param2`, `event_param3`, `event_param4`, `event_param5`, `action_type`, `action_param1`, `action_param2`, `action_param3`, `action_param4`, `action_param5`, `action_param6`, `target_type`, `target_param1`, `target_param2`, `target_param3`, `target_param4`, `target_x`, `target_y`, `target_z`, `target_o`, `comment`) VALUES
+(7727, 0, 0, 0, 0, 0, 100, 0, 2000, 4000, 2000, 4000, 0, 11, 930, 64, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - In Combat - Cast \'Chain Lightning\''),
+(7727, 0, 1, 0, 2, 0, 100, 1, 0, 50, 0, 0, 0, 11, 8499, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - Between 0-50% Health - Cast \'Fire Nova\' (No Repeat)'),
+(7727, 0, 2, 0, 2, 0, 100, 0, 0, 30, 0, 0, 0, 11, 8005, 64, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - Between 0-30% Health - Cast \'Healing Wave\'');
+```
+
+After:
+
+```sql
+-- Grimtotem Shaman SAI
+SET @ENTRY := 7727;
+SET @SPELL1 := 930;
+SET @SPELL2 := 8499;
+SET @SPELL3 := 8005;
+
+UPDATE `creature_template` SET `AIName` = 'SmartAI' WHERE `entry` = @ENTRY;
+
+DELETE FROM `smart_scripts` WHERE `entryorguid` = @ENTRY AND `source_type` = 0;
+INSERT INTO `smart_scripts` (`entryorguid`, `source_type`, `id`, `link`, `event_type`, `event_phase_mask`, `event_chance`, `event_flags`, `event_param1`, `event_param2`, `event_param3`, `event_param4`, `event_param5`, `action_type`, `action_param1`, `action_param2`, `action_param3`, `action_param4`, `action_param5`, `action_param6`, `target_type`, `target_param1`, `target_param2`, `target_param3`, `target_param4`, `target_x`, `target_y`, `target_z`, `target_o`, `comment`) VALUES
+(@ENTRY, 0, 0, 0, 0, 0, 100, 0, 2000, 4000, 2000, 4000, 0, 11, @SPELL1, 64, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - In Combat - Cast \'Chain Lightning\''),
+(@ENTRY, 0, 1, 0, 2, 0, 100, 1, 0, 50, 0, 0, 0, 11, @SPELL2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - Between 0-50% Health - Cast \'Fire Nova\' (No Repeat)'),
+(@ENTRY, 0, 2, 0, 2, 0, 100, 0, 0, 30, 0, 0, 0, 11, @SPELL3, 64, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'Grimtotem Shaman - Between 0-30% Health - Cast \'Healing Wave\'');
+```
+
+
+### Compact queries
+
+We always keep the code as compact as possible to limit the size of the files and decrease the number of queries needed to run.
+
+Wrong:
+
+```sql
+DELETE FROM `table_1` WHERE `entry` = 1000;
+DELETE FROM `table_1` WHERE `entry` = 2000;
+DELETE FROM `table_1` WHERE `entry` = 3000;
+
+
+INSERT INTO `table_1` VALUES (1000, ...);
+INSERT INTO `table_1` VALUES (2000, ...);
+INSERT INTO `table_1` VALUES (3000, ...);
+
+UPDATE `table_1` SET `field_1` = 'someValue' WHERE `entry` = 1000;
+UPDATE `table_1` SET `field_1` = 'someValue' WHERE `entry` = 2000;
+UPDATE `table_1` SET `field_1` = 'someValue' WHERE `entry` = 3000;
+```
+
+Correct:
+
+```sql
+DELETE FROM `table_1` WHERE `entry` IN (1000, 2000, 3000);
+
+INSERT INTO `table_1` VALUES
+(1000, ...),
+(2000, ...),
+(3000, ...);
+
+UPDATE `table_1` SET `field_1` = 'someValue' WHERE `entry` IN (1000, 2000, 3000);
+```
+
+### Flags & Bits
+
+For fields in the database where we work with flags it is always preffered that we add or remove flags instead of overriding them.
+
+Wrong:
+
+```sql
+UPDATE `creature_template` SET `mechanic_immune_mask` = 617299803 WHERE `entry` = 7727;
+```
+
+Correct:
+
+```sql
+-- Adding flags
+UPDATE `creature_template` SET `mechanic_immune_mask`=`mechanic_immune_mask`|64|256|1024 WHERE `entry` = 7727;
+
+-- Removing flags
+UPDATE `creature_template` SET `mechanic_immune_mask`=`mechanic_immune_mask`&~(64|256|1024) WHERE `entry` = 7727;
+```
+
+**Note for SQL reviewers:** When we work with GUID's, make sure that we use as low entries as possible to fill out the gaps in the database. This can easily be done with tools like [Unused GUID Searcher](https://github.com/azerothcore/unused-guid-search).