From: Nicolas Boisselier Date: Mon, 22 Aug 2016 21:42:41 +0000 (+0100) Subject: fix bug db.type when dump X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=7f4a041ab7f7c2c556982bf5fcb856992184829b;p=nb.git fix bug db.type when dump --- diff --git a/lib/php/db.php b/lib/php/db.php index 56c0c0b8..e2cdf446 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index f38a8c83..788cc704 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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()));