]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/types/sqlite.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 8 Jan 2018 00:25:46 +0000 (00:25 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 8 Jan 2018 00:25:46 +0000 (00:25 +0000)
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php
lib/php/db/types/sqlite.php
www/dbq/dbq.php
www/dbq/html/default.js

index 5d706db4ea7a198e5cfd303c082288ca1b022aaf..7d7003bba3498bc3da45414f1cc0fb1e1a67c6bf 100644 (file)
@@ -957,12 +957,13 @@ class Db extends nb {
 
   }
 
-  public function dump() {
+  public function dump($db_type='') {
     return $this->sql(true);
   }
 
-  public function sql($insert=null) {
+  public function sql($insert=null,$db_type='') {
     if ($insert === null) $insert = self::p('insert');
+    if (empty($db_type)) $db_type = self::p('db_type');
 
     # Params
     $this->pset('orderby',null);
@@ -1003,16 +1004,19 @@ class Db extends nb {
     echo ''
       #."-- Database : ".$this->name."\n"
       ."-- Date     : ".strftime('%F %T')."\n"
-      ."-- Pdo      : ".$this->pdo_info()."\n"
+# NB 07.01.18       ."-- Pdo      : ".$this->pdo_info()."\n"
     ;
 
     # Change db type if needed
     $type_from = $type_to = '';
-    if (self::p('db_type')) {
-      echo "-- Type     : ".self::p('db_type')."\n";
+    if ($db_type) {
+      echo "-- Type orig: ".$this->type."\n";
+      echo "-- Type     : ".$db_type."\n";
       $type_from = $this->type;
-      $type_to = self::p('db_type');
+      $type_to = $db_type;
       $this->type = $type_to;
+               } else {
+      echo "-- Type     : ".$this->type."\n";
     }
 
     # Specific function
@@ -1036,6 +1040,7 @@ class Db extends nb {
 
       #bye($this->db()->type);
     if ($type_from == 'mysql') $views = []; # Mysql store sql create view in mysql format wich only works with mysql
+    echo "\n-- SQL\n";
     foreach (array_merge($tables,$views) as $t) {
 
       # DROP / CREATE
index 0ea5d10cfbcb48cef354b6e9d6126b76cf85ce51..4dfa67a9c743ec99c1ed35a6a7bf1a9f13bf94ce 100644 (file)
@@ -38,14 +38,19 @@ class field extends nb {
   public function __construct($attr=[]) {
     if (is_scalar($attr)) $attr = ['name'=>$attr];
     foreach ($attr as $k => $v) { $this->$k = $v; }
+  }
+
+  public static function default2str($default) {
     if ($this->default == "''") $this->default = '';
-# NB 12.12.17     if (strtoupper($this->default) == "NULL") $this->default = null;
-    #if (strtoupper($this->default) == "NOW()") $this->default = null;
-    if (strtoupper($this->default) == "NOW()") $this->default = $this->date_microtime();
-    if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = $this->date_microtime();
-    if (strtoupper($this->default) == "NOW()") $this->default = date("Y-m-d H:i:s.u");
-    if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = date("Y-m-d H:i:s.u");
-    if (strtoupper($this->default) == "CURRENT_DATE)") $this->default = date("Y-m-d");
+# NB 12.12.17     if (strtoupper($default) == "NULL") $default = null;
+    #if (strtoupper($default) == "NOW()") $default = null;
+# NB 08.01.18     if (strtoupper($default) == "NOW()") $default = $date_microtime();
+# NB 08.01.18     if (strtoupper($default) == "CURRENT_TIMESTAMP") $default = $date_microtime();
+    if (strtoupper($default) == "NOW()") $default = date("Y-m-d H:i:s.u");
+    if (strtoupper($default) == "CURRENT_TIMESTAMP") $default = date("Y-m-d H:i:s.u");
+    if (strtoupper($default) == "CURRENT_DATE") $default = date("Y-m-d");
+               if (preg_match('/^(null|.*\(.*)?$/',strtolower($default))) $default = '';
+               return $default;
   }
 
   public function autoincrement() {
index 467d45f92f3db30bb8f85972898647b9d96afe51..e42903f1dc061faa600e204f3d2e26139d53431a 100644 (file)
@@ -217,17 +217,17 @@ Class Table extends nb {
    * return the sql to create the table build internaly
    *
    */
-  public function create($from_sql=true) {
+  public function create($from_engine=true) {
 
     // String replace function
-    $sql_replace_fct = $this->db()->conf_type('table.sql.create',false);
+    $sql_replace_fct = $this->db()->conf_type('table.sql.create');
     $db_sql_replace_fct = empty($this->db()->sql_replace) ? '' : $this->db()->sql_replace;
     $sql_replace = function($sql) use ($sql_replace_fct,$db_sql_replace_fct) {
       if ($db_sql_replace_fct) $sql = $db_sql_replace_fct($sql);
       return $sql_replace_fct ? $sql_replace_fct($sql,$this) : $sql;
     };
 
-    if ($from_sql) return $sql_replace($this->sql());
+    if ($from_engine) return $sql_replace($this->sql());
 
     if ($this->type() == 'view') {
       return $sql_replace('CREATE VIEW '.$this->sql_name().' AS '.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','SELECT',$this->sql()));
@@ -595,8 +595,10 @@ Class Table extends nb {
 
       foreach ($this->fields() as $name => $field) {
 
-        if ($add and !preg_match('/^(null)?$/',strtolower($field->default))) {
-          $row[$name] = $field->default;
+# NB 08.01.18         if ($add and !preg_match('/^(null|.*\(.*)?$/',strtolower($field->default))) {
+# NB 08.01.18           $row[$name] = $field->default;
+                               if ($add) {
+          $row[$name] = $field->default2str($field->default);
 
         } elseif(!isset($row[$name])) {
           $row[$name] = '';
@@ -2056,6 +2058,8 @@ Class Table extends nb {
     if (!empty($opt['buttons'])) $buttons = $opt['buttons'];
 
     $r = '<form class="menu" method="get" action="?">'.NB_EOL;
+    #$r = '<form class="menu" method="get" action="'.preg_replace('/\?.*$/','',$_SERVER["REQUEST_URI"]).'">'.NB_EOL;
+
     # See: http://html5doctor.com/html5-forms-input-types/
     #$r .= '<input id="skill" type="range" min="1" max="100" value="0" />';
     #$r .= '<input id="startdate" name="startdate" min="2012-01-01" max="2013-01-01" type="date" />';
index a47c31d52615f6a8271bef5bd87f3dd8ba35a658..f98d3c7481fad6ac1c89414ffaaed317e0c1d590 100644 (file)
@@ -128,6 +128,7 @@ $DB_TYPES['sqlite'] = array (
   $sql = str_replace('\\n',"\n",$sql);
   return $sql;
 },
+
 'connect' => function($Db) {
 
   if (!empty($Db->attach)) {
@@ -146,9 +147,21 @@ $DB_TYPES['sqlite'] = array (
 
   }
 },
+
 'disconnect' => function($Db) {
   foreach ($Db->attach as $name => $file) {
     $Db->conn->exec("DETACH DATABASE $name");
   }
 },
+
+'table.sql.create' => function($sql) {
+  $r = [
+    '/::\w+(\b|\s)/' => '$1', # pgsql cast;
+    '/"([\w_]+)"\(/' => '$1(', # pgsql function quoted;
+    "/'([\w_]+\(.*?\))'/" => '$1', # pgsql function quoted;
+    "/now\(\)/i" => 'CURRENT_TIMESTAMP', # pgsql function quoted;
+       ];
+  $sql= preg_replace(array_keys($r),array_values($r),$sql);
+  return $sql;
+},
 );?>
index 05f0210f5d008c946adc9dee3f177d9a630f1e9c..22ddf698562a4a83ef87788dc049226da19f4837 100644 (file)
@@ -427,7 +427,7 @@ class DbQ extends nb {
 
                $this->table->html_edit($values,
 # NB 14.12.17                  $this->table->base . '/' . ($add ? 'insert' : 'update' . '/' . urlencode($this->params['args'])) . '/'
-                       $this->table->base . '/' . ($add ? 'insert' : 'update') . '/'
+                       $this->table->base . '/' . ($add ? 'insert' : 'update') # NB 07.01.18 . '/'
                ,$add);
        }
 
@@ -1166,21 +1166,19 @@ EOF;
                } elseif ($action == 'databases') {
                        $this->page($this->db,'databases');
 
-               } elseif ($action == 'dump') {
+               } elseif ($action == 'csv') {
                        header('Content-type: text/plain');
-                       $this->db->dump();
+                       $this->db->dump2csv();
                        exit;
-                       #echo serialize($this->db->tables());
-                       $this->page(array_values($this->db()->conf));
 
-               } elseif ($action == 'csv') {
+               } elseif ($action == 'dump') {
                        header('Content-type: text/plain');
-                       $this->db->dump2csv();
+                       $this->db->dump($this->params['action']);
                        exit;
 
                } elseif ($action == 'schema') {
                        header('Content-type: text/plain');
-                       $this->db->sql();
+                       $this->db->sql(null,$this->params['action']);
                        exit;
 
                }
index bf1761ba64f9c34b0d6ad884da39e7d3eebb4989..65c357a7861cc11eac3483f6819e793c1415366b 100644 (file)
@@ -74,7 +74,9 @@ document.addEventListener("DOMContentLoaded", function() {
     form.querySelectorAll("form.menu select.tables").forEach(function(e) {
       e.removeAttribute('name');
       e.onchange = function() {
-        this.form.setAttribute('action',window._dbq['db.base']+'/'+this.value+'.html');
+        this.form.setAttribute('action',window._dbq['db.base']+'/'+this.value
+          + ( window._dbq['param.format'] ? '.'+window._dbq['param.format'] : '')
+        );
         return this.form.onsubmit();
       }
     });