From 739579b168178a8e28fa73352faceabebc9cb422 Mon Sep 17 00:00:00 2001 From: int2001 Date: Mon, 12 Aug 2024 12:26:12 +0000 Subject: [PATCH] Refactored Queries II --- application/models/Accumulate_model.php | 696 +++++++++++++----------- 1 file changed, 377 insertions(+), 319 deletions(-) diff --git a/application/models/Accumulate_model.php b/application/models/Accumulate_model.php index 13d61e627..035f9711b 100644 --- a/application/models/Accumulate_model.php +++ b/application/models/Accumulate_model.php @@ -152,8 +152,7 @@ class Accumulate_model extends CI_Model return $this->count_and_add_accumulated_total($query->result()); } - function count_and_add_accumulated_total($array) - { + function count_and_add_accumulated_total($array) { $counter = 0; for ($i = 0; $i < count($array); $i++) { $array[$i]->total = $array[$i]->tot + $counter; @@ -163,6 +162,7 @@ class Accumulate_model extends CI_Model } function get_accumulated_waja($band, $mode, $period, $location_list) { + $binding=[]; if ($period == "year") { $sql = "select year(thcv.col_time_on) year"; } else if ($period == "month") { @@ -187,15 +187,19 @@ class Accumulate_model extends CI_Model if ($band != 'All') { if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; } else { $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; + $sql .= " and col_band = ?"; + $binding[] = $band; } } if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; } $sql .= " and COL_DXCC in ('339') and trim(coalesce(col_state,'')) != ''"; @@ -214,15 +218,19 @@ class Accumulate_model extends CI_Model if ($band != 'All') { if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; } else { $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; + $sql .= " and col_band = ?"; + $binding[] = $band; } } if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; } $sql .= " and COL_DXCC in ('339')"; @@ -241,15 +249,19 @@ class Accumulate_model extends CI_Model if ($band != 'All') { if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; } else { $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; + $sql .= " and col_band = ?"; + $binding[] = $band; } } if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; } if ($period == "year") { @@ -260,315 +272,350 @@ class Accumulate_model extends CI_Model order by date_format(col_time_on, '%Y-%m')"; } - $query = $this->db->query($sql); + $query = $this->db->query($sql, $binding); return $this->count_and_add_accumulated_total($query->result()); } - function get_accumulated_was($band, $mode, $period, $location_list) - { - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } + function get_accumulated_was($band, $mode, $period, $location_list) { + if ($period == "year") { + $sql = "select year(thcv.col_time_on) year"; + } else if ($period == "month") { + $sql = "select date_format(col_time_on, '%Y-%m') year"; + } - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_state) as tot, year - from (select distinct "; + $sql .= ", coalesce(y.tot, 0) tot + from " . $this->config->item('table_name') . " thcv + left outer join ( + select count(col_state) as tot, year + from (select distinct "; - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= "year(col_time_on)"; + } else if ($period == "month") { + $sql .= "date_format(col_time_on, '%Y-%m')"; + } - $sql .= " year, col_state - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; + $sql .= " year, col_state + from " . $this->config->item('table_name') . + " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " and COL_DXCC in ('291', '6', '110')"; - $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; + $sql .= " and COL_DXCC in ('291', '6', '110')"; + $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; + $sql .= " order by year + ) x + where not exists (select 1 from " . $this->config->item('table_name') . " where"; - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } + if ($period == "year") { + $sql .= " year(col_time_on) < year";; + } else if ($period == "month") { + $sql .= " date_format(col_time_on, '%Y-%m') < year";; + } - $sql .= " and col_state = x.col_state"; + $sql .= " and col_state = x.col_state"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " and COL_DXCC in ('291', '6', '110')"; - $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; + $sql .= " and COL_DXCC in ('291', '6', '110')"; + $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; + $sql .= " and station_id in (" . $location_list . ")) + group by year + order by year"; - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } + if ($period == "year") { + $sql .= " ) y on year(thcv.col_time_on) = y.year"; + } else if ($period == "month") { + $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; + } - $sql .= " where station_id in (" . $location_list . ")"; + $sql .= " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= " group by year(thcv.col_time_on), y.tot + order by year(thcv.col_time_on)"; + } else if ($period == "month") { + $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot + order by date_format(col_time_on, '%Y-%m')"; + } - $query = $this->db->query($sql); + $query = $this->db->query($sql, $binding); - return $this->count_and_add_accumulated_total($query->result()); + return $this->count_and_add_accumulated_total($query->result()); } - function get_accumulated_iota($band, $mode, $period, $location_list) - { - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } + function get_accumulated_iota($band, $mode, $period, $location_list) { + $binding = []; + if ($period == "year") { + $sql = "select year(thcv.col_time_on) year"; + } else if ($period == "month") { + $sql = "select date_format(col_time_on, '%Y-%m') year"; + } - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_iota) as tot, year - from (select distinct "; + $sql .= ", coalesce(y.tot, 0) tot + from " . $this->config->item('table_name') . " thcv + left outer join ( + select count(col_iota) as tot, year + from (select distinct "; - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= "year(col_time_on)"; + } else if ($period == "month") { + $sql .= "date_format(col_time_on, '%Y-%m')"; + } - $sql .= " year, col_iota - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; + $sql .= " year, col_iota + from " . $this->config->item('table_name') . + " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; + $sql .= " order by year + ) x + where not exists (select 1 from " . $this->config->item('table_name') . " where"; - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } + if ($period == "year") { + $sql .= " year(col_time_on) < year";; + } else if ($period == "month") { + $sql .= " date_format(col_time_on, '%Y-%m') < year";; + } - $sql .= " and col_iota = x.col_iota"; + $sql .= " and col_iota = x.col_iota"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; + $sql .= " and station_id in (" . $location_list . ")) + group by year + order by year"; - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } + if ($period == "year") { + $sql .= " ) y on year(thcv.col_time_on) = y.year"; + } else if ($period == "month") { + $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; + } - $sql .= " where station_id in (" . $location_list . ")"; + $sql .= " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= " group by year(thcv.col_time_on), y.tot + order by year(thcv.col_time_on)"; + } else if ($period == "month") { + $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot + order by date_format(col_time_on, '%Y-%m')"; + } - $query = $this->db->query($sql); + $query = $this->db->query($sql, $binding); - return $this->count_and_add_accumulated_total($query->result()); + return $this->count_and_add_accumulated_total($query->result()); } - function get_accumulated_waz($band, $mode, $period, $location_list) - { - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } + function get_accumulated_waz($band, $mode, $period, $location_list) { + $binding=[]; + if ($period == "year") { + $sql = "select year(thcv.col_time_on) year"; + } else if ($period == "month") { + $sql = "select date_format(col_time_on, '%Y-%m') year"; + } - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_cqz) as tot, year - from (select distinct "; + $sql .= ", coalesce(y.tot, 0) tot + from " . $this->config->item('table_name') . " thcv + left outer join ( + select count(col_cqz) as tot, year + from (select distinct "; - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= "year(col_time_on)"; + } else if ($period == "month") { + $sql .= "date_format(col_time_on, '%Y-%m')"; + } - $sql .= " year, col_cqz - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; + $sql .= " year, col_cqz + from " . $this->config->item('table_name') . + " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; + $sql .= " order by year + ) x + where not exists (select 1 from " . $this->config->item('table_name') . " where"; - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } + if ($period == "year") { + $sql .= " year(col_time_on) < year";; + } else if ($period == "month") { + $sql .= " date_format(col_time_on, '%Y-%m') < year";; + } - $sql .= " and col_cqz = x.col_cqz"; + $sql .= " and col_cqz = x.col_cqz"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; + $sql .= " and station_id in (" . $location_list . ")) + group by year + order by year"; - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } + if ($period == "year") { + $sql .= " ) y on year(thcv.col_time_on) = y.year"; + } else if ($period == "month") { + $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; + } - $sql .= " where station_id in (" . $location_list . ")"; + $sql .= " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql .= " group by year(thcv.col_time_on), y.tot + order by year(thcv.col_time_on)"; + } else if ($period == "month") { + $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot + order by date_format(col_time_on, '%Y-%m')"; + } - $query = $this->db->query($sql); + $query = $this->db->query($sql,$binding); - return $this->count_and_add_accumulated_total($query->result()); + return $this->count_and_add_accumulated_total($query->result()); } function get_accumulated_vucc($band, $mode, $period, $location_list) { @@ -577,8 +624,7 @@ class Accumulate_model extends CI_Model $sql = ""; if ($dbversion[0] >= "8") { - $sql = $this->fastquery($band, $mode, $period, $location_list); - $query = $this->db->query($sql); + $query = $this->fastquery($band, $mode, $period, $location_list); return $query->result(); } else { $sql = $this->slowquery($band, $mode, $period, $location_list); @@ -587,83 +633,94 @@ class Accumulate_model extends CI_Model } } - function fastquery($band, $mode, $period, $location_list) { - $sql = "WITH firstseen AS ( - SELECT substr(col_gridsquare,1,4) as grid, "; + function fastquery($band, $mode, $period, $location_list) { + $binding=[]; + $sql = "WITH firstseen AS ( + SELECT substr(col_gridsquare,1,4) as grid, "; - if ($period == "year") { - $sql .= "MIN(year(col_time_on)) year"; - } else if ($period == "month") { - $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; - } + if ($period == "year") { + $sql .= "MIN(year(col_time_on)) year"; + } else if ($period == "month") { + $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; + } - $sql .= " from " . $this->config->item('table_name') . " thcv - where coalesce(col_gridsquare, '') <> '' - and station_id in (" . $location_list . ")"; + $sql .= " from " . $this->config->item('table_name') . " thcv + where coalesce(col_gridsquare, '') <> '' + and station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " GROUP BY 1 - union all - select substr(grid, 1,4) as grid, year - from ( - select TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(COL_VUCC_GRIDS, ',', x.x), ',',-1)) as grid, "; - if ($period == "year") { - $sql .= "MIN(year(col_time_on)) year"; - } else if ($period == "month") { - $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; - } + $sql .= " GROUP BY 1 + union all + select substr(grid, 1,4) as grid, year + from ( + select TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(COL_VUCC_GRIDS, ',', x.x), ',',-1)) as grid, "; + if ($period == "year") { + $sql .= "MIN(year(col_time_on)) year"; + } else if ($period == "month") { + $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; + } - $sql .= " from " . $this->config->item('table_name') . " thcv - cross join ( - select 1 as x - union all - select 2 - union all - select 3 - union all - select 4) x - where - x.x <= length(COL_VUCC_GRIDS)-length(replace(COL_VUCC_GRIDS, ',', ''))+ 1 - and coalesce(COL_VUCC_GRIDS, '') <> '' - and station_id in (" . $location_list . ")"; + $sql .= " from " . $this->config->item('table_name') . " thcv + cross join ( + select 1 as x + union all + select 2 + union all + select 3 + union all + select 4) x + where + x.x <= length(COL_VUCC_GRIDS)-length(replace(COL_VUCC_GRIDS, ',', ''))+ 1 + and coalesce(COL_VUCC_GRIDS, '') <> '' + and station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode ='" . $band . "'"; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band ='" . $band . "'"; - } - } + if ($band != 'All') { + if ($band == 'SAT') { + $sql .= " and col_prop_mode = ?"; + $binding[] = $band; + } else { + $sql .= " and col_prop_mode !='SAT'"; + $sql .= " and col_band = ?"; + $binding[] = $band; + } + } - if ($mode != 'All') { - $sql .= " and (col_mode ='" . $mode . "' or col_submode ='" . $mode . "')"; - } + if ($mode != 'All') { + $sql .= " and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } - $sql .= " GROUP BY 1) as z + $sql .= " GROUP BY 1) as z ) , z as ( SELECT grid, row_number() OVER (partition by grid ORDER BY grid asc, year asc) as rn, year FROM firstseen ) select DISTINCT COUNT(grid) OVER (ORDER BY year) as total, year from z where rn = 1 - "; +"; - return $sql; - } + $query = $this->db->query($sql, $binding); + return $query; + } function slowquery($band, $mode, $period, $location_list) { + $binding=[]; $sql = ""; if ($period == "year") { $sql = "select year(thcv.col_time_on) year"; @@ -770,7 +827,8 @@ class Accumulate_model extends CI_Model order by date_format(col_time_on, '%Y-%m')"; } - return $sql; + $query = $this->db->query($sql, $binding); + return $query; } }