CSY2028-assignment-2/CSY2028/DatabaseTable.php

77 lines
2.6 KiB
PHP
Raw Normal View History

2023-01-21 23:12:42 +00:00
<?php
namespace CSY2028;
class DatabaseTable {
2023-01-25 17:44:11 +00:00
protected $server;
protected $username;
protected $password;
protected $schema;
2023-01-25 17:44:48 +00:00
private $pdo;
2023-01-25 17:44:11 +00:00
2023-01-21 23:12:42 +00:00
private $table;
private $pk;
private $entityClass;
private $entityConstructor;
public function __construct($table, $pk = 'id', $entityClass = 'stdclass', $entityConstructor = []) {
$this->table = $table;
$this->pk = $pk;
$this->entityClass = $entityClass;
$this->entityConstructor = $entityConstructor;
2023-01-25 17:44:11 +00:00
$this->pdo = new \PDO('mysql:dbname='.$this->schema.';host='.$this->server, $this->username, $this->password);
2023-01-21 23:12:42 +00:00
}
private function insert($record) {
$keys = \array_keys($record);
$columns = \implode(', ', $keys);
$values = \implode(', :', $keys);
2023-01-25 17:44:11 +00:00
$this->pdo->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
2023-01-21 23:12:42 +00:00
}
private function update($record) {
2023-01-23 16:08:51 +00:00
$params = [];
foreach ($record as $key => $value) {
2023-01-21 23:12:42 +00:00
$params[] = $key . ' = :' .$key;
2023-01-23 16:08:51 +00:00
}
$record['primaryKey'] = $record[$this->pk];
2023-01-25 17:44:11 +00:00
$this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
2023-01-21 23:12:42 +00:00
}
2023-02-04 22:19:29 +00:00
public function find($columns, $values, $comparators = ['=', '='], $order = "ASC", $orderColumn = "id") {
$string = 'SELECT * FROM '.$this->table.' WHERE ';
for ($i = 0; $i < count($values); $i++) {
$string .= $columns[$i].' '.$comparators[$i].' :value'.$i.' ';
}
$string .= 'ORDER BY '.$orderColumn.' '.$order;
$stmt = $this->pdo->prepare($string);
2023-01-25 15:58:06 +00:00
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values);
return $stmt->fetchAll();
2023-01-21 23:12:42 +00:00
}
public function findAll() {
2023-01-25 17:44:11 +00:00
$stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table);
2023-01-23 16:08:51 +00:00
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute();
return $stmt->fetchAll();
2023-01-21 23:12:42 +00:00
}
public function delete($column, $value) {
$values = [
'value' => $value
];
2023-01-25 17:44:11 +00:00
$this->pdo->prepare('DELETE FROM '. $this->table .' WHERE '. $column .' = :value')->execute($values);
2023-01-21 23:12:42 +00:00
}
public function save($record) {
2023-01-23 18:59:40 +00:00
if (empty($record[$this->pk])) {
unset($record[$this->pk]);
2023-01-21 23:12:42 +00:00
}
try {
2023-01-23 18:59:40 +00:00
$this->insert($record);
2023-01-21 23:12:42 +00:00
}
catch (\Exception $e) {
2023-01-23 18:59:40 +00:00
$this->update($record);
2023-01-21 23:12:42 +00:00
}
}
}
?>