]> git.nbdom.net Git - nb.git/commitdiff
test
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 3 Sep 2016 22:58:52 +0000 (23:58 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 3 Sep 2016 22:58:52 +0000 (23:58 +0100)
etc/dbs.php
etc/dbs.yaml
etc/dbs/all.php [new file with mode: 0644]
etc/dbs/nb.php
lib/php/db.php
lib/php/db/index.php
lib/php/db/types/mysql.php
lib/php/db/types/sqlite.php
lib/php/nb.php

index 5fa65b0c8e3c11931026ac8e1a538ea2d1328269..7e60ac3c843d9b4b6247a9242ac12607222ac26f 100644 (file)
@@ -1,6 +1,6 @@
 <?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')
@@ -22,22 +22,61 @@ if ($DIR_SQLITE) $DIR_SQLITE .= '/var/lib/sqlite';
 
 # 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];
 }
 
 ?>
index 6ed8599a1bd3355e025df0b3449bbafcb20cce89..c0e1c98bb1c7f0f0282e3b9506473ad6bb5c759e 100644 (file)
@@ -41,13 +41,3 @@ izi-dev:
   #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"
diff --git a/etc/dbs/all.php b/etc/dbs/all.php
new file mode 100644 (file)
index 0000000..23c0a09
--- /dev/null
@@ -0,0 +1,57 @@
+<?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']);
+?>
index 50bdbad501b45a7ac8d6576ba90e59fba99396fc..76b144a518b35b00de7d17be7ad095d0e85fce3c 100644 (file)
@@ -5,8 +5,8 @@ $CONF['nb'] = array(
   'type' => 'mysql',
   'name' => 'nb',
   #'order' => ($LOCAL_DB ? null : 1),
-  'order' => 1,
-  '_import' => array('_nb','_nico'),
+  #'order' => 1,
+  '_import' => ['_nb','_nico'],
 );
 
 $CONF['nb-sqlite'] = array (
@@ -15,11 +15,11 @@ $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));
@@ -121,7 +121,7 @@ function rent_doc($table,&$r) {
 
   ) {
 
-    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))
       );
index e1b9a5e61020394a018aa63c09c821ebd71107b6..1e4953a3caf850f3bf0d91f0e4eb314a733f464a 100644 (file)
@@ -20,9 +20,7 @@ class Db extends nb {
   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;
@@ -39,8 +37,8 @@ class Db extends nb {
   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 = '') {
 
@@ -48,6 +46,12 @@ class Db extends nb {
     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);
@@ -147,7 +151,6 @@ class Db extends nb {
     # 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) {
@@ -300,8 +303,11 @@ class Db extends nb {
     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,
     ];
@@ -309,10 +315,10 @@ class Db extends nb {
     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) {
@@ -323,7 +329,7 @@ class Db extends nb {
     # 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;
     }
@@ -366,6 +372,8 @@ class Db extends nb {
 
       $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);
@@ -531,9 +539,9 @@ class Db extends nb {
 
     # 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_);
     });
 
@@ -951,8 +959,6 @@ class Db extends nb {
       $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;
       }
 
index 30e832ed1e564e58681642372a50a729b4a84012..02656a599a9f78f48e961ec76c8a24fb974eed83 100755 (executable)
@@ -17,7 +17,7 @@ Db::pdef('format',out::client_type());
 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);
index 60f64996a3fdaa0e17a733b1d136c230f8c659bb..881a739eef99413785a7d82e45e2255e1319354f 100644 (file)
@@ -25,7 +25,7 @@ $DB_TYPES['mysql'] = array (
 '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 [
@@ -103,7 +103,6 @@ $DB_TYPES['mysql'] = array (
   '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;',
   ))),
index cef379a8eb14da67297fa42d3fa6d41ed67b38f4..10b0ab8df4a0e3f3ce6d9e9f5bfdbca8a6de3cb9 100644 (file)
@@ -65,12 +65,27 @@ $DB_TYPES['sqlite'] = array (
   ,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',
 
index bd1fe8cc1ad3c3f963254175a47219ca4b4d5f88..b55c244dd1f6e5faf9f64a49978bcb83d6ad4285 100644 (file)
@@ -695,7 +695,7 @@ class NB {
       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;