From d03eeba6e1425b2fa59ef4ceed038166ac56d318 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 8 Mar 2016 18:40:28 +0100 Subject: [PATCH] add out to table --- lib/php/db/table.php | 139 +++++++++++++++++++++++++------------------ lib/php/out.php | 43 +++++++------ 2 files changed, 105 insertions(+), 77 deletions(-) diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 3f8a261b..5a1d3b18 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1,6 +1,7 @@ sql)) return $this->sql; if (!preg_match('/^[\w_-]+$/',$this->name) ) { @@ -168,7 +169,7 @@ GROUP BY sql.id,sql.table * @name (string) name of the field to return. Default: null * @return (array) return null where name does not exsts */ - function fields($name=null) { + public function fields($name=null) { if ($this->fields === null) { $this->fields = array(); @@ -281,7 +282,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this->fields; } - function url_keys($values=null,$params=array(),$sep='&') { + public function url_keys($values=null,$params=array(),$sep='&') { if ($values === null) $values = $this->p(); $url = is_array($params) ? $params : array($params); @@ -299,7 +300,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $url ? '?table='.$this->p('table').$sep.join($sep,$url) : ''; } - function fields_keys(&$others=array()) { + public function fields_keys(&$others=array()) { $fields_keys = array(); @@ -315,7 +316,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function html_edit($values = null) { + public function html_edit($values = null) { if ($values === null) $values = $this->p(); if (!is_array($values)) $values = array($values); @@ -369,7 +370,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function url_list($k='',$v='') { + public function url_list($k='',$v='') { $params = array(); $fields = ($this->p('action') == 'delete') ? array() : $this->fields(); @@ -398,7 +399,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function url_sort($name) { + public function url_sort($name) { # See: http://dev.w3.org/html5/html-author/charref $html = ''; @@ -424,7 +425,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function nav($count,$tot,$limit) { + public function nav($count,$tot,$limit) { $html = ''; @@ -456,7 +457,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function where($fields,$hvalues,$need_all_values=false) { + public function where($fields,$hvalues,$need_all_values=false) { // Construct where $where = array(); @@ -474,7 +475,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function where_criterias($values,$logic='') { + public function where_criterias($values,$logic='') { $having = $where = array(); if (empty($logic)) $logic = 'AND'; @@ -549,7 +550,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function add_extras($extras) { + public function add_extras($extras) { $this->fields(); foreach ($extras as $k => $v) { @@ -567,7 +568,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function select_extras() { + public function select_extras() { if (!$this->extras) return ''; @@ -582,7 +583,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. /****************************************************************** Html Output ******************************************************************/ - function rows($opt=array()) { + public function rows($opt=array()) { // // Select @@ -618,12 +619,22 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. : false ; + # Use the module out when format unknow + $out_conf = null; + if (!preg_match('/^text|csv|yaml|json|table|div$/',$format)) { + if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`"); + } + $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()); + if ($out_conf) { + echo out::begin($out_conf,$row); + } else { + if ($opt['is_html']) echo $this->html_nav_top(); + echo $this->{"rows_begin_$format"}($this->fields()); + } } $count++; @@ -634,7 +645,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $count_fields++; } - echo $this->{"rows_rec_$format"}($row); + if ($out_conf) { + echo out::row($out_conf,$row); + } else { + echo $this->{"rows_rec_$format"}($row); + } } @@ -663,12 +678,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - if (!$query) { - $err = $this->db->conn->errorInfo(); - $err[] = $sql; - err(join(' | ',$err)); - return $err[0]; - } + if (!$query) $this->err_sql($sql); $tot = $query->fetch(PDO::FETCH_COLUMN); $opt['tot'] = $tot; @@ -681,16 +691,21 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. if ($count === 0 and $this->p('header') === 'force') { echo $this->{"rows_begin_$format"}($this->fields()); } + if ($count) { - echo $this->{"rows_end_$format"}(); + if ($out_conf) { + out::end($out_conf); + } else { + echo $this->{"rows_end_$format"}(); - if ($opt['is_html']) { + if ($opt['is_html']) { - echo ''.TABLE_EOL - ; - } + echo ''.TABLE_EOL + ; + } + } } # < count $st->closeCursor(); @@ -701,12 +716,12 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. /*----------------------------------------------------------------- Text -----------------------------------------------------------------*/ - function rows_begin_text() { + public function rows_begin_text() { $this->_row_text = null; return ''; } - function rows_rec_text(&$row) { + public function rows_rec_text(&$row) { $text = ''; if ($this->_row_text === null) { $this->_row_text = true; @@ -716,7 +731,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $text.$row."\n"; } - function rows_end_text() { + public function rows_end_text() { unset($this->_row_text); return ''; } @@ -724,7 +739,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. /*----------------------------------------------------------------- Json -----------------------------------------------------------------*/ - function rows_begin_json() { + public function rows_begin_json() { $this->_row_json = null; return '['.TABLE_EOL; return '' @@ -733,7 +748,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. .'['.TABLE_EOL; } - function rows_rec_json(&$row) { + public function rows_rec_json(&$row) { if ($this->_row_json === null) { $json = ''; $this->_row_json = true; @@ -743,7 +758,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $json . json_encode($row); } - function rows_end_json() { + public function rows_end_json() { unset($this->_row_json); return TABLE_EOL.']'.TABLE_EOL; } @@ -751,14 +766,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. /*----------------------------------------------------------------- Yaml -----------------------------------------------------------------*/ - function rows_begin_yaml() { return "---\n"; } - function rows_rec_yaml(&$row) { return $this->yaml_encode($row); } - function rows_end_yaml() { return ''; } + public function rows_begin_yaml() { return "---\n"; } + public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); } + public function rows_end_yaml() { return ''; } /*----------------------------------------------------------------- Xml -----------------------------------------------------------------*/ - function rows_begin_xml() { + public function rows_begin_xml() { return '' .''.TABLE_EOL #db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.TABLE_EOL @@ -766,7 +781,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. ; } - function rows_rec_xml(&$row) { + public function rows_rec_xml(&$row) { $xml = ''; $xml .= TABLE_INDENT."".TABLE_EOL; foreach ($row as $k=>$v) { @@ -780,31 +795,31 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $xml; } - function rows_end_xml() { + public function rows_end_xml() { return ''.TABLE_EOL; } /*----------------------------------------------------------------- Csv -----------------------------------------------------------------*/ - function rows_begin_csv($fields) { + public function rows_begin_csv($fields) { if ($this->p('header')==="0") return ''; return join(TABLE_CSV_SEP,array_keys($fields))."\n"; } - function rows_rec_csv(&$row) { + public function rows_rec_csv(&$row) { return join(TABLE_CSV_SEP,array_values($row))."\n"; } - function rows_end_csv() { + public function rows_end_csv() { return ''; } /*----------------------------------------------------------------- Html Table -----------------------------------------------------------------*/ - function rows_begin_table($fields) { + public function rows_begin_table($fields) { $html = ''; @@ -829,7 +844,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $html; } - function rows_rec_table(&$row) { + public function rows_rec_table(&$row) { $html = ''.TABLE_EOL; @@ -853,7 +868,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $html; } - function rows_end_table($opt=array()) { + public function rows_end_table($opt=array()) { $html = ''; $html .= ''.TABLE_EOL; $html .= ''.TABLE_EOL; @@ -863,11 +878,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. /*----------------------------------------------------------------- Html Div -----------------------------------------------------------------*/ - function rows_begin_div() { + public function rows_begin_div() { return '
'.TABLE_EOL; } - function rows_rec_div(&$row) { + public function rows_rec_div(&$row) { $html = ''; @@ -892,14 +907,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $html; } - function rows_end_div($opt=array()) { + public function rows_end_div($opt=array()) { } - function sql_name($value=null) { + public function sql_name($value=null) { return $this->db->sql_name($value === null ? $this->name : $value); } - function insert($hvalues) { + public function insert($hvalues) { $fields = $values = array(); #var_dump($hvalues); foreach ($this->fields() as $name => $field) { @@ -935,7 +950,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this->db->exec($sql); } - function update($hvalues) { + public function update($hvalues) { $keys = array(); $keys_values = array(); @@ -998,7 +1013,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function delete($hvalues,&$e=null) { + public function delete($hvalues,&$e=null) { $keys = $this->fields_keys(); // If no primary keys, we use all field if (empty($keys)) $keys = $this->fields(); @@ -1013,10 +1028,10 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this->db->exec($sql); } - function out($v,$head=false) { return $this->db->out($v,$head); } - function out2($v,$head=array()) { return $this->db->out2($v,$head); } + public function out($v,$head=false) { return $this->db->out($v,$head); } + public function out2($v,$head=array()) { return $this->db->out2($v,$head); } - function action() { + public function action() { $action = $this->p('action'); if ($action == 'table.fields' or $action == 'fields') { @@ -1075,7 +1090,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return false; } - function html_nav_top() { + public function html_nav_top() { if ($this->p('header')==="0") return ''; $html = ''; @@ -1091,7 +1106,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function count() { + public function count() { if (isset($this->count)) return $this->count; $sql_count = $this->name; @@ -1108,5 +1123,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. return $this->count; } + public static function err_sql($sql) { + $err = $this->db->conn->errorInfo(); + $err[] = $sql; + self::bye(join(' | ',$err)); + } + } ?> diff --git a/lib/php/out.php b/lib/php/out.php index 40b4c43c..c73ae219 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -20,14 +20,14 @@ class Out extends Nb { 'function' => 'out_csv', ), 'labels' => array( - 'enclose' => array("
".OUT_EOL,"
"), + 'enclose' => array("
".OUT_EOL,"
".PHP_EOL), 'tag_enclose' => 'labels', 'tag' => 'label', 'function' => 'out_tag', 'head' => 'out_tag_head', ), 'table' => array( - 'enclose' => array("".OUT_EOL,"
"), + 'enclose' => array("".OUT_EOL,"
".PHP_EOL), 'tag_enclose' => 'tr', 'tag_head' => 'th', 'tag' => 'td', @@ -35,7 +35,7 @@ class Out extends Nb { 'head' => 'out_tag_head', ), 'xml' => array( - 'enclose' => array(''.OUT_EOL."".OUT_EOL,""), + 'enclose' => array(''.OUT_EOL."".OUT_EOL,"".PHP_EOL), 'function' => 'out_xml', ), 'yaml' => array( @@ -62,6 +62,21 @@ class Out extends Nb { echo $conf['function']($row,$conf); } + public static function end($conf) { + if (!empty($conf['enclose'])) echo $conf['enclose'][1]; + } + + public static function begin($conf,&$head) { + if (!isset($conf['head'])) return; + + if (!empty($conf['enclose'])) echo $conf['enclose'][0]; + if (empty($head) and self::is_hash($data[0])) { + $head = array_keys($data[0]); + } + echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol']; + + } + public static function rows($type,&$data,$head=array()) { if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`"); $conf = self::$types[$type]; @@ -81,19 +96,7 @@ class Out extends Nb { 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']; - - } - - } + if ($head !== false) self::begin($conf,$head); foreach ($data as $row) { $count++; @@ -136,8 +139,12 @@ function out_csv(&$row,$o) { function out_csv_head(&$row,$o) { if (!is_array($row)) echo 0; - if (out::is_hash($row)) echo out_csv(array_keys($row),$o); - echo out_csv($row,$o); + if (out::is_hash($row)) { + $ar = array_keys($row); + echo out_csv($ar,$o); + } else { + echo out_csv($row,$o); + } } #function out_tag_head(&$row,$o) { return ''; } -- 2.47.3