Unverified Commit 43b40ea7 by Scott Committed by GitHub

Merge pull request #666 from pupi1985/patch-113

Remove some unnecessary SQL queries
parents a1fe5e54 869db52f
...@@ -520,9 +520,12 @@ function qa_set_user_avatar($userid, $imagedata, $oldblobid = null) ...@@ -520,9 +520,12 @@ function qa_set_user_avatar($userid, $imagedata, $oldblobid = null)
$newblobid = qa_create_blob($imagedata, 'jpeg', null, $userid, null, qa_remote_ip_address()); $newblobid = qa_create_blob($imagedata, 'jpeg', null, $userid, null, qa_remote_ip_address());
if (isset($newblobid)) { if (isset($newblobid)) {
qa_db_user_set($userid, 'avatarblobid', $newblobid); qa_db_user_set($userid, array(
qa_db_user_set($userid, 'avatarwidth', $width); 'avatarblobid' => $newblobid,
qa_db_user_set($userid, 'avatarheight', $height); 'avatarwidth' => $width,
'avatarheight' => $height,
));
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_AVATAR, true); qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_AVATAR, true);
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_GRAVATAR, false); qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_GRAVATAR, false);
......
...@@ -289,8 +289,10 @@ if (QA_FINAL_EXTERNAL_USERS) { ...@@ -289,8 +289,10 @@ if (QA_FINAL_EXTERNAL_USERS) {
if (empty($userinfo['sessioncode']) || ($source !== $userinfo['sessionsource'])) { if (empty($userinfo['sessioncode']) || ($source !== $userinfo['sessionsource'])) {
$sessioncode = qa_db_user_rand_sessioncode(); $sessioncode = qa_db_user_rand_sessioncode();
qa_db_user_set($userid, 'sessioncode', $sessioncode); qa_db_user_set($userid, array(
qa_db_user_set($userid, 'sessionsource', $source); 'sessioncode' => $sessioncode,
'sessionsource' => $source,
));
} else } else
$sessioncode = $userinfo['sessioncode']; $sessioncode = $userinfo['sessioncode'];
......
...@@ -164,17 +164,30 @@ function qa_db_user_get_handle_userids($handles) ...@@ -164,17 +164,30 @@ function qa_db_user_get_handle_userids($handles)
/** /**
* Set $field of $userid to $value in the database users table * Set $field of $userid to $value in the database users table. If the $fields parameter is an array, the $value
* @param $userid * parameter is ignored and each element of the array is treated as a key-value pair of user fields and values.
* @param $field * @param mixed $userid
* @param $value * @param string|array $fields
* @param string|null $value
*/ */
function qa_db_user_set($userid, $field, $value) function qa_db_user_set($userid, $fields, $value = null)
{ {
qa_db_query_sub( if (!is_array($fields)) {
'UPDATE ^users SET ' . qa_db_escape_string($field) . '=$ WHERE userid=$', $fields = array(
$value, $userid $fields => $value,
); );
}
$sql = 'UPDATE ^users SET ';
foreach ($fields as $field => $fieldValue) {
$sql .= qa_db_escape_string($field) . ' = $, ';
}
$sql = substr($sql, 0, -2) . ' WHERE userid = $';
$params = array_values($fields);
$params[] = $userid;
qa_db_query_sub_params($sql, $params);
} }
......
...@@ -162,9 +162,12 @@ if (!QA_FINAL_EXTERNAL_USERS) { ...@@ -162,9 +162,12 @@ if (!QA_FINAL_EXTERNAL_USERS) {
if (isset($useraccount['avatarblobid'])) { if (isset($useraccount['avatarblobid'])) {
require_once QA_INCLUDE_DIR . 'app/blobs.php'; require_once QA_INCLUDE_DIR . 'app/blobs.php';
qa_db_user_set($userid, 'avatarblobid', null); qa_db_user_set($userid, array(
qa_db_user_set($userid, 'avatarwidth', null); 'avatarblobid' => null,
qa_db_user_set($userid, 'avatarheight', null); 'avatarwidth' => null,
'avatarheight' => null,
));
qa_delete_blob($useraccount['avatarblobid']); qa_delete_blob($useraccount['avatarblobid']);
} }
} }
......
...@@ -372,14 +372,26 @@ function qa_db_apply_sub($query, $arguments) ...@@ -372,14 +372,26 @@ function qa_db_apply_sub($query, $arguments)
/** /**
* Run $query after substituting ^, # and $ symbols, and return the result resource (or call fail handler). * Run $query after substituting ^, # and $ symbols, and return the result resource (or call fail handler).
* @param $query * @param string $query
* @return mixed * @return mixed
*/ */
function qa_db_query_sub($query) // arguments for substitution retrieved using func_get_args() function qa_db_query_sub($query) // arguments for substitution retrieved using func_get_args()
{ {
$funcargs = func_get_args(); $funcargs = func_get_args();
return qa_db_query_raw(qa_db_apply_sub($query, array_slice($funcargs, 1))); return qa_db_query_sub_params($query, array_slice($funcargs, 1));
}
/**
* Run $query after substituting ^, # and $ symbols, and return the result resource (or call fail handler).
* Query parameters are passed as an array.
* @param string $query
* @param array $params
* @return mixed
*/
function qa_db_query_sub_params($query, $params)
{
return qa_db_query_raw(qa_db_apply_sub($query, $params));
} }
......
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