Use same selection across all queries

This commit is contained in:
ruan 2015-10-09 09:34:26 +02:00
parent 180d757078
commit 7817c33088
4 changed files with 21 additions and 13 deletions

View File

@ -26,8 +26,9 @@ class History {
if ($count_st->execute() && ($row = $count_st->fetch()) !== null) {
$counts[$type] = $row['count'];
}
$sel = $page->get_selection($table);
$st = $page->conn->prepare("SELECT * FROM $table WHERE $field=:uuid ORDER BY time");
$st = $page->conn->prepare("SELECT $sel FROM $table WHERE $field=:uuid ORDER BY time");
$st->bindParam(":uuid", $uuid, PDO::PARAM_STR);

View File

@ -88,6 +88,21 @@ class Page {
}
}
function get_selection($table) {
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$selection = "*";
} else {
// PDO+MySQL under PHP 5.3.x has a bug with BIT columns.
// An empty string is returned no matter what the value is.
// Workaround: cast to unsigned.
$selection = "id,uuid,reason,banned_by_name,banned_by_uuid,time,until,CAST(active AS UNSIGNED) AS active";
if ($table === $this->settings->table['warnings']) {
$selection .= ",CAST(warned AS UNSIGNED) AS warned";
}
}
return $selection;
}
function run_query() {
try {
$table = $this->table;
@ -100,17 +115,7 @@ class Page {
$offset = ($limit * $page);
}
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$sel = "*";
} else {
// PDO+MySQL under PHP 5.3.x has a bug with BIT columns.
// An empty string is returned no matter what the value is.
// Workaround: cast to unsigned.
$sel = "id,uuid,reason,banned_by_name,banned_by_uuid,time,until,CAST(active AS UNSIGNED) AS active";
if ($table === $this->settings->table['warnings']) {
$sel .= ",CAST(warned AS UNSIGNED) AS warned";
}
}
$sel = $this->get_selection($table);
$query = "SELECT $sel FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT :limit OFFSET :offset";
$st = $this->conn->prepare($query);

View File

@ -99,6 +99,7 @@ final class Settings {
try {
$this->conn = new PDO($dsn, $username, $password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}

View File

@ -128,7 +128,8 @@ $id = (int)$id;
$type = $page->type;
$table = $page->table;
$query = "SELECT * FROM $table WHERE id=? LIMIT 1";
$sel = $page->get_selection($table);
$query = "SELECT $sel FROM $table WHERE id=? LIMIT 1";
$st = $page->conn->prepare($query);