Commit 7654be75 by Scott

Replace some qa_db_query_sub calls

parent 006e1162
......@@ -112,29 +112,30 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
*/
function qa_db_favorite_create($userid, $entitytype, $entityid)
{
$db = qa_service('database');
$threshold = qa_opt('max_copy_user_updates'); // if this many users subscribe to it, create a shared stream
// Add in the favorite for this user, unshared events at first (will be switched later if appropriate)
qa_db_query_sub(
'INSERT IGNORE INTO ^userfavorites (userid, entitytype, entityid, nouserevents) VALUES ($, $, #, 0)',
$userid, $entitytype, $entityid
$db->query(
'INSERT IGNORE INTO ^userfavorites (userid, entitytype, entityid, nouserevents) VALUES (?, ?, ?, 0)',
[$userid, $entitytype, $entityid]
);
// See whether this entity already has another favoriter who uses its shared event stream
$useshared = qa_db_read_one_value(qa_db_query_sub(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# AND nouserevents>0 LIMIT 1',
$entitytype, $entityid
));
$useshared = $db->query(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=? AND entityid=? AND nouserevents>0 LIMIT 1',
[$entitytype, $entityid]
)->fetchOneValueOrFail();
// If not, check whether it's time to switch it over to a shared stream
if (!$useshared) {
$favoriters = qa_db_read_one_value(qa_db_query_sub(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# LIMIT #',
$entitytype, $entityid, $threshold
));
$favoriters = $db->query(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=? AND entityid=? LIMIT ?',
[$entitytype, $entityid, $threshold]
)->fetchOneValueOrFail();
$useshared = ($favoriters >= $threshold);
}
......@@ -144,11 +145,11 @@ function qa_db_favorite_create($userid, $entitytype, $entityid)
if ($useshared) {
// ... for all the people for whom we're switching this to a shared stream, find the highest number of other shared streams they have
$maxshared = qa_db_read_one_value(qa_db_query_sub(
$maxshared = $db->query(
'SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ^userfavorites AS shared JOIN ^userfavorites AS unshared ' .
'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=$ AND unshared.entityid=# AND unshared.nouserevents=0 GROUP BY shared.userid) y',
$entitytype, $entityid
));
'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=? AND unshared.entityid=? AND unshared.nouserevents=0 GROUP BY shared.userid) y',
[$entitytype, $entityid]
)->fetchOneValueOrFail();
// ... if this number is greater than our current 'max_copy_user_updates' threshold, increase that threshold (see long comment above)
......@@ -157,9 +158,9 @@ function qa_db_favorite_create($userid, $entitytype, $entityid)
// ... now switch all unshared favoriters (including this new one) over to be shared
qa_db_query_sub(
'UPDATE ^userfavorites SET nouserevents=1 WHERE entitytype=$ AND entityid=# AND nouserevents=0',
$entitytype, $entityid
$db->query(
'UPDATE ^userfavorites SET nouserevents=1 WHERE entitytype=? AND entityid=? AND nouserevents=0',
[$entitytype, $entityid]
);
} else {
......@@ -169,11 +170,11 @@ function qa_db_favorite_create($userid, $entitytype, $entityid)
// ... copy across recent events from the shared stream
qa_db_query_sub(
$db->query(
'INSERT INTO ^userevents (userid, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated) ' .
'SELECT #, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated FROM ' .
'^sharedevents WHERE entitytype=$ AND entityid=#',
$userid, $entitytype, $entityid
'SELECT ?, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated FROM ' .
'^sharedevents WHERE entitytype=? AND entityid=?',
[$userid, $entitytype, $entityid]
);
// ... and truncate the user's stream as appropriate
......@@ -192,13 +193,15 @@ function qa_db_favorite_create($userid, $entitytype, $entityid)
*/
function qa_db_favorite_delete($userid, $entitytype, $entityid)
{
qa_db_query_sub(
'DELETE FROM ^userfavorites WHERE userid=$ AND entitytype=$ AND entityid=#',
$userid, $entitytype, $entityid
$db = qa_service('database');
$db->query(
'DELETE FROM ^userfavorites WHERE userid=? AND entitytype=? AND entityid=?',
[$userid, $entitytype, $entityid]
);
qa_db_query_sub(
'DELETE FROM ^userevents WHERE userid=$ AND entitytype=$ AND entityid=#',
$userid, $entitytype, $entityid
$db->query(
'DELETE FROM ^userevents WHERE userid=? AND entitytype=? AND entityid=?',
[$userid, $entitytype, $entityid]
);
}
......@@ -32,10 +32,10 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
*/
function qa_db_increment_views($postid)
{
$query = 'UPDATE ^posts SET views=views+1, lastviewip=UNHEX($) WHERE postid=# AND (lastviewip IS NULL OR lastviewip!=UNHEX($))';
$query = 'UPDATE ^posts SET views=views+1, lastviewip=UNHEX(?) WHERE postid=? AND (lastviewip IS NULL OR lastviewip!=UNHEX(?))';
$ipHex = bin2hex(@inet_pton(qa_remote_ip_address()));
$result = qa_db_query_sub($query, $ipHex, $postid, $ipHex);
$result = qa_service('database')->query($query, [$ipHex, $postid, $ipHex]);
return $result->affectedRows() > 0;
}
......
......@@ -35,12 +35,13 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
*/
function qa_db_usernotice_create($userid, $content, $format = '', $tags = null)
{
qa_db_query_sub(
'INSERT INTO ^usernotices (userid, content, format, tags, created) VALUES ($, $, $, $, NOW())',
$userid, $content, $format, $tags
$db = qa_service('database');
$db->query(
'INSERT INTO ^usernotices (userid, content, format, tags, created) VALUES (?, ?, ?, ?, NOW())',
[$userid, $content, $format, $tags]
);
return qa_db_last_insert_id();
return $db->lastInsertId();
}
......@@ -51,7 +52,7 @@ function qa_db_usernotice_create($userid, $content, $format = '', $tags = null)
*/
function qa_db_usernotice_delete($userid, $noticeid)
{
qa_db_query_sub(
qa_service('database')->query(
'DELETE FROM ^usernotices WHERE userid=$ AND noticeid=#',
$userid, $noticeid
);
......@@ -65,8 +66,8 @@ function qa_db_usernotice_delete($userid, $noticeid)
*/
function qa_db_usernotices_list($userid)
{
return qa_db_read_all_assoc(qa_db_query_sub(
'SELECT noticeid, tags, UNIX_TIMESTAMP(created) AS created FROM ^usernotices WHERE userid=$ ORDER BY created',
$userid
));
return qa_service('database')->query(
'SELECT noticeid, tags, UNIX_TIMESTAMP(created) AS created FROM ^usernotices WHERE userid=? ORDER BY created',
[$userid]
)->fetchAllAssoc();
}
......@@ -32,9 +32,9 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
*/
function qa_db_set_option($name, $value)
{
qa_db_query_sub(
'INSERT INTO ^options (title, content) VALUES ($, $) ' .
qa_service('database')->query(
'INSERT INTO ^options (title, content) VALUES (?, ?) ' .
'ON DUPLICATE KEY UPDATE content = VALUES(content)',
$name, $value
[$name, $value]
);
}
......@@ -224,9 +224,9 @@ function qa_db_points_update_ifuser($userid, $columns)
*/
function qa_db_points_set_bonus($userid, $bonus)
{
qa_db_query_sub(
"INSERT INTO ^userpoints (userid, bonus) VALUES ($, #) ON DUPLICATE KEY UPDATE bonus=#",
$userid, $bonus, $bonus
qa_service('database')->query(
'INSERT INTO ^userpoints (userid, bonus) VALUES (?, ?) ON DUPLICATE KEY UPDATE bonus=?',
[$userid, $bonus, $bonus]
);
}
......@@ -236,8 +236,9 @@ function qa_db_points_set_bonus($userid, $bonus)
*/
function qa_db_userpointscount_update()
{
if (qa_should_update_counts()) {
qa_db_query_sub(
$db = qa_service('database');
if ($db->shouldUpdateCounts()) {
$db->query(
"INSERT INTO ^options (title, content) " .
"SELECT 'cache_userpointscount', COUNT(*) FROM ^userpoints " .
"ON DUPLICATE KEY UPDATE content = VALUES(content)"
......
......@@ -180,14 +180,14 @@ function qa_db_user_set($userid, $fields, $value = null)
$sql = 'UPDATE ^users SET ';
foreach ($fields as $field => $fieldValue) {
$sql .= qa_db_escape_string($field) . ' = $, ';
$sql .= qa_db_escape_string($field) . ' = ?, ';
}
$sql = substr($sql, 0, -2) . ' WHERE userid = $';
$sql = substr($sql, 0, -2) . ' WHERE userid = ?';
$params = array_values($fields);
$params[] = $userid;
qa_db_query_sub_params($sql, $params);
qa_service('database')->query($sql, $params);
}
......
......@@ -25,7 +25,7 @@ class qa_event_logger
public function init_queries($table_list)
{
if (qa_opt('event_logger_to_database')) {
$tablename = qa_db_add_table_prefix('eventlog');
$tablename = (new \Q2A\Database\DbQueryHelper)->addTablePrefix('eventlog');
if (!in_array($tablename, $table_list)) {
// table does not exist, so create it
......@@ -47,7 +47,7 @@ class qa_event_logger
') ENGINE=MyISAM DEFAULT CHARSET=utf8';
} else {
// table exists: check it has the correct schema
$column = qa_db_read_one_assoc(qa_db_query_sub('SHOW COLUMNS FROM ^eventlog WHERE Field="ipaddress"'));
$column = qa_service('database')->query('SHOW COLUMNS FROM ^eventlog WHERE Field="ipaddress"')->fetchNextAssocOrFail();
if (strtolower($column['Type']) !== 'varchar(45)') {
// upgrade to handle IPv6
return 'ALTER TABLE ^eventlog MODIFY ipaddress VARCHAR(45) CHARACTER SET ascii';
......@@ -167,10 +167,10 @@ class qa_event_logger
$paramstring .= (strlen($paramstring) ? "\t" : '') . $key . '=' . $this->value_to_text($value);
}
qa_db_query_sub(
qa_service('database')->query(
'INSERT INTO ^eventlog (datetime, ipaddress, userid, handle, cookieid, event, params) ' .
'VALUES (NOW(), $, $, $, #, $, $)',
qa_remote_ip_address(), $userid, $handle, $cookieid, $event, $paramstring
'VALUES (NOW(), ?, ?, ?, ?, ?, ?)',
[qa_remote_ip_address(), $userid, $handle, $cookieid, $event, $paramstring]
);
}
......
......@@ -36,8 +36,8 @@ class qa_html_theme_layer extends qa_html_theme_base
if (!empty($postids)) {
// Retrieve the content for these questions from the database
$maxlength = qa_opt('mouseover_content_max_len');
$result = qa_db_query_sub('SELECT postid, content, format FROM ^posts WHERE postid IN (#)', $postids);
$postinfo = qa_db_read_all_assoc($result, 'postid');
$result = qa_service('database')->query('SELECT postid, content, format FROM ^posts WHERE postid IN (?)', [$postids]);
$postinfo = $result->fetchAllAssoc('postid');
// Get the regular expression fragment to use for blocked words and the maximum length of content to show
......
......@@ -137,6 +137,7 @@ class qa_xml_sitemap
public function process_request($request)
{
@ini_set('display_errors', 0); // we don't want to show PHP errors inside XML
$db = qa_service('database');
header('Content-type: text/xml; charset=utf-8');
......@@ -147,17 +148,17 @@ class qa_xml_sitemap
// Question pages
if (qa_opt('xml_sitemap_show_questions')) {
$hotstats = qa_db_read_one_assoc(qa_db_query_sub(
$hotstats = $db->query(
"SELECT MIN(hotness) AS base, MAX(hotness)-MIN(hotness) AS spread FROM ^posts WHERE type='Q'"
));
)->fetchNextAssocOrFail();
$nextpostid = 0;
while (1) {
$questions = qa_db_read_all_assoc(qa_db_query_sub(
"SELECT postid, title, hotness FROM ^posts WHERE postid>=# AND type='Q' ORDER BY postid LIMIT 100",
$nextpostid
));
$questions = $db->query(
"SELECT postid, title, hotness FROM ^posts WHERE postid>=? AND type='Q' ORDER BY postid LIMIT 100",
[$nextpostid]
)->fetchAllAssoc();
if (!count($questions))
break;
......@@ -177,10 +178,10 @@ class qa_xml_sitemap
$nextuserid = 0;
while (1) {
$users = qa_db_read_all_assoc(qa_db_query_sub(
"SELECT userid, handle FROM ^users WHERE userid>=# ORDER BY userid LIMIT 100",
$nextuserid
));
$users = $db->query(
"SELECT userid, handle FROM ^users WHERE userid>=? ORDER BY userid LIMIT 100",
[$nextuserid]
)->fetchAllAssoc();
if (!count($users))
break;
......@@ -199,10 +200,10 @@ class qa_xml_sitemap
$nextwordid = 0;
while (1) {
$tagwords = qa_db_read_all_assoc(qa_db_query_sub(
"SELECT wordid, word, tagcount FROM ^words WHERE wordid>=# AND tagcount>0 ORDER BY wordid LIMIT 100",
$nextwordid
));
$tagwords = $db->query(
"SELECT wordid, word, tagcount FROM ^words WHERE wordid>=? AND tagcount>0 ORDER BY wordid LIMIT 100",
[$nextwordid]
)->fetchAllAssoc();
if (!count($tagwords))
break;
......@@ -221,10 +222,10 @@ class qa_xml_sitemap
$nextcategoryid = 0;
while (1) {
$categories = qa_db_read_all_assoc(qa_db_query_sub(
"SELECT categoryid, backpath FROM ^categories WHERE categoryid>=# AND qcount>0 ORDER BY categoryid LIMIT 2",
$nextcategoryid
));
$categories = $db->query(
"SELECT categoryid, backpath FROM ^categories WHERE categoryid>=? AND qcount>0 ORDER BY categoryid LIMIT 2",
[$nextcategoryid]
)->fetchAllAssoc();
if (!count($categories))
break;
......@@ -245,11 +246,11 @@ class qa_xml_sitemap
$nextcategoryid = 0;
while (1) { // only find categories with a child
$categories = qa_db_read_all_assoc(qa_db_query_sub(
$categories = $db->query(
"SELECT parent.categoryid, parent.backpath FROM ^categories AS parent " .
"JOIN ^categories AS child ON child.parentid=parent.categoryid WHERE parent.categoryid>=# GROUP BY parent.categoryid LIMIT 100",
$nextcategoryid
));
[$nextcategoryid]
)->fetchAllAssoc();
if (!count($categories))
break;
......
......@@ -35,7 +35,8 @@ class DbResult
}
/**
* Return the first row from the results as an array of [column name] => 'column value'.
* Return the first row from the results as an array of [column name] => 'column value'. If no value is fetched
* from the database, return null.
* @return array|null
*/
public function fetchNextAssoc()
......@@ -47,7 +48,7 @@ class DbResult
/**
* Return the first row from the results as an array of [column name] => [column value]. If no value is fetched
* from the database then an exception is thrown.
* from the database, throw an exception.
* @return array|null
* @throws ReadingFromEmptyResultException
*/
......@@ -79,7 +80,7 @@ class DbResult
/**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries. If
* no value is fetched form the database return null.
* no value is fetched from the database, return null.
* @param $col 0-indexed column to select from (defaults to first column).
* @return string
*/
......@@ -92,7 +93,7 @@ class DbResult
/**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries. If
* no value is fetched from the database then an exception is thrown.
* no value is fetched from the database, throw an exception.
* @param $col 0-indexed column to select from (defaults to first column).
* @return string
* @throws ReadingFromEmptyResultException
......
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