]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 7 Sep 2016 23:57:06 +0000 (00:57 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 7 Sep 2016 23:57:06 +0000 (00:57 +0100)
etc/profile.d/git.sh
lib/php/db.php
lib/php/db/index.php
lib/php/db/page.php
lib/php/db/shell.php
lib/php/nb.php
lib/php/out.php

index cc933de896a17401cd326c2068b0d0c4d4ef1519..08120015210018e9573c060b9623d9e736411eb3 100644 (file)
@@ -134,7 +134,6 @@ git_conn_check() {
   fi
   #[ "$1" = "TEST" ] &&
   dest=$(url2nc "$dest")
-  #dest="$dest 22"
 
   local ok
   case "$_git_conn_check" in
index 1599f7570ab25ceded4f12c8bc40fa924cc8347e..21512d3b3cafbf24fe6542241397cb00e1a747f9 100644 (file)
@@ -283,6 +283,7 @@ class Db extends nb {
   */
   public function table($name,$params=[]) {
     #if (!array_key_exists($name,$this->tables)) {
+    if (!$name) bye('empty name');
     if (empty($this->tables[$name])) {
       $this->tables[$name] = new Table($name,['db'=>$this]+$params);
     } elseif ($params) {
@@ -394,7 +395,7 @@ class Db extends nb {
       foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) {
         #debug($row);
         $name = current($row);
-        $t = $this->table($name,$row+['status'=>$row]);
+        $this->table($name,$row+['status'=>$row]); # add to this->tables too !
       }
 
     }
@@ -493,7 +494,7 @@ class Db extends nb {
   * @param [FILES] $files Files to load
   * @return ARRAY the new/existing value of $this->db
   */
-  public static function conf_dbs($files=[],&$first=false) {
+  public static function conf_load($files=[],&$first=false) {
     if (empty($files)) return [];
 
     # Load all files into a $h if #files is not a hash
@@ -515,6 +516,7 @@ class Db extends nb {
 
     }
     unset($yaml,$CONF);
+    if (!$h) return false;
 
     # Emulate hash pointer _import
     foreach ($h as $db=>$o) {
@@ -566,7 +568,7 @@ class Db extends nb {
     $id = self::p('db');
 
     # Load databases
-    $conf = self::is_hash($conf) ? $conf : self::conf_dbs($conf);
+    $conf = self::is_hash($conf) ? $conf : self::conf_load($conf);
 
     # Check db=pdo
     if ($id and preg_match('/^(\w+):(.*)/',$id,$m)) {
index b6656f48fbe03b2c2b3d36cb022b65669b4135b6..f2bb056241e6c1bda1bc00d28821b68dd5f25101 100755 (executable)
@@ -15,8 +15,8 @@ Db::pinit();
 Db::pdef('format',out::client_type());
 
 if(defined('DB_NO_INIT') and DB_NO_INIT) return true;
-#bye(Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )));
-#$conf = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf);
+#bye(Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )));
+#$conf = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf);
 Db::init([Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml']);
 
 if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true;
index 9421f5f07942f4ffbac15c4654af0d6498b2e2f6..027e452f3eb134ea1ae4381df294b7983d5dc613 100644 (file)
@@ -1,6 +1,9 @@
 <?php
 require_once(dirname(__FILE__).'/../config.php');
 require_once(dirname(__FILE__).'/../page.php');
+#$o = new out();
+#bye($o->type);
+
 $Page = new Page([
   #'title' => 'Test',
   'content_type' => ((Page::php_cli() and !Page::p('web') )? 'text/plain' : 'text/html'),
index 3a7a2c6e1249e151fae1fc7fb0812b149b0eda19..32339c9304f9803bb6d9a253f399389092840f59 100755 (executable)
@@ -8,7 +8,7 @@ require_once(dirname(__FILE__).'/../db.php');
 #
 # Default params
 #
-Db::pinit();
+argv2request();
 
 # Set format from client Accept if != html
 #bye(out::client_type());
@@ -17,71 +17,84 @@ Db::pdef('format',out::client_type());
 #
 # Confs
 #
-$Confs = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ));
+$Confs = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ));
 
 #
 # Parse stdin
 #
 $Db = null;
 $Table = null;
-#bye(Db::conf_search_db($Conf['ui']));
 
+$no_reset_params = [
+  'db',
+  'table',
+  'format',
+];
 #bye($Confs['ui']);
 while($line = fgets(STDIN)){
+  $line = trim($line);
+
+  # Ignore comment
+  if (preg_match('/^\s*#/',$line)) continue;
+
+  # Exit command
+  if ($line == 'quit' or $line == 'q') break;
 
   # Set params
-  #if (preg_match('/^\s*([^=]+)\s*=\s*(.*?)\s*$/',$line,$args)) {
-    #echo "IGNORE: $line";
-  while ($line and preg_match('/^\s*([^=]+)\s*=\s*(.*?)\s*$/',$line,$args)) {
+  while ($line and (0
+    #or preg_match('/^([\w\.]+)=("[^"]+"|\S+)/',$line,$args)
+    or preg_match("/^([\w\.]+)='([^']+)'/",$line,$args)
+    or preg_match('/^([\w\.]+)="([^"]+)"/',$line,$args)
+    or preg_match('/^([\w\.]+)=(\S+)/',$line,$args)
+  )) {
+  # (?:(?:[^()]++|"(?R)")*)
+    $line = mb_substr($line,mb_strlen($args[0])+1);
+    #debug($args);
 
     array_shift($args);
-    #var_dump($args);
-    Db::pset($args[0],$args[1]);
-    $line = mb_substr($line,mb_strlen($line)-1);
+    list ($k,$v) = $args;
 
-  }
+    Db::pset($k,$v);
+    Db::pinit();
 
-  Db::pinit();
-  if (!Db::p('action')) continue;
-  if (true and !db_shell_init($Confs)) {
-    echo "ERR: $line\n";
-    continue;
-  }
+    switch ($k) {
 
-  #Db::bye(Db::p());
-  #var_dump(Db::p());
-  #echo ">>".$Db->p('action')."\n";
+      case 'action':
 
-  $r = $Db->action($Db->p('action'),$Table);
-  #foreach ($args
+        if ($v == 'reset') {
+          $keep = [];
+          foreach (Db::p() as $k => $v) { if (in_array($k,$no_reset_params)) $keep[$k] = $v; }
+          $_REQUEST = [];
+          foreach ($keep as $k => $v) { Db::pset($k,$v); }
+          $keep = [];
+          break;
+        }
 
+        $Db->action($Db->p('action'),$Table);
+        break;
 
-}
+      case 'db':
+#bye($Confs[$v]);
+        $Db = new Db($Confs[$v]+['conf'=>$Confs]);
+        $Db->connect();
+        break;
 
-function db_shell_init($confs) {
-  global $Db, $Table;
+      case 'table':
+        $Table = new Table($v);
+        break;
 
-  static $p_db = null;
-  if (Db::p('db') and (
-    $p_db === null or $p_db !== Db::p('db')
-  )) {
-    #if (!empty(
-    unset($Table);
-    unset($DB);
-    return Db::init($confs);
-    if (!($conf = Db::conf_search_db($confs))) return;
-#unset($conf['dbs']); unset($conf['conf']); bye($conf);
-    #bye($conf);
-    $Db = new Db($conf);
-    $p_db = Db::p('db');
-  }
-  #$r = $Db->action($Db->p('action'),$Table);
-  #bye(var_dump(Db::p(),true));
+      case 'format':
+        break;
 
-  static $p_table = null;
-  if ($p_table === null) $p_table = Db::p('table');
+      default:
+        echo "ERR: $k=$v\n";
+        Db::pset($k,null);
+        break;
 
-  bye([$p_db,$p_table]);
+    }
+
+  }
 
 }
+
 ?>
index 4d8c83c28a00b06ce929efd1e26b635b59a76bfd..ca5abe8e2ffa797dcda40080fb07e44853c77b64 100644 (file)
@@ -8,6 +8,7 @@ if (!function_exists('yaml_emit')) {
 }
 require_once(realpath(dirname(__FILE__).'/functions.php'));
 #$nb = new NB(); debug($nb->test());
+#if (!defined('NB_PARAMS')) define('NB_PARAMS',$_REQUEST);
 if (class_exists('NB')) return;
 
 class NB {
@@ -237,9 +238,14 @@ class NB {
    * Function: argv2request
    * Set $_REQUEST from argv
    */
-  public static function argv2request() {
+  public static function argv2request($args=null) {
 
-    global $argv;
+    if (isset($args)) {
+      $argv = $args;
+      $args = [];
+    } else {
+      global $argv;
+    }
 
     $new_argv = array();
    
index 37c63ce8a3aa0f9374ac5eadefbfa2b51c73416e..fb56deb73c2de1508337473effc9b8a181274259 100644 (file)
@@ -512,10 +512,14 @@ function out_human(&$row,&$o) {
   if (empty($GLOBALS['_human_len'])) {
     $GLOBALS['_human_len'] = [];
     if (empty($o['_human'])) $o['_human'] = [];
+    $o['tmpfile'] = tmpfile();
+
+    # switch for later use of out_csv()
     $o['sep.human'] = $o['sep'];
     $o['sep'] = "\t";
+
+    # no echo with out_csv()
     $o['return'] = true;
-    $o['tmpfile'] = tmpfile();
   }
 
   $line = out_csv($row,$o);
@@ -546,8 +550,9 @@ function out_human_end(&$o) {
 
   $sep_line = '';
   if (out::p('sep_line') !== '0') {
+
     foreach ($len as $i) {
-      $sep_line .= '+'.str_repeat('-',$i+2);
+      $sep_line .= '+'.str_repeat('-',$i+strlen($o['sep.human'])-1);
     }
     $sep_line .= '+'."\n";
   }