]> git.nbdom.net Git - nb.git/commitdiff
rename DB_JUST_INIT, fix bugs
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 29 Mar 2016 23:03:09 +0000 (00:03 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 29 Mar 2016 23:03:09 +0000 (00:03 +0100)
lib/css/db.css
lib/php/db.php
lib/php/db/index.php
lib/php/db/table.php

index 5113a0910192560d9f4f2c7cd2fead3da49e69c1..1efb6c756b8ab899a1a96f76e4933884b91a6f01 100644 (file)
@@ -43,6 +43,9 @@
   text-align: center;
 }
 
+table.rows th.key:before { content: "["; }
+table.rows th.key:after { content: "]"; }
+
 table.rows tr.row {
   vertical-align: top;
 }
index b1abb3f152b9a5033841e75507dca7ed9d1651e2..cdfc571f2246cd14fe4a0263284eeddb51926172 100644 (file)
@@ -50,9 +50,10 @@ class Db extends nb {
   function __construct($opt = '') {
 
     # Args
+    if ($opt==='') $opt = array();
     $opt = is_scalar($opt) ? array('pdo' => $opt) : $opt;
 
-    # Tables - We dont want to affect this->tables
+    # Tables - We dont want to affect $this
     $tables = array();
     if (isset($opt['tables'])) {
       $tables = $opt['tables'];
@@ -70,12 +71,14 @@ class Db extends nb {
 
     # Tables - Add missing infos
     if (!empty($tables)) {
-      foreach (array_values($this->tables()) as $name=>$t) {
+      foreach ($this->tables() as $name=>$t) {
         if (empty($tables[$name])) continue;
-        foreach($tables[$name] as $k=>$v) $t->$k = $v;
+        $this->table($name,$tables[$name]);
       }
     }
 
+    # Extras should disapear - NB 29.03.16
+    if (!empty($this->extras)) self::bye($this->extras);
     return true;
   }
 
@@ -137,11 +140,10 @@ class Db extends nb {
     if ($this->type('use_path') and !is_readable($this->name)) {
       $this->bye("Can't read database file `".$this->name."` type=`$this->type`",false);
 
-    } else {
-      $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
-
-      if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
     }
+    $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
+
+    if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
 
     if (empty($this->conn)) return false;
 
@@ -245,11 +247,14 @@ class Db extends nb {
   }
 
   function table($name,$opt=array()) {
-    if (empty($opt['db'])) $opt['db'] = $this;
-# NB 28.03.16     if (sset($this->tables) and array_key_exists($name,$this->tables)) {
-# NB 28.03.16       foreach ($this->tables[$name] as $k=>$v) { $opt[$k] = $v; }
-# NB 28.03.16     }
+# NB 29.03.16     if (empty($opt['db'])) $opt['db'] = $this;
+    if ($this->tables() and array_key_exists($name,$this->tables)) {
+      #if ($opt) bye($opt);
+      if ($opt) $this->tables[$name]->__construct($name,$opt);
+      return $this->tables[$name];
+    }
     return new Table($name,$opt);
+    bye("Unknow table `$name`");
   }
 
   public function localFile() {
@@ -278,6 +283,8 @@ class Db extends nb {
 
     static $require = array();
     if (empty($require[$this->type])) {
+      #if (empty($this->type)) return;
+      if (empty($this->type)) self::bye("Type is required");
       require_once(dirname(__FILE__).'/db/types/'.$this->type.'.php');
       $require[$this->type] = 1;
     }
@@ -295,6 +302,7 @@ class Db extends nb {
   public function tables() {
     if (isset($this->tables)) return $this->tables;
     $this->tables = array();
+
     foreach ($this->conn->query($this->type('tables',true),PDO::FETCH_ASSOC) as $row) {
       $name = current($row);
       $this->tables[$name] = $this->table($name,$row);
@@ -302,6 +310,7 @@ class Db extends nb {
     }
 #bye($this->tables);
     #return array_values($this->tables);
+
     return $this->tables;
   }
 
@@ -592,7 +601,6 @@ EOF;
     #bye($Db->p('db'));
     if (!isset($dbs[$Db->p('db')])) $Db->bye("Can't find db: `".$Db->p('db')."` in `".join(",",array_keys($dbs))."`",false);
     $db = array_merge($dbs[$Db->p('db')],array('dbs'=>array_keys($dbs)));
-    unset($dbs);
 
     $Db->__construct($db);
 
@@ -605,8 +613,8 @@ EOF;
     if (!$Db->p('table') and isset($Db->default_table)) $Db->pset('table',$Db->default_table);
     if (!$Db->p('table') and ($v = array_keys($Db->tables()))) $Db->pset('table',$Db->ar_first($v));
 
-    # NB 10.01.16 $Table = new table($Db->p('table'),array('db' => $Db));
-    if ($Db->p('table')) $Table = $Db->table($Db->p('table'));
+    if (empty($db['tables'][$Db->p('table')])) $db['tables'][$Db->p('table')] = array();
+    if ($Db->p('table')) $Table = $Db->table($Db->p('table'),$db['tables'][$Db->p('table')]);
 
     return true;
 
index a2f4f45c0ab00042e9201a4d335c202c9bc3c19f..5fbeef32993f8d042100023e675d30c3ab963984 100755 (executable)
@@ -22,6 +22,6 @@ if (preg_match('/^(\w+)\.(.*?)$/',Db::p('table'),$m)) {
 #$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','/etc/dbs.yaml' ));
 
-if( defined('DB_JUST_INIT')) return true;
+if( defined('DB_NO_ACTION')) return true;
 return $Db->action($Table);
 ?>
index f96bd3da96c47695c4920f2d68d5d3e8d6ee825d..15281c0dec5d0752b2c182b239fd579f4aa85a73 100644 (file)
@@ -35,51 +35,67 @@ Class Table extends nb {
 
     if (!is_scalar($name)) {
       $opt = $name;
-      $name = $opt['name'];
+      #$name = $opt['name'];
+      $name = isset($opt['name']) ? $opt['name'] : '';
+      if (!empty($opt['name'])) $this->name = $opt['name'];
+
+    } elseif(!empty($name)) {
+      $this->name = $name;
+
     }
+
     #unset($opt['db']); bye($opt);
 
+    // Extras - We dont want to affect $this
+
+    // Extras
+    if ($this->p('extras')==='0') {
+        $this->extras = array();
+
+    } elseif (isset($opt['extras'])) {
+      $this->add_extras($opt['extras']);
+
+    # Extras should disapear - NB 29.03.16
+# NB 29.03.16       } elseif ($this->db()
+# NB 29.03.16         and isset($this->db()->extras)
+# NB 29.03.16         and isset($this->db()->extras[$this->name])
+# NB 29.03.16       ) {
+# NB 29.03.16         $this->add_extras($this->db()->extras[$this->name]);
+
+    }
+    unset($opt['extras']);
+
     // Connection
+#bye($opt['db']);
     if (isset($opt['db'])) {
       $this->db(is_object($opt['db']) ? $opt['db'] : new Db($opt['db']));
+    } elseif(isset($GLOBALS['Db'])) {
+      $this->db($GLOBALS['Db']);
     } else {
       $this->db(new Db());
     }
-
     unset($opt['db']);
+
+    // Add others
+#if (empty($opt)) bye($opt);
     foreach ($opt as $k => $v) { $this->$k = $v; }
 
     // Name, could be a select
-    if (stripos($name,'SELECT ')===0) {
-      #$temp = '_'.substr(md5($name),0,6);
+    if (stripos($this->name,'SELECT ')===0) {
+      #$temp = '_'.substr(md5($this->name),0,6);
       $temp = DB_TABLE_QUERY_NAME;
-      $this->db()->conn->query("CREATE TEMPORARY TABLE $temp AS $name");
-      $name = $temp;
-    } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$name)) {
-      bye("Query not Allowed !");
-    }
-
-    $this->name = $name;
+      $this->db()->conn->query("CREATE TEMPORARY TABLE $temp AS $this->name");
+      $this->name = $temp;
 
-    // Extras
-    if ($this->p('extras')==='0') {
-      $this->extras = array();
-
-    } else if (isset($opt['extras'])) {
-      $this->add_extras($opt['extras']);
-
-    } elseif ($this->db()
-      and isset($this->db()->extras)
-      and isset($this->db()->extras[$this->name])
-    ) {
-      $this->add_extras($this->db()->extras[$this->name]);
+    } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) {
+      bye("Query not Allowed !");
 
     }
 
     // Sort
     if ($this->orderby) $this->pset('orderby',$this->orderby);
 
-    return $this->fields();
+# NB 29.03.16     return $this->fields();
   }
 
   /*
@@ -89,6 +105,7 @@ Class Table extends nb {
    *
    */
   public function db($set=null) {
+#return $GLOBALS['Db'];
     static $db = null;
     if ($set !== null) $db = $set;
     return $db;
@@ -167,7 +184,7 @@ Class Table extends nb {
           'default'       => (isset($row['default']) ? $row['default'] : null),
           'key'           => (preg_match('/^(f.*|no|0)?\s*$/i',$row['key']) ? 0 : 1),
           'null'          => (preg_match('/^(f.*|no|0)?\s*$/i',$row['null']) ? 0 : 1),
-          'extra'         => null, # !!! nothing todo with class variable $extras, this info from the sql server
+          'extra'         => null, # !!! nothing todo with array $extra, this info from the sql server
           'autoincrement' => (isset($row['autoincrement']) ? $row['autoincrement'] : 0),
         );
 
@@ -456,7 +473,7 @@ Class Table extends nb {
         $having[] = "$k$equal$v";
 
       } elseif ($this->db()->type == 'pgsql' and $field->extra) {
-        $where[] = $this->extras[$k]."$equal$v";
+        $where[] = $this->extras[$k]->sql_name()."$equal$v";
 
       } else {
         $where[] = "$k$equal$v";
@@ -486,6 +503,7 @@ Class Table extends nb {
       $this->extras[$k] = $v;
 
     }
+#bye($this->extras);
 
   }
 
@@ -493,9 +511,8 @@ Class Table extends nb {
 
     if (!$this->extras) return '';
 
-    $select = array(); foreach ($this->extras as $k => $v) {
-# NB 27.02.16       $select[] = $v->extra." AS ".$v->quote($k,true);
-      $select[] = $v->extra." AS ".$this->sql_name($k);
+    $select = array(); foreach ($this->extras as $name => $field) {
+      $select[] = $field->extra." AS ".$field->sql_name();
     }
 
     return ','.join(',',$select);
@@ -809,9 +826,9 @@ Class Table extends nb {
 
       if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.NB_EOL;
 
-      foreach (array_keys($fields) as $f) {
+      foreach ($fields as $name => $f) {
         #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.NB_EOL;
-        $html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).'</th>'.NB_EOL;
+        $html .= '<th class="'.$name.($f->key?' key':'').'">'.($this->p('buttons')==='0' ? $this->prettyText($name) : $this->url_sort($name)).'</th>'.NB_EOL;
       }
       if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.NB_EOL;
       $html .= '</tr>'.NB_EOL.'</thead>'.NB_EOL;