Commit f577c125 authored by webchick's avatar webchick

#196862 by Damien Tournoud, et al: Replace COUNT(*) queries with SELECT 1 ......

#196862 by Damien Tournoud, et al: Replace COUNT(*) queries with SELECT 1 ... LIMIT 1 queries when all that's required is a check for whether rows exist.
parent 8b63d832
......@@ -272,7 +272,8 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
function block_admin_configure_validate($form, &$form_state) {
if ($form_state['values']['module'] == 'block') {
if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {box} WHERE bid <> %d AND info = '%s'", $form_state['values']['delta'], $form_state['values']['info']))) {
$box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE bid <> :bid AND info = :info', array(':bid' => $form_state['values']['delta'], ':info' => $form_state['values']['info']), 0, 1)->fetchField();
if (empty($form_state['values']['info']) || $box_exists) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
......@@ -301,7 +302,9 @@ function block_add_block_form(&$form_state) {
}
function block_add_block_form_validate($form, &$form_state) {
if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {box} WHERE info = '%s'", $form_state['values']['info']))) {
$box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE info = :info', array(':info' => $form_state['values']['info']), 0, 1)->fetchField();
if (empty($form_state['values']['info']) || $box_exists) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
......
......@@ -494,8 +494,8 @@ function block_system_themes_form_submit(&$form, &$form_state) {
}
if ($form_state['values']['admin_theme'] && $form_state['values']['admin_theme'] != variable_get('admin_theme', 0)) {
// If we're changing themes, make sure the theme has its blocks initialized.
$result = db_result(db_query("SELECT COUNT(*) FROM {block} WHERE theme = '%s'", $form_state['values']['admin_theme']));
if (!$result) {
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $form_state['values']['admin_theme']), 0, 1)->fetchField();
if (!$has_blocks) {
block_initialize_theme_blocks($form_state['values']['admin_theme']);
}
}
......@@ -515,7 +515,8 @@ function block_system_themes_form_submit(&$form, &$form_state) {
*/
function block_initialize_theme_blocks($theme) {
// Initialize theme's blocks if none already registered.
if (!(db_result(db_query("SELECT COUNT(*) FROM {block} WHERE theme = '%s'", $theme)))) {
$has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $theme), 0, 1)->fetchField();
if (!$has_blocks) {
$default_theme = variable_get('theme_default', 'garland');
$regions = system_region_list($theme);
$result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $default_theme);
......
......@@ -555,7 +555,7 @@ function node_types_rebuild() {
function node_type_save($info) {
$is_existing = FALSE;
$existing_type = !empty($info->old_type) ? $info->old_type : $info->type;
$is_existing = db_query("SELECT COUNT(*) FROM {node_type} WHERE type = :type", array(':type' => $existing_type))->fetchField();
$is_existing = (bool) db_query_range('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $existing_type), 0, 1)->fetchField();
$type = node_type_set_defaults($info);
$fields = array(
......@@ -3085,8 +3085,8 @@ function node_assign_owner_action_form($context) {
}
function node_assign_owner_action_validate($form, $form_state) {
$count = db_query('SELECT COUNT(*) FROM {users} WHERE name = :name', array(':name' => $form_state['values']['owner_name']))->fetchField();
if (intval($count) != 1) {
$exists = (bool) db_query_range('SELECT 1 FROM {users} WHERE name = :name', array(':name' => $form_state['values']['owner_name']), 0, 1)->fetchField();
if (!$exists) {
form_set_error('owner_name', t('Please enter a valid username.'));
}
}
......
......@@ -15,8 +15,8 @@ function path_admin_overview($keys = NULL) {
// Add the filter form above the overview table.
$output = drupal_get_form('path_admin_filter_form', $keys);
// Enable language column if locale is enabled or if we have any alias with language
$count = db_query("SELECT COUNT(*) FROM {url_alias} WHERE language <> ''")->fetchField();
$multilanguage = (module_exists('locale') || $count);
$alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', array(':language' => ''), 0, 1)->fetchField();
$multilanguage = (module_exists('locale') || $alias_exists);
$header = array(
array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
......
......@@ -10,7 +10,7 @@
* Implementation of hook_install().
*/
function php_install() {
$format_exists = db_query("SELECT COUNT(*) FROM {filter_format} WHERE name = 'PHP code'")->fetchField();
$format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', array(':name' => 'PHP code'), 0, 1)->fetchField();
// Add a PHP code text format, if it does not exist. Do this only for the
// first install (or if the format has been manually deleted) as there is no
// reliable method to identify the format in an uninstall hook or in
......
......@@ -511,7 +511,8 @@ function profile_category_access($account, $category) {
return TRUE;
}
else {
return user_edit_access($account) && db_result(db_query("SELECT COUNT(*) FROM {profile_field} WHERE category = '%s' AND visibility <> %d", $category, PROFILE_HIDDEN));
$category_visible = (bool) db_query_range('SELECT 1 FROM {profile_field} WHERE category = :category AND visibility <> :visibility', array(':category' => $category, ':visibility' => PROFILE_HIDDEN), 0, 1)->fetchField();
return user_edit_access($account) && $category_visible;
}
}
......
......@@ -110,7 +110,8 @@ function profile_browse() {
*/
function profile_autocomplete($field, $string) {
$matches = array();
if (db_result(db_query("SELECT COUNT(*) FROM {profile_field} WHERE fid = %d AND autocomplete = 1", $field))) {
$autocomplete_field = (bool) db_query_range("SELECT 1 FROM {profile_field} WHERE fid = :fid AND autocomplete = 1", array(':fid' => $field), 0, 1)->fetchField();
if ($autocomplete_field) {
$result = db_query_range("SELECT value FROM {profile_value} WHERE fid = :fid AND LOWER(value) LIKE LOWER(:value) GROUP BY value ORDER BY value ASC", array(
':fid' => $field,
':value' => $string .'%',
......
......@@ -1199,8 +1199,8 @@ function hook_file_move($file, $source) {
*/
function hook_file_references($file) {
// If upload.module is still using a file, do not let other modules delete it.
$count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField();
if ($count) {
$file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($file_used) {
// Return the name of the module and how many references it has to the file.
return array('upload' => $count);
}
......
......@@ -282,8 +282,8 @@ function upload_file_load($files) {
*/
function upload_file_references($file) {
// If upload.module is still using a file, do not let other modules delete it.
$count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField();
if ($count) {
$file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($file_used) {
// Return the name of the module and how many references it has to the file.
return array('upload' => $count);
}
......
......@@ -731,16 +731,13 @@ function user_admin_role() {
function user_admin_role_validate($form, &$form_state) {
if ($form_state['values']['name']) {
if ($form_state['values']['op'] == t('Save role')) {
$existing_role = db_query("SELECT COUNT(*) FROM {role} WHERE name = :name AND rid != :rid",
array(':name' => $form_state['values']['name'],
':rid' => $form_state['values']['rid']))
->fetchField();
$existing_role = (bool) db_query_range("SELECT 1 FROM {role} WHERE name = :name AND rid != :rid", array(':name' => $form_state['values']['name'], ':rid' => $form_state['values']['rid']), 0, 1)->fetchField();
if ($existing_role) {
form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
}
}
elseif ($form_state['values']['op'] == t('Add role')) {
if (db_query("SELECT COUNT(*) FROM {role} WHERE name = :name", array(':name' => $form_state['values']['name']))->fetchField()) {
if ((bool) db_query_range('SELECT 1 FROM {role} WHERE name = :name', array(':name' => $form_state['values']['name']), 0, 1)->fetchField()) {
form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
}
}
......
......@@ -822,8 +822,8 @@ function user_file_download($filepath) {
*/
function user_file_references($file) {
// Determine if the file is used by this module.
$count = db_query('SELECT COUNT(*) FROM {users} WHERE picture = :fid', array(':fid' => $file->fid))->fetchField();
if ($count) {
$file_used = (bool) db_query_range('SELECT 1 FROM {users} WHERE picture = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($file_used) {
// Return the name of the module and how many references it has to the file.
return array('user' => $count);
}
......@@ -935,7 +935,7 @@ function user_user_validate(&$edit, &$account, $category = NULL) {
if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error);
}
elseif (db_query("SELECT COUNT(*) FROM {users} WHERE uid != :uid AND LOWER(name) = LOWER(:name)", array(':uid' => $uid, ':name' => $edit['name']))->fetchField() > 0) {
elseif ((bool) db_query_range("SELECT 1 FROM {users} WHERE uid != :uid AND LOWER(name) = LOWER(:name)", array(':uid' => $uid, ':name' => $edit['name']), 0, 1)->fetchField()) {
form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name'])));
}
}
......@@ -944,7 +944,7 @@ function user_user_validate(&$edit, &$account, $category = NULL) {
if ($error = user_validate_mail($edit['mail'])) {
form_set_error('mail', $error);
}
elseif (db_query("SELECT COUNT(*) FROM {users} WHERE uid != :uid AND LOWER(mail) = LOWER(:mail)", array(':uid' => $uid, ':mail' => $edit['mail']))->fetchField() > 0) {
elseif ((bool) db_query_range("SELECT 1 FROM {users} WHERE uid != :uid AND LOWER(mail) = LOWER(:mail)", array(':uid' => $uid, ':mail' => $edit['mail']), 0, 1)->fetchField()) {
// Format error message dependent on whether the user is logged in or not.
if ($GLOBALS['user']->uid) {
form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $edit['mail'])));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment