From e9f549e9d1d2833dfde6f000913d7bf859b6124d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 30 Mar 2016 00:03:09 +0100 Subject: [PATCH] rename DB_JUST_INIT, fix bugs --- lib/css/db.css | 3 ++ lib/php/db.php | 36 ++++++++++++-------- lib/php/db/index.php | 2 +- lib/php/db/table.php | 79 +++++++++++++++++++++++++++----------------- 4 files changed, 74 insertions(+), 46 deletions(-) diff --git a/lib/css/db.css b/lib/css/db.css index 5113a091..1efb6c75 100644 --- a/lib/css/db.css +++ b/lib/css/db.css @@ -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; } diff --git a/lib/php/db.php b/lib/php/db.php index b1abb3f1..cdfc571f 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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; diff --git a/lib/php/db/index.php b/lib/php/db/index.php index a2f4f45c..5fbeef32 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -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); ?> diff --git a/lib/php/db/table.php b/lib/php/db/table.php index f96bd3da..15281c0d 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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 .= ''.NB_EOL; - foreach (array_keys($fields) as $f) { + foreach ($fields as $name => $f) { #$html .= ''.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).''.NB_EOL; - $html .= ''.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).''.NB_EOL; + $html .= ''.($this->p('buttons')==='0' ? $this->prettyText($name) : $this->url_sort($name)).''.NB_EOL; } if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= ''.NB_EOL; $html .= ''.NB_EOL.''.NB_EOL; -- 2.47.3