From ce11b79a0cf69e81a307eeb5dc30c46b1cd6cb8d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 27 Jul 2016 17:07:20 +0100 Subject: [PATCH] virtual fix --- lib/php/db/table.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 5800d29d..2164017e 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -56,10 +56,9 @@ Class Table extends nb { // Extras - We dont want to affect $this // Type - $sql = ''; if (isset($opt['sql'])) { - $this->sql = $opt['sql']; unset($opt['sql']); $this->type = 'sql'; unset($opt['type']); + $this->sql = $opt['sql']; unset($opt['sql']); } // Extras @@ -84,22 +83,36 @@ Class Table extends nb { // Add others foreach ($opt as $k => $v) { $this->$k = $v; } + } + + /* + * Function create_temporary + * + * Create temporary if needed + * + */ + public function create_temporary() { + if (!empty($this->_create_temporary)) return; + $this->_create_temporary = 1; + // Name, could be a select if (DB_TABLE_QUERY_NAME and stripos($this->name,'SELECT ')===0) { - $GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID']; - $this->db()->conn->query("CREATE TEMPORARY TABLE $name AS $this->name"); + #$GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID']; + $this->db()->conn->query("CREATE TEMPORARY TABLE ".DB_TABLE_QUERY_NAME." AS $this->name"); $this->name = $name; // Virtual Table } elseif (DB_TABLE_QUERY_NAME and !empty($this->sql)) { - $name = $this->name; - $this->db()->conn->query("CREATE TEMPORARY TABLE $name AS $this->sql"); + $this->db()->conn->query("CREATE TEMPORARY TABLE $this->name AS $this->sql"); } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) { bye("Query not Allowed !"); + } else { + return false; } + return true; } /* @@ -250,8 +263,8 @@ Class Table extends nb { */ public function fields($name=null) { - #if (!empty($this->sql)) return []; # Virtual - NB 04.07.16 - #if ($this->type == 'sql') return []; # Virtual - NB 04.07.16 + $this->create_temporary(); + if (!isset($this->fields)) { $this->fields = array(); @@ -654,6 +667,7 @@ Class Table extends nb { Html Output ******************************************************************/ public function rows_sql(&$opt=array()) { + if (isset($this->orderby)) self::pdef('orderby',$this->orderby); if (self::p('order')) self::pset('orderby',self::p('orderby').' '.self::p('order')); # from Wordpress if (self::p('paged')) { @@ -707,6 +721,8 @@ Class Table extends nb { public function rows(&$opt=array(),$opt_by_val=null) { + $this->create_temporary(); + if (empty($this->type) or $this->type != 'table') $this->pset('buttons','0');; if ($opt_by_val !== null) $opt = $opt_by_val; -- 2.47.3