]> git.nbdom.net Git - nb.git/commitdiff
fix bug db.type when dump
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 22 Aug 2016 21:42:41 +0000 (22:42 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 22 Aug 2016 21:42:41 +0000 (22:42 +0100)
lib/php/db.php
lib/php/db/table.php

index 56c0c0b80d476c09e298879e799227c0859e532b..e2cdf4460daa98459a4b886325a4fee3dfa50da0 100644 (file)
@@ -675,45 +675,6 @@ class Db extends nb {
       $t->sql();
     }
 
-    # Table first
-    if (0) uasort($tables,function($a,$b) use($views){
-      #if ($a->type == 'table' and $b->type == 'table') return strcmp($a->name,$b->name);
-
-      $embed = function($t) use($views) {
-        return preg_match('/ FROM .*\b'.join('|',$views).'\b/',$t->sql()) ? 1 : 0;
-      };
-
-      if ($a->type == 'table' and $b->type !='table') return -1;
-      if ($a->type != 'table' and $b->type =='table') return +1;
-      if ($a->type == 'table' and $b->type =='table') return strcmp($a->name,$b->name);
-
-      if (!$embed($a) and $embed($b)) return -1;
-      if ($embed($a) and !$embed($b)) return +1;
-      $diff = strlen($a->name) - strlen($b->name);
-      if (!$diff) return strcmp($a->name,$b->name);
-echo(">".$a->name."\n");
-      return ($diff>0 ? +1 : -1);
-return;
-      if ($a->type != 'table') return ($embed($a) ? -1 : +1);
-      if ($b->type != 'table') return ($embed($b) ? -1 : +1);
-      #if ($a->type == 'table') return +1;
-      #if ($b->type == 'table') return -1;
-
-      #if ($embed($a)) return +1;
-      #if ($embed($b)) return -1;
-      return (strlen($b->name) < strlen($a->name)) ? 1 : -1;
-      return 0;
-      return strcmp($a->name,$b->name);
-      $names = strcmp($a->name,$b->name);
-      $len = (strlen($b->name) < strlen($a->name));
-      return (
-        # Tables first
-        ( ($b->type == 'table' ? 0 : 1) < ($a->type == 'table' ? 0 : 1) )
-        # Short name first for embed views
-        #+ (strlen($b->name) < strlen($a->name))
-      );
-    });
-
     # HEADER
     echo ''
       #."-- Database : ".$this->name."\n"
@@ -723,15 +684,20 @@ return;
     ;
 
     # Change db type if needed
+    $type_from = $type_to = '';
     if (self::p('db.type')) {
       echo "-- Type     : ".self::p('db.type')."\n";
+      $type_from = $this->type;
+      $type_to = self::p('db.type');
+      #$this->type_out = self::p('db.type');
       $this->type = self::p('db.type');
+      #$this->type_out = self::p('db.type');
     }
 
     # Contruct DROP / CREATE
     foreach ($tables as $t) {
 
-      $sql = rtrim($t->create(),';');
+      $sql = rtrim($t->create(true),';');
       $sql = str_replace(';CREATE',";\nCREATE",$sql);
 
       if ($drop = $this->type('sql.drop')) {
@@ -765,7 +731,11 @@ return;
 
       echo $t->sql_create;
 
-      if ($insert and $t->type == 'table') $t->rows();
+      if ($insert and $t->type == 'table') {
+        if ($type_from) $this->type = $type_from;
+        $t->rows();
+        if ($type_to) $this->type = $type_to;
+      }
     }
 
     return true;
index f38a8c83a29e2d2e3b3443180de1cee20d69bef6..788cc70485ec6bceb75e4605cb81b59b336e3084 100644 (file)
@@ -133,7 +133,7 @@ Class Table extends nb {
    * return the sql to create the table build internaly
    *
    */
-  public function create() {
+  public function create($from_sql=false) {
 
     // String replace function
     $sql_replace_fct = $this->db()->type('sql.replace',false);
@@ -143,7 +143,7 @@ Class Table extends nb {
       return $sql_replace_fct ? $sql_replace_fct($sql,$this) : $sql;
     };
 
-    if (!self::p('db.type')) return $sql_replace($this->sql());
+    if ($from_sql) return $sql_replace($this->sql());
 
     if ($this->type == 'view') {
       return $sql_replace('CREATE VIEW '.$this->sql_name().' AS SELECT'.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','',$this->sql()));