$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"
;
# 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')) {
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;
* 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);
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()));