From 13fce7426e26cca92d53bac200850544908c7179 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 4 Jul 2016 23:43:06 +0100 Subject: [PATCH] fix bug --- lib/php/db.php | 7 ++----- lib/php/db/table.php | 17 ++++++++++++++--- lib/php/db/types/mysql.php | 23 ++++++++++++----------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 1141d746..5379130d 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -81,12 +81,9 @@ class Db extends nb { # Virtual - NB 04.07.16 foreach ($tables as $name => $t) { if (empty($t['sql'])) continue; - $sql = $t['sql']; - unset($t['sql']); - #debug($name); + #$sql = $t['sql']; unset($t['sql']); $t = $this->table($name,$t); - $t->sql = $sql; - $t->type = 'sql'; + #$t->sql = $sql; $this->tables[] = $t; } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 31e2fbc0..a44e5279 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -54,6 +54,12 @@ Class Table extends nb { // Extras - We dont want to affect $this + // Type + if (isset($opt['sql'])) { + unset($opt['type']); + $this->type = 'sql'; + } + // Extras if (isset($opt['extras'])) { $this->add_extras($opt['extras']); @@ -179,10 +185,14 @@ Class Table extends nb { */ public function indexes() { if (!isset($this->indexes)) { - $sql = str_replace('',$this->name,$this->db()->type('table.sql.index')); - if (is_array($sql)) list($sql,$fct) = count($sql)==1 ? [$sql[0],null] : $sql; + $sql = $this->db()->type('table.sql.index'); + $fct = ''; + + if (is_array($sql)) list($sql,$fct) = (count($sql)==1 ? [$sql[0],null] : $sql); if (!$sql) return []; + $sql = str_replace('',$this->name,$sql); + $sql = str_replace('',$this->db()->name,$sql); $st = $this->db()->conn->prepare($sql); $st->execute(); $this->indexes = []; @@ -244,7 +254,8 @@ Class Table extends nb { */ public function fields($name=null) { - if (!empty($this->sql)) return []; # Virtual - NB 04.07.16 + #if (!empty($this->sql)) return []; # Virtual - NB 04.07.16 + if ($this->type == 'sql') return []; # Virtual - NB 04.07.16 if (!isset($this->fields)) { $this->fields = array(); diff --git a/lib/php/db/types/mysql.php b/lib/php/db/types/mysql.php index e155064e..dece702a 100644 --- a/lib/php/db/types/mysql.php +++ b/lib/php/db/types/mysql.php @@ -25,16 +25,17 @@ $DB_TYPES['mysql'] = array ( 'localFile' => array (getenv('HOME').'/.my.cnf','^(?:user(?:name)?=(?P\\S+)|password=(?P\\S+))'), 'table.sql' => 'SHOW CREATE TABLE ``', -'table.sql.index' => [ - 'SHOW INDEX FROM ``', function(&$r) { - if ($r['Key_name'] != 'PRIMARY') return [ - 'id' => $r['Key_name'], - 'field' => $r['Column_name'], - 'uniq' => ($r['Non_unique'] ? 0 : 1), - 'type' => $r['Index_type'] - ]; - }, -], +'table.sql.index' => "SELECT ".(Db::p('db.type') ? "CONCAT(s.TABLE_NAME,'_',s.INDEX_NAME,'_idx')" : 's.INDEX_NAME')." as name,(CASE NON_UNIQUE WHEN 1 THEN 0 ELSE 1 END) as uniqe,GROUP_CONCAT(COLUMN_NAME) as field FROM INFORMATION_SCHEMA.STATISTICS s LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t ON t.TABLE_SCHEMA=s.TABLE_SCHEMA AND t.TABLE_NAME=s.TABLE_NAME AND s.INDEX_NAME=t.CONSTRAINT_NAME WHERE 0=0 AND t.CONSTRAINT_NAME IS NULL AND s.TABLE_SCHEMA = '' AND s.TABLE_NAME='' GROUP BY name ORDER BY SEQ_IN_INDEX", +# NB 04.07.16 '_table.sql.index' => [ +# NB 04.07.16 'SHOW INDEX FROM ``', function(&$r) { +# NB 04.07.16 if ($r['Key_name'] != 'PRIMARY') return [ +# NB 04.07.16 'name' => $r['Key_name'], +# NB 04.07.16 'field' => $r['Column_name'], +# NB 04.07.16 'uniq' => ($r['Non_unique'] ? 0 : 1), +# NB 04.07.16 'type' => $r['Index_type'] +# NB 04.07.16 ]; +# NB 04.07.16 }, +# NB 04.07.16 ], 'tables' => 'SELECT TABLE_NAME as name,LOWER(IF(TABLE_TYPE=\'BASE TABLE\',\'TABLE\',TABLE_TYPE)) as type,ENGINE as engine,CREATE_TIME as created FROM information_schema.tables WHERE TABLE_SCHEMA=DATABASE()', @@ -99,7 +100,7 @@ $DB_TYPES['mysql'] = array ( 'sql.replace' => function($sql) { $sql = preg_replace('/ESCAPE \'.*?\'/','',$sql); - $sql = preg_replace('/CHARSET=latin1\s*$/','CHARSET='.str_replace('-','',Db::$encoding),$sql); + $sql = preg_replace('/CHARSET=\S+\s*$/','CHARSET='.str_replace('-','',Db::$encoding),$sql); return $sql; }, -- 2.47.3