page: replace output caching by article object
[minimedit.git] / database.inc.php
index 6e040d46d8cba08d3a9cef90d9cbba5637f0b2ac..218335814bebf8e993aeb034449d7dc17efae940 100644 (file)
@@ -1,4 +1,34 @@
 <?php
 $dsn = require '.dbconfig.inc.php';
-$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ];
-$Db = new PDO($dsn, NULL, NULL, $options);
+$Db = new DB($dsn);
+
+class DB
+{
+       public $dbh;
+
+       function __construct($config, $options = [])
+       {
+               $options += [
+                       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+                       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
+               ];
+               $this->dbh = new PDO($config, NULL, NULL, $options);
+       }
+
+       function query($sql, $params = [])
+       {
+               $stmt = $this->dbh->prepare($sql);
+               $stmt->execute($params);
+               return $stmt;
+       }
+
+       function insert($table, $row)
+       {
+               $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
+                       '"'.$table.'"',
+                       implode(', ', array_keys($row)),
+                       implode(', ', array_fill(0, count($row), '?'))
+               );
+               return $this->query($sql, array_values($row));
+       }
+}