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