# Table first
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;
};
+ $cmp = function($t) use($views,$embed) {
+ #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);
+ 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 (DB_TABLE_QUERY_NAME and stripos($this->name,'SELECT ')===0) {
#$GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID'];
$this->db()->conn->query("CREATE TEMPORARY TABLE ".DB_TABLE_QUERY_NAME." AS $this->name");
- $this->name = $name;
+ $this->name = DB_TABLE_QUERY_NAME;
// Virtual Table
} elseif (DB_TABLE_QUERY_NAME and !empty($this->sql)) {