Add insertInto and update as special methods
authorJoey Schulze <joey@infodrom.org>
Fri, 4 Apr 2014 18:40:58 +0000 (18:40 +0000)
committerJoey Schulze <joey@infodrom.org>
Fri, 4 Apr 2014 18:40:58 +0000 (18:40 +0000)
class/database.class.php

index 8b08da6..00eba85 100644 (file)
@@ -36,7 +36,7 @@ class Database {
     if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
   }
 
-  private function handleError($sth, $sql)
+  private function handleError($sth, $sql, $data=false)
   {
     if (!$this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
     $arr = $this->db->errorInfo();
@@ -68,6 +68,7 @@ class Database {
       if (count($_POST)) $body .= 'POST: ' . var_export($_POST,true)."\n";
       if (count($_GET)) $body .= 'GET: ' . var_export($_GET,true)."\n";
       if (count($_SESSION)) $body .= 'SESSION: ' . var_export($_SESSION,true)."\n";
+      if ($data) $body .= 'data: ' . var_export($data,true)."\n";
 
       $mail->send($body);
     }
@@ -160,16 +161,28 @@ class Database {
     $sth = $this->db->prepare($sql);
     if ($sth === false) return $this->handleError($sth,$sql);
 
-    foreach ($data as $k => $v)
-      $sth->bindValue(':'.$k, $v); // , PDO::PARAM_STR);
+    foreach ($data as $key => $value) {
+      if(is_int($value))
+       $param = PDO::PARAM_INT;
+      elseif(is_bool($value))
+       $param = PDO::PARAM_BOOL;
+      elseif(is_null($value))
+       $param = PDO::PARAM_NULL;
+      elseif(is_string($value))
+       $param = PDO::PARAM_STR;
+      else
+       $param = FALSE;
+
+      $sth->bindValue(':'.$key, $value, $param);
+    }
 
-    $ok = $this->db->execute();
-    if ($ok === false) return $this->handleError($sth,$sql);
+    $ok = $sth->execute();
+    if ($ok === false) return $this->handleError($sth,$sql,$data);
 
     if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches))
       $this->lastInsertTable = $matches[1];
 
-    return $sth;
+    return $ok;
   }
 
   public function insertInto($table, $data)
@@ -187,27 +200,22 @@ class Database {
                   implode(',', $columns),
                   implode(',', $values));
     return $this->executeBind($sql, $data);
+  }
 
-    /*
-   $db = new PDO('sqlsrv:server=SQLSERVERNAME;Database=own_exchange', 'user', 'password');
-   $sql = "INSERT INTO dbo.files(file_name, file_source) VALUES(:file_name, :file_source)";
-   $stmt = $db->prepare($sql);
-   $stmt->bindParam(":file_name", $files->name, PDO::PARAM_STR);
-   $stmt->bindParam(":file_source", file_get_contents($files->tempName), PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
-   $stmt->execute();
-
-
-   $stmt = $dbh->prepare("INSERT INTO tExample (id,value) VALUES (:id,:value)");
-   $taValues = array(
-    'id' => '1',
-    'value' => '2'
-   ); // array
-   PDOBindArray($stmt,$taValues);
-   $stmt->execute();
-
+  public function update($table, $data, $condition)
+  {
+    if (!is_array($data)) throw Exception('update called without data array');
+    if (empty($condition)) throw Exception('update called without condition');
 
+    $set = array();
+    foreach ($data as $k => $v)
+      $set[] = $k.'=:'.$k;
 
-     */
+    $sql = sprintf("UPDATE %s SET %s\n    WHERE %s",
+                  $table,
+                  implode(',', $set),
+                  $condition);
+    return $this->executeBind($sql, $data);
   }
 
 }