SQL connection now site specific

This commit is contained in:
Joshua Perry 2023-01-25 17:44:11 +00:00
parent ddd3b719fe
commit 71c1dd89fb
8 changed files with 32 additions and 27 deletions

View File

@ -1,6 +1,12 @@
<?php <?php
namespace CSY2028; namespace CSY2028;
class DatabaseTable { class DatabaseTable {
protected $server;
protected $username;
protected $password;
protected $schema;
protected $pdo;
private $table; private $table;
private $pk; private $pk;
private $entityClass; private $entityClass;
@ -11,21 +17,14 @@ class DatabaseTable {
$this->pk = $pk; $this->pk = $pk;
$this->entityClass = $entityClass; $this->entityClass = $entityClass;
$this->entityConstructor = $entityConstructor; $this->entityConstructor = $entityConstructor;
} $this->pdo = new \PDO('mysql:dbname='.$this->schema.';host='.$this->server, $this->username, $this->password);
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) { private function insert($record) {
$keys = \array_keys($record); $keys = \array_keys($record);
$columns = \implode(', ', $keys); $columns = \implode(', ', $keys);
$values = \implode(', :', $keys); $values = \implode(', :', $keys);
$this->startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record); $this->pdo->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
} }
private function update($record) { private function update($record) {
@ -34,7 +33,7 @@ class DatabaseTable {
$params[] = $key . ' = :' .$key; $params[] = $key . ' = :' .$key;
} }
$record['primaryKey'] = $record[$this->pk]; $record['primaryKey'] = $record[$this->pk];
$this->startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record); $this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
} }
public function find($column, $value, $column2 = "", $value2 = "") { public function find($column, $value, $column2 = "", $value2 = "") {
@ -42,7 +41,7 @@ class DatabaseTable {
$values = [ $values = [
'value' => $value 'value' => $value
]; ];
$stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value'); $stmt = $this->pdo->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value');
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values); $stmt->execute($values);
return $stmt->fetchAll(); return $stmt->fetchAll();
@ -52,7 +51,7 @@ class DatabaseTable {
'value' => $value, 'value' => $value,
'value2' => $value2 'value2' => $value2
]; ];
$stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value AND '. $column2 .' = :value2'); $stmt = $this->pdo->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value AND '. $column2 .' = :value2');
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values); $stmt->execute($values);
return $stmt->fetchAll(); return $stmt->fetchAll();
@ -60,7 +59,7 @@ class DatabaseTable {
} }
public function findAll() { public function findAll() {
$stmt = $this->startDB()->prepare('SELECT * FROM ' . $this->table); $stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table);
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute(); $stmt->execute();
return $stmt->fetchAll(); return $stmt->fetchAll();
@ -70,7 +69,7 @@ class DatabaseTable {
$values = [ $values = [
'value' => $value 'value' => $value
]; ];
$this->startDB()->prepare('DELETE FROM '. $this->table .' WHERE '. $column .' = :value')->execute($values); $this->pdo->prepare('DELETE FROM '. $this->table .' WHERE '. $column .' = :value')->execute($values);
} }
public function save($record) { public function save($record) {

View File

@ -9,7 +9,7 @@ class Applicant {
public $jobId; public $jobId;
private $jobsTable; private $jobsTable;
public function __construct(\CSY2028\DatabaseTable $jobsTable) { public function __construct(\jobs\JobDatabaseTable $jobsTable) {
$this->jobsTable = $jobsTable; $this->jobsTable = $jobsTable;
} }

View File

@ -11,7 +11,7 @@ class Job {
public $clientId; public $clientId;
private $catsTable; private $catsTable;
public function __construct(\CSY2028\DatabaseTable $catsTable) { public function __construct(\jobs\JobDatabaseTable $catsTable) {
$this->catsTable = $catsTable; $this->catsTable = $catsTable;
} }

View File

@ -0,0 +1,8 @@
<?php
namespace jobs;
class JobDatabaseTable extends \CSY2028\DatabaseTable {
protected $server = 'mysql';
protected $username = 'student';
protected $password = 'student';
protected $schema = 'job';
}

View File

@ -3,13 +3,12 @@ namespace jobs;
class Routes implements \CSY2028\Routes { class Routes implements \CSY2028\Routes {
public function getController($controllerName, $functionName) { public function getController($controllerName, $functionName) {
$catsTable = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category'); $catsTable = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
$jobsTable = new \CSY2028\DatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]); $jobsTable = new \jobs\JobDatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \CSY2028\DatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]); $appsTable = new \jobs\JobDatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
$usersTable = new \CSY2028\DatabaseTable('users', 'id', '\jobs\Entity\User'); $usersTable = new \jobs\JobDatabaseTable('users', 'id', '\jobs\Entity\User');
$controllers = []; $controllers = [];
//TODO: Add Controllers
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable, $appsTable); $controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable, $appsTable);
$controllers['portal'] = new \jobs\controllers\Portal($catsTable, $jobsTable, $appsTable); $controllers['portal'] = new \jobs\controllers\Portal($catsTable, $jobsTable, $appsTable);
$controllers['user'] = new \jobs\controllers\User($usersTable, $catsTable); $controllers['user'] = new \jobs\controllers\User($usersTable, $catsTable);
@ -36,7 +35,6 @@ class Routes implements \CSY2028\Routes {
public function checkLogin($name) { public function checkLogin($name) {
$loginRoutes = []; $loginRoutes = [];
//TODO: Add login routes
$loginRoutes['portal'] = true; $loginRoutes['portal'] = true;
$requiresLogin = $loginRoutes[$name] ?? false; $requiresLogin = $loginRoutes[$name] ?? false;
@ -48,7 +46,7 @@ class Routes implements \CSY2028\Routes {
} }
public function notFound() { public function notFound() {
$cats = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category'); $cats = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
return ['template' => 'response.html.php', return ['template' => 'response.html.php',
'title' => 'Jo\'s Jobs- 404 Not Found', 'title' => 'Jo\'s Jobs- 404 Not Found',
'vars' => ['cats' => $cats->findAll(), 'vars' => ['cats' => $cats->findAll(),
@ -57,7 +55,7 @@ class Routes implements \CSY2028\Routes {
} }
public function nav() { public function nav() {
$cats = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category'); $cats = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
return ['template' => 'nav.html.php', return ['template' => 'nav.html.php',
'vars' => ['cats' => $cats->findAll()] 'vars' => ['cats' => $cats->findAll()]
]; ];

View File

@ -6,7 +6,7 @@ class Jobs {
private $appsTable; private $appsTable;
private $vars = []; private $vars = [];
public function __construct(\CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $catsTable, \CSY2028\DatabaseTable $appsTable) { public function __construct(\jobs\JobDatabaseTable $jobsTable, \jobs\JobDatabaseTable $catsTable, \jobs\JobDatabaseTable $appsTable) {
$this->jobsTable = $jobsTable; $this->jobsTable = $jobsTable;
$this->catsTable = $catsTable; $this->catsTable = $catsTable;
$this->appsTable = $appsTable; $this->appsTable = $appsTable;

View File

@ -6,7 +6,7 @@ class Portal {
private $appsTable; private $appsTable;
private $vars; private $vars;
public function __construct(\CSY2028\DatabaseTable $catsTable, \CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $appsTable) { public function __construct(\jobs\JobDatabaseTable $catsTable, \jobs\JobDatabaseTable $jobsTable, \jobs\JobDatabaseTable $appsTable) {
$this->catsTable = $catsTable; $this->catsTable = $catsTable;
$this->jobsTable = $jobsTable; $this->jobsTable = $jobsTable;
$this->appsTable = $appsTable; $this->appsTable = $appsTable;

View File

@ -5,7 +5,7 @@ class User {
private $catsTable; private $catsTable;
private $vars; private $vars;
public function __construct(\CSY2028\DatabaseTable $usersTable, \CSY2028\DatabaseTable $catsTable) { public function __construct(\jobs\JobDatabaseTable $usersTable, \jobs\JobDatabaseTable $catsTable) {
$this->usersTable = $usersTable; $this->usersTable = $usersTable;
$this->catsTable = $catsTable; $this->catsTable = $catsTable;
$this->vars['cats'] = $this->catsTable->findAll(); $this->vars['cats'] = $this->catsTable->findAll();