$this->tables = array();
#debug((array)$this->conn->query("SELECT name,type FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name",PDO::FETCH_ASSOC));
-#debug($this->type('tables',true));
- foreach ($this->conn->query($this->type('tables',true),PDO::FETCH_ASSOC) as $row) {
+#debug($this->type('tables',true,$this->type));
+ foreach ($this->conn->query($this->type('tables',true,$this->type),PDO::FETCH_ASSOC) as $row) {
$name = current($row);
$this->tables[$name] = $this->table($name,$row);
#bye($this->tables);
public function sql($insert=null) {
if ($insert === null) $insert = self::p('insert');
$this->pset('orderby',null);
+ $this->pset('extras','0');
$this->pset('format','sql');
# Tables param filter
}
#if (NB_EOL == "\n")
- # Change db type
+ # Cache before changing db type
foreach ($tables as $t) {
- unset($t->orderby); # uggly need to be change - NB 14.06.16
+ unset($t->orderby);
$t->fields();
- }
-
- if (self::p('db.type')) {
- $this->type = self::p('db.type');
+ $t->sql();
}
echo ''
#."-- Host : ".$this->host."\n"
;
+ if (self::p('db.type')) {
+ $this->type = self::p('db.type');
+ echo "-- Type : ".self::p('db.type')."\n";
+ }
+
if ($sql = $this->type('exec')) {
foreach ((is_array($sql) ? $sql : array($sql)) as $sql) {
echo rtrim($sql,';').";\n";
*/
public function create() {
+ if (!self::p('db.type')) return $this->sql();
// Type specific
global $_create_fct, $_create_fct_type;
- $_create_fct_type = $this->db()->type('create',false,self::p('db.type'));
+ $_create_fct_type = $this->db()->type('create',false);
+#die('enum!'.$this->db()->type.' '.(string)$this->db()->type('create',false));
$_create_fct = function(&$field) {
global $_create_fct_type; if ($_create_fct_type and ($sql=$_create_fct_type($field))) return $sql;
;
};
- $sql = 'CREATE TABLE '.$this->sql_name()
+ if ($this->type == 'view') {
+ return 'CREATE VIEW '.$this->sql_name().' AS SELECT'.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','',$this->sql());
+ $sql = $this->sql();
+ if ( strpos('CREATE ',$sql) !==0 ) $sql = 'CREATE VIEW '.$this->sql_name().' AS '.$sql;
+ return 'CREATE VIEW '.$this->sql_name().' AS '.$this->sql();
+ }
+
+ $sql = 'CREATE '.strtoupper($this->type).' '.$this->sql_name()
.' ('
.join(",",array_map(function($f){global $_create_fct;return $_create_fct($f);},array_values($this->fields())))
# done at the end for primary keys .')'
}
$sql = str_replace('<NAME>',$this->name,$this->db()->type('table.sql',true));
- $this->sql = $this->db()->row($sql);
# Noise before CREATE like MySql
- $this->sql = preg_replace("/^\w+\s+(CREATE)/i",'$1',$this->sql,1);
+ $this->sql = explode('\0',$this->db()->row($sql,'\0'));
+ if (count($this->sql) > 1) {
+ $this->sql = $this->sql[1];
+ } else {
+ $this->sql = $this->sql[0];
+ }
# Remove comments
$this->sql = join(' ',preg_grep('/^ *--/',explode("\n",$this->sql),true));