From 6cd37ef9fed6b2d2bbdbd932fbad48b2248f4fde Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 9 Mar 2016 00:23:21 +0000 Subject: [PATCH] out --- etc/profile.d/envs | 1 + etc/profile.d/functions | 21 ++++++++ lib/php/db/table.php | 112 +++++++++++++++++++++++----------------- lib/php/nb.php | 3 +- lib/php/out.php | 58 ++++++++++++--------- 5 files changed, 124 insertions(+), 71 deletions(-) diff --git a/etc/profile.d/envs b/etc/profile.d/envs index e09048f0..b03fc08a 100644 --- a/etc/profile.d/envs +++ b/etc/profile.d/envs @@ -12,6 +12,7 @@ [ -z "$UID" ] && UID=`id -u` [ -z "$USER" ] && USER=`whoami` [ -z "$OSTYPE" ] && OSTYPE=`uname|tr [:upper:] [:lower:]` +[ -z "PWD" ] && PWD=`pwd` # # PATHS diff --git a/etc/profile.d/functions b/etc/profile.d/functions index c3dc62d8..2b5977ca 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -476,3 +476,24 @@ END { } ' $noheader } + +mail_open_relay() { + shell_help "Usage: $FUNCNAME MAIL_SERVER" "$@" && return; + cat <p('out') or !preg_match('/^text|csv|yaml|json|table|div$/',$format)) { if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`"); + if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('',''); + debug('Using out module!'); } $count = 0; while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) { - if ($count === 0) { + $count++; + + # + # Head + # + if ($count === 1) { + if ($opt['is_html']) echo $this->html_nav_top(); + if ($out_conf) { - echo out::begin($out_conf,$row); + out::begin($out_conf,$this->fields()); + } else { - if ($opt['is_html']) echo $this->html_nav_top(); echo $this->{"rows_begin_$format"}($this->fields()); } + } + + # + # Row + # + if ($out_conf and $count !== 1) out::row_end($out_conf); - $count++; $count_fields = 0; foreach ($this->fields() as $f => $field) { @@ -646,7 +660,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } if ($out_conf) { - echo out::row($out_conf,$row); + out::row($out_conf,$row); } else { echo $this->{"rows_rec_$format"}($row); } @@ -656,56 +670,62 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $opt['tot'] = $count; $opt['count'] = $count; $opt['limit'] = $limit; - if ($count and $opt['is_html']) { - // - // Tot - // - if (!$where and !$limit) { - debug("Table.rows(): Not using count(*)",1); - $query = $this->db->conn->query("SELECT $count"); - - } elseif ($select_count) { - $query = $this->db->conn->query('SELECT FOUND_ROWS()'); - - } elseif ($where) { - $sql_count = $sql; - $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count); - $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count); - $query = $this->db->conn->query($sql_count); - } else { - $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name()); + if ($count) { - } + if ($opt['is_html']) { + // + // Tot + // + if (!$where and !$limit) { + debug("Table.rows(): Not using count(*)",1); + $query = $this->db->conn->query("SELECT $count"); - if (!$query) $this->err_sql($sql); + } elseif ($select_count) { + $query = $this->db->conn->query('SELECT FOUND_ROWS()'); - $tot = $query->fetch(PDO::FETCH_COLUMN); - $opt['tot'] = $tot; - #if (!$tot) return; + } elseif ($where) { + $sql_count = $sql; + $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count); + $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count); + $query = $this->db->conn->query($sql_count); - $opt['count'] = $count; - $opt['limit'] = $limit; - } + } else { + $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name()); - if ($count === 0 and $this->p('header') === 'force') { - echo $this->{"rows_begin_$format"}($this->fields()); - } + } - if ($count) { - if ($out_conf) { - out::end($out_conf); - } else { - echo $this->{"rows_end_$format"}(); + if (!$query) $this->err_sql($sql); - if ($opt['is_html']) { + $tot = $query->fetch(PDO::FETCH_COLUMN); + $opt['tot'] = $tot; + #if (!$tot) return; - echo ''.TABLE_EOL - ; - } + $opt['count'] = $count; + $opt['limit'] = $limit; } + + if ($count === 0 and $this->p('header') === 'force') { + echo $this->{"rows_begin_$format"}($this->fields()); + } + + if ($count) { + if ($out_conf) { + out::end($out_conf); + } else { + echo $this->{"rows_end_$format"}(); + + if ($opt['is_html']) { + + echo ''.TABLE_EOL + ; + } + } + + } # < is_html + } # < count $st->closeCursor(); diff --git a/lib/php/nb.php b/lib/php/nb.php index 5603ecbd..1eefc139 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -326,8 +326,7 @@ class nb { * @return BOOL */ public static function is_hash(&$arr) { - if (is_scalar($arr)) return false; - //debug (array_values($arr)); + if (!is_array($arr)) return false; foreach ($arr as $k=>$v) { return ($k === 0) ? false : true; } diff --git a/lib/php/out.php b/lib/php/out.php index c73ae219..51f3afef 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -19,9 +19,9 @@ class Out extends Nb { 'head' => 'out_csv_head', 'function' => 'out_csv', ), - 'labels' => array( + 'center' => array( 'enclose' => array("
".OUT_EOL,"
".PHP_EOL), - 'tag_enclose' => 'labels', + 'tag_enclose' => 'div', 'tag' => 'label', 'function' => 'out_tag', 'head' => 'out_tag_head', @@ -44,7 +44,7 @@ class Out extends Nb { ), 'json' => array( 'enclose' => array('['.OUT_EOL,OUT_EOL.']'), - 'function' => 'json_encode', + 'function.ext' => 'json_encode', 'eol' => OUT_EOL, 'rec' => ',', ), @@ -55,26 +55,42 @@ class Out extends Nb { if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head); } - public static function row($conf,&$row) { + public static function begin($o,$head) { + if (!empty($o['enclose'])) echo $o['enclose'][0]; + if (!isset($o['head'])) return; - #if (isset($conf['eol'])) $row = rtrim($row,$conf['eol']); - if (!isset($conf['function'])) return; - echo $conf['function']($row,$conf); - } + if ($head === false) return; + if (empty($head) and self::is_hash($data[0])) { + $head = array_keys($data[0]); + } + echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol']; - public static function end($conf) { - if (!empty($conf['enclose'])) echo $conf['enclose'][1]; } - public static function begin($conf,&$head) { - if (!isset($conf['head'])) return; + public static function row($o,&$row) { - if (!empty($conf['enclose'])) echo $conf['enclose'][0]; - if (empty($head) and self::is_hash($data[0])) { - $head = array_keys($data[0]); + #if (isset($o['eol'])) $row = rtrim($row,$o['eol']); + #bye($o['function']); + if (isset($o['function.ext'])) { + echo $o['function.ext']($row); + return true; } - echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol']; + if (isset($o['function'])) { + echo $o['function']($row,$o); + return true; + } + #if (!isset($o['sep'])) $o['sep'] = "\t"; out_csv($row,$o); + out_tag($row,$o); + return false; + } + public static function row_end($o) { + if (!empty($o['rec'])) echo $o['rec']; + if (!empty($o['eol'])) echo $o['eol']; + } + + public static function end($o) { + if (!empty($o['enclose'])) echo $o['enclose'][1]; } public static function rows($type,&$data,$head=array()) { @@ -93,12 +109,11 @@ class Out extends Nb { $tot = count($data); $count = 0; - echo $conf['enclose'][0]; - # Function head - if ($head !== false) self::begin($conf,$head); + self::begin($conf,$head); foreach ($data as $row) { + if ($count>0) out::row_end($conf); $count++; # Transform simple array into hash @@ -117,11 +132,9 @@ class Out extends Nb { 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]; + if ($count) self::end($conf); } } Out::types() ; # < Class @@ -151,7 +164,6 @@ function out_csv_head(&$row,$o) { function out_tag_head(&$data,$o) { #return var_dump($row,true); return ''; $o['tag'] = isset($o['tag_head']) ? $o['tag_head'] : $o['tag']; - #echo out_tag($row,$o); return true; $row = out::ar_first($data); if (!out::is_hash($row)) $row = $data; #if (!is_array($row)) return out_csv(array_fill(0,count($row),'?')); -- 2.47.3