From ebed21e95b38a784a027c5cf7f0b4b8df3b4e982 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 30 Sep 2016 16:37:51 +0200 Subject: [PATCH] first attent to fix db.dump --- lib/php/db.php | 15 ++++++++++++--- lib/php/db/field.php | 9 +++++---- lib/php/db/table.php | 10 +++++++--- lib/php/db/types/sqlite.php | 7 ++++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 00fde73f..6aaa3406 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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; } diff --git a/lib/php/db/field.php b/lib/php/db/field.php index c90c542a..35cc575c 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -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 ); } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 9f85f3b2..3422422c 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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; } diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index 0e6ed43a..298e3e68 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -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"; -- 2.47.3