From: Nicolas Boisselier Date: Tue, 27 Oct 2015 17:03:04 +0000 (+0100) Subject: SQL_CALC_FOUND_ROWS X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=83156fc2966b995e4e7aaea1290e45474875b83e;p=nb.git SQL_CALC_FOUND_ROWS --- diff --git a/lib/css/db.css b/lib/css/db.css index 5fdf4314..99ac90ef 100644 --- a/lib/css/db.css +++ b/lib/css/db.css @@ -52,6 +52,10 @@ div.rows .row { text-align: left; } text-align: center; } +table.rows tr.row { + vertical-align: top; +} + .db div.rows .row *:not([class~=buttons]):hover { opacity: 0.7; } diff --git a/lib/php/db.php b/lib/php/db.php index fefcf381..4d36eb96 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -8,6 +8,7 @@ *****************************************************************************/ require_once(dirname(__FILE__).'/nb.php'); require_once(dirname(__FILE__).'/db/table.php'); +define('SQL_NAME_FORCE_QUOTE_EXP','^(range)$'); if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS','' #.'' #.'' @@ -336,7 +337,12 @@ EOF; } function sql_name($value) { - if (preg_match('/^\w+$/',$value)) return $value; + if (preg_match('/^\w+$/',$value) and + ( + !SQL_NAME_FORCE_QUOTE_EXP or + !preg_match('/'.SQL_NAME_FORCE_QUOTE_EXP.'/',$value) + ) + ) return $value; if ($this->type == 'mysql') return '`'.$value.'`'; return '"'.$value.'"'; } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 92398ea8..686f6a0b 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -566,24 +566,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. // // Select // - $sql = "SELECT *" . $this->select_extras(); + $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; $this->debug($sql,1); - // - // Tot - // - $query = $this->db->conn->query("SELECT count(*) FROM ($sql) count",PDO::FETCH_COLUMN,0); - if (!$query) { - $err = $this->db->conn->errorInfo(); - $err[] = $sql; - err(join(' | ',$err)); - return $err[0]; - } - $tot = $query->fetch(); - #if (!$tot) return; - if ($this->p('sort')) $sql .= ' ORDER BY '.$this->p('sort'); if ($this->p('limit')) { @@ -620,8 +607,26 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - $opt['count'] = $count; + // + // Tot + // + if ($this->db->type =='mysql') { + $query = $this->db->conn->query('SELECT FOUND_ROWS()'); + } else { + $query = $this->db->conn->query("SELECT count(*)".preg_replace('/^SELECT .*?FROM/',' FROM',$sql)); + #$query = $this->db->conn->query("SELECT count(*) FROM ($sql) count"); + } + if (!$query) { + $err = $this->db->conn->errorInfo(); + $err[] = $sql; + err(join(' | ',$err)); + return $err[0]; + } + $tot = $query->fetch(PDO::FETCH_COLUMN); $opt['tot'] = $tot; + #if (!$tot) return; + + $opt['count'] = $count; $opt['limit'] = $limit; echo $this->{"rows_end_$format"}($opt); if ($opt['is_html']) echo '