From fa43121c1a7ff5ff03f7fda332a05d9f8fd186b8 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 12 Jan 2018 02:52:37 +0000 Subject: [PATCH] lib/php/db.php --- lib/php/db.php | 45 +++++++++++++++++-------------------- lib/php/db/types/pgsql.php | 6 ++--- lib/php/db/types/sqlite.php | 4 ++-- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 64cce67d..ee5c71c0 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -282,23 +282,14 @@ class Db extends nb { } } - #try { - #foreach ($this->sql_pre() as $s) { $this->conn->exec($s); } $this->method('connect'); - #} catch (PDOException $e) { - #return false; - #} return true; } public function disconnect() { if (empty($this->conn)) return null; -# NB 19.12.16 foreach ((array)$this->method('sql.post') as $s) { -# NB 19.12.16 $r = $this->conn->exec($s); -# NB 19.12.16 } $this->method('disconnect'); -# NB 09.09.16 return $this->conn->close(); } function exec($sql) { @@ -425,7 +416,6 @@ class Db extends nb { return str_replace(array_keys($replace),array_values($replace),$value); } -# NB 04.04.17 public function method($key=null,$args=null) { public function method($key=null) { $method = $this->conf_type($key); if (is_callable($method)) return $this->unvar($method($this)); @@ -1042,17 +1032,9 @@ class Db extends nb { 'format' => 'sql', 'db_type_from' => $type_from, ]; - # NB 10.01.18: Just create problems ! should be used ony for CREATE sql -# NB 10.01.18 if ($fct = $this->conf_type('table.sql.create')) { -# NB 10.01.18 $row_opt['fct'] = $fct; -# NB 10.01.18 } # SQL_PRE - $i = 0; foreach ($this->sql_pre() as $s) { - if (($i++) == 0) echo "\n-- SQL_PRE\n"; - echo rtrim($s,';').";\n"; - } - $i = 0; foreach ((array)$this->method('sql.pre') as $s) { + $i = 0; foreach ((array)$this->method('schema.pre') as $s) { if (($i++) == 0) echo "\n-- SQL.PRE\n"; echo rtrim($s,';').";\n"; } @@ -1065,7 +1047,7 @@ class Db extends nb { $create = rtrim($t->create(false),';'); $create = str_replace(';CREATE',";\nCREATE",$create); - if (!($drop = $t->unvar($this->method('sql.drop')))) $drop = + if (!($drop = $this->method('sql.drop'))) $drop = 'DROP '.strtoupper($t->type).' IF EXISTS '.$t->sql_name() ; @@ -1080,16 +1062,13 @@ class Db extends nb { # INSERT if ($insert and $t->type == 'table') { -# NB 04.12.17: Use instead "cleaner" option db_type_from -# NB 04.12.17 if ($type_from) $this->type = $type_from; $o = $row_opt; $t->rows($o); -# NB 04.12.17 if ($type_to) $this->type = $type_to; } } - $i = 0; foreach ((array)$this->method('sql.post') as $s) { + $i = 0; foreach ((array)$this->method('schema.post') as $s) { if (($i++) == 0) echo "\n-- SQL.POST\n"; echo rtrim($s,';').";\n"; } @@ -1188,16 +1167,32 @@ class Db extends nb { public function sql_pre() { $return = []; + foreach ((array)$this->conf_type('sql_pre') as $s) { + if (!empty($s)) $return[] = $s; + } + if (!empty($this->sql_pre)) { foreach ((array)$this->sql_pre as $s) { if (!empty($s)) $return[] = $s; } } - foreach ((array)$this->conf_type('sql_pre') as $s) { + return $return; + } + + public function sql_post() { + $return = []; + + foreach ((array)$this->conf_type('sql_post') as $s) { if (!empty($s)) $return[] = $s; } + if (!empty($this->sql_post)) { + foreach ((array)$this->sql_post as $s) { + if (!empty($s)) $return[] = $s; + } + } + return $return; } diff --git a/lib/php/db/types/pgsql.php b/lib/php/db/types/pgsql.php index d55b4261..804c2a0c 100644 --- a/lib/php/db/types/pgsql.php +++ b/lib/php/db/types/pgsql.php @@ -1,11 +1,12 @@ [ +'schema.pre' => [ 'BEGIN TRANSACTION', ], -'sql.post' => [ +'schema.post' => [ 'COMMIT', ], +'sql.drop' => 'DROP IF EXISTS "" CASCADE', 'delete_no_limit' => true, 'replace_insert' => function(&$sql,&$table,$fields=[]) { $keys = $table->fields_keys($others); @@ -23,7 +24,6 @@ $DB_TYPES['pgsql'] = array ( 'databases' => 'SELECT datname as name,pg_catalog.pg_get_userbyid(datdba) as owner,pg_catalog.pg_encoding_encoding) as encoding, datcollate as "collate",datctype as "Ctype" FROM pg_catalog.pg_database', -'sql.drop' => 'DROP IF EXISTS "" CASCADE', # NB 10.01.18 'tables' => "SELECT table_name as name,LOWER(CASE table_type WHEN 'BASE TABLE' THEN 'TABLE' ELSE table_type END) as type,table_type FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema = 'public'", 'tables' => "SELECT table_name as name,LOWER(CASE table_type WHEN 'BASE TABLE' THEN 'TABLE' ELSE table_type END) as type FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema = 'public'", diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index c6a72e63..5e8efa92 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -5,11 +5,11 @@ if (!class_exists('Db')) { } $DB_TYPES['sqlite'] = array ( 'use_path' => true, -'sql.pre' => [ +'schema.pre' => [ 'PRAGMA foreign_keys=OFF', 'BEGIN TRANSACTION', ], -'sql.post' => [ +'schema.post' => [ 'COMMIT', ], 'extra_where' => 'denorm', -- 2.47.3