]> git.nbdom.net Git - nb.git/commitdiff
Bed !
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Mar 2016 02:04:59 +0000 (02:04 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Mar 2016 02:04:59 +0000 (02:04 +0000)
bin/dbq
etc/dbs.yaml
lib/php/db.php
lib/php/db/table.php
lib/php/out.php

diff --git a/bin/dbq b/bin/dbq
index ecf23f0ba9f024b4f00bea97c77f1575942a87ff..f5ec83602f0af9bf3973b40952bc2bbbb069f0ba 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -160,7 +160,7 @@ if ($keys{format} eq 'nc') {
   $keys{format} = 'json';
   open(STDOUT,"|$0 --parse_debug|jq .")
 
-} elsif ( $keys{debug} or $Opt{debug}) {
+} elsif ( $keys{debug} or $Opt{debug} ) {
   open(STDOUT,"|$0 --parse_debug")
 
 }
index f28d23bf69bbc4adb66764b7509f9c6c7b813d9e..27700cd54ebf68e50d0e8b46ebb27f89637aad48 100644 (file)
@@ -73,8 +73,8 @@ rent:
 # NEW- NB 10.01.16
   tables:
 
-    template:
-      View: "'<a href=\"/template/'||id||'\">'||id||'</a>'"
+# NB 08.03.16     template:
+# NB 08.03.16       View: "'<a href=\"/template/'||id||'\">'||id||'</a>'"
 
     place:
       replace:
index d8efff596a60e6e9ee1f742411f5ff9be300836d..17fd8d906552d8c376eed9ff9b4b9b34dd49ba33 100644 (file)
@@ -23,6 +23,22 @@ if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS',''
 #if (!defined('DB_HTML_BUTTON_ADD'))    define('DB_HTML_BUTTON_ADD','<a class="button" href="?" onclick="nb.form_clear(this.parentElement) ? 0: 0">Add</a>');
 #if (!defined('DB_HTML_BUTTON_ADD'))    define('DB_HTML_BUTTON_ADD','<input type="submit" class="button" value="Add" onclick="this.setAttribute(\'name\',\'edit\')?0:0"/>');
 
+$DB_TYPES = array(
+  'tables' => array(
+    'pgsql' => "SELECT table_name as name"
+      .",LOWER(CASE table_type WHEN 'BASE TABLE' THEN 'TABLE' ELSE table_type END) as type"
+      .",table_type"
+      ." FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema NOT IN ('pg_catalog', 'information_schema')"
+    ,
+    'mysql' => "SELECT TABLE_NAME as name"
+      .",LOWER(IF(TABLE_TYPE='BASE TABLE','TABLE',TABLE_TYPE)) as type"
+      .",ENGINE as engine"
+      .",CREATE_TIME as created"
+      ." FROM information_schema.tables WHERE TABLE_SCHEMA=DATABASE()"
+    ,
+    'sqlite' => "SELECT name,type FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
+  ),
+);
 class Db extends nb {
 
   # PDO Connection
@@ -227,7 +243,6 @@ class Db extends nb {
   }
 
   function row($sql,$sep=' ') {
-    #$sql = "SELECT * FROM addr LIMIT 1";
     $query = $this->query($sql);
     $result = $query->fetch(PDO::FETCH_NUM);
     if (!is_array($result)) return $result;
@@ -267,29 +282,40 @@ class Db extends nb {
     return new Table($name,array_merge($opt));
   }
 
-  function tables() {
-    if (isset($this->_tables) and $this->_tables) return $this->_tables;
+  public function types($key) {
+    #if (!isset($DB_TYPES[$key][$this->type])) return;
+    global $DB_TYPES;
 
-    if ($this->type == 'sqlite') {
-      $sql = "SELECT name FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name";
+    if (
+      !isset($DB_TYPES[$key])
+      or !isset($DB_TYPES[$key][$this->type])
+    )
+      #$this->bye("db.types(`$key`): Unknow db type: ".$this->type." from ".print_r($DB_TYPES,1))
+      return
+    ;
 
-    } elseif ($this->type == 'pgsql') {
-      $sql = "SELECT table_name FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema NOT IN ('pg_catalog', 'information_schema')";
+    return $DB_TYPES[$key][$this->type];
+  }
 
-    } elseif ($this->type == 'mysql') {
-      $sql = "SHOW TABLES";
+  public function tables($return_hash=false) {
+    if (isset($this->_tables) and $this->_tables) return ($return_hash ? $this->tables : $this->_tables);
 
-    } else {
-      if ($this->pdo) $this->bye('db.tables(): Unknow db type: '.$this->type);
-      $this->_tables = array();
-      return array();
-    }
+    if (!($sql = $this->types('tables')))
+      $this->bye("db.types(`$key`): Unknow db type: ".$this->type." from ".print_r($DB_TYPES,1))
+    ;
 
-    $rows = $this->conn->query($sql);
+    $rows = $this->conn->query($sql,PDO::FETCH_ASSOC);
     foreach ($rows as $row) {
-      $this->_tables[] = current($row);
+      $name = current($row);
+      $this->_tables[] = $name;
+
+      if (!isset($this->tables[$name])) $this->tables[$name] = array();
+      foreach ($row as $k => $v) {
+        $this->tables[$name][$k] = $v;
+      }
     }
 
+    if ($return_hash) return $this->tables;
     return $this->_tables;
 
   }
@@ -525,7 +551,6 @@ EOF;
     #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') {
@@ -533,12 +558,11 @@ EOF;
         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.]rows','Dump one table, use format='),
         array('[table.]fields','List fields'),
-        array('table.count','Table count(*)'),
+        array('table.inf','Table infos (count(*). Type, ...)'),
         array('table.sql','Get the sql source'),
 
         array('[table.]insert','Insert a record'),
@@ -549,15 +573,18 @@ EOF;
       return true;
 
     } elseif ($action == 'db.tables' or $action == 'tables') {
-      return $this->out2($this->tables(),array('name'));
-
-    } elseif ($action == 'db.count') {
-      foreach ($this->tables() as $t) { $rows[] = array(
-        'name' => $t,
-        'count' => $this->table($t)->count(),
-      ); }
+      foreach ($this->tables(true) as $name => $t) { $row=array('name'=>$t['name']);
+        if ($this->p('count')==='1') $row['count'] = $this->table($name)->count();
+        foreach (array(
+          'type',
+          'engine',
+          'created',
+        ) as $f) { if (isset($t[$f])) $row[$f] = $t[$f]; }
+        $rows[] = $row;
+      }
       return $this->out2($rows);
-      return $this->out2(self::ar_map('array("table"=>$a,"count"=>$GLOBALS["Db"]->table($a)->count())',$this->tables()));
+      return $this->out2(array_values($this->tables(true)));
+      return $this->out2($this->tables(),array('name'));
 
     } elseif ($action == 'db.dbs' or $action == 'dbs') {
       return $this->out2($this->dbs,"name");
@@ -575,12 +602,6 @@ EOF;
     return false;
   }
 
-  /**
-  * @copyright NB 05.03.16
-  */
-  static function conf ($default,$files=array()) {
-       }
-
   /**
   * @copyright NB 05.03.16
   * @param [FILES] $files Files to load
@@ -619,8 +640,12 @@ EOF;
     echo "__destruct\n";
   }
 
+  //
+  // Create globals from $conf=array(yaml_fileds)
+  //
   public static function init($conf) {
     global $Db, $Table;
+
     if (isset($Table)) self::bye("Table.init(): GLOBALS['Table'] already exists !");
     if (isset($Db)) self::bye("Db.init(): GLOBALS['Db'] already exists !");
 
@@ -631,11 +656,6 @@ EOF;
 
     # Load databases
     if (! ($dbs = $Db->config2h($conf)) ) return false;
-# NB 05.03.16     $ar = array('a');
-# NB 05.03.16     #$ar[] = 'b';
-# NB 05.03.16     array_unshift($ar,'b');
-# NB 05.03.16     bye($ar);
-    #bye($dbs['izi']);
 
     # Param - Extract dbname from table
     if (preg_match('/^(\w+)\.(.*?)$/',$Db->p('table'),$m)) {
index 0e0fd1b6319107664d54488833ef1b8f38cd6e3c..6d24da7013f8d6b932350455324ed09c27f5fd18 100644 (file)
@@ -14,11 +14,14 @@ if (false and empty($_SERVER['DOCUMENT_ROOT'])) {
   #print_r($Table->sql());
 }
 
+define('TABLE_EOL',defined(NB_EOL) ? NB_EOL : PHP_EOL);
+define('TABLE_INDENT',TABLE_EOL ? "\t" : "");
+define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
 if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
 if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
 if (!defined('DB_TABLE_QUERY_NAME')) define('DB_TABLE_QUERY_NAME','_query_');
 
-class table extends nb {
+class Table extends nb {
 
   public $name;
   public $db;
@@ -334,8 +337,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     $st = $this->db->conn->prepare($sql);
     $st->execute();
 
-    echo '<form class="db edit" method="post" action="?">'.PHP_EOL;
-    echo '<div class="fields">'.PHP_EOL;
+    echo '<form class="db edit" method="post" action="?">'.TABLE_EOL;
+    echo '<div class="fields">'.TABLE_EOL;
     $count = 0;
     if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) {
       $count ++;
@@ -349,20 +352,20 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       }
 
     }
-    echo '</div>'.PHP_EOL;
+    echo '</div>'.TABLE_EOL;
 
     echo ''
       .'<div class="db buttons">'
       .( empty($_SERVER['HTTP_REFERER']) ? '' : '<input type="button" onclick="document.location=document.referrer" value="Cancel" />')
       .'<input type="reset" />'
       .'<input type="submit"/>'
-      .'</div>'.PHP_EOL
+      .'</div>'.TABLE_EOL
       .'<input type="hidden" name="table" value="'.$this->name.'"/>'
       .'<input type="hidden" name="action" value="update"/>'
       .'<input type="hidden" name="db" value="'.$this->p('db').'"/>'
       .'<input type="hidden" name="debug" value="'.$this->p('debug').'"/>'
       .'<input type="hidden" name="referer" value="'.urlencode(@$_SERVER['HTTP_REFERER']).'"/>'
-    .'</form>'.PHP_EOL;
+    .'</form>'.TABLE_EOL;
 
   }
 
@@ -685,7 +688,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
         echo '<div class="nav bottom">'
           .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
-         .'</div>'.PHP_EOL
+         .'</div>'.TABLE_EOL
         ;
        }
     } # < count
@@ -723,11 +726,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   -----------------------------------------------------------------*/
   function rows_begin_json() {
     $this->_row_json = null;
-    return '['.PHP_EOL;
+    return '['.TABLE_EOL;
     return ''
       #."// database: ".$this->db->name."\n"
       #."// table: $this->name\n"
-    .'['.PHP_EOL;
+    .'['.TABLE_EOL;
   }
 
   function rows_rec_json(&$row) {
@@ -735,14 +738,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       $json = '';
       $this->_row_json = true;
     } else {
-      $json = ','.PHP_EOL;
+      $json = ','.TABLE_EOL;
     }
     return $json . json_encode($row);
   }
 
   function rows_end_json() {
     unset($this->_row_json);
-    return PHP_EOL.']'.PHP_EOL;
+    return TABLE_EOL.']'.TABLE_EOL;
   }
 
   /*-----------------------------------------------------------------
@@ -757,28 +760,28 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   -----------------------------------------------------------------*/
   function rows_begin_xml() {
     return ''
-      .'<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL #<?
-      #.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.PHP_EOL
-      .'<rows name="'.$this->name.'" database="'.$this->db->name.'" database-type="'.$this->db->type.'">'.PHP_EOL
+      .'<?xml version="1.0" encoding="utf-8"?>'.TABLE_EOL #<?
+      #.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.TABLE_EOL
+      .'<rows name="'.$this->name.'" database="'.$this->db->name.'" database-type="'.$this->db->type.'">'.TABLE_EOL
     ;
   }
 
   function rows_rec_xml(&$row) {
     $xml = '';
-    $xml .= "\t<row>".PHP_EOL;
+    $xml .= TABLE_INDENT."<row>".TABLE_EOL;
     foreach ($row as $k=>$v) {
       if ($v !== '') $xml .= ''
-        . "\t\t<".$k.">"
+        . TABLE_INDENT.TABLE_INDENT."<".$k.">"
         .'<![CDATA['.$v.']]>'
         . '</'.$k.'>'
-      .PHP_EOL;
+      . TABLE_EOL;
     }
-    $xml .= "\t</row>".PHP_EOL;
+    $xml .= TABLE_INDENT."</row>".TABLE_EOL;
     return $xml;
   }
 
   function rows_end_xml() {
-    return '</rows>'.PHP_EOL;
+    return '</rows>'.TABLE_EOL;
   }
 
   /*-----------------------------------------------------------------
@@ -787,11 +790,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   function rows_begin_csv($fields) {
 
     if ($this->p('header')==="0") return '';
-    return join("\t",array_keys($fields))."\n";
+    return join(TABLE_CSV_SEP,array_keys($fields))."\n";
   }
 
   function rows_rec_csv(&$row) {
-    return join("\t",array_values($row))."\n";
+    return join(TABLE_CSV_SEP,array_values($row))."\n";
   }
 
   function rows_end_csv() {
@@ -805,55 +808,55 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     $html = '';
 
-    #$html .= '<table data-role="table" class="ui-responsive rows '.$this->name.'">'.PHP_EOL;
-    $html .= '<table class="rows '.$this->name.'">'.PHP_EOL;
-    #if (!empty($opt)) $html .= '<caption>' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '</caption>' .PHP_EOL;
+    #$html .= '<table data-role="table" class="ui-responsive rows '.$this->name.'">'.TABLE_EOL;
+    $html .= '<table class="rows '.$this->name.'">'.TABLE_EOL;
+    #if (!empty($opt)) $html .= '<caption>' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '</caption>' .TABLE_EOL;
 
     if ($this->p('header')!=="0") {
-      $html .= '<thead>'.PHP_EOL.'<tr class="head">'.PHP_EOL;
+      $html .= '<thead>'.TABLE_EOL.'<tr class="head">'.TABLE_EOL;
 
-      if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.PHP_EOL;
+      if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.TABLE_EOL;
 
       foreach (array_keys($fields) as $f) {
-        #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.PHP_EOL;
-        $html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).'</th>'.PHP_EOL;
+        #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.TABLE_EOL;
+        $html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).'</th>'.TABLE_EOL;
       }
-      if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.PHP_EOL;
-      $html .= '</tr>'.PHP_EOL.'</thead>'.PHP_EOL;
+      if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.TABLE_EOL;
+      $html .= '</tr>'.TABLE_EOL.'</thead>'.TABLE_EOL;
     }
 
-    $html .= '<tbody>'.PHP_EOL;
+    $html .= '<tbody>'.TABLE_EOL;
     return $html;
   }
 
   function rows_rec_table(&$row) {
 
-    $html = '<tr class="row">'.PHP_EOL;
+    $html = '<tr class="row">'.TABLE_EOL;
 
     if ($this->p('buttons')!=='0') $html .=
       '<td class="action"><a class="edit button" href="'
         .$this->url_keys($row,'action=edit')
-      .'">'.DB_HTML_EDIT.'</a></td>'.PHP_EOL;
+      .'">'.DB_HTML_EDIT.'</a></td>'.TABLE_EOL;
 
     foreach ($row as $k => $v) {
       if (isset($this->extras[$k])) $k .= " extra";
-      $html .= '<td class="'.$k.'">'.$v.'</td>'.PHP_EOL;
+      $html .= '<td class="'.$k.'">'.$v.'</td>'.TABLE_EOL;
     }
 
     if ($this->p('buttons')!=='0') $html .=
       '<td class="action"><a class="delete button" href="'
         .$this->url_keys($row,'action=delete')
-      .'">'.DB_HTML_DELETE.'</a></td>'.PHP_EOL;
+      .'">'.DB_HTML_DELETE.'</a></td>'.TABLE_EOL;
 
-    $html .= '</tr>'.PHP_EOL;
+    $html .= '</tr>'.TABLE_EOL;
 
     return $html;
   }
 
   function rows_end_table($opt=array()) {
     $html = '';
-    $html .= '</tbody>'.PHP_EOL;
-    $html .= '</table>'.PHP_EOL;
+    $html .= '</tbody>'.TABLE_EOL;
+    $html .= '</table>'.TABLE_EOL;
     return $html;
   }
 
@@ -861,30 +864,30 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     Html Div
   -----------------------------------------------------------------*/
   function rows_begin_div() {
-    return '<div class="rows '.$this->name.'">'.PHP_EOL;
+    return '<div class="rows '.$this->name.'">'.TABLE_EOL;
   }
 
   function rows_rec_div(&$row) {
 
     $html = '';
 
-    $html .= '<ul class="row">'.PHP_EOL;
+    $html .= '<ul class="row">'.TABLE_EOL;
 
     if ($this->p('buttons')!=='0') {
-      $html .= '<li class="buttons">'.PHP_EOL;
-      $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.PHP_EOL;
-      $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>'.PHP_EOL;
-      $html .= '</li>'.PHP_EOL;
+      $html .= '<li class="buttons">'.TABLE_EOL;
+      $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.TABLE_EOL;
+      $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>'.TABLE_EOL;
+      $html .= '</li>'.TABLE_EOL;
     }
 
     foreach ($row as $k => $v) {
       $html .= '<li>'
         .( $k == '0' ? '' : '<label>'.prettyText($k).'</label>')
         .'<span class="'.$k.'">'.$v.'</span>'
-        .'</li>'.PHP_EOL;
+        .'</li>'.TABLE_EOL;
     }
 
-    $html .= '</ul>'.PHP_EOL;
+    $html .= '</ul>'.TABLE_EOL;
 
     return $html;
   }
@@ -914,7 +917,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     ;
 
     if (!($query = $this->db->conn->prepare($sql))) {
-      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
       return false;
     }
 
@@ -922,7 +925,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
 #debug(array($sql,$values));
     if (!($execute = $query->execute())) {
-      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
       return false;
     }
 
@@ -972,7 +975,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     $this->debug($sql,1);
 
     if (!($query = $this->db->conn->prepare($sql))) {
-      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
       return false;
     }
      
@@ -987,7 +990,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     #return $sql;
     #bye($sql);
     if (!($execute = $query->execute())) {
-      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+      err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
       return false;
     }
 
@@ -1080,7 +1083,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       $replace = array();
       foreach ($this->replace as $k=>$v) { $replace[] = "$k.$v"; }
-      $html .= '<span id="db-table-replace" style="display:none">'.join(' ',$replace).'</span>'.PHP_EOL;
+      $html .= '<span id="db-table-replace" style="display:none">'.join(' ',$replace).'</span>'.TABLE_EOL;
 
     }
 
