public $title;
public $name;
public static $type;
- public $tables;
+ public $tables = [];
public $row_parse; # Function to call in rows()
# Web
$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']);
}
# 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;
}
* @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='') {
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);
}
}
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='],
} 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;
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;
# 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')]
);
}
}
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
}
+ 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
?>
# 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();
#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());