]> git.nbdom.net Git - nb.git/commitdiff
first attent to fix db.dump
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 30 Sep 2016 14:37:51 +0000 (16:37 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 30 Sep 2016 14:37:51 +0000 (16:37 +0200)
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php
lib/php/db/types/sqlite.php

index 00fde73f61c8fc22eb47f0d568560c5b0fb0cd7f..6aaa3406706bf04d18e79a2ce8fc27d792e47b7d 100644 (file)
@@ -816,6 +816,14 @@ class Db extends nb {
       $this->type = $type_to;
     }
 
+    # Specific function
+    $row_opt = [
+      'parser' => false,
+    ];
+    if ($fct = $this->conf_type('table.sql.create')) {
+      $row_opt['fct'] = $fct;
+    }
+
     # SQL_PRE
     $i = 0; foreach ($this->sql_pre() as $s) {
       if (($i++) == 0) echo "\n-- SQL_PRE\n";
@@ -823,10 +831,11 @@ class Db extends nb {
     }
 
       #bye($this->db()->type);
-    $fct = $this->conf_type('table.sql.create',false);
-    if (!$fct) $fct = false;
     foreach ($tables as $t) {
 
+      #if ($fct) $t->row_parse_post = $fct;
+      #$t->row_parse_pre = null;
+
       # DROP / CREATE
       $create = rtrim($t->create(false),';');
       $create = str_replace(';CREATE',";\nCREATE",$create);
@@ -855,7 +864,7 @@ class Db extends nb {
       # INSERT
       if ($insert and $t->type == 'table') {
         if ($type_from) $this->type = $type_from;
-        $t->rows();
+        $t->rows($row_opt);
         if ($type_to) $this->type = $type_to;
       }
 
index c90c542ac1b96018d8f14de4501f0276979b6792..35cc575c3ef19bd13ce43d3cb8eb009a1c064e26 100644 (file)
@@ -23,7 +23,7 @@ class field extends nb {
   public $extra;
   public $extras;
 
-  public function __sleep() { return array(
+  public function __sleep() { return [
     'name',
     'type',
     'null',
@@ -32,10 +32,10 @@ class field extends nb {
     'default',
     'autoincrement',
     'extra',
-  ); }
+  ]; }
 
-  public function __construct($attr=array()) {
-    if (is_scalar($attr)) $attr = array('name'=>$attr);
+  public function __construct($attr=[]) {
+    if (is_scalar($attr)) $attr = ['name'=>$attr];
     foreach ($attr as $k => $v) { $this->$k = $v; }
     if ($this->default == "''") $this->default = '';
   }
@@ -85,6 +85,7 @@ class field extends nb {
   }
 
   public function db() {
+    if (!empty($this->table) and !empty($this->table->db)) return $this->table->db();
     global $Db;
     return ( isset($Db) ? $Db : null );
   }
index 9f85f3b26a20329f1dd7c2545715addb9f2ffee5..3422422ce6e395592b9af0000db0e69879a3bdd8 100644 (file)
@@ -296,6 +296,7 @@ Class Table extends nb {
         if (isset($conf['fct'])) $conf['fct']($row) ;
 
         $field = array(
+          'table'         => $this,
           'name'          => $row['name'],
           'type'          => strtolower($row['type']),
           'key'           => (preg_match('/^(f.*|no|0)?\s*$/i',$row['key']) ? 0 : 1),
@@ -843,7 +844,7 @@ Class Table extends nb {
           out::head($out_conf,$this->fields(),[$row]);
 
         } else {
-          echo $this->{"rows_begin_$format"}($this->fields());
+          echo $this->{"rows_begin_$format"}($this->fields(),$opt);
         }
 
       }
@@ -861,7 +862,7 @@ Class Table extends nb {
       if ($out_conf) {
         out::row($out_conf,$row);
       } else {
-        echo $this->{"rows_rec_$format"}($row);
+        echo $this->{"rows_rec_$format"}($row,$opt);
       }
 
     } # < rows
@@ -974,7 +975,7 @@ Class Table extends nb {
     return "\n-- ".$this->name."\n";
   }
 
-  public function rows_rec_sql(&$row) {
+  public function rows_rec_sql(&$row,&$opt) {
     $keys = $values = array();
 
     foreach ($row as $k=>$v) {
@@ -988,6 +989,9 @@ Class Table extends nb {
     $sql = "INSERT INTO ".$this->sql_name()." VALUES (".join(',',array_values($values)).");";
     #$sql = "INSERT INTO ".$this->sql_name()." (".join(',',$keys).") VALUES (".join(',',array_values($values)).");";
     #if ($fct and $this->name=='place') debug($sql);
+    $fct = !empty($opt['fct']) ? $opt['fct'] : null;
+    if ($fct) $sql = $fct($sql,$this);
+
     return $sql.NB_EOL;
   }
 
index 0e6ed43a524fa73e53d053bbfce7e21057372f2a..298e3e68a4816b36bc7b485c29a61e5c4c5d5e03 100644 (file)
@@ -8,7 +8,10 @@ $DB_TYPES['sqlite'] = array (
 'extra_where' => 'denorm',
 
 'quote_name' => '`',
-'quote' => function(&$v) { return "'".str_replace("'","''",$v)."'"; },
+'quote' => function($v) {
+  $v = "'".str_replace("'","''",$v)."'";
+  return $v;
+},
 'databases' => [
   'PRAGMA database_list',function(&$r) {
 #debug($GLOBALS['Db']->databases());
@@ -107,6 +110,8 @@ $DB_TYPES['sqlite'] = array (
 'table.sql.create' => function($sql,$t) {
   #$sql .= $t->db()->row("SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name=".$t->sql_name());
   $sql = str_replace("\\'","''",$sql);
+  $sql = str_replace('\\r',"\r",$sql);
+  $sql = str_replace('\\n',"\n",$sql);
   #$sql = preg_replace("/\\'/","''",$sql);
   #$sql = 'espace';
   #$sql = "zaza $sql";