]> git.nbdom.net Git - nb.git/commitdiff
dbq2
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 14 Jun 2016 21:57:37 +0000 (22:57 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 14 Jun 2016 21:57:37 +0000 (22:57 +0100)
lib/js/nb.js
lib/php/db.php
lib/php/db/table.php

index 51a49bf264fdd9625a2bc4cc7f5c0302337327d8..3e175ce042a9763883f6afe559ee3803f0c45b40 100644 (file)
@@ -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 = '';
 
index d0588dfe7976192e41a007915e314e8784c0a0e8..b584459b6369f7c65d35f865c83bc6f406060364 100644 (file)
@@ -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";
index 8f40e40838236307b060fb661a448c95dba35a8e..6f6f336e36ba68078cc0702a6588c17981b4b232 100644 (file)
@@ -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('<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));