]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Aug 2016 22:15:49 +0000 (23:15 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Aug 2016 22:15:49 +0000 (23:15 +0100)
etc/dbs.php
etc/dbs/nb.php
lib/php/db.php
lib/php/db/table.php
lib/php/nb.php

index 9b83f03c44d0c19f48809ea1a6793f1ede5f57d8..4b0fc1e7952ba89c40fd86a3eafa668bab80544c 100644 (file)
@@ -1,5 +1,11 @@
 <?php
 $CONF = array();
+#$dev = (bool)(stripos(gethostname(),'macbook') !== false);
+$LOCAL_DB = (
+  file_exists('/opt/local/var/run/mysql5')
+  and ! file_exists('/opt/local/var/run/mysql5/mysqld.sock')
+) ? true : false;
+
 $dir = dirname(__FILE__).'/dbs';
 foreach (array(
   'postfix',
index a6b29b00cd7775db1cc9e0f26a974acb4f83ce76..4cd914e54701c10763ff1bee7a7b2ba3278e312a 100644 (file)
@@ -1,15 +1,22 @@
 <?php
+
+#bye($LOCAL_DB);
 $CONF['nb'] = array(
   'host' => 'admin.izideal.vpn',
   'type' => 'mysql',
   'name' => 'nb',
-  'order' => '6',
+  #'order' => ($LOCAL_DB ? null : 1),
+  'order' => 1,
   '_import' => array('_rent','_nico'),
 );
 
 $CONF['nb-sqlite'] = array (
-  'order' => 5,
   'pdo' => 'sqlite:/opt/rent/nb.db',
   '_import' => '_rent',
 );
+
+if ($LOCAL_DB and !empty($CONF['nb-sqlite'])) {
+  $CONF['nb-sqlite']['order'] = isset($CONF['nb']['order']) ? $CONF['nb']['order'] : 1;
+  unset($CONF['nb']['order']);
+}
 ?>
index e3d51d02988565e06029d36e844922f6390397c9..094c45e84bb342a4396f6cf05dfbd301145b0a94 100644 (file)
@@ -32,7 +32,7 @@ class Db extends nb {
   public $title;
   public $name;
   public static $type;
-  public $tables;
+  public $tables = [];
   public $row_parse; # Function to call in rows()
 
   # Web
@@ -49,9 +49,8 @@ class Db extends nb {
     $opt = is_scalar($opt) ? array('pdo' => $opt) : $opt;
 
     # Tables - We dont want to affect $this
-    $tables = array();
     if (isset($opt['tables'])) {
-      $tables = $opt['tables'];
+      foreach ($opt['tables'] as $name=>$param) $this->table($name,$param);
       unset($opt['tables']);
     }
 
@@ -75,32 +74,9 @@ class Db extends nb {
 
     # Pdo
     $this->connect_init();
-    if (empty($this->conn)) $this->connect();
+    if (empty($this->conn) and empty($this->_no_connect)) $this->connect();
 
-    # Tables - Add missing infos
-    if (!empty($tables)) {
-
-      foreach ($this->tables() as $name=>$t) {
-      #foreach (array_merge(array_keys($this->tables()),[]) as $name) {
-      #foreach (array_merge(array_keys($this->tables()),array_keys($tables)) as $name) {
-
-        if (empty($tables[$name])) continue;
-        $this->table($name,$tables[$name]);
-
-      }
-
-      # Virtual - NB 04.07.16
-      foreach ($tables as $name => $t) {
-        if (empty($t['sql'])) continue;
-        #$sql = $t['sql']; unset($t['sql']);
-        $t = $this->table($name,$t);
-        #$t->sql = $sql;
-        $this->tables[$name] = $t;
-      }
-
-    }
-
-    # Extras should disapear - NB 29.03.16
+    # Extras must disapear - NB 29.03.16
     if (!empty($this->extras)) self::bye($this->extras);
     return true;
   }
@@ -293,13 +269,11 @@ class Db extends nb {
   * @author NB 12.08.16
   * Return a table instance
   */
-  function table($name,$opt=array()) {
-    if ($this->tables() and array_key_exists($name,$this->tables)) {
-      if ($opt) $this->tables[$name]->__construct($name,$opt);
-      return $this->tables[$name];
-    }
+  public function table($name,$params=array()) {
+    if (!array_key_exists($name,$this->tables)) $this->tables[$name] = new Table($name,['db'=>$this]+$params);
+    elseif ($params) $this->tables[$name]->__construct($name,$params);
 
-    return new Table($name,$opt+['db'=>$this]);
+    return $this->tables[$name];
   }
 
   public static function localFile($name='') {
@@ -349,12 +323,13 @@ class Db extends nb {
 
   public function tables() {
 
-    if (!isset($this->tables)) {
-      $this->tables = array();
+    if (!isset($this->_tables)) {
+    #if (!isset($this->tables)) {
+      $this->_tables = true;
 
       foreach ($this->conn->query($this->type('tables',true,self::$type),PDO::FETCH_ASSOC) as $row) {
         $name = current($row);
-        $this->tables[$name] = $this->table($name,$row);
+        $this->table($name,$row);
       }
 
     }
@@ -384,7 +359,7 @@ class Db extends nb {
   public function action($action,$table=null) {
     $available = [
       ['(db.)?help','This help'],
-      ['(db.)?dbs','List databases from conf'],
+      ['(db.)?ls','List databases from conf'],
       ['(db.)?tables','List tables (name=* type=* count=1 engine=*)'],
 
       ['(table.)?rows','Dump one table, use format='],
@@ -439,34 +414,6 @@ class Db extends nb {
         } elseif ($action == 'db.conf') {
           $return = $this->out(array_values($this->conf));
 
-        } elseif ($action == 'db.dbs' or $action == 'dbs') {
-          if (true or self::p('hash') or self::p('all')) {
-
-            $fields = ['id','name','title','type','host','order'];
-            $dbs = [];
-            $name = self::p('name','');
-            $type = self::p('type','');
-
-            foreach ($this->conf as $id => $attr) {
-              $d = new Db();
-              foreach ($attr as $k=>$v) $d->$k = $v;
-              $d->connect_init();
-              $db = []; $d->id = $id;
-
-              foreach ($fields as $k) {
-                if (!isset($d->$k)) continue; $db[$k] = $d->$k;
-              }
-
-              if (!empty($name) and !$this->str_match($db['name'],$name)) continue;
-              if (!empty($type) and !$this->str_match($db['type'],$type)) continue;
-              $dbs[] = $db;
-            }
-
-            $return = $this->out($dbs,$fields);
-
-          }
-          else $return = $this->out(array_keys($this->conf),"id");
-
         } elseif ($r=self::class_action_out($this,$action) !== null) {
           return $r;
 
@@ -536,13 +483,12 @@ class Db extends nb {
       if (empty($o['id'])) $h[$db]['id'] = $db;
     }
 
-    # Sort
-
+    # 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_ = isset($a["order"]) ? $a["order"] : -1;
-      $b_ = isset($b["order"]) ? $b["order"] : -1;
-      return($b_-$a_);
+      $a_ = !empty($a["order"]) ? $a["order"] : 9999999;
+      $b_ = !empty($b["order"]) ? $b["order"] : 9999999;
+      return($a_-$b_);
     });
 
     if (!$h) return false;
@@ -639,8 +585,8 @@ class Db extends nb {
       # Choose first table
       if (!self::p('table') and ($v = array_keys($Db->tables()))) self::pset('table',$Db->ar_first($v));
 
-      if ($Db->p('table')) $Table = $Db->table($Db->p('table'),
-        ( empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')] )
+      if (self::p('table')) $Table = $Db->table(self::p('table'),
+        empty($db['tables'][$Db->p('table')]) ? [] : $db['tables'][$Db->p('table')]
       );
 
     }
@@ -891,16 +837,36 @@ class Db extends nb {
   }
 
   public static function pinit() {
-    self::paliases(array(
+    self::paliases([
       'd' => 'db',
       't' => 'table',
       'f' => 'format',
-      'out' => 'format',
       'l' => 'limit',
       'a' => 'action',
       'h' => 'header',
-    ));
+      'out' => 'format',
+    ]);
+
+  if ($action=self::p('action')) {
+    foreach ([
+
+      'ls' => 'db.ls',
+      'tables' => 'db.tables',
 
+      'fields' => 'table.fields',
+      'rows' => 'table.rows',
+      'insert' => 'table.insert',
+      'replace' => 'table.replace',
+      'update' => 'table.update',
+      'delete' => 'table.delete',
+
+    ] as $src => $dest) {
+      if ($action === $src) {
+        $action = $dest;
+      }
+      self::pset('action',$action);
+    }
+  }
     #if($format = out::client_type()) self::pset('format',$format);
 
     # Param - Extract dbname from table
@@ -917,6 +883,31 @@ class Db extends nb {
 
   }
 
+  public function ls(&$fields=[]) {
+
+    $fields = ['id','name','title','type','host','order'];
+    $dbs = [];
+    $name = self::p('name','');
+    $type = self::p('type','');
+
+    foreach ($this->conf as $id => $attr) {
+      $attr['_no_connect'] = true;
+      $attr['id'] = $id;
+      $d = new Db($attr);
+      if (!empty($name) and !$this->str_match($d->name,$name)) continue;
+      if (!empty($type) and !$this->str_match($d->type,$type)) continue;
+      $db = [];
+
+      foreach ($fields as $k) {
+        if (!isset($d->$k)) continue; $db[$k] = $d->$k;
+      }
+
+      $dbs[] = $db;
+    }
+
+    return $dbs;
+  }
+
 } # < Class
 
 ?>
index 2003e4ecadc907ffb18c2402791718e1e58a9e51..723654a0fb8cfb1caf849c5b7687c2a799fef31d 100644 (file)
@@ -1386,7 +1386,7 @@ Class Table extends nb {
 # NB 23.05.16       }
 # NB 23.05.16     };
 
-    if ($action == 'table.fields' or $action == 'fields') {
+    if ($action == 'table.fields') {
       $rows = array_values($this->object2array($this->fields()));
 
       list($sql,$where,$limit,$select_count) = $this->rows_sql();
@@ -1414,32 +1414,32 @@ Class Table extends nb {
       #return $this->out(array_values($this->object2array($this->fields())));
       return $this->out($rows);
 
-    } elseif ($action == 'table.rows' or $action == 'rows') {
+    } elseif ($action == 'table.rows') {
       $this->db()->print_header($this->p('format'));
       $this->rows(); return true;
 
     } elseif ($action == 'table.info') {
       return $this->out($this->info());
 
-    } elseif ($action == 'table.delete' or $action == 'delete') {
+    } elseif ($action == 'table.delete') {
       if (!$this->delete($this->p(),$e)) bye($e);
       header('Location: '.$this->url_referer(str_replace('&amp;','&',$this->url_list())));
       $this->out($e);
       return true;
 
-    } elseif ($action == 'table.replace' or $action == 'replace') {
+    } elseif ($action == 'table.replace') {
       if (!$this->replace($this->p(),$e)) bye($e);
       header('Location: '.$this->url_referer());
       $this->out($e);
       return true;
 
-    } elseif ($action == 'table.insert' or $action == 'insert') {
+    } elseif ($action == 'table.insert') {
       if (!$this->insert($this->p(),$e)) bye($e);
       header('Location: '.$this->url_referer());
       $this->out($e);
       return true;
 
-    } elseif ($action == 'table.update' or $action == 'update') {
+    } elseif ($action == 'table.update') {
       #$this->bye($this->p());
       if (!$this->update($this->p(),$e)) bye($e);
       header('Location: '.$this->url_referer());
index 0a344f796769133c0672ad8573696e0da8068891..21ec681609835def7fdfba17536e98d4e3cf45e6 100644 (file)
@@ -62,9 +62,9 @@ class NB {
    */
   public static function paliases($aliases) {
     foreach ($aliases as $short=>$long) {
-      if (!preg_match('/^\s*$/',self::p($short))) DB::pset($long,Db::p($short));
-      #echo "===$short => $long\n";
-      Db::pset($short,null);
+      if (!preg_match('/^\s*$/',self::p($short))) self::pset($long,self::p($short));
+      #if (!preg_match('/^\s*$/',self::p($short))) echo ">$long => short=".self::p($short);
+      self::pset($short,null);
     }
   }