From: Nicolas Boisselier Date: Tue, 30 Aug 2016 15:38:54 +0000 (+0200) Subject: db.type no more static X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=b8b166c2960b66a202ec9fd5058a1b8ac74adcb2;p=nb.git db.type no more static --- diff --git a/lib/php/db.php b/lib/php/db.php index a26bf015..757553db 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -31,7 +31,7 @@ class Db extends nb { # Database infos public $title; public $name; - public static $type; + public $type; public $tables = []; public $row_parse; # Function to call in rows() @@ -45,7 +45,6 @@ class Db extends nb { function __construct($opt = '') { # Args - self::$type = null; if ($opt==='') $opt = array(); $opt = is_scalar($opt) ? array('pdo' => $opt) : $opt; @@ -56,8 +55,9 @@ class Db extends nb { } # Statics - if (isset($opt['type'])) { - self::$type = $opt['type']; + #$this->type = null; + if (isset($opt['Ztype'])) { + $this->type = $opt['type']; unset($opt['type']); } # Args @@ -75,7 +75,6 @@ class Db extends nb { # Pdo $this->connect_init(); - if (empty($this->conn) and empty($this->_no_connect)) $this->connect(); # Extras must disapear - NB 29.03.16 if (!empty($this->extras)) self::bye($this->extras); @@ -88,15 +87,15 @@ class Db extends nb { } function connect_init() { - if (empty($this->pdo) and self::$type) $this->pdo = self::$type.':'; - if (empty(self::$type)) self::$type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo)); - if (!self::$type) return false; + if (empty($this->pdo) and $this->type) $this->pdo = $this->type.':'; + if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo)); + if (!$this->type) return false; if ($this->type('use_path')) { if ($p = $this->p('db.host')) $this->host = $p; if (empty($this->host)) $this->host = preg_replace('/^\w+:/','',$this->pdo); # Add file - if (trim($this->pdo,'pdo:'.self::$type)=='') $this->pdo .= $this->host; + if (trim($this->pdo,'pdo:'.$this->type)=='') $this->pdo .= $this->host; } else { @@ -143,15 +142,16 @@ class Db extends nb { function connect() { + if (!empty($this->conn)) return false; # Pdo if (empty($this->pdo)) return false; # Type - if (!$this->type(null)) $this->bye("Unknow type = `".self::$type."`"); + if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`"); # Connect if ($this->type('use_path') and !is_readable($this->host)) { - $this->bye("Can't read database path `".$this->host."` type=`".self::$type."`",false); + $this->bye("Can't read database path `".$this->host."` type=`".$this->type."`",false); } try { @@ -299,9 +299,22 @@ class Db extends nb { return $return; } - public static function type($key=null,$die=false,$type=null) { + private function unvar($value) { + return $value; + if (!is_scalar($value)) return $value; + $replace = [ + '' => $this->name, + '' => $this->name, + '' => $this->type, + '' => self::$encoding, + ]; + debug( str_replace(array_keys($replace),array_values($replace),$value) ); + return str_replace(array_keys($replace),array_values($replace),$value); + } + + public function type($key=null,$die=false,$type=null) { global $DB_TYPES; - if (!$type) $type = self::$type; + if (!$type) $type = $this->type; if ($key == 'type') return $type; # Load php file @@ -320,6 +333,17 @@ class Db extends nb { return; } + $value = $DB_TYPES[$type][$key]; + /* + */ + if (is_scalar($value)) { + $value = $this->unvar($value); + } elseif (is_scalar($value) and !$this->is_hash($value)) { + for ($i=0;$iunvar($value[$i]); + } + } + return $value; return $DB_TYPES[$type][$key]; } @@ -329,7 +353,7 @@ class Db extends nb { #if (!isset($this->tables)) { $this->_tables = true; - foreach ($this->conn->query($this->type('tables',true,self::$type),PDO::FETCH_ASSOC) as $row) { + foreach ($this->conn->query($this->type('tables',true,$this->type),PDO::FETCH_ASSOC) as $row) { $name = current($row); $this->table($name,$row); } @@ -453,7 +477,6 @@ class Db extends nb { } - #if ( ($yaml = @yaml_parse_file($file)) ) $h = array_replace_recursive($h,$yaml); } unset($yaml,$CONF); @@ -560,17 +583,19 @@ class Db extends nb { $Db = new self(); if (empty($Db)) return false; - $db = self::conf_search_db($conf); - if (self::p('localFile')) { - foreach (self::localFile($db['name']) as $k=>$v) { + $db = $Db->conf_search_db($conf); + if ($Db->p('localFile')) { + foreach ($Db->localFile($db['name']) as $k=>$v) { if (empty($db[$k])) $db[$k] = $v; } } # Connection $Db->__construct($db); - - if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`"); + if (empty($Db->_no_connect)) { + if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`"); + $Db->connect(); + } /* Table @@ -655,10 +680,10 @@ class Db extends nb { $type_from = $type_to = ''; if (self::p('db.type')) { echo "-- Type : ".self::p('db.type')."\n"; - $type_from = self::$type; + $type_from = $this->type; $type_to = self::p('db.type'); #$this->type_out = self::p('db.type'); - self::$type = self::p('db.type'); + $this->type = self::p('db.type'); #$this->type_out = self::p('db.type'); } @@ -700,9 +725,9 @@ class Db extends nb { echo $t->sql_create; if ($insert and $t->type == 'table') { - if ($type_from) self::$type = $type_from; + if ($type_from) $this->type = $type_from; $t->rows(); - if ($type_to) self::$type = $type_to; + if ($type_to) $this->type = $type_to; } } @@ -718,7 +743,7 @@ class Db extends nb { +[ 'id' => (empty($this->id) ? '' : $this->id), 'name' => $this->name, - 'type' => self::$type, + 'type' => $this->type, 'host' => $this->host, #'tables' => count($this->tables()), #'conf' => count(array_keys($this->conf)), @@ -896,13 +921,12 @@ class Db extends nb { $attr['_no_connect'] = true; $attr['id'] = $id; $d = new Db($attr); - #debug([$d->id,$d::$type]); $db = []; foreach ($fields as $k) { #if (isset($d::$k)) $db[$k] = $d::$k; - if ($k=='type' and isset($d::$type)) $db[$k] = $d::$type; - elseif (isset($d->$k)) $db[$k] = $d->$k; + #if ($k=='type' and isset($d::$type)) $db[$k] = $d::$type; + if (isset($d->$k)) $db[$k] = $d->$k; } if (!empty($name) and !$this->str_match($db['name'],$name)) continue; diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 2997dae4..c90c542a 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -225,7 +225,7 @@ class field extends nb { #if ($value === null) return ''; static $sqlite = null; if ($sqlite === null) { $db = $this->db(); - $sqlite = $db::$type == 'sqlite'; + $sqlite = (bool)($db->type == 'sqlite'); } # Anoying, sqlite does not format properly !