From: Nicolas Boisselier Date: Fri, 2 Sep 2016 16:26:16 +0000 (+0100) Subject: sqlite attach X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=8f793b68ada535f414462608119802500d3a32b7;p=nb.git sqlite attach --- diff --git a/etc/dbs.yaml b/etc/dbs.yaml index c0e1c98b..6ed8599a 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -41,3 +41,13 @@ izi-dev: #user: nico _import: - izi + +mem: + name: mem + host: ":memory:" + type: sqlite + types: + exec: + - "ATTACH DATABASE '/opt/www/sem_ui/var/db/semantico.db' as ui" + - "ATTACH DATABASE '/home.local/nicoadm/ownCloud/var/lib/sqlite/wp.db' as wp" + - "ATTACH DATABASE '/home.local/nicoadm/ownCloud/var/lib/sqlite/nb.db' as nb" diff --git a/etc/dbs/ui.php b/etc/dbs/ui.php index af308515..6790edb3 100644 --- a/etc/dbs/ui.php +++ b/etc/dbs/ui.php @@ -1,8 +1,8 @@ 'Semantico UI', - 'pdo' => 'sqlite:/opt/www/sem_ui/var/db/semantico.db', - #'type' => 'sqlite', + 'type' => 'sqlite', + 'host' => '/opt/www/sem_ui/var/db/semantico.db', 'default_table' => 'node', 'sql_replace' => function($sql) { diff --git a/lib/php/db.php b/lib/php/db.php index 182810e0..bfec2396 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -151,7 +151,7 @@ class Db extends nb { 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); } @@ -327,15 +327,23 @@ class Db extends nb { $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)) { @@ -346,7 +354,7 @@ class Db extends nb { } } return $value; - return $DB_TYPES[$type][$key]; + return $db_type[$key]; } public function tables() { @@ -355,7 +363,10 @@ class Db extends nb { #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); } @@ -375,7 +386,13 @@ class Db extends nb { } 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.'"'; } @@ -842,6 +859,9 @@ class Db extends nb { return $return; } + public function fct() { + } + public function databases() { if (!isset($this->databases)) { @@ -849,13 +869,16 @@ class Db extends nb { $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; } } @@ -899,7 +922,7 @@ class Db extends nb { #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]); } diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index 18a4ce8e..ee32fc60 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -19,6 +19,13 @@ $DB_TYPES['sqlite'] = array ( '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=\'\'', 'table.sql' => "SELECT GROUP_CONCAT(sql,';') FROM sqlite_master WHERE tbl_name=''", @@ -58,8 +65,14 @@ $DB_TYPES['sqlite'] = array ( ,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(\'\')',