Improvements
authorJoey Schulze <joey@infodrom.org>
Fri, 6 Jun 2014 20:54:33 +0000 (20:54 +0000)
committerJoey Schulze <joey@infodrom.org>
Fri, 6 Jun 2014 20:54:33 +0000 (20:54 +0000)
 . Support decimal / float values
 . Support now() for update and insertInto

class/database.class.php

index 7b9a88e..b579b03 100644 (file)
@@ -165,6 +165,8 @@ class Database {
     foreach ($data as $key => $value) {
       if(is_int($value))
        $param = PDO::PARAM_INT;
+      elseif(is_float($value))
+       $param = PDO::PARAM_STR;
       elseif(is_bool($value))
        $param = PDO::PARAM_BOOL;
       elseif(is_null($value))
@@ -188,12 +190,16 @@ class Database {
 
   public function insertInto($table, $data)
   {
-    if (!is_array($data)) throw Exception('insertInto called without data array');
+    if (!is_array($data)) throw new Exception('insertInto called without data array');
 
     $columns = array();
     foreach ($data as $k => $v) {
       $columns[] = $k;
-      $values[] = ':' . $k;
+      if (strtolower($v) == 'now()' || strtolower($v) == 'current_timestamp') {
+       $values[] = 'now()';
+       unset($data[$k]);
+      } else
+       $values[] = ':' . $k;
     }
 
     $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)",
@@ -205,12 +211,17 @@ class Database {
 
   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');
+    if (!is_array($data)) throw new Exception('update called without data array');
+    if (empty($condition)) throw new Exception('update called without condition');
 
     $set = array();
-    foreach ($data as $k => $v)
-      $set[] = $k.'=:'.$k;
+    foreach ($data as $k => $v) {
+      if (strtolower($v) == 'now()' || strtolower($v) == 'current_timestamp') {
+       $set[] = $k.'=now()';
+       unset($data[$k]);
+      } else
+       $set[] = $k.'=:'.$k;
+    }
 
     $sql = sprintf("UPDATE %s SET %s WHERE %s",
                   $table,