]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 18 Sep 2016 23:55:58 +0000 (00:55 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 18 Sep 2016 23:55:58 +0000 (00:55 +0100)
etc/dbs.php
etc/profile.d/php.sh
etc/vim/source/map.vim
lib/php/db.php
lib/php/db/shell.php
lib/php/db/table.php
lib/php/out.php

index 9e690ca86bd019942fdb341e186f91d293585cdb..173415b5a71601dd6e37b2ff3c29ac117b6ce3df 100644 (file)
@@ -106,10 +106,6 @@ return 1;
 function conf_merge(&$c1,&$c2) {
 
   if (!empty($c1['tables'])) {
-  #debug('zaza');
-    #$c2['tables'] = array_merge($c2['tables'],$c1['tables']);
-    #$c2['tables']
-    #$c1['tables']
     if (empty($c1['tables'])) $c1['tables'] = [];
     foreach ($c1['tables'] as $k=>$v) {
       #debug($k);
@@ -120,6 +116,7 @@ function conf_merge(&$c1,&$c2) {
   foreach ([
     'default_table',
     'title',
+    'row_parse',
     #'_import',
   ] as $k) if (!empty($c1[$k]))
     $c2[$k] = $c1[$k];
index cdab7ed8e297cde3410fcc46b99ba8304a02557e..71845bb76619fd1e988bbccc708b8fc46bcf7ea4 100644 (file)
@@ -7,5 +7,5 @@ php425() {
   perl -e '$_=join("",<>); s/\barray\(((?:[^()]++|\((?R)\))*)\)/[$1]/sg; print $_; ' $@
 }
 
-function php_check_file() { php5 --define error_reporting=22519 --define display_errors=1 --define log_errors=1 --define html_errors=0 $@; }
+function php_check() { php5 --define error_reporting=22519 --define display_errors=1 --define log_errors=1 --define html_errors=0 $@; }
 
index 7fcacc45454011fe8ff3ae4be261676774c911ea..d440e1d72ba87dc427df48f86afa208b912a4e6b 100644 (file)
@@ -5,6 +5,7 @@
 "
 " imap <Tab> :Z
 " set encoding=utf-8
+" See: http://vimdoc.sourceforge.net/htmldoc/insert.html
 "
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
@@ -39,8 +40,10 @@ map <F4> :update <CR>:!~/.vim/checkspell.sh %<CR>:e<CR>
 
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " F5 - Comment
-map <F5> o<C-R>=FileGet('c').' - '.FileGet('me_now').FileGet('cEnd')<CR><Esc>^<CR>b^wi
-" - NB 18.04.13
+map <F5> o<C-R>=FileGet('c').' '.FileGet('me_now').': foo '.FileGet('cEnd')<CR><Esc>?foo<CR>cw
+" NB 19.09.16
+
+" NB 18.04.13
 " vmap <F5> :s/^/\=VimShortcurtsAuto('comment_line_toggle').' '/<CR>
 " vmap <F5> :call NERDComment(1, "invert")<CR>
 vmap <F5> :call FileCommentToggle()<CR>
index e862fd58fc26735ee87e72d107a5ac84314de400..6a842204f17501f5f6a729a76ae56fc338a8dc97 100644 (file)
@@ -39,15 +39,15 @@ class Db extends nb {
 
   # Params
   protected static $paliases = [
-    'd' => 'db',
-    't' => 'table',
-    'l' => 'limit',
-    'a' => 'action',
-    'h' => 'header',
+    'd'   => 'db',
+    't'   => 'table',
+    'l'   => 'limit',
+    'a'   => 'action',
+    'h'   => 'header',
 
-    'f' => 'format',
+    'f'   => 'format',
     'out' => 'format',
-    'o' => 'format',
+    'o'   => 'format',
   ];
 
   protected static $action_aliases = [
@@ -697,6 +697,22 @@ class Db extends nb {
     },$pdo);
   }
 
+  public function tables2csv() {
+    $this->pset('format','csv');
+    # Filters
+    $name = self::p('name',self::p('table',''));
+    $sep = self::p('sep',"\t");
+    foreach ($this->tables() as $t) {
+      if (
+        ($t->type != 'table')
+        or (!empty($name) and !$this->str_match($t->name,$name))
+      ) continue;
+      #$this->pset('table.rows_preffix',$t->name);
+      $o = ['preffix'=>'::'.$t->name.'::'.$sep];
+      $o = [];
+      $t->rows($o);
+    }
+  }
   public function dump() { return $this->sql(true); }
 
   public function sql($insert=null) {
@@ -714,7 +730,7 @@ class Db extends nb {
     # Tables param filter
     $tables = $this->tables();
 
-    # Cache before changing db type
+    # Caching before changing db type
     $views = [];
     foreach ($tables as $k=>$t) {
 
index bf99f8403b6f9f6aaf22f040610edb8f08f172c2..a318204309f40b84d5e63c3c0f678bb6074093dd 100755 (executable)
@@ -7,7 +7,6 @@ require_once(dirname(__FILE__).'/config.php');
 #
 
 # Set format from client Accept if != html
-#bye(out::client_type());
 Db::pdef('format','human');
 
 #
index b6534041d48adfde0a31271b5eb46ba4fdf6b426..c4bd3df9e116b6b46d0530d88a7e08b1fe4af8be 100644 (file)
@@ -783,23 +783,20 @@ Class Table extends nb {
 
     if ($opt['is_html']) out::type('html');
     $count = 0;
-    $extras = self::p('extras') === '0' ? 0 : 1;
-    #bye($extras);
+
+    # Parser on/off (default: on)
+    $parse = self::p('table.parse') === '0' ? 0 : 1;
+    #bye($parse);
     while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
 
       $count++;
       $call = null;
 
-      # Delete char 0 (from wordpress)
-      foreach ($row as $k=>$v) {
-        $row[$k] = str_replace(chr(0),'',$v);
-      }
-
-      if ($extras and !empty($this->db()->row_parse)) {
+      if ($parse and !empty($this->db()->row_parse)) {
         $call = $this->db()->row_parse; $call($row);
       }
 
-      if ($extras and !empty($this->row_parse_pre)) {
+      if ($parse and !empty($this->row_parse_pre)) {
         $call = $this->row_parse_pre; $call($row);
       }
 
@@ -810,10 +807,15 @@ Class Table extends nb {
 
       }
 
-      if ($extras and !empty($this->row_parse_post)) {
+      if ($parse and !empty($this->row_parse_post)) {
         $call = $this->row_parse_post; $call($row);
       }
 
+      #
+      # Preffix
+      #
+      if (!empty($opt['preffix'])) echo $opt['preffix'];
+
       #
       # Head
       #
@@ -831,7 +833,7 @@ Class Table extends nb {
         }
 
         if ($out_conf) {
-          out::head($out_conf,$this->fields(),array($row));
+          out::head($out_conf,$this->fields(),[$row]);
 
         } else {
           echo $this->{"rows_begin_$format"}($this->fields());
@@ -1036,14 +1038,6 @@ Class Table extends nb {
     return NB_EOL.']'.NB_EOL;
   }
 
-  /*-----------------------------------------------------------------
-    Yaml
-  -----------------------------------------------------------------*/
-  public function rows_begin_yaml() { return "---\n"; }
-  #public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
-  public function rows_rec_yaml(&$row) { $o=['row'=>'out_yaml'];out::row($o,$row); }
-  public function rows_end_yaml() { return ''; }
-
   /*-----------------------------------------------------------------
     Xml
   -----------------------------------------------------------------*/
@@ -1570,9 +1564,6 @@ Class Table extends nb {
       // Format
       $r .= '<span class="label">';
 
-    #$criteria[] = html_select_array(ar_map('array($a == "html" ? "" : $a,strtoupper($a))',$this->formats));
-  # NB 01.03.16     $r .= '<label for="format">Format</label>'.html_select_array(array(
-  # NB 01.03.16       'table','div','csv','xml','json','yaml'
       $r .= '<label for="format">Format</label>'.html_select_array($this->db()->formats,array(
         'html'       => 'class="format" name="format" id="format"',
         'selected'   => $this->p('format'),
index 33e651e2dc45cf8aab3f0ead9f38ee115889e3e1..04e0f832a78d192191d9890052637b6f8a9743a8 100644 (file)
@@ -8,7 +8,6 @@ Class Out extends Nb {
   protected static $type;
   public static function init() {
 
-    $self = get_class();
     self::$types = array(
 
       'human' => [
@@ -21,30 +20,29 @@ Class Out extends Nb {
         'end' => 'out_human_end',
       ],
 
-      'sql' => array(),
+      'sql' => array(), # Code still in db/table.php !
 
       'sh' => array(
-        'row' => function(&$r) {
+        'preff' => self::p('preff',""),
+        'row' => function(&$o,&$r) {
           $line = [];
           foreach ($r as $k=>$v) {
             if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
             $v = str_replace('"','\\"',$v);
-            $line[] = "$k=\"$v\"";
+            $line[] = $o['preff']."$k=\"$v\"";
           }
           if ($line) echo join(' ',$line).";\n";
         }
       ),
 
-      #'php' => array( 'row' => function(&$r) {var_dump($r);},),
       'php' => array(
-        #'enclose' => array('$VAR=array(',');'),
-        'enclose' => array('$CONF = array('.NB_EOL,NB_EOL.');'),
+        'enclose' => [
+           '<?php'.(NB_EOL ? NB_EOL:' ') . '$CONF = array('.NB_EOL
+          ,NB_EOL.');' . (NB_EOL ? NB_EOL:' ').'?'.'>'.NB_EOL
+        ],
         'eol' => self::p('eol',NB_EOL),
         'rec' => ',',
-        #'row' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));},
-        #'row' => function(&$r) {print_r($r);}
-        #'row' => function(&$r) {var_dump($r);}
-        'row' => function(&$r) {
+        'row' => function(&$o,&$r) {
           return var_export($r);
           echo preg_replace(array(
             '/(=>\s*)\n\s+/m',
@@ -54,12 +52,10 @@ Class Out extends Nb {
             "),".NB_EOL."array"
           ),var_export($r,true));
         }
-        #'row' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
-        #'row' => function(&$r) {var_export($r);}
       ),
-      #'var_dump' => array( 'row' => function(&$r) {var_dump($r);},),
-      #'print_r' => array( 'row' => function(&$r) {print_r($r);},),
-      #'var_export' => array( 'row' => function(&$r) {var_export($r);},),
+      'php_var_dump' => array( 'row' => function(&$o,&$r) {var_dump($r);},),
+      'php_print_r' => array( 'row' => function(&$o,&$r) {print_r($r);},),
+      'php_var_export' => array( 'row' => function(&$o,&$r) {var_export($r);},),
 
       'div' => array(
         'is_html' => true,
@@ -93,7 +89,7 @@ Class Out extends Nb {
       'xml' => array(
         'enclose' => array('<?xml version="1.0" encoding="utf-8"?'.'>'.NB_EOL."<rows>".NB_EOL,"</rows>".NB_EOL),
         'eol' => self::p('eol',NB_EOL),
-        'row' => function (&$row,$o) {
+        'row' => function (&$o,&$row) {
           echo ($o['eol'] ? '  ' : '')."<row>".$o['eol'];
           foreach ($row as $k => $v) {
             echo ($o['eol'] ? '    ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".$o['eol'];
@@ -105,30 +101,18 @@ Class Out extends Nb {
       'txt' => array(
         'col' => self::p('col',' : '),
         'sep' => self::p('sep',"\n"),
-        'eol' => self::p('eol',">\n"),
+        'eol' => self::p('eol',"\n"),
       ),
 
       'csv' => array(
         'sep' => self::p('sep',"\t"),
         'eol' => self::p('eol',"\n"),
         'enclose' => array("","\n"),
-        'head' => function(&$o,&$row) {
-          if (!is_array($row)) echo 0;
-          echo "\r";
-          if (out::is_hash($row)) {
-            $ar = array_keys($row);
-            return out_csv($ar,$o);
-          } else {
-            return out_csv($row,$o);
-          }
-        },
-
-        'row' => 'out_csv',
       ),
 
       'yaml' => [
         'enclose' => ["---\n",""],
-        'row' => function (&$row,&$o) {
+        'row' => function (&$o,&$row) {
           $yaml = '- '.out::yaml_encode($row);
           $yaml = preg_replace("/^(?!-)/m","  ",$yaml);
           echo $yaml;
@@ -137,7 +121,7 @@ Class Out extends Nb {
 
       'json' => array(
         'enclose' => array('['.NB_EOL,NB_EOL.']'),
-        'row' => function($row) { return json_encode($row); },
+        'row' => function(&$o,&$row) { return json_encode($row); },
         'eol' => self::p('eol',NB_EOL),
         'rec' => ',',
       ),
@@ -194,32 +178,41 @@ Class Out extends Nb {
     return is_scalar($v) ? $v : json_encode($v);
   }
 
-  public static function type_call($k,&$o,&$row) {
+  public static function type_call($k,&$o,&$row=[]) {
 
-    $type = self::type();
+    static $methods;
 
     if (!isset($o[$k])) {
 
-      # Try internal method
-      $call = self::type() ? self::type()."_$k" : '';
-      $call = str_replace('_row','',$call);
-      #if ($call and in_array($call,get_class_methods(get_class()))) return call_user_func_array([get_class(),$call],$params);
-      #if ($call and in_array($call,get_class_methods(get_class()))) debug($call);
-      if ($call and in_array($call,get_class_methods(get_class()))) return self::$call($o,$row);
+      # Cache methods
+      if (!isset($methods))
+        foreach (get_class_methods(get_class()) as $m) $methods[$m]=1
+      ;
+
+      # Try internal function
+      $call = $k.'_'.self::type();
+      if (!$call or empty($methods[$call])) return false;
+      echo self::$call($o,$row);
 
-    } elseif(is_callable($o[$k]) and is_callable($o[$k])) {
+    # External function
+    } elseif(is_callable($o[$k])) {
 
       $call = $o[$k];
       echo $call($o,$row);
-      return '';
 
+    # String
+    } elseif(is_scalar($o[$k])) {
+      echo $o[$k];
+
+    } else {
+      return false;
     }
 
-    return '';
+    return true;
 
   }
 
-  public static function head(&$o,$head,$data=array()) {
+  public static function head(&$o,$head,$data=[]) {
     $o['header'] = (self::p('header')==='0') ? false : true;
 
     # Format $head
@@ -265,25 +258,15 @@ Class Out extends Nb {
 
   public static function row(&$o,&$row) {
 
-    #self::type_call('row',$o,$row);
-    # Defined function
-    if (isset($o['row'])) {
-
-      if (is_scalar($o['row']) and preg_match('/^out_/',$o['row'])) {
-        $o['row']($row,$o);
-      } else {
-        echo $o['row']($row,$o);
-      }
-      return true;
-    }
+    if (self::type_call('row',$o,$row)) return true;
 
     # Assume html/xml tag style
     if (isset($o['tag'])) {
-      return out_tag($row,$o);
+      return out_tag($o,$row);
     }
 
     # txt
-    return self::txt($row,$o);
+    return self::row_txt($o,$row);
 
     return true;
   }
@@ -294,7 +277,8 @@ Class Out extends Nb {
   }
 
   public static function end(&$o) {
-    if (isset($o['end'])) echo $o['end']($o);
+    #if (isset($o['end'])) echo $o['end']($o);
+    self::type_call('end',$o);
     if (isset($o['enclose'])) echo $o['enclose'][1];
   }
 
@@ -330,8 +314,6 @@ Class Out extends Nb {
     $head = self::head($conf,$head,$data);
 
     foreach ($data as $row) {
-      if ($count>0) self::concat($conf);
-      $count++;
 
       # Transform simple array into hash
       if (isset($row[0]) and $head) {
@@ -347,8 +329,9 @@ Class Out extends Nb {
 
       }
 
+      if ($count>0) self::concat($conf);
+      $count++;
       self::row($conf,$row);
-      #echo "   ($count)";
 
     }
 
@@ -421,8 +404,9 @@ Class Out extends Nb {
     return $v;
   }
 
-  public static function txt(&$row,&$o) {
-    $header = (out::p('header')==='0') ? false : true;
+  public static function row_txt(&$o,&$row) {
+    $header = (self::p('header')==='0') ? false : true;
+    if (!isset($o['col'])) $o['col'] = '';
     foreach ($row as $k => $v) {
 
       if (!$header) {
@@ -441,21 +425,31 @@ Class Out extends Nb {
     }
   }
 
-} Out::init() ; # < Class
-/*--------------------------------------------------------------------------*/
-// Functions
-/*--------------------------------------------------------------------------*/
+  public static function head_csv(&$o,&$row) {
+    if (!is_array($row)) echo 0;
+    if (self::is_hash($row)) {
+      $ar = array_keys($row);
+      return self::row_csv($o,$ar);
+    } else {
+      return self::row_csv($o,$row);
+    }
+  }
 
-function out_csv(&$row,$o) {
-  $values = array();
+  public static function row_csv(&$o,$row) {
+    $values = [];
 
-  foreach (array_values($row) as $k=>$v) {
-    $values[] = str_replace("\n",'\\n',out::scalar($v));
+    foreach (array_values($row) as $k=>$v) {
+      $values[] = str_replace("\n",'\\n',out::scalar($v));
+    }
+
+    if (!empty($o['return'])) return join($o['sep'],$values);
+    echo join($o['sep'],$values);
   }
 
-  if (!empty($o['return'])) return join($o['sep'],$values);
-  echo join($o['sep'],$values);
-}
+} Out::init() ; # < Class
+/*--------------------------------------------------------------------------*/
+// Functions
+/*--------------------------------------------------------------------------*/
 
 //
 // Tag
@@ -472,10 +466,10 @@ function out_tag_head($o,&$data) {
 
   # Avoid: <th class="0">name</th>
   if (!out::is_hash($row)) $row =  array_combine(array_values($row),array_values($row));
-  out_tag($row,$o);
+  out_tag($o,$row);
 }
 
-function out_tag(&$row,&$o) {
+function out_tag(&$o,&$row) {
   if (isset($o['tag_enclose'])) echo (NB_EOL ? '  ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL;
 
   foreach ($row as $k => $v) {
@@ -508,7 +502,7 @@ function out_tag(&$row,&$o) {
 //
 // Human
 //
-function out_human_head(&$o,&$head) {
+function out_human_head(&$o,$head) {
   #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
 
   if (out::p('header')==='0') return '';
@@ -516,59 +510,62 @@ function out_human_head(&$o,&$head) {
     'head' => 1,
   ];
   if (out::is_hash($head)) $head = array_keys($head);
-  return out_human($head,$o);
+  return out_human($o,$head);
 }
 
-function out_human(&$row,&$o) {
+function out_human(&$o,&$row) {
 
-  # switch options
-  if (!isset($o['mark.human'])) {
-    if (empty($o['_human'])) $o['_human'] = [];
-    $o['mark.human'] = 1;
+  # Init options
+  if (empty($o['_human'])) $o['_human'] = [];
+  if (!isset($o['_human']['len'])) {
+    $o['_human']['len'] = [];
 
-    $o['sep.human'] = $o['sep'];
+    $o['_human']['sep'] = $o['sep'];
     $o['sep'] = "\t";
 
-    $o['eol.human'] = $o['eol'];
+    $o['_human']['eol'] = $o['eol'];
     $o['eol'] = "";
-  }
 
-  if (empty($GLOBALS['_human_len'])) {
-    $GLOBALS['_human_len'] = [];
     $o['tmpfile'] = tmpfile();
 
-    # no echo with out_csv()
+    # no csv echo call
     $o['return'] = true;
+    $o['_human']['len'] = [];
   }
 
-  $line = out_csv($row,$o);
+  $line = out::row_csv($o,$row);
+
+  # Search max length string
   $i = 0;
   foreach (explode($o['sep'],$line) as $v) {
     $len = mb_strlen($v,out::$charset);
+
     if (0
-      or empty($GLOBALS['_human_len'][$i])
-      or $len > $GLOBALS['_human_len'][$i]
-    ) $GLOBALS['_human_len'][$i] = $len;
+      or empty($o['_human']['len'][$i])
+      or $len > $o['_human']['len'][$i]
+    ) $o['_human']['len'][$i] = $len;
+
     $i++;
   }
 
-  #debug($line);
+  if (!isset($o['tmpfile'])) {
+    echo "\n";
+    out::bye("Option tmpfile is missing, wrong uage of this funtion!");
+  }
   fwrite($o['tmpfile'],$line."\n");
 }
 
 function out_human_end(&$o) {
-#debug($GLOBALS['_human_len']);
-  $len = $GLOBALS['_human_len'];
-  #bye($len);
+  $len = $o['_human']['len'];
   fseek($o['tmpfile'], 0);
 
   $sep_line = '';
   if (out::p('sep_line') !== '0') {
 
     foreach ($len as $i) {
-      $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['sep.human'])-1);
+      $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1);
     }
-    $sep_line .= $o['corner'].$o['eol.human'];
+    $sep_line .= $o['corner'].$o['_human']['eol'];
   }
 
   $header = empty($o['_human']['head']) ? 0 : 1;
@@ -578,29 +575,24 @@ function out_human_end(&$o) {
     $values = [];
     $i = 0;
     foreach (explode($o['sep'],rtrim($line,"\n")) as $v) {
-      #if ($v=='Comunicación') $values[] = str_pad($v, $len[$i]+1, ' ', STR_PAD_LEFT);
       $values[] = out::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT);
       $i++;
     }
 
     # header
-    if (!$count) {
-      echo $sep_line;
-    }
+    if (!$count) echo $sep_line;
 
-    #debug($count);
     if ($header or $count>0) echo ''
-      . ltrim($o['sep.human'])
-      . join($o['sep.human'],$values)
-      . rtrim($o['sep.human'])
-    .$o['eol.human'];
+      . ltrim($o['_human']['sep'])
+      . join($o['_human']['sep'],$values)
+      . rtrim($o['_human']['sep'])
+    .$o['_human']['eol'];
 
     if ($header and !$count) echo $sep_line;
     $count++;
   }
 
   fclose($o['tmpfile']);
-  unset($GLOBALS['_human_len']);
   unset($o['_human']);
 
   if ($header) $count--;