]> git.nbdom.net Git - nb.git/commitdiff
fix bug
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 4 Jul 2016 22:43:06 +0000 (23:43 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 4 Jul 2016 22:43:06 +0000 (23:43 +0100)
lib/php/db.php
lib/php/db/table.php
lib/php/db/types/mysql.php

index 1141d74684d69338b807b5ee50571e04c0a85fbb..5379130d764dee28dc4c2a187294bd8a261d2f21 100644 (file)
@@ -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;
       }
 
index 31e2fbc043dba75abb12cea24f01732f60468b2e..a44e5279f99fd3de2864e9093d052d00f9d18cb5 100644 (file)
@@ -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('<NAME>',$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('<NAME>',$this->name,$sql);
+      $sql = str_replace('<DB>',$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();
index e155064e921b8f637a8d792cac7f042cd4c5fce4..dece702a5eab47099718cda01e33b92f48ace31e 100644 (file)
@@ -25,16 +25,17 @@ $DB_TYPES['mysql'] = array (
 'localFile' => array (getenv('HOME').'/.my.cnf','^(?:user(?:name)?=(?P<user>\\S+)|password=(?P<password>\\S+))'),
 
 'table.sql' => 'SHOW CREATE TABLE `<NAME>`',
-'table.sql.index' => [
-  'SHOW INDEX FROM `<NAME>`', 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 = '<DB>' AND s.TABLE_NAME='<NAME>' GROUP BY name ORDER BY SEQ_IN_INDEX",
+# NB 04.07.16 '_table.sql.index' => [
+# NB 04.07.16   'SHOW INDEX FROM `<NAME>`', 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;
 },