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'];
# 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;
}
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;
}
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() {
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;
}
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);
}
#bye($this->tables);
#return array_values($this->tables);
+
return $this->tables;
}
#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);
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;
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();
}
/*
*
*/
public function db($set=null) {
+#return $GLOBALS['Db'];
static $db = null;
if ($set !== null) $db = $set;
return $db;
'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),
);
$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";
$this->extras[$k] = $v;
}
+#bye($this->extras);
}
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);
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;