]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 4 Dec 2016 02:35:53 +0000 (02:35 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 4 Dec 2016 02:35:53 +0000 (02:35 +0000)
14 files changed:
etc/dbq/zzz-all.php
lib/php/benchmark.php
lib/php/db.php
lib/php/db/init.php
lib/php/db/table.php
lib/php/default.php [deleted file]
lib/php/default.php.TODEL [new file with mode: 0644]
lib/php/functions.php
lib/php/nb.php
lib/php/page.php
share/templates/hosts.php
share/templates/nginx-lb.php
src/db/http_status.script [new file with mode: 0644]
www/dbq/html/index.php

index fb25631c73a9218aa35f7fda497f4d74a37edd26..37f1a7028d26a3b825cef116379f3531d0b47e58 100644 (file)
@@ -79,3 +79,4 @@ foreach ($DBQ as $id => $db) {
   $DBQ['all']['_import'][] = $id;
 
 }
+if (nb::p('db')!='nb') $DBQ['all']['_import'][] = 'nb';
index 94716afca4f46add71da4722a38221ea3403da13..a1d7cd3732b28314e54fcd7194f748f5c86aedd3 100755 (executable)
@@ -11,12 +11,6 @@ $hash = array(); for ($i=0;$i<100000;$i++) $hash[$i] = $i;
 $a = $b = '';
 echo "Start benchmark\n";
 
-/*
-$a = array('a' => 'A');
-if (isset($a['a'])) echo "OK\n";
-echo realpath('lib/php/default.php')."\n";
-*/
-
 /*
 print_r(parse_str('first=value&arr[]=foo+bar&arr[]=baz'));
 echo $first;
index 9d995226a68cbeb394ad807d5030b5b7bcf2e759..ce8ea2188748f07625d046e84078fe46f7fb65af 100644 (file)
@@ -99,18 +99,19 @@ class Db extends nb {
   ];
 
   protected static $action_help = [
-    ['(db.)?help','This help'],
-    ['(db.)?ls','List databases from conf'],
-    ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'],
+    ['db.help','This help'],
+    ['db.ls','List databases from conf'],
+    ['db.database','List other databases on the same server'],
+    ['db.tables','List tables (name=* type=* count=1 engine=*)'],
 
-    ['(table.)?rows','Dump one table, use format='],
-    ['(table.)?fields','List fields'],
-    ['table.infos','Table infos (count(*). Type, ...)'],
-    ['table.sql','Get the sql source'],
+    ['table.rows','Dump one table, use format='],
+    ['table.fields','List fields'],
+    ['table.status','Table status (count= maxlen= max=)'],
+    ['table.sql','Sql source to create the table'],
 
-    ['(table.)?insert','Insert a record'],
-    ['(table.)?update','Update a record'],
-    ['(table.)?delete','Delete a record'],
+    ['table.insert','Insert a record'],
+    ['table.update','Update a record'],
+    ['table.delete','Delete a record'],
 
   ];
   # Web
@@ -378,9 +379,9 @@ class Db extends nb {
   * Return a table instance
   */
   public function table($name='',$params=[]) {
-    #if (!array_key_exists($name,$this->tables)) {
     if (!$name and !empty($this->default_table)) $name = $this->default_table;
     if (!$name) bye('table name is empty');
+
     if (empty($this->tables[$name])) {
       $this->tables[$name] = new Table($name,['db'=>$this]+$params);
     } elseif ($params) {
@@ -845,7 +846,7 @@ class Db extends nb {
       if (!self::p('table') and ($v = array_keys($Db->tables()))) self::pset('table',$Db->ar_first($v));
 
       if (self::p('table')) $Table = $Db->table(self::p('table'),
-        empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')]
+        ( empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] )
       );
 
     }
@@ -1265,7 +1266,9 @@ class Db extends nb {
         return 'csv';
     }
   }
+
   public function page($opt=[]) {
+    require_once(realpath(dirname(__FILE__).'/page.php'));
     return new Page(array_merge([
       'title' => ($this->title ? $this->title : this::prettyText($this->name)),
       'css' => [
index 990aba06bec6a0a72346280fa6aa44405a04bf95..b3dd1650e331a06e24727b8262c67155811e5abd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 require_once(realpath(dirname(__FILE__).'/../config.php'));
-require_once(realpath(dirname(__FILE__).'/../db.php'));
+# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../db.php'));
 
 require_once(realpath(dirname(__FILE__).'/config.php'));
 
index 43e503b4db1a2a4b9a10f4c24798a5d53fd779e9..bfee51509c396ab2d280bc5af6277dd651602837 100644 (file)
@@ -1884,6 +1884,13 @@ Class Table extends nb {
     $HEAD = &$opt['var']['head'];
     $ROWS = &$opt['var']['rows'];
     $ROW = &$opt['var']['rows'][0];
+    foreach ($ROWS as $key=>$row) {
+      $i=0;
+      foreach ($row as $k=>$v) {
+        $ROWS[$key][$i] = $v;
+        $i++;
+      }
+    }
     #$opt['var']['rows']=[];
 
     $_REQUEST_BAK = $_REQUEST;
diff --git a/lib/php/default.php b/lib/php/default.php
deleted file mode 100644 (file)
index 4f30e1c..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-if (!defined('DEBUG')) define('DEBUG',(int)@$_REQUEST['debug']);
-ini_set('include_path', realpath(dirname(__FILE__)).':'.ini_get('include_path'));
-
-require_once((dirname(__FILE__).'/functions.php'));
-require_once((dirname(__FILE__).'/page.php'));
-?>
diff --git a/lib/php/default.php.TODEL b/lib/php/default.php.TODEL
new file mode 100644 (file)
index 0000000..4f30e1c
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+if (!defined('DEBUG')) define('DEBUG',(int)@$_REQUEST['debug']);
+ini_set('include_path', realpath(dirname(__FILE__)).':'.ini_get('include_path'));
+
+require_once((dirname(__FILE__).'/functions.php'));
+require_once((dirname(__FILE__).'/page.php'));
+?>
index 2c5d8f4b67095bb34f7a65d5f6a4fd32dabe0314..abc718671d6af63cd64faf82b89a27598f9b9980 100644 (file)
@@ -9,7 +9,6 @@ s/array_key_exists(\([^,]\+\),\([^)]\+\))/isset(\2[\1])/g
 s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/
 
 */
-// NB 28.06.15 require_once((dirname(__FILE__).'/default.php'));
 
 function file_write($file,$data,$mode='w') {
   return nb::file_write($file,$data,$mode);
@@ -78,7 +77,6 @@ function warn ($msg) {
 }
 
 function err($msg='__err__',$preff='err',$backtrace_deep=0) {
-# NB 05.04.16   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
   $preff_msg = $preff ? strtoupper($preff).': ' : '';
   if ($msg !== '__err__' and $backtrace_deep !== false) {
index 6a97667512e357af42b5865ea20b8e15ccb2a15a..ec349bf827cb63c48037217ee23daa8e79bce473 100644 (file)
@@ -24,10 +24,11 @@ class NB {
     'doc'  => 'application/msword',
     'xls'  => 'application/vnd.ms-excel',
     'xml'  => 'application/xml',
+    'txt'  => 'text/plain',
     'csv'  => 'text/csv',
     'html' => 'text/html',
-    'txt'  => 'text/plain',
     'yaml' => 'text/yaml',
+    'php'  => 'text/x-php',
   );
 
   #public static function zaza() { return (is_object($this) ? 'TRUE' : 'FALSE'); }
index cadc3b25ac4b969ca5da3e3d5b7f329f144c7937..884da78a80430ba392b38c9faa669854e83261c5 100644 (file)
@@ -42,9 +42,25 @@ class Page extends nb {
   public $h1 = '';
   public $sep = ' / ';
 
-  /*
-    New
-  */
+  public function call($calls) {
+    foreach ((array)($calls) as $call) {
+
+      if (is_scalar($call)) {
+        $this->$call();
+
+      } elseif ( is_array($call) ) {
+        $k = $call[0];
+        $this->$k($call[1]);
+
+      } elseif (is_callable($call)) {
+        $call($this);
+
+      }
+
+    }
+
+  }
+
   public function __construct($opt = []) {
 
     // Content-type
@@ -74,25 +90,7 @@ class Page extends nb {
     if ( empty($this->h1) ) $this->h1 = !empty($nav) ? join($this->sep,$nav) : $this->title;
 
     // Call / Print
-
-    // Nav
-    foreach ((array)($this->call) as $call) {
-
-      if (is_scalar($call)) {
-        $this->$call();
-
-      } elseif ( is_array($call) ) {
-        $k = $call[0];
-        $this->$k($call[1]);
-
-      } elseif (is_callable($call)) {
-        $call($this);
-
-      } else {
-
-      }
-
-    }
+    if (!empty($this->call)) $this->call($this->call);
 
   }
 
@@ -348,6 +346,7 @@ class Page extends nb {
   public function headers() {
 
     #header('Content-type: ' . self::$content_type);
+    self::$content_type = 'text/plain';
                header('Content-type: '.self::$content_type . (($c = strtoupper ( self::$charset )) ? "; charset=$c" : ""));
 
     if (isset($this->expires)) self::headers_cache($this->expires);
@@ -520,7 +519,7 @@ class Page extends nb {
     return join("\n",$m[0]);
   }
 
-  public function infos() {
+  public function status() {
     $out = array(
       'path' => self::path()
     );
@@ -579,8 +578,10 @@ class Page extends nb {
 
     if (!$format and $content_type) {
       self::p('format',self::mime2ext($content_type));
+
     } elseif (!$format and ($client_type = self::client_content_type())) {
       self::p('format',self::mime2ext($client_type));
+
     }
 
     if (!$content_type and $format) $content_type = self::ext2mime($format);
index 00232ce474328e5cd6ea54e3cd790914d923c0dd..eb55f477ea7580ef96dcbaf1cc04290da96a67bd 100644 (file)
@@ -7,6 +7,7 @@ if (isset($ROWS)) foreach ($ROWS as $r) {
     'ip',
     'addr',
     'address',
+    0,
   ] as $k) {
     if (!isset($r[$k])) continue;
     $line[] = $r[$k];
@@ -18,6 +19,7 @@ if (isset($ROWS)) foreach ($ROWS as $r) {
     'host',
     'name',
     'id',
+    1,
   ] as $k) {
     if (!isset($r[$k])) continue;
     $line[] = $r[$k];
index 3c49d933a13fdaf2d6eb89e314f9cc4eeb71ef74..f08e20ece57c1db8f3a87507b92c6ef27db4f2ad 100644 (file)
@@ -1,11 +1,14 @@
 <?php
 # NB 02.12.16: Nginx LB 
-if (!isset($ROWS)) continue;
+if (!isset($ROWS)) return;
+
 $lb = [];
+
 foreach ($ROWS as $r) {
   $lb[$r[0]][] = $r[1];
   $name = $r[0];
   $server = $r[1];
+
   continue;
   foreach ([
     'hostname',
@@ -17,6 +20,7 @@ foreach ($ROWS as $r) {
     break;
   }
 }
+
 foreach ($lb as $name => $servers) {
   echo "upstream $name {\n";
   foreach ($servers as $server) {
diff --git a/src/db/http_status.script b/src/db/http_status.script
new file mode 100644 (file)
index 0000000..cd3afb3
--- /dev/null
@@ -0,0 +1,6 @@
+DROP TABLE IF EXISTS http_status;
+CREATE TABLE http_status(id int primary key,name varchar(100),description varchar(1000));
+DELETE FROM http_status;
+.sep "\t"
+.import /Users/nico/tmp/http_status.txt http_status
+-- SELECT * FROM http_status limit 5;
index 298c2d6d77353d611b2618d176fb220310b68445..f907dd8c6efe7d4e712e7e24bfc45bfe8a477118 100644 (file)
@@ -4,45 +4,49 @@ if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = '';
 
 $params = [
   #'db',
-  'action',
   'table',
+  'action',
   'format',
 ];
-# NB 03.12.16 list(
-# NB 03.12.16   $action,
-# NB 03.12.16   $db,
-# NB 03.12.16   $table,
-# NB 03.12.16   $format,
-# NB 03.12.16 ) = 
-#if (strpos($_SERVER['REQUEST_URI'],'?')!==false) return header('HTTP/1.1 501 Not Implemented');
-if (strpos($_SERVER['REQUEST_URI'],'?')!==false) return http_response_code(501);
-$values = explode('/',$_SERVER['REQUEST_URI']);
-while (count($values)>0 and $values[0]==='') {
-  array_shift($values);
+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;
 }
-#for ($i=count($values);$i>0 and $values[
+$_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
+$values = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
 require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
-#debug($_SERVER['REQUEST_URI']);
+#bye($_REQUEST);
 
 foreach ($values as $k=>$v) {
+  #if ($v!=='') debug($params[$k].'='.$v);
   if ($v!=='') Page::pdef($params[$k],$v);
 }
 
 Page::pdef('format','human');
-Page::pdef('action','ls');
+#Page::pdef('action','db.ls');
 
 require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/init.php');
-$Db->page([
-    'call' => [
+header('Content-type: text/plain');
+$Page = $Db->page();
+$Page->headers_no_cache();
+$Page->call([
+# NB 04.12.16 $Db->page([
+# NB 04.12.16     'call' => [
       'begin',
       #['out', "Hello World !!!\n"],
 
       function($page) use($Db){
-        if (!$page->action()) return $Db->action($Db->p('action'),$Db->table());
+        #debug($Db->p());
+        #global $values; debug($values);
+      },
+      function($page) use($Db){
+        if (!$page->action()) return $Db->action($Db->p('action'),$Db->table($Db->p('table')));
       },
 
       function($page) use($Db){
-        #global $values; debug($values);
         if (!$page->is('html')) return;
         if (!empty($Db)) echo $page->tag('p class="db-infos"',join(' | ',[
           $Db->type,
@@ -52,7 +56,7 @@ $Db->page([
       },
 
       'end',
-    ],
+# NB 04.12.16     ],
 ]);
 #require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/page.php');
 # /DB/ACTION/[TABLE]/[FORMAT]