if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`");
# Connect
- if ($this->type('use_path') and !is_readable($this->host)) {
+ if ($this->type('use_path') and ($this->host!=':memory:') and !is_readable($this->host)) {
$this->bye("Can't read database path `".$this->host."` type=`".$this->type."`",false);
}
$require[$type] = 1;
}
+ $db_type = $DB_TYPES[$type];
+
# Dump all array
- if ($key === null) return $DB_TYPES[$type];
+ if ($key === null) return $db_type;
+
+ # Search for specifics from types
+ if (!empty($this->types) and !empty($this->types[$key])) {
+ $db_type[$key] = array_merge((empty($db_type[$key]) ? [] : $db_type[$key]),$this->types[$key]);
+ }
- if (empty($DB_TYPES[$type][$key])) {
+ # Check key exists
+ if (empty($db_type[$key])) {
if ($die) self::bye("db.type(): Unknow key `$key` for type `".$type."`");#.' - '.print_r($DB_TYPES,true));
return;
}
- $value = $DB_TYPES[$type][$key];
+ $value = $db_type[$key];
/*
*/
if (is_scalar($value)) {
}
}
return $value;
- return $DB_TYPES[$type][$key];
+ return $db_type[$key];
}
public function tables() {
#if (!isset($this->tables)) {
$this->_tables = true;
- foreach ($this->conn->query($this->type('tables',true,$this->type),PDO::FETCH_ASSOC) as $row) {
+ $sql = $this->type('tables',true,$this->type);
+ if (is_callable($sql)) $sql = $sql($this);
+
+ foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) {
$name = current($row);
$this->table($name,$row);
}
}
public function sql_name($value) {
+ # no quote if contain comma for db
+ if (strpos($value,'.') !== false) return $value;
+
+ # specific quote
if ($q=$this->type('quote_name')) return $q.$value.$q;
+
+ # default quote
return '"'.$value.'"';
}
return $return;
}
+ public function fct() {
+ }
+
public function databases() {
if (!isset($this->databases)) {
$this->databases = [];
$name = self::p('name','');
if ($sql = $this->type('databases')) {
+ $fct = '';
+ if (is_array($sql)) list($sql,$fct) = count($sql)>1 ? $sql : [$sql[0],''];
$st = $this->conn->prepare($sql);
$st->execute();
$this->databases = [];
while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
- $this->databases[] = $row;
+ if ($fct) $fct($row,$this);
+ if ($row) $this->databases[] = $row;
}
}
#if($format = out::client_type()) self::pset('format',$format);
# Param - Extract dbname from table
- if (preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) {
+ if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) {
self::pset('db',$m[1]);
self::pset('table',$m[2]);
}
'quote_name' => '`',
'quote' => function(&$v) { return "'".str_replace("'","''",$v)."'"; },
+'databases' => [
+ 'PRAGMA database_list',function(&$r) {
+#debug($GLOBALS['Db']->databases());
+ if (empty($r['file'])) $r = null;;
+ unset($r['seq']);
+ },
+],
#'table.sql' => 'SELECT sql FROM sqlite_master WHERE name=\'<NAME>\'',
'table.sql' => "SELECT GROUP_CONCAT(sql,';') FROM sqlite_master WHERE tbl_name='<NAME>'",
,2),
),
-'tables' => "SELECT name,type FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
-#'tables' => 'SELECT name,type FROM sqlite_master WHERE type IN(\'table\',\'view\') AND name NOT LIKE \'sqlite_%\' ORDER BY name',
+#'tables' => "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
+'tables' => function($Db) {
+ #debug($Db->databases());
+ $sql = "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'";
+ $dbs = $Db->databases();
+ if (count($dbs)<2) return $sql;
+},
+#'tables' => '.tables',
'table.fields' => array (
'sql' => 'PRAGMA table_info(\'<NAME>\')',