]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 12 Jan 2018 02:52:37 +0000 (02:52 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 12 Jan 2018 02:52:37 +0000 (02:52 +0000)
lib/php/db.php
lib/php/db/types/pgsql.php
lib/php/db/types/sqlite.php

index 64cce67de591bcf955bf5c9ac656bc69d769be53..ee5c71c0db8073af228d69643b7274f507d6f463 100644 (file)
@@ -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;
   }
 
index d55b42616d7340d907f444742e229da42d86f14a..804c2a0c87e301854d18556ad1999c79f40c796c 100644 (file)
@@ -1,11 +1,12 @@
 <?php
 $DB_TYPES['pgsql'] = array (
-'sql.pre' => [
+'schema.pre' => [
   'BEGIN TRANSACTION',
 ],
-'sql.post' => [
+'schema.post' => [
   'COMMIT',
 ],
+'sql.drop' => 'DROP <T.TYPE> IF EXISTS "<T.NAME>" 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 <T.TYPE> IF EXISTS "<T.NAME>" 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'",
 
index c6a72e63c6ab8b782f7d78dc66665502af5f2e5a..5e8efa92b937aecb2839debf841a3d86eae699dd 100644 (file)
@@ -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',