From 2a6815ae9f9fa1d70dc5b76c431ccf445b2bd847 Mon Sep 17 00:00:00 2001 From: ruan Date: Fri, 1 Jun 2018 12:13:14 +0200 Subject: [PATCH] Array-based dynamic column selection generation --- inc/page.php | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/inc/page.php b/inc/page.php index d606a69..d4dddd5 100644 --- a/inc/page.php +++ b/inc/page.php @@ -168,17 +168,47 @@ class Page { } } - function get_selection($table) { + function get_selection($table, $fatquery = null, $phpIsBroken = true) { + $columns = array("id", "uuid", "reason", "banned_by_name", "banned_by_uuid", "time", "until", "server_origin", "server_scope", "active", "ipban"); + $bitColumns = array("active", "ipban"); + + if ($table === $this->settings->table['warnings']) { + array_push($columns, "warned"); + array_push($bitColumns, "warned"); + } + + if ($table !== $this->settings->table['kicks']) { + array_push($columns, "removed_by_uuid", "removed_by_name", "removed_by_date"); + } + // Under certain versions of PHP, there is 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,server_origin,server_scope,CAST(active AS UNSIGNED) AS active,CAST(ipban AS UNSIGNED) AS ipban"; - if ($table === $this->settings->table['warnings']) { - $selection .= ",CAST(warned AS UNSIGNED) AS warned"; - } - if ($table !== $this->settings->table['kicks']) { - $selection .= ",removed_by_uuid,removed_by_name,removed_by_date"; + if ($phpIsBroken === true) { + foreach ($bitColumns as $column) { + unset($columns[$column]); + $alias = $column; +// if ($fatquery !== null) { +// $alias = "$fatquery.$column"; +// } + array_push($columns, "CAST($column AS UNSIGNED) AS $alias"); + } } + + // is there really no better way to do this? +// if ($fatquery !== null) { +// foreach ($columns as $column) { +// if (!array_key_exists($column, $bitColumns)) { +// unset($columns[$column]); +// array_push($columns, "$column AS $fatquery.$column"); +// } +// } +// } + + $selection = implode(",", $columns); + +// echo $selection; + return $selection; }