]> git.nbdom.net Git - nb.git/commitdiff
attach mechanisum
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 3 Dec 2016 16:12:40 +0000 (16:12 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 3 Dec 2016 16:12:40 +0000 (16:12 +0000)
etc/dbq/zzz-all.php
lib/php/db.php
lib/php/db/page.php
lib/php/db/types/sqlite.php
lib/php/nb.php
www/dbq/html/index.php

index fce91a2f02a65436f7d1174c0a75d9d921052aeb..fb25631c73a9218aa35f7fda497f4d74a37edd26 100644 (file)
@@ -74,44 +74,8 @@ foreach ($DBQ as $id => $db) {
   $fname = basename($db['host'],'.db');
   $fname = preg_replace('/\..*$/','',basename($db['host']));
 
-  #array_unshift($DBQ['all']['types']['sql_pre'],"ATTACH DATABASE '".$db['host']."' as ".$fname);
-  $DBQ['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname;
+  $DBQ['all']['attach'][$fname] = $db['host'];
   $DBQ['all']['_import'] = empty($DBQ['all']['_import']) ? [] : (array)$DBQ['all']['_import'];
   $DBQ['all']['_import'][] = $id;
-# NB 21.11.16   if (!isset($DBQ[$fname]['name'])) $DBQ[$fname]['name'] = $fname;
-# NB 21.11.16   conf_merge($DBQ[$fname],$DBQ['all']);
 
 }
-#if (isset($DBQ['nb']) and isset($DBQ['nb']['default_table']))
-  #$DBQ['all']['default_table'] = $DBQ['nb']['default_table']
-#;
-
-#debug($DBQ['nb-sqlite']['default_table']);
-#if (!empty($DBQ['nb-sqlite'])) debug($DBQ['nb-sqlite']);
-#if (!empty($DBQ['nb'])) conf_merge($DBQ['nb'],$DBQ['all']);
-
-#
-# Functions
-#
-function conf_merge(&$c1,&$c2) {
-
-  // .*
-  foreach ([
-    'default_table',
-    'title',
-    'row_parse',
-    #'_import',
-  ] as $k) {
-    if (isset($c1[$k])) $c2[$k] = $c1[$k];
-  }
-
-  // tables.*
-  if (!empty($c1['tables'])) {
-    foreach ((array)$c1['tables'] as $k=>$v) {
-      $c2['tables'][$k] = $v + ['database' => $c1['name']];
-    }
-  }
-
-  return [$c1,$c2];
-}
-?>
index 51c37256ea7ab6026dce5575d7f1018aae2a8995..80eb96d823bd969906f3184d699e77c3b5128ad2 100644 (file)
@@ -49,9 +49,11 @@ class Db extends nb {
   public $title;
   public $notice;
   public $type;
+  public $table; # table name
   public $tables = [];
   public $types = [];
   public $conf = [];
+  public $attach = []; # for sqlite
   public $row_parse; # Function to call in table.rows()
 
   # Encryption
@@ -290,6 +292,7 @@ class Db extends nb {
 
     #try {
     foreach ($this->sql_pre() as $s) { $this->conn->exec($s); }
+    $this->method('connect');
     #} catch (PDOException $e) {
     #return false;
     #}
@@ -375,8 +378,9 @@ class Db extends nb {
   * @author NB 12.08.16
   * Return a table instance
   */
-  public function table($name,$params=[]) {
+  public function table($name='',$params=[]) {
     #if (!array_key_exists($name,$this->tables)) {
+    if (!$name and !empty($this->table)) $name = $this->table;
     if (!$name) bye('table name is empty');
     if (empty($this->tables[$name])) {
       $this->tables[$name] = new Table($name,['db'=>$this]+$params);
@@ -1262,6 +1266,46 @@ class Db extends nb {
         return 'csv';
     }
   }
+  public function page($opt=[]) {
+    return new Page([
+      'title' => ($this->title ? $this->title : this::prettyText($this->name)),
+      'css' => [
+        'css/*.css',
+        '/*.css',
+      ],
+      'js' => [
+        'js/*.js',
+        '/*.js',
+      ],
+      'content_type' => Page::content_type_and_set_format(),
+      'nav' => [
+        [ (!empty($this) and !empty($this->title)) ? $this->title : 'Home', '/'],
+        ( (!empty($this->table()) and !empty($this->table()->name)) ? [Page::prettyText($this->table()->name),Page::path().'?table='.urlencode($this->table()->name)] : '' ),
+        ( Page::p('action') ? Page::prettyText(Page::p('action')) : '' ),
+      ],
+      'call' => array(
+        'begin',
+        #['out', "Hello World !!!\n"],
+
+        function($page){
+          $this->pdef('format',$this->content_type2format($page->content_type()));
+          if (!$page->action()) return $this->action($this->p('action'),$this->table());
+        },
+
+        function($page){
+          #if (!$page->is('html')) return;
+          if (!empty($this)) echo $page->tag('p class="db-infos"',join(' | ',[
+            $this->type,
+            $this->host,
+            ($this->name ? $this->name : null),
+          ]));
+        },
+
+        'end',
+      ),
+    ]);
+  }
+
   #public function default_table() { return $this->default_table; }
 } # < Class
 
index c0ac38129068bd0f4392315643de2c4ca30cd13d..1baa64e609408e5745259a42600219cbc586791f 100644 (file)
@@ -2,7 +2,6 @@
 require_once(dirname(__FILE__).'/init.php');
 require_once(dirname(__FILE__).'/../page.php');
 
-
 $Page = new Page([
   'title' => ($Db->title ? $Db->title : Db::prettyText($Db->name)),
   'css' => [
@@ -44,4 +43,3 @@ $Page = new Page([
     'end',
   ),
 ]);
-unset($js,$css);
index 7133da1993b60de76f8f6815f3d88724e204b25b..8825db23f0dc8b2ec5ce53f1361addd2b0e8673a 100644 (file)
@@ -124,12 +124,22 @@ $DB_TYPES['sqlite'] = array (
   #debug($sql);
   return $sql;
 },
+'connect' => function($Db) {
+  if (!empty($Db->attach)) {
+    foreach ($Db->attach as $name => $file) {
+    try {
+      $Db->conn->exec("ATTACH DATABASE '$file' as $name");
+    } catch (PDOException $e) {
+      if (!strpos($e->getMessage(),'database sys is already in use')) {
+        err($e->getMessage());
+      }
+    }
+    }
+  }
+},
 'disconnect' => function($Db) {
-  foreach ($Db->sql_pre() as $s) {
-    //if (strpos($s,'ATTACH')!==false) debug('zaza');
-    if (strpos($s,'ATTACH')!==false and
-      ($s = preg_replace('/^ATTACH DATABASE.*? as (.*?)$/','DETACH DATABASE $1',$s))
-    ) $Db->conn->exec($s);
+  foreach ($Db->attach as $name => $file) {
+    $Db->conn->exec("DETACH DATABASE $name");
   }
 },
 );?>
index b99e1819e2a3df69d9b6428e67fb251012b40c42..6a97667512e357af42b5865ea20b8e15ccb2a15a 100644 (file)
@@ -217,12 +217,21 @@ class NB {
 
   /*
    * Function: client_header
-   * Return a client header
+   * Return a client header. Work only with apache !
    */
   public static function client_header($name=null,$match=null) {
     static $header = null;
     if ( $header === null ) {
-      $header = function_exists('getallheaders') ? getallheaders() : array();
+      $header = [];
+                       if (function_exists('getallheaders')) {
+        $header = getallheaders();
+      } else {
+        foreach ($_SERVER as $name => $value) {
+            if (substr($name, 0, 5) == 'HTTP_') {
+                $header[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
+            }
+        }
+                       }
     }
 
     # Array
index 850d9a766223afbb0f380f3dcf8ca46301a199fb..c2280d329cef92ddf434b9faec8b3b5c2377cef5 100644 (file)
@@ -2,4 +2,5 @@
 require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
 Page::pdef('action','ls');
 require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/page.php');
+#debug($_SERVER);
 ?>