]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Dec 2016 00:39:07 +0000 (00:39 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Dec 2016 00:39:07 +0000 (00:39 +0000)
lib/php/db.php
lib/php/db/field.php
lib/php/db/init.php
lib/php/db/table.php
lib/php/functions.php
lib/php/nb.php
lib/php/page.php
www/dbq/html/index.php

index 97f0a713d03843bea88216b277463561815973b9..4c9c744323108ca59585e3975d227de80f413a57 100644 (file)
@@ -118,6 +118,7 @@ class Db extends nb {
   public $default_table;
   public $sort;
   public $extras;
+  public $format = 'json';
   public $formats = [ 'table','div','csv','xml','json','yaml','sh' ];
   public $limits = ['10','20','50','100','500','1000'];
 
@@ -545,7 +546,19 @@ class Db extends nb {
 
   public function out($rows,$head=[]) {
 
-    if ($this->p('format') == 'null') return true;
+    if ($this->format) {
+      $format = $this->format;
+
+    } elseif ($this->p('format') == 'null') {
+      return true;
+
+    } elseif ($format=$this->p('format')) {
+
+    } else {
+      # NB 04.12.16: TODEL one day ! 
+      $format = $this->out->php_cli() ? 'csv' : 'table';
+    }
+
     // Handle limit
     if ($this->p('limit')) {
 
@@ -559,7 +572,7 @@ class Db extends nb {
 
     }
 
-    $this->out->rows($this->p('format',$this->out->php_cli() ? 'csv' : 'table'),$rows,$head);
+    $this->out->rows($format,$rows,$head);
     return true;
   }
 
@@ -818,7 +831,6 @@ class Db extends nb {
     # Db
     #
     $Db = new self();
-    if (empty($Db)) return false;
 
     #
     # Params
index e1b14f248fb9ab5843e7628bf1a516066986d8d6..9d543be330173a4b77dd9bccccc55b62b6e25562 100644 (file)
@@ -93,7 +93,7 @@ class field extends nb {
   }
 
   public function db() {
-    if (!empty($this->table) and !empty($this->table->db)) return $this->table->db();
+    if (!empty($this->table) and !empty($this->table->db())) return $this->table->db();
     global $Db;
     return ( isset($Db) ? $Db : null );
   }
index b3dd1650e331a06e24727b8262c67155811e5abd..f4279ab42bf01e26246a2a4bddb98c86a1dbeca1 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-require_once(realpath(dirname(__FILE__).'/../config.php'));
+# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../config.php'));
 # NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../db.php'));
 
 require_once(realpath(dirname(__FILE__).'/config.php'));
index bfee51509c396ab2d280bc5af6277dd651602837..fdcca9a45fd0b30b60704ce8d3618222724f2380 100644 (file)
@@ -790,7 +790,11 @@ Class Table extends nb {
     } else {
 
       $format = empty($opt['format']) ? $this->p('format') : $opt['format'];
-      if (!$format) bye("Parameter `format` missing!");
+      if (!$format and $this->db()->format) {
+        $format = $this->db()->format;
+        #$this->pset('format',$format);
+      }
+      if (!$format) $this->bye("Parameter `format` missing!");
     }
 
     list($sql,$where,$limit,$select_count) = $this->rows_sql($opt);
@@ -1850,6 +1854,8 @@ Class Table extends nb {
 
       if (empty($key)) {
         if (!self::p($k)) continue;
+      } elseif(is_array($key)) {
+        if (!in_array($k,$key)) continue;
       } else {
         if ($k!=$key) continue;
       }
@@ -1858,7 +1864,7 @@ Class Table extends nb {
       if (is_array($this->status[$k])) $this->status[$k] = count($this->status[$k]);
     }
 
-    if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
+    if (!empty($key) and is_scalar($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
     return $this->status;
   }
 
index abc718671d6af63cd64faf82b89a27598f9b9980..b23024811f756e46635bd7b2d3e22b101da78213 100644 (file)
@@ -62,11 +62,13 @@ function txt2md($txt) {
 
 function bye($msg='__bye__',$backtrace_deep=0) {
   #if ($msg) err($msg,'bye',$backtrace_deep === 0 ? 1 : $backtrace_deep);
+
   if ($msg!=='__bye__') {
     #throw new Exception($msg);
     err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep ));
     exit(1);
   }
+
   #if ($msg) err($msg,'bye',1+$backtrace_deep);
   exit;
   #die("Can't exit from BYE!!!");
@@ -79,13 +81,16 @@ function warn ($msg) {
 function err($msg='__err__',$preff='err',$backtrace_deep=0) {
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
   $preff_msg = $preff ? strtoupper($preff).': ' : '';
+
   if ($msg !== '__err__' and $backtrace_deep !== false) {
     $msg = trim($preff_msg.$msg).' '
       .nb::debug_backtrace_msg(1+$backtrace_deep,NULL,$preff_msg)
       #.nb::debug_backtrace_msg(1+$backtrace_deep,NULL)
     ;
+
   } else { 
     $msg = $preff_msg.$msg;
+
   }
 
   #nb::msg( !nb::php_cli()
index e9a06cd33ff702bdd6dd0f952d24b489e7ffe869..85090a2883489b7625605260aa8a3cfeab71999f 100644 (file)
@@ -17,6 +17,8 @@ class NB {
   protected static $content_types_aliases = array(
     'text' => 'txt',
     'human' => 'txt',
+    'table' => 'html',
+    'div' => 'html',
   );
 
   protected static $content_types = array(
@@ -409,7 +411,9 @@ class NB {
       E_DEPRECATED  => 'E_DEPRECATED ERROR',
       E_USER_DEPRECATED  => 'E_USER_DEPRECATED ERROR',
     );
-    if (isset($call_info['type']) and isset($errorType[$call_info['type']])) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
+    if (isset($call_info['type'])
+      and isset($errorType[$call_info['type']])
+    ) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
     #else ($call_info['type']) $call_info['type'] = $call_info['type'];
 
     // Default values
@@ -418,17 +422,19 @@ class NB {
     ;
 
     // Get infos
-    $msg = array();
-    foreach (array(
+    $msg = [];
+    foreach ([
       'file',
       'line',
       'function',
       'type',
       'message',
       'code',
-    ) as $i) {
+    ] as $i) {
 
-      if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i];
+      if (!array_key_exists($i,$call_info)) continue;
+      if ($i == 'type' and preg_match('/^\W\W/',$call_info[$i])) continue;
+      $msg[] = "$i=".$call_info[$i];
 
     }
 
index 2d3f32cdf4cbdcf90bf40efbadc6f38c96ffab70..57b6d0dd2473cc1258a959d12990838a11e1c7b5 100644 (file)
@@ -179,6 +179,9 @@ class Page extends nb {
    * Function: tag
    * Return html code for a tag
    */
+  public static function h1($content='',$attrs='') { return self::tag('h1',$content,$attrs); }
+  public static function div($content='',$attrs='') { return self::tag('div',$content,$attrs); }
+  public static function pp($content='',$attrs='') { return self::tag('p',$content,$attrs); }
   public static function tag($tag,$content='',$attrs='') {
     static $is_html = null;
     if ($is_html === null) $is_html = preg_match('/ml$/',self::content_type()) ? true : false;
@@ -456,6 +459,7 @@ class Page extends nb {
     if ($is == 'xhtml') return preg_match('/xhtml$/',self::$content_type);
     if ($is == 'html') return preg_match('/html$/',self::$content_type);
     if ($is == 'xml') return preg_match('/xml$/',self::$content_type);
+    if ($is == 'text') return preg_match('/^text\//',self::$content_type);
     die("Db->is(): unknow argument '$is'. Accepted values are xhtml, html, xml");
   }
 
index 8ac3a3d281903acc81de26decdb70296bcde6873..c4c4e2e49fd495b3b0f16e1f37c7e17448d2d8bd 100644 (file)
 <?php
+# Make it work in php_cli
 if (empty($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
-if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = '';
-function parse_uri() {
-  $path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
-  $values = explode('/',trim($path,'/'));
-
-  $P = [
-    'db' => '',
-    'table' => '',
-    'action' => '',
-    'format' => '',
-  ];
+#var_export([$_SERVER['REQUEST_URI']]);
+if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
+
+function run() {
+  $params = parse_uri([
+    'db' => 'ls',
+    'table' => 'ls',
+    'action' => 'ls',
+    'format' => 'human',
+  ]);
+
+  if (!$params['db']) not_implemented();
+
+  #
+  # Page
+  require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
+  $Page = new Page();
+  $Page->content_type($Page->ext2mime($params['format']) ? $Page->ext2mime($params['format']) : 'text/plain');
+  #$Page->content_type('text/plain');
+  #if ($Page->is('text') and !$Page->is('html')) $Page->content_type('text/plain');
+  $Page->headers_no_cache();
+  $Page->begin();
+
+  #
+  # Db Init
+  global $DB_CONFS;
+  require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/config.php');
+  #if (empty($DB_CONFS[$params['db']])) not_implemented();
+  #$Db = new Db($DB_CONFS[$params['db']]+['conf'=>$DB_CONFS]);
+  $Db = new Db(['conf'=>$DB_CONFS]);
+  $Db->format = $params['format'];
+
+  #
+  # Db actions pre conn
+  if ($params['db'] == 'help') {
+    #$this->out->rows($format,$rows,$head);
+    $Db->out([
+      [ 'help', 'This help' ],
+      [ 'ls', 'List databases' ],
+    ],['command','description']);
+    return $Page->end();
+
+  } elseif ($params['db'] == 'ls') {
+    $Db->out($Db->ls());
+    return $Page->end();
+
+  }
+
+  #
+  # Connections post conn
+  if (empty($DB_CONFS[$params['db']])) not_implemented();
+  $Db->__construct($DB_CONFS[$params['db']]);
+  $Db->connect();
+
+  #
+  # Db actions
+  if ($params['table'] == 'help') {
+    #$this->out->rows($format,$rows,$head);
+    $Db->out([
+      [ 'help', 'This help' ],
+      [ 'ls', 'List tables' ],
+    ],['command','description']);
+    return $Page->end();
+
+  } elseif ($params['table'] == 'ls') {
+    $Db->action('db.tables');
+    return $Page->end();
+
+  } elseif ($params['table'] == 'status') {
+    $Db->out($Db->status());
+    return $Page->end();
+
+  } elseif ($params['table'] == 'databases') {
+    $Db->out($Db->databases());
+    return $Page->end();
+
+  }
+
+  #
+  # Table Init
+  $Table = $Db->table($params['table']);
+  if (!$Table) not_implemented();
+  
+  #
+  # Table action
+  if ($params['action'] == 'help') {
+    #$this->out->rows($format,$rows,$head);
+    $Db->out([
+      [ 'help', 'This help' ],
+      [ 'ls', 'List records' ],
+    ],['command','description']);
+    return $Page->end();
+
+  } elseif ($params['action'] == 'ls') {
+    $Table->rows();
+    return $Page->end();
+
+  } elseif ($params['action'] == 'status') {
+    $Db->out($Table->status()+$Table->status(['fields','sql']));
+    return $Page->end();
+
+  } elseif ($params['action'] == 'fields') {
+    $Table->action('table.fields');
+    return $Page->end();
+
+  }
+  return false;
+}
+
+function parse_uri($params=[]) {
+  if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('&',count($argv)>0 ? array_slice($argv,1) : []);
+  $path = trim(preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']),'/');
+  $values = $path ? explode('/',$path) : [];
+#var_export($values);
 
   $i=0;
   $count = count($values);
-  foreach ($P as $p => $default) {
+  foreach ($params as $p => $default) {
     if ($i>=$count) break;
-    $P[$p] = $values[$i];
+    $params[$p] = $values[$i];
     $i++;
   }
 
+  return $params;
 }
-#var_dump(parse_uri());
+run();
+exit;
 
 $params = [
   'db',
@@ -30,11 +136,7 @@ $params = [
   'format',
 ];
 if (false and strpos($_SERVER['REQUEST_URI'],'?')!==false) {
-  #return http_response_code(501);
-  $msg = '501 Not Implemented';
-  header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
-  echo "$msg\n";
-  return;
+  return not_implemented();
 }
 $_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
 $values = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
@@ -85,4 +187,12 @@ $Page->call([
 #debug($_SERVER['REQUEST_URI']);
 #debug($_SERVER['QUERY_STRING']);
 #debug($_SERVER);
+
+function not_implemented() {
+  #return http_response_code(501);
+  $msg = '501 Not Implemented';
+  header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
+  echo "$msg\n";
+  exit;
+}
 ?>