]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 9 Sep 2016 00:37:57 +0000 (01:37 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 9 Sep 2016 00:37:57 +0000 (01:37 +0100)
etc/dbs.php
lib/php/db.php
lib/php/db/types/sqlite.php

index 82321a49bdb409af93566d0c2b93cdc81c54d0d5..c6e63d2fc860c8527a22f992084859b9d1063e94 100644 (file)
@@ -7,7 +7,9 @@ $LOCAL_DB = (
   and ! file_exists('/opt/local/var/run/mysql5/mysqld.sock')
 ) ? true : false;
 
-# Look for sqlite db path
+#
+# Search existing DIR_SQLITE
+#
 $DIR_SQLITE = '';
 foreach ([
   '~nico/ownCloud',
@@ -20,14 +22,44 @@ foreach ([
 }
 if ($DIR_SQLITE) $DIR_SQLITE .= '/var/lib/sqlite';
 
-# Includes
+#
+# Includes dbs/*php
+#
 $dir = dirname(__FILE__).'/dbs';
 foreach (nb::ls_dir($dir,'\.php$') as $file) {
   require_once("$dir/$file");
 }
 
 #
-# Db All
+# Create *-sqlite databases
+#
+$host_already_exists = [];
+foreach ($CONF as $id => $db) {
+  if (1
+    and !empty($db['type']) and $db['type'] == 'sqlite'
+    and !empty($db['host']) 
+  ) $host_already_exists[$db['host']] = $id;
+}
+if ($DIR_SQLITE) {
+  foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) {
+
+    if (isset($host_already_exists["$DIR_SQLITE/$file"])) {
+      $CONF[$host_already_exists["$DIR_SQLITE/$file"]]['exec'][] = 'PRAGMA journal_mode=OFF';
+      continue;
+    }
+
+    $id = 'sqlite-'.basename($file,'.db');
+
+    $CONF[$id] = [
+      'host' => "$DIR_SQLITE/$file",
+      'type' => 'sqlite',
+      'title' => 'created by dbs.php',
+    ];
+  }
+}
+
+#
+# Sqlite all Attach db type=sqlite
 #
 #return;
 $CONF['all'] = [
@@ -42,20 +74,26 @@ $CONF['all'] = [
   'types' => [
     'exec' => [
       'PRAGMA busy_timeout = 1000',
-      'PRAGMA synchronous=NORMAL',
+      #'PRAGMA synchronous=NORMAL',
       'PRAGMA journal_mode=MEMORY',
     ],
   ],
 ];
+
+#debug($CONF['postfix']);
 foreach ($CONF as $id => $db) {
+  #debug($db);
   if (0
     or empty($db['type'] )
     or $db['type']!='sqlite'
     or empty($db['host'] )
+    #or !is_readable($fname=basename($db['host'],'.db'))
+    #or !is_readable($fname)
     or !is_readable($db['host'])
   ) continue;
   #debug(basename($db['host'],'.db'));
-  $CONF['all']['types']['exec'][] = "ATTACH DATABASE '".$db['host']."' as ".basename($db['host'],'.db')."";
+  $fname = basename($db['host'],'.db');
+  $CONF['all']['types']['exec'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname."";
   conf_merge($CONF[$id],$CONF['all']);
 
 }
index 78d5be664e334d1f68cf005ea7775ddd78f594f2..2bfc6531201798a71d427c6aeb0c8f4e01be2307 100644 (file)
@@ -37,6 +37,32 @@ class Db extends nb {
   public $conf = [];
   public $row_parse; # Function to call in rows()
 
+  # Params
+  protected static $paliases = [
+    'd' => 'db',
+    't' => 'table',
+    'l' => 'limit',
+    'a' => 'action',
+    'h' => 'header',
+
+    'f' => 'format',
+    'out' => 'format',
+    'o' => 'format',
+  ];
+
+  protected static $action_aliases = [
+    'ls' => 'db.ls',
+    'databases' => 'db.databases',
+
+    'tables' => 'db.tables',
+    'fields' => 'table.fields',
+    'rows' => 'table.rows',
+    'insert' => 'table.insert',
+    'replace' => 'table.replace',
+    'update' => 'table.update',
+    'delete' => 'table.delete',
+  ];
+
   # Web
   public $default_table;
   public $sort;
@@ -96,8 +122,8 @@ class Db extends nb {
   }
 
   public function __destruct() {
-    foreach (array_keys((array)$this) as $k) { if (isset($this->$k)) unset($this->$k); };
     $this->disconnect();
+    foreach (array_keys((array)$this) as $k) { if (isset($this->$k)) unset($this->$k); };
   }
 
        function connect_init() {
@@ -152,7 +178,8 @@ class Db extends nb {
        function disconnect() {
     if (empty($this->conn)) return null;
     #$this->conn->exec('COMMIT');
-    return $this->conn->close();
+    $this->method('disconnect');
+# NB 09.09.16     return $this->conn->close();
   }
 
        function connect() {
@@ -185,7 +212,7 @@ class Db extends nb {
     foreach ($this->sql_exec() as $s) {
       $this->conn->exec($s);
     }
-    #$this->conn->exec('BEGIN');
+    #$this->conn->exec('BEGIN TRANSACTION');
 
     # Create functions
     if ($this->type('sqliteCreateFunction')) {
@@ -387,9 +414,9 @@ class Db extends nb {
     #if (!isset($this->tables)) {
       $this->_tables = true;
 
-      $sql = $this->type('tables',true,$this->type);
-      if (is_callable($sql)) $sql = $sql($this);
-      $sql = $this->unvar($sql);
+# NB 09.09.16       $sql = $this->type('tables',true,$this->type);
+# NB 09.09.16       if (is_callable($sql)) $sql = $sql($this);
+# NB 09.09.16       $sql = $this->unvar($sql);
       $sql = $this->method('tables');
 
       foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) {
@@ -908,32 +935,11 @@ class Db extends nb {
   }
 
   public static function pinit() {
-    self::paliases([
-      'd' => 'db',
-      't' => 'table',
-      'l' => 'limit',
-      'a' => 'action',
-      'h' => 'header',
-
-      'f' => 'format',
-      'out' => 'format',
-      'o' => 'format',
-    ]);
-
-    if ($action=self::p('action')) {
-      foreach ([
-
-        'ls' => 'db.ls',
-        'tables' => 'db.tables',
-
-        'fields' => 'table.fields',
-        'rows' => 'table.rows',
-        'insert' => 'table.insert',
-        'replace' => 'table.replace',
-        'update' => 'table.update',
-        'delete' => 'table.delete',
-
-      ] as $src => $dest) {
+    if (!empty(self::$paliases)) self::paliases(self::$paliases);
+
+    if ($action=self::p('action') and !empty(self::$action_aliases)) {
+      foreach (self::$action_aliases
+      as $src => $dest) {
         if ($action === $src) {
           $action = $dest;
         }
index fc115433223d8176874ecfc0b3fd814a06543b5a..4c4763d127932fee9cab2d29ff2bf43e31d02b5c 100644 (file)
@@ -4,6 +4,13 @@ if (!class_exists('Db')) {
        exit;
 }
 $DB_TYPES['sqlite'] = array (
+'dump.pre' => [
+  'PRAGMA foreign_keys=OFF',
+],
+'_disconnect' => function($db) {
+  #$table->db()->exec("COMMIT");
+  $db->conn->commit();
+},
 'exec' => [
   #'PRAGMA journal_mode = MEMORY',
   #'PRAGMA journal_mode = WAL',
@@ -13,6 +20,7 @@ $DB_TYPES['sqlite'] = array (
   'PRAGMA encoding = "'.strtoupper(Db::$encoding).'"', 
   #'PRAGMA temp_store = 2',
   #'PRAGMA read_uncommitted = false',
+  #'BEGIN TRANSACTION',
 ],
 'use_path' => true,
 'extra_where' => 'denorm',