From: Nicolas Boisselier Date: Mon, 7 Mar 2016 03:38:23 +0000 (+0000) Subject: Bed ! X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=344a6d681500f3163aeeded905e9d17582ac8af1;p=nb.git Bed ! --- diff --git a/bin/dbq b/bin/dbq index ad1f3385..34f810f1 100755 --- a/bin/dbq +++ b/bin/dbq @@ -142,7 +142,7 @@ while (my ($k,$v) = each %ACTION) { $keys{$k} = $v; } -$keys{header} = '0' if $keys{action} ne 'rows'; +$keys{header} = '0' if !defined($keys{header}) and $keys{action} ne 'rows'; $keys{table} = $1 if $keys{db} and $keys{db} =~ s/:(.*)$//; ################################################################################# diff --git a/etc/dbs.yaml b/etc/dbs.yaml index c658a5b2..be9f6317 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -96,3 +96,9 @@ rent: # NB 10.01.16 #revision: (SELECT GROUP_CONCAT(''||substr(id,0,instr(id,'.'))||'',' ') FROM template) # NB 10.01.16 template: # NB 10.01.16 View: "''||id||''" +postfix: + pdo: 'mysql:host=media.izideal.vpn;port=3306;dbname=postfix' + username: postfix + title: Postfix on Izideal + options: + - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8'" diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 2b9687d7..c3dc62d8 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -159,6 +159,10 @@ yaml2perl() { perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@ } +json2perl() { + perl -MJSON -MData::Dumper -e 'print Dumper JSON::decode_json(join("",<>))' $@ +} + ################################################################################# # # FUNCTIONS - BASH diff --git a/lib/php/db.php b/lib/php/db.php index a9c34a21..b61bcbe1 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -8,6 +8,7 @@ *****************************************************************************/ require_once(dirname(__FILE__).'/nb.php'); require_once(dirname(__FILE__).'/db/table.php'); +require_once('out.php'); if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS','' #.'' #.'' @@ -58,7 +59,7 @@ class Db extends nb { function __construct($db = '') { # Args - $db = is_scalar($db) ? array('name' => $db) : $db; + $db = is_scalar($db) ? array('pdo' => $db) : $db; if (is_array($db)) { # NB 05.03.16 if (!empty($db->configs)) { @@ -404,49 +405,136 @@ EOF; return '"'.$value.'"'; } - function out($v) { + function out($row,$head=false) { + if (!$this->p('header',1) and $head) $head = false; $f = $this->p('format','json'); #if (preg_match('/yaml|yml/',$f)) bye($this->yaml_encode($this->object2array($this->fields()))); $mime = $this->print_header($f); - #echo $v.PHP_EOL; + #echo $row.PHP_EOL; #bye($f); return true; - if ($f == 'json') { echo json_encode($v).PHP_EOL; } - #elseif ($f == 'csv' and is_array($v)) { echo join("\n",$v)."\n"; } - #elseif ($f == 'csv') { echo join("\t",nb::ar_map('json_encode($a)',$v))."\n"; } - #elseif ($f == 'csv') { echo json_encode($v).PHP_EOL; } + if ($f == 'json') { echo json_encode($row).PHP_EOL; } + #elseif ($f == 'csv' and is_array($row)) { echo join("\n",$row)."\n"; } + #elseif ($f == 'csv') { echo join("\t",nb::ar_map('json_encode($a)',$row))."\n"; } + #elseif ($f == 'csv') { echo json_encode($row).PHP_EOL; } elseif ($f == 'csv') { - #if (is_object($row)) echo "$row\n"; - #$isHash = array_values($v) !== $v; - foreach ($v as $row) { - if (is_scalar($row)) echo "$row\n"; - else echo join("\t",array_values($row))."\n"; + #if (is_object($data)) echo "$data\n"; + #$isHash = array_values($row) !== $row; + if ($head) { + if ($head === true) { + $first = self::ar_first($row); + $first = array_keys($first); + } else { + $first = $head; + } + self::out($first); + } + if (is_scalar($row)) { + echo $row."\n"; + return true; + } + if (is_array($row) and !self::is_hash($row)) { + echo join("\t",array_values($row))."\n"; + return true; + } + $type = 0; + foreach ($row as $k => $v) { + +# NB 06.03.16 if ($type == 1 or $k === 0) { +# NB 06.03.16 # Normal array +# NB 06.03.16 $type = 1; +# NB 06.03.16 echo "$v\n"; +# NB 06.03.16 +# NB 06.03.16 } elseif ($type == 2 or self::is_hash($row[$k])) { +# NB 06.03.16 echo ($type === 2 ? "\t" : "").$v; +# NB 06.03.16 $type = 2; +# NB 06.03.16 +# NB 06.03.16 } elseif ($type == 3 or is_scalar($v)) { +# NB 06.03.16 $type = 3; +# NB 06.03.16 echo "$v\n"; +# NB 06.03.16 + if (is_scalar($v)) { + echo "$v\n"; + } else { + #debug($v); + echo join("\t",array_values($v))."\n"; + } + #} elseif ($type == 2 or $k === 0) { +# NB 06.03.16 elseif (nb::is_hash($v)) { +# NB 06.03.16 echo join("\t",array_values($v); +# NB 06.03.16 } + #else echo join("\t",array_values($v))."\n"; + } + #echo json_encode($row).PHP_EOL; + if ($type === 2) { + echo "\n"; } - #echo json_encode($v).PHP_EOL; } - elseif (preg_match('/yaml|yml/',$f)) { echo $this->yaml_encode($v); } - elseif (preg_match('/php|var_export/',$f)) { echo var_export($v,true); } - elseif (preg_match('/var_dump/',$f)) { echo var_dump($v,true); } - elseif (is_scalar($v)) { echo($v.PHP_EOL); } - //else { echo($v.PHP_EOL); } - else{ echo var_export($v,true); } + elseif (preg_match('/yaml|yml/',$f)) { echo $this->yaml_encode($row); } + elseif (preg_match('/php|var_export/',$f)) { echo var_export($row,true); } + elseif (preg_match('/var_dump/',$f)) { echo var_dump($row,true); } + elseif (is_scalar($row)) { echo($row.PHP_EOL); } + //else { echo($row.PHP_EOL); } + else{ echo var_export($row,true); } return true; } + function out2($rows,$head=array()) { + #if (is_scalar($head)) bye(var_dump($head,true)); + if (is_scalar($head)) $head = array($head); + #var_dump($head); + #return; + if ($this->p('header') === '0' ) $head = false; + #self::bye($head); + #self::bye($this->p('header')); + #self::bye($this->p('format')); + out::rows($this->p('format','csv'),$rows,$head); + return true; + } + function action($table) { #if ($this->p('format') == 'table') $this->pset('format',''); $this->pdef('format',($this->php_cli() ? 'csv' : '')); $action = $this->p('action'); + $head = ($this->p('header',1) and $this->p('format','csv') == 'csv' ) ? true : false; + $rows = array(); + + if ($action == 'db.help') { + return $this->out2(array( + array('db.help','This help'), + array('[db.]dbs','List databases'), + array('[db.]tables','List tables'), + array('db.count','Tables count(*)'), + array('[db.]html_menu','Html menu for tables with links'), + + array('[table.]fields','List fields'), + array('table.count','Table count(*)'), + array('table.sql','Get the sql source'), + + array('[table.]insert','Insert a record'), + array('[table.]update','Update a record'), + array('[table.]delete','Delete a record'), + + ),array('command','description')); + return true; + + } elseif ($action == 'db.tables' or $action == 'tables') { + return $this->out2($this->tables(),array('name')); - if ($action == 'tables') { - return $this->out($this->tables()); + } elseif ($action == 'db.count') { + foreach ($this->tables() as $t) { $rows[] = array( + 'name' => $t, + 'count' => $this->table($t)->count(), + ); } + return $this->out2($rows); + return $this->out2(self::ar_map('array("table"=>$a,"count"=>$GLOBALS["Db"]->table($a)->count())',$this->tables())); - } elseif ($action == 'dbs') { - return $this->out($this->dbs); + } elseif ($action == 'db.dbs' or $action == 'dbs') { + return $this->out2($this->dbs,"name"); - } elseif ($action == 'html_menu') { + } elseif ($action == 'db.html_menu' or $action == 'html_menu') { $this->print_header($this->p('format','html')); echo $this->html_menu(); return true; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index e6ef0bf2..b4a14e4c 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -23,6 +23,7 @@ class table extends nb { public $name; public $db; public $sql; + public $type; # table, view, sql public $fields = null; public $fields_keys = null; public $replace = array(); # replace by javascript @@ -614,12 +615,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. : false ; - if ($opt['is_html']) echo $this->html_nav_top(); - echo $this->{"rows_begin_$format"}($this->fields()); - $count = 0; while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) { + if ($count === 0) { + if ($opt['is_html']) echo $this->html_nav_top(); + echo $this->{"rows_begin_$format"}($this->fields()); + } + $count++; $count_fields = 0; @@ -632,12 +635,15 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - if ($opt['is_html']) { + $opt['tot'] = $count; + $opt['count'] = $count; + $opt['limit'] = $limit; + if ($count and $opt['is_html']) { // // Tot // - if (true and !$where and !$limit) { - debug("Table.rows(): Not using count(*)"); + if (!$where and !$limit) { + debug("Table.rows(): Not using count(*)",1); $query = $this->db->conn->query("SELECT $count"); } elseif ($select_count) { @@ -669,15 +675,20 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $opt['limit'] = $limit; } - echo $this->{"rows_end_$format"}(); + if ($count === 0 and $this->p('header') === 'force') { + echo $this->{"rows_begin_$format"}($this->fields()); + } + if ($count) { + echo $this->{"rows_end_$format"}(); - if ($opt['is_html']) { + if ($opt['is_html']) { - echo ''.PHP_EOL - ; - } + echo ''.PHP_EOL + ; + } + } # < count $st->closeCursor(); @@ -999,35 +1010,36 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this->db->exec($sql); } - function out($v) { return $this->db->out($v); } + function out($v,$head=false) { return $this->db->out($v,$head); } + function out2($v,$head=array()) { return $this->db->out2($v,$head); } function action() { $action = $this->p('action'); - if ($action == 'fields') { - return $this->out($this->object2array($this->fields())); + if ($action == 'table.fields' or $action == 'fields') { + return $this->out2(array_values($this->object2array($this->fields()))); - } elseif ($action == 'sql') { return $this->out($this->sql()); - } elseif ($action == 'count') { return $this->out($this->count()); + } elseif ($action == 'table.sql') { return $this->out2($this->sql(),'sql'); + } elseif ($action == 'table.count') { return $this->out2($this->count(),'count'); - } elseif ($action == 'rows') { + } elseif ($action == 'table.rows' or $action == 'rows') { #if ($this->p('page')==='0') $this->pset('inc',1); $this->rows(); return true; - } elseif ($action == 'table') { - return $this->out(array( - #"count" => $this->count(), - "sql_name" => $this->sql_name(), - "sql" => $this->sql(), - "fields" => $this->object2array($this->fields()), - )); +# NB 06.03.16 } elseif ($action == 'table') { +# NB 06.03.16 return $this->out(array( +# NB 06.03.16 #"count" => $this->count(), +# NB 06.03.16 "sql_name" => $this->sql_name(), +# NB 06.03.16 "sql" => $this->sql(), +# NB 06.03.16 "fields" => $this->object2array($this->fields()), +# NB 06.03.16 ),true); - } elseif ($action == 'delete') { + } elseif ($action == 'table.delete' or $action == 'delete') { if (!$this->delete($this->p(),$e)) bye($e); header('Location: '.str_replace('&','&',$this->url_list())); return true; - } elseif ($action == 'insert') { + } elseif ($action == 'table.insert' or $action == 'insert') { $this->insert($this->p()); if ($this->p('referer')) { header('Location: '.urldecode($this->p('referer'))); @@ -1037,7 +1049,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. #header('Location: '.str_replace('&','&',$this->url_list())); return true; - } elseif ($action == 'update') { + } elseif ($action == 'table.update' or $action == 'update') { #$this->bye($this->p()); $this->update($this->p()); if ($this->p('referer')) { diff --git a/lib/php/nb.php b/lib/php/nb.php index 3562c4ea..5603ecbd 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -1,10 +1,12 @@ 'txt', @@ -83,8 +85,8 @@ class nb { * Function: ar_map * Does what it says */ - public static function ar_map($text) { - return ar_map($text); + public static function ar_map($return,$array,$as_hash=false) { + return ar_map($return,$array,$as_hash); } /* @@ -123,6 +125,7 @@ class nb { break; } #$is_hash = (array_values($ar) === $ar); + # Should return the first k => v - NB 06.03.16 if ($is_hash) return ar_first($hkeys ? array_keys($ar) : array_values($ar)); return ar_first($ar); } @@ -216,14 +219,12 @@ class nb { * Function: yaml_encode */ public static function yaml_encode($row) { - #$yaml = yaml_emit(self::object2array($row)); $yaml = yaml_emit($row); - $yaml = preg_replace('/^---\n/','',$yaml); + $yaml = preg_replace('/^---\s*/','',$yaml); $yaml = preg_replace('/\n\.\.\.$/','',$yaml); $yaml = trim($yaml); - if ($yaml === "---") return '' ; - if (preg_match('/^---\s*$/m',$yaml)) return '' ; - return "--- \n$yaml\n"; + #if ($yaml === "---") return '' ; + return "$yaml\n"; } /* @@ -324,7 +325,8 @@ class nb { * @param ARRAY $arr * @return BOOL */ - public static function is_hash(Array &$arr) { + public static function is_hash(&$arr) { + if (is_scalar($arr)) return false; //debug (array_values($arr)); foreach ($arr as $k=>$v) { return ($k === 0) ? false : true; diff --git a/lib/php/out.php b/lib/php/out.php new file mode 100644 index 00000000..c28c0551 --- /dev/null +++ b/lib/php/out.php @@ -0,0 +1,207 @@ + array( + 'enclose' => array("","\n"), + 'eol' => "\n", + 'head' => 'out_csv_head', + 'function' => 'out_csv', + ), + 'table' => array( + 'enclose' => array("".OUT_EOL,"
"), + 'tag_enclose' => 'tr', + 'tag_head' => 'th', + 'tag' => 'td', + 'function' => 'out_tag', + 'head' => 'out_tag_head', + ), + 'xml' => array( + 'enclose' => array("".OUT_EOL,""), + 'function' => 'out_xml', + ), + 'yaml' => array( + 'enclose' => array("---\n",""), + 'function' => 'out_yaml', + ), + 'json' => array( + 'enclose' => array('['.OUT_EOL,OUT_EOL.']'), + 'function' => 'json_encode', + 'eol' => OUT_EOL, + 'rec' => ',', + ), + ); + } + + public function __construct($type,&$data,$head=array()) { + if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head); + } + + public static function row($conf,&$row) { + + #if (isset($conf['eol'])) $row = rtrim($row,$conf['eol']); + if (!isset($conf['function'])) return; + echo $conf['function']($row,$conf); + } + + public static function rows($type,&$data,$head=array()) { + if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`"); + $conf = self::$types[$type]; + + #echo $conf['function']($data); return true; + if (empty($conf['enclose'])) $conf['enclose'] = array('',''); + if (empty($conf['eol'])) $conf['eol'] = ''; + + if (self::is_hash($data)) $data = array($data); + if (is_scalar($data)) $data = array($data); + + if (!is_array($head) and !is_bool($head)) $head = array($head); + + $tot = count($data); + $count = 0; + + echo $conf['enclose'][0]; + + # Function head + if ($head !== false) { + + if (isset($conf['head'])) { + + $send = $head; + if (empty($head) and self::is_hash($data[0])) { + $head = array_keys($data[0]); + } + echo $conf['head']($head,$conf) . $conf['eol']; + + } + + } + + foreach ($data as $row) { + $count++; + + # Transform simple array into hash + if (isset($row[0]) and $head) { + #bye($conf); + $i = 0; + $new = array(); + if (is_scalar($row)) $row = array($row); + foreach ($head as $k => $h) { + $new[$h] = $row[$i]; + $i++; + } + $row = $new; + } + + self::row($conf,$row); + #echo " ($count)"; + + if (!empty($conf['rec']) and $count<$tot) echo $conf['rec']; + if (!empty($conf['eol'])) echo $conf['eol']; + } + + print $conf['enclose'][1]; + } + +} # < Class +/****************************************************************************/ + +function out_yaml(&$row,$o) { + #echo out::yaml_encode($row); return; + $yaml = '- '.out::yaml_encode($row); + echo preg_replace("/^(?!-)/m"," ",$yaml); +} + +function out_csv(&$row,$o) { + if(is_scalar($row)) echo $row; +# NB 06.03.16 elseif(out::is_hash($row)) { +# NB 06.03.16 return join("\t",array_values($row)); +# NB 06.03.16 } + else { + #bye($row); + #debug(array_values($row)); + #$row = array_values($row); + #$row = array(print_r($row,true)); + echo join("\t",array_values($row)); + } + +} Out::types() ; + +function out_csv_head(&$row,$o) { +# NB 07.03.16 function out_csv_head(&$data,$o) { +# NB 07.03.16 $row = out::ar_first($data); + if (!is_array($row)) echo 0; + if (out::is_hash($row)) echo out_csv(array_keys($row),$o); + echo out_csv($row,$o); +} + +#function out_tag_head(&$row,$o) { return ''; } +function out_tag_head(&$data,$o) { + #return var_dump($row,true); return ''; + $o['tag'] = $o['tag_head']; + $row = out::ar_first($data); + if (!out::is_hash($row)) $row = $data; + #if (!is_array($row)) return out_csv(array_fill(0,count($row),'?')); + if (!is_array($row)) return 0; + if (out::is_hash($row)) $row = array_keys($row); + echo out_tag($row,$o); +} +function out_tag(&$row,$o) { +#var_dump($row); return ''; + if (!is_array($row)) return var_export($row,true); + foreach ($row as $k => $v) { + #if (!is_scalar($v)) continue; + if (!is_scalar($v)) return var_export($row,true); + $k = empty($o['tag']) ? $k : $o['tag']; + return '' + .(OUT_EOL ? ' ' : '') + .(isset($o['tag_enclose']) ? '<'.$o['tag_enclose'].'>' : '') + ."<$k>$v" + .(isset($o['tag_enclose']) ? '' : '') + .OUT_EOL + ; + } +} + +function out_xml(&$row,$o) { + echo (OUT_EOL ? ' ' : '')."".OUT_EOL; + foreach ($row as $k => $v) { + echo (OUT_EOL ? ' ' : '')."<$k>![CDATA[$v]]>".OUT_EOL; + } + echo (OUT_EOL ? ' ' : '')."".OUT_EOL; +} + +# < Functions +/****************************************************************************/ + +#die("A OUT_EOL B \n"); +if (empty($argv) or $argv[1] != 'test') return true; +$data = array( + 'a' => 1, + 'b' => 2, +); +$data = array( 'a', 'b', 'c' ); +$data = array( + array('a1','b1','c1'), + array('a2','b2','c2'), + array('a3','b3','c3'), +); +$field = array('A','B','C'); +$data = array( + array('a' => 'a1', 'b' => 'b1'), + array('a' => 'a2', 'b' => 'b2'), +); +#$data = array( array('a'=>'A'), array('b'=>'B'), array('c'=>'C') ); +$field = array(); +$o = new Out($argv[2] ? $argv[2] : 'yaml',$data,$field); +?>