1
0
mirror of https://github.com/glmdev/eecs448-lab10 synced 2026-03-02 03:39:24 +00:00

Initial commit

This commit is contained in:
2020-12-06 13:58:02 -06:00
commit 5a54fe251d
24 changed files with 911 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php
namespace common\database;
class Connection {
protected static $mysqli;
protected $config;
function __construct() {
$this->config = config('database');
}
public function connect() {
if ( !static::$mysqli ) {
static::$mysqli = new \mysqli(
$this->config['url'],
$this->config['username'],
$this->config['password'],
$this->config['database']
);
}
if ( static::$mysqli->connect_errno ) {
throw new \Exception('Unable to connect to database: ' . static::$mysqli->connect_error);
}
}
public function escape($value) {
return static::$mysqli->real_escape_string($value);
}
public function execute($query, $args = [], $returns_statement = false) {
$statement = static::$mysqli->prepare($query);
if ( sizeof($args) > 0 ) {
$types = '';
foreach ( $args as $arg ) {
if ( is_int($arg) ) {
$types .= 'i';
} else if ( is_float($arg) || is_double($arg) ) {
$types .= 'd';
} else if ( is_string($arg) ) {
$types .= 's';
} else {
$types .= 'b';
}
}
$statement->bind_param($types, ...$args);
}
$statement->execute();
if ( $returns_statement ) return $statement;
return $statement->get_result();
}
public function insert($query, $args = []) {
$statement = $this->execute($query, $args, true);
return $statement->insert_id;
}
public function fetch($query, $args = []) {
$result = $this->execute($query, $args);
$rows = [];
while ( $row = $result->fetch_assoc() ) {
$rows[] = $row;
}
return $rows;
}
public function close() {
if ( static::$mysqli ) {
static::$mysqli->close();
static::$mysqli = null;
}
}
}

View File

@@ -0,0 +1,134 @@
<?php
namespace common\database;
class Repository {
protected $table;
protected $primary_key;
protected $fields = [];
protected $connection;
function __construct() {
$this->connection = new Connection();
$this->connection->connect();
}
function __destruct() {
$this->connection->close();
}
public function create($record) {
$fields = [];
$arg_parts = [];
$args = [];
foreach ( $this->fields as $field ) {
if ( isset($record[$field]) ) {
$fields[] = $field;
$args[] = $record[$field];
$arg_parts[] = '?';
}
}
$query = 'INSERT INTO ' . $this->table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $arg_parts) . ')';
$insert_id = $this->connection->insert($query, $args);
$record[$this->primary_key] = $insert_id;
return $record;
}
public function find_by_id($primary_key) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ' . $this->primary_key . ' = ?';
$results = $this->connection->fetch($query, [$primary_key]);
if ( sizeof($results) > 0 ) {
return $results[0];
}
}
public function find($filter = []) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ';
list($wheres, $where_args) = $this->build_wheres_from_filter($filter);
$query .= implode(' AND ', $wheres);
$query .= ' ORDER BY ' . $this->primary_key . ' ASC';
return $this->connection->fetch($query, $where_args);
}
public function find_one($filter = []) {
$query = 'SELECT ' . implode(', ', $this->fields) . ' FROM ' . $this->table . ' WHERE ';
list($wheres, $where_args) = $this->build_wheres_from_filter($filter);
$query .= implode(' AND ', $wheres) . ' LIMIT 1';
$results = $this->connection->fetch($query, $where_args);
if ( sizeof($results) > 0 ) {
return $results[0];
}
}
public function update($record) {
$query = 'UPDATE ' . $this->table . ' SET ';
$query_args = [];
$sets = [];
foreach ( $this->fields as $field ) {
if ( isset($record[$field]) ) {
$sets[] = $field . '=?';
$query_args[] = $record[$field];
}
}
$query = 'UPDATE ' . $this->table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $this->primary_key . ' = ?';
$query_args[] = $record[$this->primary_key];
$this->connection->execute($query, $query_args);
return $record;
}
public function save($record) {
if ( isset($record[$this->primary_key]) ) {
return $this->update($record);
} else {
return $this->create($record);
}
}
public function delete($record) {
$primary_key = $record[$this->primary_key];
$query = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->primary_key . ' = ?';
$this->connection->execute($query, [$primary_key]);
}
protected function build_wheres_from_filter($filter = []) {
if ( !$filter ) {
return [['1=1'], []];
}
$wheres = [];
$where_args = [];
foreach ( $this->fields as $field ) {
if ( isset($filter[$field]) ) {
$val = $filter[$field];
if ( is_string($val) || is_numeric($val) ) {
$wheres[] = $field . ' = ?';
$where_args[] = $val;
} else if ( is_array($val) ) {
$where_items = [];
foreach ( $val as $item ) {
$where_items[] = '?';
$where_args[] = $item;
}
$wheres[] = $field . ' IN (' . implode(',', $where_items) . ')';
}
}
}
return [$wheres, $where_args];
}
}