From: Nicolas Boisselier Date: Tue, 14 Jun 2016 21:57:37 +0000 (+0100) Subject: dbq2 X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=68378c774fc97ec819eaed72dd2f00f808f21bd0;p=nb.git dbq2 --- diff --git a/lib/js/nb.js b/lib/js/nb.js index 51a49bf2..3e175ce0 100644 --- a/lib/js/nb.js +++ b/lib/js/nb.js @@ -220,6 +220,8 @@ function NB(args) { var i = 0; var url = ''; var action = f.getAttribute('action'); + var method = f.getAttribute('method'); + if (method != 'get') return true; //if (typeof(action) == 'undefined') action = ''; diff --git a/lib/php/db.php b/lib/php/db.php index d0588dfe..b584459b 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -329,8 +329,8 @@ class Db extends nb { $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); @@ -582,6 +582,7 @@ class Db extends nb { 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 @@ -595,14 +596,11 @@ class Db extends nb { } #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 '' @@ -612,6 +610,11 @@ class Db extends nb { #."-- 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"; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 8f40e408..6f6f336e 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -119,9 +119,11 @@ Class Table extends nb { */ 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; @@ -134,7 +136,14 @@ Class Table extends nb { ; }; - $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 .')' @@ -166,10 +175,14 @@ Class Table extends nb { } $sql = str_replace('',$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));