reply: set method to abstract update queries
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 7 Nov 2019 03:59:13 +0000 (04:59 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 9 Nov 2019 06:08:13 +0000 (07:08 +0100)
Extend insert, renamed to generic "set", to allow updates using the same
syntax with an additional select parameter.

database.inc.php
issue/index.php
widget/reply.php

index 464126b3fad1370fe17f6e32dd4863a92dd759a5..aaf20234fd42a27da4ee5dd7e9059a13d22f438e 100644 (file)
@@ -24,22 +24,46 @@ class DB
 
        function _value($val, &$params)
        {
+               if (is_array($val)) {
+                       $sql = array_shift($val);
+                       $params = array_merge($params, $val);
+                       return $sql;
+               }
+
                $params[] = $val;
                return '?';
        }
 
-       function insert($table, $row)
+       function set($table, $row, $filter = NULL)
        {
                $params = [];
-               $cols = [];
-               foreach ($row as $col => $val) {
-                       $cols[] = $this->_value($val, $params);
+               if (is_null($filter)) {
+                       $cols = [];
+                       foreach ($row as $col => $val) {
+                               $cols[] = $this->_value($val, $params);
+                       }
+                       $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
+                               '"'.$table.'"',
+                               implode(', ', array_keys($row)),
+                               implode(', ', $cols)
+                       );
+               }
+               else {
+                       $sql = 'UPDATE "'.$table.'"';
+                       $cols = [];
+                       foreach ($row as $col => $val) {
+                               $cols[] = $col . ' = ' . $this->_value($val, $params);
+                       }
+
+                       $sql .= ' SET ' . implode(', ', $cols);
+                       if (is_array($filter)) {
+                               $sql .= ' WHERE ' . array_shift($filter);
+                               $params = array_merge($params, $filter);
+                       }
+                       else {
+                               $sql .= ' ' . $filter;
+                       }
                }
-               $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
-                       '"'.$table.'"',
-                       implode(', ', array_keys($row)),
-                       implode(', ', $cols)
-               );
                return $this->query($sql, $params);
        }
 }
index abd96119bee1e5c02fbb548e062218c0036c0d9c..10efc5ebcc87537a549c056f3ca9b2cbe81031a4 100644 (file)
@@ -33,7 +33,7 @@ if ($id) {
 if ($_POST) {
                $html = nl2br(htmlspecialchars($_POST['body']));
                $html = empty($html) ? NULL : "<p>$html</p>";
-               $query = $Db->insert('issues', [
+               $query = $Db->set('issues', [
                        'page'    => $Page,
                        'subject' => $_POST['subject'],
                        'body'    => $html,
index abb364cc1354876b078a0c0a698fc91647b098df..4d504bbeb122c30ec7e4bb4888dbcfc32a57a785 100644 (file)
@@ -8,7 +8,7 @@ if ($_POST) {
        try {
                $html = nl2br(htmlspecialchars($_POST['reply']));
                $html = "<p>$html</p>";
-               $query = $Db->insert('comments', [
+               $query = $Db->set('comments', [
                        'page'    => $Page,
                        'message' => $html,
                        'author'  => $User->login,
@@ -17,10 +17,8 @@ if ($_POST) {
                        throw new Exception('Fout bij opslaan');
                }
                if (@list ($cat, $issue) = explode('/', $Page) and ctype_digit($issue)) {
-                       $Db->query(
-                               'UPDATE issues SET updated = now() WHERE page = ? AND id = ?',
-                               [$cat, $issue]
-                       );
+                       $row = ['updated' => ['now()']];
+                       $Db->set('issues', $row, ['page = ? AND id = ?', $cat, $issue]);
                }
                $_POST['reply'] = NULL;
        }