From fd066f9c2bec0eea54b13f45f5274668fd258419 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 29 Aug 2016 23:15:49 +0100 Subject: [PATCH] Bed --- etc/dbs.php | 6 ++ etc/dbs/nb.php | 11 +++- lib/php/db.php | 143 ++++++++++++++++++++----------------------- lib/php/db/table.php | 12 ++-- lib/php/nb.php | 6 +- 5 files changed, 91 insertions(+), 87 deletions(-) diff --git a/etc/dbs.php b/etc/dbs.php index 9b83f03c..4b0fc1e7 100644 --- a/etc/dbs.php +++ b/etc/dbs.php @@ -1,5 +1,11 @@ 'admin.izideal.vpn', 'type' => 'mysql', 'name' => 'nb', - 'order' => '6', + #'order' => ($LOCAL_DB ? null : 1), + 'order' => 1, '_import' => array('_rent','_nico'), ); $CONF['nb-sqlite'] = array ( - 'order' => 5, 'pdo' => 'sqlite:/opt/rent/nb.db', '_import' => '_rent', ); + +if ($LOCAL_DB and !empty($CONF['nb-sqlite'])) { + $CONF['nb-sqlite']['order'] = isset($CONF['nb']['order']) ? $CONF['nb']['order'] : 1; + unset($CONF['nb']['order']); +} ?> diff --git a/lib/php/db.php b/lib/php/db.php index e3d51d02..094c45e8 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -32,7 +32,7 @@ class Db extends nb { public $title; public $name; public static $type; - public $tables; + public $tables = []; public $row_parse; # Function to call in rows() # Web @@ -49,9 +49,8 @@ class Db extends nb { $opt = is_scalar($opt) ? array('pdo' => $opt) : $opt; # Tables - We dont want to affect $this - $tables = array(); if (isset($opt['tables'])) { - $tables = $opt['tables']; + foreach ($opt['tables'] as $name=>$param) $this->table($name,$param); unset($opt['tables']); } @@ -75,32 +74,9 @@ class Db extends nb { # Pdo $this->connect_init(); - if (empty($this->conn)) $this->connect(); + if (empty($this->conn) and empty($this->_no_connect)) $this->connect(); - # Tables - Add missing infos - if (!empty($tables)) { - - foreach ($this->tables() as $name=>$t) { - #foreach (array_merge(array_keys($this->tables()),[]) as $name) { - #foreach (array_merge(array_keys($this->tables()),array_keys($tables)) as $name) { - - if (empty($tables[$name])) continue; - $this->table($name,$tables[$name]); - - } - - # Virtual - NB 04.07.16 - foreach ($tables as $name => $t) { - if (empty($t['sql'])) continue; - #$sql = $t['sql']; unset($t['sql']); - $t = $this->table($name,$t); - #$t->sql = $sql; - $this->tables[$name] = $t; - } - - } - - # Extras should disapear - NB 29.03.16 + # Extras must disapear - NB 29.03.16 if (!empty($this->extras)) self::bye($this->extras); return true; } @@ -293,13 +269,11 @@ class Db extends nb { * @author NB 12.08.16 * Return a table instance */ - function table($name,$opt=array()) { - if ($this->tables() and array_key_exists($name,$this->tables)) { - if ($opt) $this->tables[$name]->__construct($name,$opt); - return $this->tables[$name]; - } + public function table($name,$params=array()) { + if (!array_key_exists($name,$this->tables)) $this->tables[$name] = new Table($name,['db'=>$this]+$params); + elseif ($params) $this->tables[$name]->__construct($name,$params); - return new Table($name,$opt+['db'=>$this]); + return $this->tables[$name]; } public static function localFile($name='') { @@ -349,12 +323,13 @@ class Db extends nb { public function tables() { - if (!isset($this->tables)) { - $this->tables = array(); + if (!isset($this->_tables)) { + #if (!isset($this->tables)) { + $this->_tables = true; foreach ($this->conn->query($this->type('tables',true,self::$type),PDO::FETCH_ASSOC) as $row) { $name = current($row); - $this->tables[$name] = $this->table($name,$row); + $this->table($name,$row); } } @@ -384,7 +359,7 @@ class Db extends nb { public function action($action,$table=null) { $available = [ ['(db.)?help','This help'], - ['(db.)?dbs','List databases from conf'], + ['(db.)?ls','List databases from conf'], ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'], ['(table.)?rows','Dump one table, use format='], @@ -439,34 +414,6 @@ class Db extends nb { } elseif ($action == 'db.conf') { $return = $this->out(array_values($this->conf)); - } elseif ($action == 'db.dbs' or $action == 'dbs') { - if (true or self::p('hash') or self::p('all')) { - - $fields = ['id','name','title','type','host','order']; - $dbs = []; - $name = self::p('name',''); - $type = self::p('type',''); - - foreach ($this->conf as $id => $attr) { - $d = new Db(); - foreach ($attr as $k=>$v) $d->$k = $v; - $d->connect_init(); - $db = []; $d->id = $id; - - foreach ($fields as $k) { - if (!isset($d->$k)) continue; $db[$k] = $d->$k; - } - - if (!empty($name) and !$this->str_match($db['name'],$name)) continue; - if (!empty($type) and !$this->str_match($db['type'],$type)) continue; - $dbs[] = $db; - } - - $return = $this->out($dbs,$fields); - - } - else $return = $this->out(array_keys($this->conf),"id"); - } elseif ($r=self::class_action_out($this,$action) !== null) { return $r; @@ -536,13 +483,12 @@ class Db extends nb { if (empty($o['id'])) $h[$db]['id'] = $db; } - # Sort - + # Sort by `order`, min first uasort($h,function($a,$b){ if (empty($a["order"]) and empty($b["order"])) return strcmp($a['id'],$b['id']); - $a_ = isset($a["order"]) ? $a["order"] : -1; - $b_ = isset($b["order"]) ? $b["order"] : -1; - return($b_-$a_); + $a_ = !empty($a["order"]) ? $a["order"] : 9999999; + $b_ = !empty($b["order"]) ? $b["order"] : 9999999; + return($a_-$b_); }); if (!$h) return false; @@ -639,8 +585,8 @@ class Db extends nb { # Choose first table if (!self::p('table') and ($v = array_keys($Db->tables()))) self::pset('table',$Db->ar_first($v)); - if ($Db->p('table')) $Table = $Db->table($Db->p('table'), - ( empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] ) + if (self::p('table')) $Table = $Db->table(self::p('table'), + empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] ); } @@ -891,16 +837,36 @@ class Db extends nb { } public static function pinit() { - self::paliases(array( + self::paliases([ 'd' => 'db', 't' => 'table', 'f' => 'format', - 'out' => 'format', 'l' => 'limit', 'a' => 'action', 'h' => 'header', - )); + 'out' => 'format', + ]); + + if ($action=self::p('action')) { + foreach ([ + + 'ls' => 'db.ls', + 'tables' => 'db.tables', + 'fields' => 'table.fields', + 'rows' => 'table.rows', + 'insert' => 'table.insert', + 'replace' => 'table.replace', + 'update' => 'table.update', + 'delete' => 'table.delete', + + ] as $src => $dest) { + if ($action === $src) { + $action = $dest; + } + self::pset('action',$action); + } + } #if($format = out::client_type()) self::pset('format',$format); # Param - Extract dbname from table @@ -917,6 +883,31 @@ class Db extends nb { } + public function ls(&$fields=[]) { + + $fields = ['id','name','title','type','host','order']; + $dbs = []; + $name = self::p('name',''); + $type = self::p('type',''); + + foreach ($this->conf as $id => $attr) { + $attr['_no_connect'] = true; + $attr['id'] = $id; + $d = new Db($attr); + if (!empty($name) and !$this->str_match($d->name,$name)) continue; + if (!empty($type) and !$this->str_match($d->type,$type)) continue; + $db = []; + + foreach ($fields as $k) { + if (!isset($d->$k)) continue; $db[$k] = $d->$k; + } + + $dbs[] = $db; + } + + return $dbs; + } + } # < Class ?> diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 2003e4ec..723654a0 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1386,7 +1386,7 @@ Class Table extends nb { # NB 23.05.16 } # NB 23.05.16 }; - if ($action == 'table.fields' or $action == 'fields') { + if ($action == 'table.fields') { $rows = array_values($this->object2array($this->fields())); list($sql,$where,$limit,$select_count) = $this->rows_sql(); @@ -1414,32 +1414,32 @@ Class Table extends nb { #return $this->out(array_values($this->object2array($this->fields()))); return $this->out($rows); - } elseif ($action == 'table.rows' or $action == 'rows') { + } elseif ($action == 'table.rows') { $this->db()->print_header($this->p('format')); $this->rows(); return true; } elseif ($action == 'table.info') { return $this->out($this->info()); - } elseif ($action == 'table.delete' or $action == 'delete') { + } elseif ($action == 'table.delete') { if (!$this->delete($this->p(),$e)) bye($e); header('Location: '.$this->url_referer(str_replace('&','&',$this->url_list()))); $this->out($e); return true; - } elseif ($action == 'table.replace' or $action == 'replace') { + } elseif ($action == 'table.replace') { if (!$this->replace($this->p(),$e)) bye($e); header('Location: '.$this->url_referer()); $this->out($e); return true; - } elseif ($action == 'table.insert' or $action == 'insert') { + } elseif ($action == 'table.insert') { if (!$this->insert($this->p(),$e)) bye($e); header('Location: '.$this->url_referer()); $this->out($e); return true; - } elseif ($action == 'table.update' or $action == 'update') { + } elseif ($action == 'table.update') { #$this->bye($this->p()); if (!$this->update($this->p(),$e)) bye($e); header('Location: '.$this->url_referer()); diff --git a/lib/php/nb.php b/lib/php/nb.php index 0a344f79..21ec6816 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -62,9 +62,9 @@ class NB { */ public static function paliases($aliases) { foreach ($aliases as $short=>$long) { - if (!preg_match('/^\s*$/',self::p($short))) DB::pset($long,Db::p($short)); - #echo "===$short => $long\n"; - Db::pset($short,null); + if (!preg_match('/^\s*$/',self::p($short))) self::pset($long,self::p($short)); + #if (!preg_match('/^\s*$/',self::p($short))) echo ">$long => short=".self::p($short); + self::pset($short,null); } } -- 2.47.3