# Database infos
public $title;
public $name;
- public static $type;
+ public $type;
public $tables = [];
public $row_parse; # Function to call in rows()
function __construct($opt = '') {
# Args
- self::$type = null;
if ($opt==='') $opt = array();
$opt = is_scalar($opt) ? array('pdo' => $opt) : $opt;
}
# Statics
- if (isset($opt['type'])) {
- self::$type = $opt['type'];
+ #$this->type = null;
+ if (isset($opt['Ztype'])) {
+ $this->type = $opt['type'];
unset($opt['type']);
}
# Args
# Pdo
$this->connect_init();
- if (empty($this->conn) and empty($this->_no_connect)) $this->connect();
# Extras must disapear - NB 29.03.16
if (!empty($this->extras)) self::bye($this->extras);
}
function connect_init() {
- if (empty($this->pdo) and self::$type) $this->pdo = self::$type.':';
- if (empty(self::$type)) self::$type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
- if (!self::$type) return false;
+ if (empty($this->pdo) and $this->type) $this->pdo = $this->type.':';
+ if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
+ if (!$this->type) return false;
if ($this->type('use_path')) {
if ($p = $this->p('db.host')) $this->host = $p;
if (empty($this->host)) $this->host = preg_replace('/^\w+:/','',$this->pdo);
# Add file
- if (trim($this->pdo,'pdo:'.self::$type)=='') $this->pdo .= $this->host;
+ if (trim($this->pdo,'pdo:'.$this->type)=='') $this->pdo .= $this->host;
} else {
function connect() {
+ if (!empty($this->conn)) return false;
# Pdo
if (empty($this->pdo)) return false;
# Type
- if (!$this->type(null)) $this->bye("Unknow type = `".self::$type."`");
+ if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`");
# Connect
if ($this->type('use_path') and !is_readable($this->host)) {
- $this->bye("Can't read database path `".$this->host."` type=`".self::$type."`",false);
+ $this->bye("Can't read database path `".$this->host."` type=`".$this->type."`",false);
}
try {
return $return;
}
- public static function type($key=null,$die=false,$type=null) {
+ private function unvar($value) {
+ return $value;
+ if (!is_scalar($value)) return $value;
+ $replace = [
+ '<NAME>' => $this->name,
+ '<DB>' => $this->name,
+ '<TYPE>' => $this->type,
+ '<ENCODING>' => self::$encoding,
+ ];
+ debug( str_replace(array_keys($replace),array_values($replace),$value) );
+ return str_replace(array_keys($replace),array_values($replace),$value);
+ }
+
+ public function type($key=null,$die=false,$type=null) {
global $DB_TYPES;
- if (!$type) $type = self::$type;
+ if (!$type) $type = $this->type;
if ($key == 'type') return $type;
# Load php file
return;
}
+ $value = $DB_TYPES[$type][$key];
+ /*
+ */
+ if (is_scalar($value)) {
+ $value = $this->unvar($value);
+ } elseif (is_scalar($value) and !$this->is_hash($value)) {
+ for ($i=0;$i<count($value);$i++) {
+ if (is_scalar($value[$i])) $value[$i] = $this->unvar($value[$i]);
+ }
+ }
+ return $value;
return $DB_TYPES[$type][$key];
}
#if (!isset($this->tables)) {
$this->_tables = true;
- foreach ($this->conn->query($this->type('tables',true,self::$type),PDO::FETCH_ASSOC) as $row) {
+ foreach ($this->conn->query($this->type('tables',true,$this->type),PDO::FETCH_ASSOC) as $row) {
$name = current($row);
$this->table($name,$row);
}
}
- #if ( ($yaml = @yaml_parse_file($file)) ) $h = array_replace_recursive($h,$yaml);
}
unset($yaml,$CONF);
$Db = new self();
if (empty($Db)) return false;
- $db = self::conf_search_db($conf);
- if (self::p('localFile')) {
- foreach (self::localFile($db['name']) as $k=>$v) {
+ $db = $Db->conf_search_db($conf);
+ if ($Db->p('localFile')) {
+ foreach ($Db->localFile($db['name']) as $k=>$v) {
if (empty($db[$k])) $db[$k] = $v;
}
}
# Connection
$Db->__construct($db);
-
- if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`");
+ if (empty($Db->_no_connect)) {
+ if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`");
+ $Db->connect();
+ }
/*
Table
$type_from = $type_to = '';
if (self::p('db.type')) {
echo "-- Type : ".self::p('db.type')."\n";
- $type_from = self::$type;
+ $type_from = $this->type;
$type_to = self::p('db.type');
#$this->type_out = self::p('db.type');
- self::$type = self::p('db.type');
+ $this->type = self::p('db.type');
#$this->type_out = self::p('db.type');
}
echo $t->sql_create;
if ($insert and $t->type == 'table') {
- if ($type_from) self::$type = $type_from;
+ if ($type_from) $this->type = $type_from;
$t->rows();
- if ($type_to) self::$type = $type_to;
+ if ($type_to) $this->type = $type_to;
}
}
+[
'id' => (empty($this->id) ? '' : $this->id),
'name' => $this->name,
- 'type' => self::$type,
+ 'type' => $this->type,
'host' => $this->host,
#'tables' => count($this->tables()),
#'conf' => count(array_keys($this->conf)),
$attr['_no_connect'] = true;
$attr['id'] = $id;
$d = new Db($attr);
- #debug([$d->id,$d::$type]);
$db = [];
foreach ($fields as $k) {
#if (isset($d::$k)) $db[$k] = $d::$k;
- if ($k=='type' and isset($d::$type)) $db[$k] = $d::$type;
- elseif (isset($d->$k)) $db[$k] = $d->$k;
+ #if ($k=='type' and isset($d::$type)) $db[$k] = $d::$type;
+ if (isset($d->$k)) $db[$k] = $d->$k;
}
if (!empty($name) and !$this->str_match($db['name'],$name)) continue;