<?php
require_once dirname(__FILE__).'/../lib/php/nb.php';
-$CONF = array();
+$CONF = [];
#$dev = (bool)(stripos(gethostname(),'macbook') !== false);
$LOCAL_DB = (
file_exists('/opt/local/var/run/mysql5')
# Includes
$dir = dirname(__FILE__).'/dbs';
-#debug(nb::ls_dir($dir,'\.php$'));
foreach (nb::ls_dir($dir,'\.php$') as $file) {
require_once("$dir/$file");
- #if (file_exists("$dir/$file.php")) echo("$dir/$file.php\n");
}
-return;
-foreach (array(
- 'postfix',
- 'nb',
- 'rent',
- 'puppetdb',
- 'ui',
- 'wp',
-) as $file) {
- if (file_exists("$dir/$file.php")) require_once("$dir/$file.php");
- #if (file_exists("$dir/$file.php")) echo("$dir/$file.php\n");
+
+#
+# Db All
+#
+#return;
+$CONF['all'] = [
+ 'host' => ':memory:',
+ 'type' => 'sqlite',
+ 'pdo' => '',
+ #'order' => '0',
+ 'options' => [
+ PDO::ATTR_PERSISTENT => true,
+ ],
+];
+$CONF['all']['tables'] = [];
+$CONF['all']['types']['exec'] = [];
+foreach ($CONF as $id => $db) {
+ if (0
+ or empty($db['type'] )
+ or $db['type']!='sqlite'
+ or empty($db['host'] )
+ or !is_readable($db['host'])
+ ) continue;
+ #debug(basename($db['host'],'.db'));
+ $CONF['all']['types']['exec'][] = "ATTACH DATABASE '".$db['host']."' as ".basename($db['host'],'.db')."";
+ conf_merge($CONF[$id],$CONF['all']);
+
+}
+
+conf_merge($CONF['_nb'],$CONF['all']);
+conf_merge($CONF['nb'],$CONF['all']);
+function conf_merge(&$c1,&$c2) {
+
+ if (!empty($c1['tables']))
+ #debug('zaza');
+ #$c2['tables'] = array_merge($c2['tables'],$c1['tables']);
+ #$c2['tables']
+ #$c1['tables']
+ foreach ($c1['tables'] as $k=>$v) {
+ $c2['tables'][$k] = $v;
+ }
+ ;
+
+ foreach ([
+ 'default_table',
+ 'title',
+ #'_import',
+ ] as $k) if (!empty($c1[$k]))
+ $c2[$k] = $c1[$k];
+ ;
+
+ return [$c1,$c2];
}
?>
#user: nico
_import:
- izi
-
-mem:
- name: mem
- host: ":memory:"
- type: sqlite
- types:
- exec:
- - "ATTACH DATABASE '/opt/www/sem_ui/var/db/semantico.db' as ui"
- - "ATTACH DATABASE '/home.local/nicoadm/ownCloud/var/lib/sqlite/wp.db' as wp"
- - "ATTACH DATABASE '/home.local/nicoadm/ownCloud/var/lib/sqlite/nb.db' as nb"
--- /dev/null
+<?php
+return;
+###############################################################################
+#
+# - NB 03.09.16
+# Create a big sqlite memory database with attach
+#
+###############################################################################
+require_once dirname(__FILE__).'/../../lib/php/config.php';
+require_once dirname(__FILE__).'/../dbs.php';
+
+$CONF['all'] = array(
+ 'host' => ':memory:',
+ 'type' => 'sqlite',
+ #'order' => '0',
+ 'options' => [
+ PDO::ATTR_PERSISTENT => true,
+ ],
+);
+$attach = [];
+foreach ([
+ '/opt/www/sem_ui/var/db/semantico.db',
+ '/dev/shm/crypt.db',
+] as $file) {
+ if (file_exists($file)) $attach[] = $file;
+}
+
+foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) {
+ $attach[] = "$DIR_SQLITE/$file";
+}
+
+# nb last to win the merge
+usort($attach,function($a,$b){
+ if ( strpos($a,'/nb.db') !== false ) return 1;
+ if ( strpos($b,'/nb.db') !== false ) return -1;
+ return strcmp($a,$b);
+ return 0;
+});
+if (!$attach) return;
+
+$CONF['all']['types']['exec'] = [];
+$CONF['all']['_import'] = [];
+if (nb::p('debug')=='all') debug($attach);
+
+foreach ($attach as $file) {
+ if (!is_readable($file)) continue;
+
+ $name = basename($file,'.db');
+ $CONF['all']['types']['exec'][] = "ATTACH DATABASE '$file' as $name";
+ #$CONF['all']['_import'][] = $name;
+}
+$CONF['all']['_import'][] = 'rent';
+#bye($CONF['all']['default_table']);
+
+#$CONF['all']['default_table'] = 'rent';
+#debug($CONF['all']);
+?>
'type' => 'mysql',
'name' => 'nb',
#'order' => ($LOCAL_DB ? null : 1),
- 'order' => 1,
- '_import' => array('_nb','_nico'),
+ #'order' => 1,
+ '_import' => ['_nb','_nico'],
);
$CONF['nb-sqlite'] = array (
'_import' => '_nb',
);
-if ($LOCAL_DB and !empty($CONF['nb-sqlite'])) {
+/*****************************************************************************
+if ($LOCAL_DB and !empty($CONF['nb-sqlite']) and isset($CONF['nb']['order'])) {
$CONF['nb-sqlite']['order'] = isset($CONF['nb']['order']) ? $CONF['nb']['order'] : 1;
unset($CONF['nb']['order']);
}
-/*****************************************************************************
*****************************************************************************/
#die(print_r(posix_getpwnam('nico'),true));
#die(print_r(posix_getpwuid(posix_getuid()),true));
) {
- foreach (ls_dir($dir,true) as $p) {
+ foreach (nb::ls_dir($dir,'',true) as $p) {
$r['doc'] .= sprintf('<a href="%s" class="small">%s</a> ',"$url/$p",
nb::prettyText(preg_replace('@^.*?([^/\.]+).*?$@','\1',$p))
);
public static $encoding = 'utf-8';
public $conn;
public $pdo;
-# NB 07.04.16 public $pdo_error = array( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # See: http://php.net/manual/en/pdo.error-handling.php
- public $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); # See: http://php.net/manual/en/pdo.error-handling.php
- #public $options;
+ public $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; # See: http://php.net/manual/en/pdo.error-handling.php
public $host = null;
public $port = null;
public $user = null;
public $default_table;
public $sort;
public $extras;
- public $formats = array( 'table','div','csv','xml','json','yaml' );
- public $limits = array('10','20','50','100','500','1000');
+ public $formats = [ 'table','div','csv','xml','json','yaml','sh' ];
+ public $limits = ['10','20','50','100','500','1000'];
function __construct($opt = '') {
if ($opt==='') $opt = [];
$opt = is_scalar($opt) ? ['pdo' => $opt] : $opt;
+ # Options
+ if (isset($opt['options'])) {
+ foreach ($opt['options'] as $k=>$v) $this->options[$k] = $v;
+ unset($opt['options']);
+ }
+
# Tables
if (isset($opt['tables'])) {
foreach ($opt['tables'] as $name=>$param) $this->table($name,$param);
# Connect
try {
$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
- #$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->pdo_error);
#if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]);
} catch (PDOException $e) {
return $value;
if (!is_scalar($value)) return $value;
$replace = [
+ '<D.NAME>' => $this->db()->name,
+ '<T.NAME>' => $this->name,
+ '<T.TYPE>' => $this->type,
+ '<DB>' => $this->name.'___TODEL___',
'<NAME>' => $this->name,
- '<DB>' => $this->name,
'<TYPE>' => $this->type,
'<ENCODING>' => self::$encoding,
];
return str_replace(array_keys($replace),array_values($replace),$value);
}
- public function info($key=null,$die=false,$type=null) {
- $info = $this->type($key,$die,$type);
- if (is_callable($info)) return $info($this);
- return $this->unvar($info);
+ public function method($key=null,$type=null) {
+ $method = $this->type($key,false,$type);
+ if (is_callable($method)) return $metod = $method($this);
+ return $this->unvar($method);
}
public function type($key=null,$die=false,$type=null) {
# Load php file
static $require = [];
if (empty($require[$type])) {
- if (empty($type)) self::bye('Db::$type is required');
+ if (empty($type)) self::bye('Db->type is required');
require_once(dirname(__FILE__).'/db/types/'.$type.'.php');
$require[$type] = 1;
}
$sql = $this->type('tables',true,$this->type);
if (is_callable($sql)) $sql = $sql($this);
+ $sql = $this->unvar($sql);
+ $sql = $this->method('tables');
foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) {
#debug($row);
# Sort by `order`, min first
uasort($h,function($a,$b){
- if (empty($a["order"]) and empty($b["order"])) return strcmp($a['id'],$b['id']);
- $a_ = !empty($a["order"]) ? $a["order"] : 9999999;
- $b_ = !empty($b["order"]) ? $b["order"] : 9999999;
+ if (empty($a['order']) and empty($b['order'])) return strcmp($a['id'],$b['id']);
+ $a_ = !empty($a['order']) ? $a['order'] : 9999999;
+ $b_ = !empty($b['order']) ? $b['order'] : 9999999;
return($a_-$b_);
});
$db = [];
foreach ($fields as $k) {
- #if (isset($d::$k)) $db[$k] = $d::$k;
- #if ($k=='type' and isset($d::$type)) $db[$k] = $d::$type;
if (isset($d->$k)) $db[$k] = $d->$k;
}
if(defined('DB_NO_INIT') and DB_NO_INIT) return true;
#bye(Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )));
#$conf = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf);
-Db::init(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ));
+Db::init([Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml']);
if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true;
return $Db->action($Db->p('action'),$Table);
'databases' => "SELECT SCHEMA_NAME as `name`,DEFAULT_CHARACTER_SET_NAME as `encoding`,DEFAULT_COLLATION_NAME as `collate` FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME NOT IN ('performance_schema','information_schema','mysql')",
'table.sql' => 'SHOW CREATE TABLE `<NAME>`',
-'table.sql.index' => "SELECT ".(Db::p('db.type') ? "CONCAT(s.TABLE_NAME,'_',s.INDEX_NAME,'_idx')" : 's.INDEX_NAME')." as name,(CASE NON_UNIQUE WHEN 1 THEN 0 ELSE 1 END) as uniqe,GROUP_CONCAT(COLUMN_NAME) as field FROM INFORMATION_SCHEMA.STATISTICS s LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t ON t.TABLE_SCHEMA=s.TABLE_SCHEMA AND t.TABLE_NAME=s.TABLE_NAME AND s.INDEX_NAME=t.CONSTRAINT_NAME WHERE 0=0 AND t.CONSTRAINT_NAME IS NULL AND s.TABLE_SCHEMA = '<DB>' AND s.TABLE_NAME='<NAME>' GROUP BY name ORDER BY SEQ_IN_INDEX",
+'table.sql.index' => "SELECT ".(Db::p('db.type') ? "CONCAT(s.TABLE_NAME,'_',s.INDEX_NAME,'_idx')" : 's.INDEX_NAME')." as name,(CASE NON_UNIQUE WHEN 1 THEN 0 ELSE 1 END) as uniqe,GROUP_CONCAT(COLUMN_NAME) as field FROM INFORMATION_SCHEMA.STATISTICS s LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t ON t.TABLE_SCHEMA=s.TABLE_SCHEMA AND t.TABLE_NAME=s.TABLE_NAME AND s.INDEX_NAME=t.CONSTRAINT_NAME WHERE 0=0 AND t.CONSTRAINT_NAME IS NULL AND s.TABLE_SCHEMA = DATABASE() AND s.TABLE_NAME='<NAME>' GROUP BY name ORDER BY SEQ_IN_INDEX",
# NB 04.07.16 '_table.sql.index' => [
# NB 04.07.16 'SHOW INDEX FROM `<NAME>`', function(&$r) {
# NB 04.07.16 if ($r['Key_name'] != 'PRIMARY') return [
'fct' => create_function('&$r',join('',array(
'$r["autoincrement"] = $r["extra"] == "auto_increment" ? 1 : 0;',
'$r["name"] = $r["field"];',
- #'debug($r["key"]);',
'$r["uniq"] = $r["key"] == "UNI" ? 1 : 0;',
'$r["key"] = $r["key"] == "PRI" ? 1 : 0;',
))),
,2),
),
-'tables' => "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
-'_tables' => function($Db) {
- #debug($Db->databases());
- $sql = "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'";
+'_tables' => "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
+'tables' => function($Db) {
+ # NB 03.09.16
+ # Handle attach mechnisum
$dbs = $Db->databases();
- if (count($dbs)<2) return $sql;
+ $debug = 0;#$Db->p('db')=='all';
+
+ if (count($dbs)<2) {
+ return "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'";
+ }
+
+ if ($debug) debug($dbs);
+ $sql = '';
+ foreach ($dbs as $db) {
+ $sql .= ($sql ? ' UNION ' : '').str_replace('sqlite_',$db['name'].'.sqlite_',
+ "SELECT '".$db['name'].".'||name as name,type FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'"
+ );
+ }
+ if ($debug) debug($sql);
+
+ return $sql;
},
#'tables' => '.tables',
if ($recurse and is_dir("$path/$file")) {
#print ">>>$path/$file\n";
- foreach (ls_dir("$path/$file",$recurse,$exp) as $l) {
+ foreach (self::ls_dir("$path/$file",$recurse,$exp) as $l) {
#print ">>>$path/$file << $l\n";
$ls[] = "$file/$l";
#$ls[] = $l;