5 * This include file implements coder functionality for SQL strings
9 * Implements hook_reviews().
11 function coder_review_sql_reviews() {
12 $table = '\{[A-Za-z_]+\}'; // table-regex
15 // NOTE: this doesn't catch all non-upper case keywords, but is a good start
18 '#value' => '^(select\s+.*\s+from\s+' .
$table .
'|insert\s+into\s+' .
$table .
'|update\s+' .
$table .
'\s+set|delete\s+from\s+' .
$table .
')',
20 '#warning' => 'SQL keywords should be upper case',
21 '#case-sensitive' => TRUE
,
22 '#severity' => 'minor'
26 '#value' => '^(select\s+.*\s+from\s+' .
$bad .
'|insert\s+into\s+' .
$bad .
'|update\s+' .
$bad .
'\s+set|delete\s+from\s' .
$bad .
')',
28 '#warning' => 'table names should be enclosed in {curly_brackets}',
29 '#severity' => 'critical',
33 '#value' => '^(select\s+.*\s+from\s+' .
$table .
'|insert\s+into\s+' .
$table .
'|update\s+' .
$table .
'\s+set|delete\s+from\s' .
$table .
')\s+.*[Ll][Ii][Mm][Ii][Tt]\s[0-9]+',
35 '#warning_callback' => '_coder_review_sql_db_query_range_warning',
39 '#value' => '^(select\s+.*\s+from\s+' .
$table .
'|update\s+' .
$table .
'\s+set|delete\s+from\s' .
$table .
')\s+.*!=',
41 '#warning' => 'Use ANSI standard <> instead of !=',
45 '#value' => '^(select\s+.*\s+from\s+' .
$table .
'\s+.+?=\s*`|insert\s+into\s+' .
$table .
'\s+.*?VALUES\s*(\(\s*`|\(.*?,\s*`)|update\s+' .
$table .
'\s+set\s+.*?=\s*`|delete\s+from\s' .
$table .
'\s+.*?=\s*`)',
47 '#warning' => "Don't use back ticks to quote values as it is not compliant with ANSI SQL"
51 '#source' => 'allphp',
52 '#value' => 'db_query\s*\(\s*[\'"]select\s+count\s*\(\s*\*\s*\)\s+from\s+',
53 '#warning_callback' => '_coder_review_sql_select_count_warning',
54 '#severity' => 'minor',
58 '#title' => t('Drupal SQL Standards'),
60 '#description' => t('new review, so use with caution'),
62 return array('sql' => $review);
66 * Define the warning callbacks
69 function _coder_review_sql_db_query_range_warning() {
71 '#warning' => t('Use !db_query_range() instead of the SQL LIMIT clause',
73 '!db_query_range' => theme('drupalapi', array('function' => 'db_query_range')),
76 '#link' => 'http://drupal.org/node/1395',
80 function _coder_review_sql_select_count_warning() {
82 '#warning' => t('You may not want to use SELECT COUNT(*), if all you want to do is check for the existance of any rows, rather than the actual number of rows.'),
83 '#link' => 'http://drupal.org/node/224333#select_count',