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 21:55:07 +00:00
public function find ( $column , $value , $column2 = " " , $value2 = " " , $comparator = " = " , $comparator2 = " = " , $order = " ASC " , $orderColumn = " id " ) {
if ( $column2 == " " || $value2 == " " ) {
2023-01-25 15:58:06 +00:00
$values = [
'value' => $value
];
2023-02-04 21:55:07 +00:00
$stmt = $this -> pdo -> prepare ( 'SELECT * FROM ' . $this -> table . ' WHERE ' . $column . ' ' . $comparator . ' :value ORDER BY ' . $orderColumn . " " . $order );
2023-01-25 15:58:06 +00:00
$stmt -> setFetchMode ( \PDO :: FETCH_CLASS , $this -> entityClass , $this -> entityConstructor );
$stmt -> execute ( $values );
return $stmt -> fetchAll ();
}
else {
$values = [
'value' => $value ,
'value2' => $value2
];
2023-02-04 21:55:07 +00:00
$stmt = $this -> pdo -> prepare ( 'SELECT * FROM ' . $this -> table . ' WHERE ' . $column . ' ' . $comparator . ' :value AND ' . $column2 . ' ' . $comparator2 . ' :value2 ORDER BY ' . $orderColumn . " " . $order );
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
}
}
}
?>