OC Coding Style: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
→SQL
Keine Bearbeitungszusammenfassung |
(→SQL) |
||
Zeile 19: | Zeile 19: | ||
== SQL == | == SQL == | ||
See also: [[Entwicklung/Codedoku#Datenbank]] | |||
SQL | All SQL language elements are written in CAPS, and all user-defined identifiers in `backticks`. Inner joins are coded as JOIN and not as WHERE. All variable values inserted into SQLs '''must''' be escaped, either via placeholders like <code>'&1'</code> or via function <code>sql_escape()</code>. | ||
SQL code inserted into PHP code is enclosed in double quotes and generally formatted like this: | |||
$result = sql( | $result = sql( | ||
Zeile 27: | Zeile 29: | ||
FROM `caches` | FROM `caches` | ||
JOIN `cache_logs` ON `cache_logs`.`id` = `caches`.`id` | JOIN `cache_logs` ON `cache_logs`.`id` = `caches`.`id` | ||
WHERE `caches`.` | WHERE `caches`.`status` IN ('&1', '&2')" | ||
$status1, | $status1, | ||
$status2 | $status2 | ||
); | |||
Note that the starting " is on a tab position, while the following lines start one column to the right. | |||
If lines get too long, the parameters are moved to the next line(s) and indented to the next Tab position (which is ''three'' chars right of the preceding line start): | |||
$result = sql( | |||
"SELECT | |||
`caches`.`cache_id`, | |||
`caches`.`type` AS `cache_type`, | |||
`cache_logs`.`date` AS `log_date`, | |||
`cache_logs`.`type` AS `log_type`, | |||
FROM `caches` | |||
JOIN `cache_logs` | |||
ON `cache_logs`.`id` = `caches`.`id` | |||
AND `cache_logs`.`type` = '&1' | |||
WHERE | |||
`caches`.`status` = '&1' | |||
AND `cache_logs`.`user_id` = '&2' | |||
$cacheStatus | |||
$userId | |||
); | ); |