2023-01-21 23:12:42 +00:00
|
|
|
<?php
|
|
|
|
|
namespace CSY2028;
|
|
|
|
|
class DatabaseTable {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function startDB() { //TODO: Maybe move
|
|
|
|
|
$server = 'mysql';
|
|
|
|
|
$username = 'student';
|
|
|
|
|
$password = 'student';
|
|
|
|
|
$schema = 'job';
|
|
|
|
|
return new \PDO('mysql:dbname='.$schema.';host='.$server, $username, $password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function insert($record) {
|
|
|
|
|
$keys = \array_keys($record);
|
|
|
|
|
$columns = \implode(', ', $keys);
|
|
|
|
|
$values = \implode(', :', $keys);
|
2023-01-23 16:08:51 +00:00
|
|
|
$this->startDB()->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];
|
|
|
|
|
$this->startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
|
2023-01-21 23:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-25 15:58:06 +00:00
|
|
|
public function find($column, $value, $column2 = "", $value2 = "") {
|
|
|
|
|
if ($column2 == "" && $value2 == "") {
|
|
|
|
|
$values = [
|
|
|
|
|
'value' => $value
|
|
|
|
|
];
|
|
|
|
|
$stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value');
|
|
|
|
|
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
|
|
|
|
|
$stmt->execute($values);
|
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$values = [
|
|
|
|
|
'value' => $value,
|
|
|
|
|
'value2' => $value2
|
|
|
|
|
];
|
2023-01-25 16:32:54 +00:00
|
|
|
$stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value AND '. $column2 .' = :value2');
|
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-23 16:08:51 +00:00
|
|
|
$stmt = $this->startDB()->prepare('SELECT * FROM ' . $this->table);
|
|
|
|
|
$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 15:35:35 +00:00
|
|
|
$this->startDB()->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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
?>
|