Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

SQL coding style: Difference between revisions

From MoodleDocs
m (Protected "SQL coding style": Developer Docs Migration ([Edit=Allow only administrators] (indefinite)))
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Template:Migrated|newDocId=/general/development/policies/codingstyle/sql}}
This page describes recommended coding style for complex database queries.  
This page describes recommended coding style for complex database queries.  


Line 12: Line 13:
* Do not use right joins.
* Do not use right joins.
* Always use AS keyword for column aliases.
* Always use AS keyword for column aliases.
* Never use AS keyword for table aliases.
* Use <> for comparing if values are not equals and do not use !=


==Double quotes==
==Double quotes==
Line 17: Line 20:
All sql queries and fragments should be enclosed in double quotes, do not concat SQL from multiple parts if possible. The single quotes are used for sql strings, it also helps with visual highlighting and SQL code completion in some IDEs.
All sql queries and fragments should be enclosed in double quotes, do not concat SQL from multiple parts if possible. The single quotes are used for sql strings, it also helps with visual highlighting and SQL code completion in some IDEs.


<code php>
<syntaxhighlight lang="php">
$records = $DB->get_records_select('some_table', "id > ?", array(111));
$records = $DB->get_records_select('some_table', "id > ?", array(111));
</code>
</syntaxhighlight>


==Parameter placeholders==
==Parameter placeholders==


All variable query parameters must be specified via placeholders. It is possible to use three different types of placeholders: :named, ? and $1. It is recommended to use named parameters if there is more than one.
All variable query parameters must be specified via placeholders. It is possible to use three different types of placeholders: :named, ? and $1. It is recommended to use named parameters if there is more than one parameter.


<code php>
<syntaxhighlight lang="php">
$sql = "SELECT *
$sql = "SELECT *
           FROM {some_table}
           FROM {some_table}
         WHERE id > :above";
         WHERE id > :above";
$records = $DB->get_records_sql($sql, array('above'=>111));
$records = $DB->get_records_sql($sql, array('above'=>111));
</code>
</syntaxhighlight>


==Indentation==
==Indentation==


[[File:sql_indentation.png]]
[[File:sql_indentation.png]]
==Subqueries==
There are no strict rules for subquery indentation, the deciding factor is good readability - see MDLSITE-1914.


==See also==
==See also==


* [[Data manipulation API]]
* [[Data manipulation API]]
* [[Database]]
* [[Coding style]]
* [[Coding style]]


Line 44: Line 52:
[[Category:XMLDB]]
[[Category:XMLDB]]
[[Category:DB]]
[[Category:DB]]
[[ja:SQLコーディングスタイル]]

Latest revision as of 14:07, 13 June 2022

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!

This page describes recommended coding style for complex database queries.

Full SQL queries are used in $DB->get_records_sql(), $DB->get_recordset_sql() or $DB->execute(). SQL fragments may be used in DML method with _select() suffix.

General rules

  • Use parameter placeholders!
  • All SQL keywords are in UPPER CASE.
  • All SQL queries and fragments should be enclosed in double quotes.
  • Complex SQL queries should be on multiple lines.
  • Multiline SQL queries should be right aligned on SELECT, FROM, JOIN, WHERE, GROUPY BY and HAVING.
  • Use JOIN instead of INNER JOIN.
  • Do not use right joins.
  • Always use AS keyword for column aliases.
  • Never use AS keyword for table aliases.
  • Use <> for comparing if values are not equals and do not use !=

Double quotes

All sql queries and fragments should be enclosed in double quotes, do not concat SQL from multiple parts if possible. The single quotes are used for sql strings, it also helps with visual highlighting and SQL code completion in some IDEs.

$records = $DB->get_records_select('some_table', "id > ?", array(111));

Parameter placeholders

All variable query parameters must be specified via placeholders. It is possible to use three different types of placeholders: :named, ? and $1. It is recommended to use named parameters if there is more than one parameter.

$sql = "SELECT *
          FROM {some_table}
         WHERE id > :above";
$records = $DB->get_records_sql($sql, array('above'=>111));

Indentation

sql indentation.png

Subqueries

There are no strict rules for subquery indentation, the deciding factor is good readability - see MDLSITE-1914.

See also