From b94c1aa13dcc0b602127fc993853da088b1a7f7d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 10 Nov 2016 22:03:39 +0000 Subject: [PATCH] out handle limit --- lib/php/db.php | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 89c772ab..6e122fdf 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -84,6 +84,21 @@ class Db extends nb { 'delete' => 'table.delete', ]; + const action_help = [ + ['(db.)?help','This help'], + ['(db.)?ls','List databases from conf'], + ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'], + + ['(table.)?rows','Dump one table, use format='], + ['(table.)?fields','List fields'], + ['table.infos','Table infos (count(*). Type, ...)'], + ['table.sql','Get the sql source'], + + ['(table.)?insert','Insert a record'], + ['(table.)?update','Update a record'], + ['(table.)?delete','Delete a record'], + + ]; # Web public $default_table; public $sort; @@ -506,27 +521,27 @@ class Db extends nb { } public function out($rows,$head=[]) { + + // Handle limit + if ($this->p('limit')) { + + $limit = (int)$this->p('limit'); + $i = empty($head) ? 1 : 0; + + foreach ($rows as $k=>$v) { + if ($i>$limit) unset($rows[$k]); + $i++; + } + + } + $this->out->rows($this->p('format',$this->out->php_cli() ? 'csv' : 'table'),$rows,$head); return true; } public function action($action,$table=null) { #debug(($this->tables)); - $available = [ - ['(db.)?help','This help'], - ['(db.)?ls','List databases from conf'], - ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'], - - ['(table.)?rows','Dump one table, use format='], - ['(table.)?fields','List fields'], - ['table.infos','Table infos (count(*). Type, ...)'], - ['table.sql','Get the sql source'], - - ['(table.)?insert','Insert a record'], - ['(table.)?update','Update a record'], - ['(table.)?delete','Delete a record'], - - ]; + $available = self::action_help; $actions = explode(',',$action); $this->pdef('format',($this->php_cli() ? 'csv' : '')); $rows = []; @@ -571,6 +586,7 @@ class Db extends nb { $type = $this->p('type',''); $name = $this->p('name',''); $engine = $this->p('engine',''); + $database = $this->p('database',''); #var_dump ($this->tables()); #foreach($this->tables as $t) debug($t->name.'='.$t->type); @@ -582,6 +598,7 @@ class Db extends nb { if (!empty($type) and !$this->str_match($t->type,$type)) continue; if (!empty($count) and !$this->str_match($t->count,$count)) continue; if (!empty($engine) and !$this->str_match($t->engine,$engine)) continue; + if (!empty($database) and isset($t->database) and !$this->str_match($t->database,$database)) continue; $row = $t->status(); if ($this->p('count')) $row['count'] = $t->status('count'); -- 2.47.3