]> git.nbdom.net Git - nb.git/commitdiff
table.update
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 Mar 2016 02:37:14 +0000 (02:37 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 Mar 2016 02:37:14 +0000 (02:37 +0000)
lib/php/db/field.php
lib/php/db/table.php
lib/php/functions.php
lib/php/out.php

index 18cd60ce34f9dc36a8e99276518dec93700b363b..c4cd3bf91e59e11109b1dc2ebd95fe4104c180c7 100644 (file)
@@ -19,20 +19,20 @@ class field {
 
   private $autoincrement = false;
 
-  function __construct($attr=array()) {
+  public function __construct($attr=array()) {
     if (is_scalar($attr)) $attr = array('name'=>$attr);
     foreach ($attr as $k => $v) { $this->$k = $v; }
   }
 
-  function autoincrement() {
+  public function autoincrement() {
     return $this->autoincrement;
   }
 
-  function numeric() {
+  public function numeric() {
     return preg_match('/int|float|number|currency/',$this->type) ? true : false;
   }
 
-  function html_edit($value) {
+  public function html_edit($value) {
 
     return '<div class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
       .($this->extra
@@ -43,40 +43,23 @@ class field {
 
   }
 
-  function db() {
+  public function db() {
     global $Db;
     return ( isset($Db) ? $Db : null );
   }
 
-  function sql_name() {
+  public function sql_name() {
     return $this->db()->sql_name($this->name);
   }
 
-  function quote($value,$force_quote=false) {
-
-    if ($value === null) return 'NULL';
-    if ($force_quote or !$this->numeric()) {
-
-      if (($db = $this->db()) === null) return "'".preg_replace("/'/","\\'",$value)."'";
-      return $db->quote($value);
-
-    } else {
-
-      if (strcmp($value,'') == 0) $value = 'NULL';
-      #if ($value === null) $value = 'NULL';
-    }
-
-    return $value;
-  }
-
-  function size() {
+  public function size() {
     if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null;
     array_shift($m);
     return count($m) == 1 ? $m[0] : $m;
 // NB 02.08.15     return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null;
   }
 
-  function where($values) {
+  public function where($values) {
     // No empty value
     $v = isset($value) ? $value : null;
     if (strcmp($v,'')==0
@@ -123,7 +106,41 @@ class field {
     return "$k$equal$v";
   }
 
-  function pdo_type() {
+  public function quote($value,$force_quote=false) {
+
+    if ($value === null) return 'NULL';
+    if ($force_quote or !$this->numeric()) {
+
+      if (($db = $this->db()) === null) return "'".preg_replace("/'/","\\'",$value)."'";
+      return $db->quote($value);
+
+    } else {
+
+      if (strcmp($value,'') == 0) $value = 'NULL';
+      #if ($value === null) $value = 'NULL';
+    }
+
+    return $value;
+  }
+
+  /*
+  # - NB 25.03.16
+  * Return the value quoted
+  public function sql($v) {
+    $type = $this->pdo_type($this->type);
+    if ($type === PDO::PARAM_NULL) {
+      return 'NULL';
+    } elseif ($type === PDO::PARAM_INT) {
+      return $v;
+    } elseif ($type === PDO::PARAM_BOOL) {
+      return $v ? 1 : 0;
+    } else {
+      return "'".str_replace("'","\\'",$v)."'";
+    }
+  }
+  */
+
+  public function pdo_type() {
     if (func_get_args()>0 and func_get_arg(0) === null) return PDO::PARAM_NULL;
     if ($this->numeric()) return PDO::PARAM_INT;
     if (strpos($this->type,'bool') !== false ) return PDO::PARAM_BOOL;
@@ -131,7 +148,7 @@ class field {
     return PDO::PARAM_STR;
   }
 
-  function bindParam(&$sth,$value,$name=null) {
+  public function bindParam(&$sth,$value,$name=null) {
     # See: http://php.net/manual/en/pdostatement.bindparam.php
     if ($name === null) $name = ':'.$this->name;
 #debug($this->name . " | $name | $value | $this->type | " .  $this->size()."<br/>");
@@ -139,7 +156,7 @@ class field {
 // NB 27.07.15     return $sth->bindParam($name, $value, $this->pdo_type($value));
   }
 
-  function out($value,$html_escape=false) {
+  public function out($value,$html_escape=false) {
 
     #if ($value === null) return '';
     static $sqlite = null; if ($sqlite === null) $sqlite = $this->db()->type == 'sqlite';
index 020d3a1bf28b04770f92129f2fa9b750fbfb5d66..04b9c2882684164a03a8dec6162329b5b3c0edfc 100644 (file)
@@ -262,9 +262,9 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
           $field['default'] = $row[$f];
         }
 
-        foreach (array('pk','key') as $f) {
+        foreach (array('pk','key','Key') as $f) {
           if (!isset($row[$f])) continue;
-          $field['key'] = preg_match('/^1|yes|t/i',$row[$f]) ? 1 : 0;
+          $field['key'] = preg_match('/^1|yes|t|PRI/i',$row[$f]) ? 1 : 0;
         }
 
         $this->fields[$field['name']] = new field($field);
@@ -272,7 +272,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       }
 
       if (empty($this->fields)) bye("Table `".$this->name."` does not exists into database ".$this->db->name."!");
-    }
+
+    } # < $this->fields
 
 #bye($this->fields);
     if ($name !== null ) {
@@ -1021,32 +1022,41 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $this->db->exec($sql);
   }
 
-  public function update($hvalues) {
+  public function update($hvalues,&$info=null) {
+  # dbq t=nb.mime_type a=table.update type=application/rss+xml name='RSS - Really Simple Syndication' ext=rss debug=2
     
     $keys = array();
     $keys_values = array();
     $fields = array();
     $fields_values = array();
-    $values = array();
+
+    #$update = array();
+    #$where = array();
 
     foreach ($this->fields() as $name => $field) {
 
-      if (!isset($hvalues[$name])) continue;
+      if (!isset($hvalues[$name])) {
+        if ($field->key) $this->bye("Missing `$name`!");
+        continue;
+      }
+
       $value = $hvalues[$name];
 
       if ($field->key) {
         $keys[] = $name;
-        $keys_values[] = $name;
+        $keys_values[] = $value;
 
       } else {
         $fields[] = $name;
-        $fields_values[] = $name;
+        $fields_values[] = $value;
 
       }
 
     }
 
+    if (empty($fields)) $this->bye("Missing keys!");
     if (empty($keys)) {
+      $this->bye("Missing keys!");
       $keys = $fields;
       $keys_values = $fields_values;
     }
@@ -1057,14 +1067,6 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       .' WHERE ' . join(' AND ',$this->ar_map('"$a=:key_$a"',$keys))
     ;
 
-    $sql =  ''
-      .'SELECT * FROM ' . $this->sql_name()
-      .' WHERE ' . join(' AND ',$this->ar_map('"$a=:key_$a"',$keys))
-    ;
-
-    #bye($sql);
-    $this->debug($sql,1);
-
     if (!($query = $this->db->conn->prepare($sql))) {
       err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .NB_EOL);
       return false;
@@ -1072,25 +1074,25 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
      
     foreach ($fields as $name) {
       #debug("$name: ".$hvalues[$name]);
-      #$field->bindParam($query,$hvalues[$name],":$name");
+      $field->bindParam($query,$hvalues[$name],":$name");
     }
 
     foreach ($keys as $name) {
       #debug("$name: ".$hvalues[$name]);
-      #$field->bindParam($query,$hvalues[$name],":key_$name");
-      $query->bindValue(":key_$name", $hvalues[$name], PDO::PARAM_STR);
+      $field->bindParam($query,$hvalues[$name],":key_$name");
     }
 
     #return $sql;
     #bye($sql);
-    if (!($execute = $query->execute())) {
+    if (!($ex = $query->execute())) {
       err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .NB_EOL);
       return false;
     }
 
     #debug($execute);
-    debug($query->fetch());
-    return $execute;
+    $info = array('sql'=>$sql,'values'=>$hvalues,'return'=>$query->rowCount());
+    $this->debug($info,1);
+    return $query->rowCount();
 
   }
 
@@ -1150,14 +1152,16 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     } elseif ($action == 'table.update' or $action == 'update') {
       #$this->bye($this->p());
-      $this->update($this->p());
-      if ($this->p('referer')) {
-        header('Location: '.urldecode($this->p('referer')));
-      } else {
-        header('Location: ?table=' . $this->name . ($this->p('db') ? '&db='.$this->p('db') : ''));
+      if ($r=$this->update($this->p(),$out)) {
+        if ($this->p('referer')) {
+          header('Location: '.urldecode($this->p('referer')));
+        } else {
+          header('Location: ?table=' . $this->name . ($this->p('db') ? '&db='.$this->p('db') : ''));
+        }
       }
       #header('Location: '.str_replace('&amp;','&',$this->url_list()));
-      return true;
+      $this->out2($out);
+      return $r;
 
     } elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
 
index 22083257a958390ed5c5892013d9ee24c4c85f86..bf13f2c46d9cd29cb13b2c37e5c5ba6986f2174b 100644 (file)
@@ -9,18 +9,17 @@ s/array_key_exists(\([^,]\+\),\([^)]\+\))/isset(\2[\1])/g
 s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/
 
 */
-if (!defined('DEBUG')) define('DEBUG',(int)nb::p('debug'));
 // NB 28.06.15 require_once((dirname(__FILE__).'/default.php'));
 
 function file_write($file,$data,$mode='w') {
 
   if (!$ftmp = fopen($file,$mode)) {
-    if (DEBUG) warn("file_write(): Cant open $file in mode $mode");
+    bye("file_write(): Cant open $file in mode $mode");
     return;
   }
 
   if (fwrite($ftmp,$data) === FALSE) {
-    if (DEBUG) warn("file_write(): Cant write $file in mode $mode");
+    bye("file_write(): Cant write $file in mode $mode");
     return;
   }
 
@@ -129,7 +128,7 @@ function err($msg='__err__',$preff='err',$backtrace_deep=0) {
 }
 
 function debug($msg,$level=0) {
-  if ($level and $level>DEBUG) return;
+  if ($level and $level>nb::p('debug')) return;
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
 
   if (nb::client_header('Accept','ml')) {
index b47a28335b99b1bd49f827ac5e02d0084e163f06..1b6844c5d2b0fbe21c6b778cc7668df8ef63bb9f 100644 (file)
@@ -109,6 +109,11 @@ class Out extends Nb {
     if (isset($o['enclose'])) echo $o['enclose'][1];
   }
 
+  public static function rows_get($type,$data,$head=array()) {
+    ob_start();
+    self::rows($type,$data,$head);
+    return ob_get_flush();
+  }
   public static function rows($type,&$data,$head=array()) {
 
     # Ex: for action=tables when header=0