]> git.nbdom.net Git - nb.git/commitdiff
sqlite attach
authorNicolas Boisselier <nicolas.boisselier@semantico.com>
Fri, 2 Sep 2016 16:26:16 +0000 (17:26 +0100)
committerNicolas Boisselier <nicolas.boisselier@semantico.com>
Fri, 2 Sep 2016 16:26:16 +0000 (17:26 +0100)
etc/dbs.yaml
etc/dbs/ui.php
lib/php/db.php
lib/php/db/types/sqlite.php

index c0e1c98bb1c7f0f0282e3b9506473ad6bb5c759e..6ed8599a1bd3355e025df0b3449bbafcb20cce89 100644 (file)
@@ -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"
index af308515620b2fa82f108b3dd103d4a4ae1d7fe3..6790edb322ea93fc6090b287d05b39cd60e153e5 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 $CONF['_ui'] = array(
   'title' => '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) {
index 182810e019ace258f9c5e94a88949fbf115fe6c4..bfec23962071645cda0e1462cb9e527d5d8dcd3b 100644 (file)
@@ -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]);
     }
index 18a4ce8ee1feb060d9057ce10f9a139948ba8ff5..ee32fc60070528ad77292d8b3978866a11988225 100644 (file)
@@ -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=\'<NAME>\'',
 'table.sql' => "SELECT GROUP_CONCAT(sql,';') FROM sqlite_master WHERE tbl_name='<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(\'<NAME>\')',