From c7c9045fda7bfb404378a97bbec402149424a1de Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 6 Jan 2016 02:32:47 +0000 Subject: [PATCH] Delete SQL_CALC_FOUND_ROWS --- lib/php/db/table.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/php/db/table.php b/lib/php/db/table.php index af388ecf..c0113f2d 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -17,6 +17,7 @@ if (false and empty($_SERVER['DOCUMENT_ROOT'])) { if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit'); if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete'); if (!defined('DB_HTML_NAV_TOP')) define('DB_HTML_NAV_TOP',''.PHP_EOL); +if (!defined('DB_TABLE_QUERY_NAME')) define('DB_TABLE_QUERY_NAME','_query_'); class table extends nb { @@ -44,7 +45,7 @@ class table extends nb { // Name, could be a select if (stripos($name,'SELECT ')===0) { #$temp = '_'.substr(md5($name),0,6); - $temp = '_query_'; + $temp = DB_TABLE_QUERY_NAME; $this->db->conn->query("CREATE TEMPORARY TABLE $temp AS $name"); $name = $temp; } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$name)) { @@ -579,12 +580,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. ******************************************************************/ function rows($opt=array()) { + $select_count = ( (false and $this->db->type =='mysql') ? " SQL_CALC_FOUND_ROWS" : ""); // // Select // - $sql = "SELECT".($this->db->type =='mysql' ? " SQL_CALC_FOUND_ROWS" : "")." *" . $this->select_extras(); - $sql .= " FROM ".$this->sql_name().$this->where_criterias($this->p(),$this->p('op')); - $this->sql = $sql; + $sql = "SELECT$select_count *" . $this->select_extras(); + $sql .= " FROM ".$this->sql_name(); + $where = $this->where_criterias($this->p(),$this->p('op')); + $this->sql = $sql = $sql . $where; $this->debug($sql,1); if ($this->p('sort')) $sql .= ' ORDER BY '.$this->p('sort'); @@ -626,13 +629,18 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. // // Tot // - if ($this->db->type =='mysql') { + if ($select_count) { $query = $this->db->conn->query('SELECT FOUND_ROWS()'); + +# NB 06.01.16 } elseif ($where) { +# NB 06.01.16 $sql_count = $sql; +# NB 06.01.16 $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count); +# NB 06.01.16 $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count); +# NB 06.01.16 $query = $this->db->conn->query($sql_count); + } else { - $sql_count = $sql; - $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count); - $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM (',$sql_count).') count'; - $query = $this->db->conn->query($sql_count); + $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name()); + } if (!$query) { -- 2.47.3