From: Nicolas Boisselier Date: Fri, 9 Sep 2016 00:37:57 +0000 (+0100) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=b986abb42827b167247aa7508f3a535fc474d3aa;p=nb.git Bed --- diff --git a/etc/dbs.php b/etc/dbs.php index 82321a49..c6e63d2f 100644 --- a/etc/dbs.php +++ b/etc/dbs.php @@ -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']); } diff --git a/lib/php/db.php b/lib/php/db.php index 78d5be66..2bfc6531 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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; } diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index fc115433..4c4763d1 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -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',