public $tables;
public $row_parse; # Function to call in rows()
- # Objects
-# NB 28.03.16 public $table;
-
# Web
public $default_table;
public $sort;
return true;
}
+ public function __destruct() {
+ foreach (array_keys((array)$this) as $k) { if (isset($this->$k)) unset($this->$k); };
+ $this->disconnect();
+ }
+
function connect_init() {
if (empty($this->pdo) and $this->type) $this->pdo = $this->type.':';
if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
# Title
if (empty($this->title)) $this->title = prettyText($this->name);
+ }
+
+ function disconnect() {
+ if (empty($this->conn)) return null;
+ return $this->conn->close();
}
function connect() {
return $this->conn->quote($v);
}
+ /**
+ * @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) bye($opt);
if ($opt) $this->tables[$name]->__construct($name,$opt);
return $this->tables[$name];
}
- return new Table($name,$opt);
+
+ return new Table($name,$opt+['db'=>$this]);
}
public function localFile() {
$available = [
['(db.)?help','This help'],
['(db.)?dbs','List databases from conf'],
- #['(db.)?dbs','List databases (name=* type=* all=1)'],
['(db.)?tables','List tables (name=* type=* count=1 engine=*)'],
['(table.)?rows','Dump one table, use format='],
if (!empty($type) and !$this->str_match($t->type,$type)) continue;
if (!empty($count) and !$this->str_match($t->count,$count)) continue;
if (!empty($engine) and !$this->str_match($t->engine,$engine)) continue;
- $rows[] = $t->infos();
+
+ $rows[] = []
+ +[
+ 'name' => $t->name,
+ 'type' => $t->type,
+ ]
+ +$t->infos()
+ +( self::p('fields') ?['fields' => count($t->fields())] : [] )
+ +( self::p('count') ?['count' => $t->count()] : [] )
+ +( self::p('sql') ? ['sql' => $t->sql()] : [] )
+ ;
}
usort($rows,function($a,$b) { return strcmp($a['name'],$b['name']); });
return $h;
}
- //
- // Create globals from $conf=array(yaml_fileds)
- //
- public static function init($conf) {
- global $Db, $Table;
-
- if (isset($Table)) self::bye("Table.init(): GLOBALS['Table'] already exists !");
- if (isset($Db)) self::bye("Db.init(): GLOBALS['Db'] already exists !");
-
- $Db = new self();
+ /**
+ * @copyright NB 12.08.16
+ * Return a db hash to create a new instance from $conf
+ */
+ public static function conf_search_db($conf) {
# Load databases
- $conf = self::is_hash($conf) ? $conf : $Db->conf_dbs($conf);
+ $conf = self::is_hash($conf) ? $conf : self::conf_dbs($conf);
# Check db=pdo
- if (preg_match('/^\w+:/',$Db->p('db'))) {
- $conf[$Db->p('db')] = array(
- 'pdo' => $Db->p('db'),
+ if (preg_match('/^\w+:/',self::p('db'))) {
+ $conf[self::p('db')] = array(
+ 'pdo' => self::p('db'),
);
}
if (!$conf) return false;
# Param - Default base on order hight num value
- if (!$Db->p('db')) {
- $Db->pset('db',$Db->ar_first($conf,true));
+ if (!self::p('db')) {
+ self::pset('db',self::ar_first($conf,true));
}
- if (!isset($conf[$Db->p('db')])) {
- $Db->bye("Can't find db: `".$Db->p('db')."` in `".join(",",array_keys($conf))."`",false);
+ if (!isset($conf[self::p('db')])) {
+ self::bye("Can't find db: `".self::p('db')."` in `".join(",",array_keys($conf))."`",false);
}
- # Connection
- $db = array_merge($conf[$Db->p('db')],array(
+ if (empty($conf[self::p('db')])) return [];
+
+ # Construct assoc array
+ $db = array_merge($conf[self::p('db')],array(
'dbs'=>array_keys($conf),
'conf'=>$conf,
- 'id'=>$Db->p('db'),
+ 'id'=>self::p('db'),
));
+ # Search table
+ #$table = Db::p('table');
+
+ return $db;
+ }
+
+ /**
+ * @copyright NB 12.08.16
+ * Create globals Db and Table
+ */
+ public static function init($conf) {
+ global $Db, $Table;
+ if (isset($Table)) self::bye("Table.init(): GLOBALS['Table'] already exists !");
+ if (isset($Db)) self::bye("Db.init(): GLOBALS['Db'] already exists !");
+ $Db = new self();
+
+ $db = self::conf_search_db($conf);
+ if (empty($Db)) return false;
+
+ # Connection
$Db->__construct($db);
if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`");
/*
Table
*/
- if (empty($Db)) return false;
if (self::p('action') and
!self::p('table') and
!preg_match('/^(table\.\w+|rows|insert|edit|delete|update)$/',self::p('action'))
- ) return true;
- 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));
+ ) {
+ } else {
- 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')]);
+ # Search default_table
+ if (!self::p('table') and isset($Db->default_table)) self::pset('table',$Db->default_table);
- return true;
+ # 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')] )
+ );
+ }
+
+ return true;
}
+ /**
+ * @copyright NB 12.08.16
+ * Transform pdo string into assoc array
+ */
public function pdo_info() {
return preg_replace_callback('/(\w+)=([^;]*)(;?)/',function($m){
- return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
+ return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
},$this->pdo);
}
# Cache before changing db type
$views = [];
foreach ($tables as $k=>$t) {
+
if (
($t->type != 'table' and $t->type != 'view')
or (!empty($name) and !$this->str_match($t->name,$name))
unset($tables[$k]);
continue;
}
+
if ($t->type == 'view') $views[] = $t->name;
unset($t->orderby);
+
$t->fields();
$t->sql();
}
'type' => $this->type,
'host' => $this->host,
#'tables' => count($this->tables()),
- #'dbs' => count(array_keys($this->conf)),
+ #'conf' => count(array_keys($this->conf)),
'tables' => count($this->tables()),
]
+($this->type('use_path') ? array() : array(
}
public static function pinit() {
- Db::paliases(array(
+ self::paliases(array(
'd' => 'db',
't' => 'table',
'f' => 'format',
'h' => 'header',
));
- #if($format = out::client_type()) Db::pset('format',$format);
+ #if($format = out::client_type()) self::pset('format',$format);
# Param - Extract dbname from table
- if (preg_match('/^(\w+)\.(.*?)$/',Db::p('table'),$m)) {
- Db::pset('db',$m[1]);
- Db::pset('table',$m[2]);
+ if (preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) {
+ self::pset('db',$m[1]);
+ self::pset('table',$m[2]);
}
}