index 83989195f47adc2220a913fd42b6c2a6017cba8b..40b4c43c4d305497926afb0f3e626ee540cde99c 100644 (file)
@@ -15,6 +15,7 @@ class Out extends Nb {
       'csv' => array(
         'enclose' => array("","\n"),
         'eol' => "\n",
+        'sep' => (self::p('sep') ? self::p('sep') : "\t"),
         'head' => 'out_csv_head',
         'function' => 'out_csv',
       ),
@@ -120,7 +121,7 @@ class Out extends Nb {
     print $conf['enclose'][1];
   }
 
-} # < Class
+} Out::types() ; # < Class
 /****************************************************************************/
 
 function out_yaml(&$row,$o) {
@@ -130,23 +131,10 @@ function out_yaml(&$row,$o) {
 }
 
 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() ;
+  echo join($o['sep'],array_values($row));
+}
 
 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);
@@ -154,7 +142,7 @@ function out_csv_head(&$row,$o) {
 
 #function out_tag_head(&$row,$o) { return ''; }
 function out_tag_head(&$data,$o) {
-  #return var_dump($row,true); return '';
+#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);
@@ -167,10 +155,10 @@ function out_tag_head(&$data,$o) {
 
 function out_tag(&$row,$o) {
 #var_dump($row); return '';
-  if (!is_array($row)) return var_export($row,true);
+# NB 07.03.16   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);
+# NB 07.03.16     if (!is_scalar($v)) return var_export($row,true);
     $k = empty($o['tag']) ? $k : $o['tag'];
     return ''
       .(OUT_EOL ? '  ' : '')