$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];
-}
-?>
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
#try {
foreach ($this->sql_pre() as $s) { $this->conn->exec($s); }
+ $this->method('connect');
#} catch (PDOException $e) {
#return false;
#}
* @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);
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
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' => [
'end',
),
]);
-unset($js,$css);
#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");
}
},
);?>
/*
* 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
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);
?>