From 11cf6b199b7be3b2a5eada0f1030593c536dedd4 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Sat, 3 Dec 2016 16:12:40 +0000 Subject: [PATCH] attach mechanisum --- etc/dbq/zzz-all.php | 38 +----------------------------- lib/php/db.php | 46 ++++++++++++++++++++++++++++++++++++- lib/php/db/page.php | 2 -- lib/php/db/types/sqlite.php | 20 ++++++++++++---- lib/php/nb.php | 13 +++++++++-- www/dbq/html/index.php | 1 + 6 files changed, 73 insertions(+), 47 deletions(-) diff --git a/etc/dbq/zzz-all.php b/etc/dbq/zzz-all.php index fce91a2f..fb25631c 100644 --- a/etc/dbq/zzz-all.php +++ b/etc/dbq/zzz-all.php @@ -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]; -} -?> diff --git a/lib/php/db.php b/lib/php/db.php index 51c37256..80eb96d8 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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 diff --git a/lib/php/db/page.php b/lib/php/db/page.php index c0ac3812..1baa64e6 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -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); diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index 7133da19..8825db23 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -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"); } }, );?> diff --git a/lib/php/nb.php b/lib/php/nb.php index b99e1819..6a976675 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -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 diff --git a/www/dbq/html/index.php b/www/dbq/html/index.php index 850d9a76..c2280d32 100644 --- a/www/dbq/html/index.php +++ b/www/dbq/html/index.php @@ -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); ?> -- 2.47.3