@@ -27,46 +27,46 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class DatabaseQueryFactory |
| 29 | 29 | { |
| 30 | - public static function insert(string $table, array $data) |
|
| 31 | - { |
|
| 32 | - $insertBuilder = (new Builder("QueryBuilder","Insert"))->getBuilder(); |
|
| 33 | - $columns = []; |
|
| 34 | - $values = []; |
|
| 30 | + public static function insert(string $table, array $data) |
|
| 31 | + { |
|
| 32 | + $insertBuilder = (new Builder("QueryBuilder","Insert"))->getBuilder(); |
|
| 33 | + $columns = []; |
|
| 34 | + $values = []; |
|
| 35 | 35 | |
| 36 | - foreach ($data as $index=>$datum) |
|
| 37 | - { |
|
| 38 | - $columns[] = $index; |
|
| 39 | - $values[] = $datum; |
|
| 40 | - } |
|
| 36 | + foreach ($data as $index=>$datum) |
|
| 37 | + { |
|
| 38 | + $columns[] = $index; |
|
| 39 | + $values[] = $datum; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - $columns = "(".implode(", ", $columns).")"; |
|
| 43 | - $values = "(".implode(", ", $values).")"; |
|
| 44 | - $insertBuilder->into($table.$columns); |
|
| 42 | + $columns = "(".implode(", ", $columns).")"; |
|
| 43 | + $values = "(".implode(", ", $values).")"; |
|
| 44 | + $insertBuilder->into($table.$columns); |
|
| 45 | 45 | |
| 46 | - $query = (string)$insertBuilder." VALUES ".$values; |
|
| 47 | - // die($query); |
|
| 48 | - try |
|
| 49 | - { |
|
| 50 | - $parts = explode(".", $table); |
|
| 51 | - $schemaName = $parts[0]; |
|
| 52 | - $tableName = $parts[1]; |
|
| 46 | + $query = (string)$insertBuilder." VALUES ".$values; |
|
| 47 | + // die($query); |
|
| 48 | + try |
|
| 49 | + { |
|
| 50 | + $parts = explode(".", $table); |
|
| 51 | + $schemaName = $parts[0]; |
|
| 52 | + $tableName = $parts[1]; |
|
| 53 | 53 | |
| 54 | - $connection = DBConnectionFactory::getConnection(); |
|
| 54 | + $connection = DBConnectionFactory::getConnection(); |
|
| 55 | 55 | |
| 56 | - $result = $connection->prepare((string)$query)->execute(); |
|
| 56 | + $result = $connection->prepare((string)$query)->execute(); |
|
| 57 | 57 | |
| 58 | - DatabaseLog::log(Session::get('USER_ID'), Constant::EVENT_INSERT, $schemaName, $tableName, $query); |
|
| 58 | + DatabaseLog::log(Session::get('USER_ID'), Constant::EVENT_INSERT, $schemaName, $tableName, $query); |
|
| 59 | 59 | |
| 60 | - $lastInsertId = ($connection->query("SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) as id")->fetchColumn()); |
|
| 60 | + $lastInsertId = ($connection->query("SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) as id")->fetchColumn()); |
|
| 61 | 61 | |
| 62 | - return [$result, "lastInsertId"=>$lastInsertId]; |
|
| 63 | - } |
|
| 64 | - catch (\PDOException $e) |
|
| 65 | - { |
|
| 66 | - throw new SQLException(sprintf( |
|
| 67 | - "%s", |
|
| 68 | - $e->getMessage() |
|
| 69 | - ), Constant::UNDEFINED); |
|
| 70 | - } |
|
| 71 | - } |
|
| 62 | + return [$result, "lastInsertId"=>$lastInsertId]; |
|
| 63 | + } |
|
| 64 | + catch (\PDOException $e) |
|
| 65 | + { |
|
| 66 | + throw new SQLException(sprintf( |
|
| 67 | + "%s", |
|
| 68 | + $e->getMessage() |
|
| 69 | + ), Constant::UNDEFINED); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | } |
| 73 | 73 | \ No newline at end of file |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare(strict_types=1); |
|
| 1 | +<?php declare(strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * @license MIT |
| 4 | 4 | * @author Samuel Adeshina <[email protected]> |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | { |
| 30 | 30 | public static function insert(string $table, array $data) |
| 31 | 31 | { |
| 32 | - $insertBuilder = (new Builder("QueryBuilder","Insert"))->getBuilder(); |
|
| 32 | + $insertBuilder = (new Builder("QueryBuilder", "Insert"))->getBuilder(); |
|
| 33 | 33 | $columns = []; |
| 34 | 34 | $values = []; |
| 35 | 35 | |
@@ -40,10 +40,10 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | $columns = "(".implode(", ", $columns).")"; |
| 43 | - $values = "(".implode(", ", $values).")"; |
|
| 43 | + $values = "(".implode(", ", $values).")"; |
|
| 44 | 44 | $insertBuilder->into($table.$columns); |
| 45 | 45 | |
| 46 | - $query = (string)$insertBuilder." VALUES ".$values; |
|
| 46 | + $query = (string) $insertBuilder." VALUES ".$values; |
|
| 47 | 47 | // die($query); |
| 48 | 48 | try |
| 49 | 49 | { |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | $connection = DBConnectionFactory::getConnection(); |
| 55 | 55 | |
| 56 | - $result = $connection->prepare((string)$query)->execute(); |
|
| 56 | + $result = $connection->prepare((string) $query)->execute(); |
|
| 57 | 57 | |
| 58 | 58 | DatabaseLog::log(Session::get('USER_ID'), Constant::EVENT_INSERT, $schemaName, $tableName, $query); |
| 59 | 59 | |
@@ -60,8 +60,7 @@ |
||
| 60 | 60 | $lastInsertId = ($connection->query("SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) as id")->fetchColumn()); |
| 61 | 61 | |
| 62 | 62 | return [$result, "lastInsertId"=>$lastInsertId]; |
| 63 | - } |
|
| 64 | - catch (\PDOException $e) |
|
| 63 | + } catch (\PDOException $e) |
|
| 65 | 64 | { |
| 66 | 65 | throw new SQLException(sprintf( |
| 67 | 66 | "%s", |
@@ -17,65 +17,65 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class DirtChecker |
| 19 | 19 | { |
| 20 | - public $dirtFilterConditions = [">", "<", "="]; |
|
| 20 | + public $dirtFilterConditions = [">", "<", "="]; |
|
| 21 | 21 | |
| 22 | - public $workingValue; |
|
| 23 | - public $conditionValue; |
|
| 24 | - public $workingConditions = []; |
|
| 22 | + public $workingValue; |
|
| 23 | + public $conditionValue; |
|
| 24 | + public $workingConditions = []; |
|
| 25 | 25 | |
| 26 | - public function __construct(string $value, string $conditionValue, array $conditions = []) |
|
| 27 | - { |
|
| 28 | - $this->workingValue = $value; |
|
| 29 | - $this->conditionValue = $conditionValue; |
|
| 26 | + public function __construct(string $value, string $conditionValue, array $conditions = []) |
|
| 27 | + { |
|
| 28 | + $this->workingValue = $value; |
|
| 29 | + $this->conditionValue = $conditionValue; |
|
| 30 | 30 | |
| 31 | - if (!empty($conditions)){ |
|
| 32 | - foreach ($conditions as $value) { |
|
| 33 | - if (!in_array($value, $this->dirtFilterConditions)){ |
|
| 34 | - throw new \Exception("Invalid Dirt Filter Condition Provided: ".$value); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - $this->workingConditions = $conditions; |
|
| 38 | - } |
|
| 39 | - else { |
|
| 40 | - $this->workingConditions = $this->dirtFilterConditions; |
|
| 41 | - } |
|
| 42 | - } |
|
| 31 | + if (!empty($conditions)){ |
|
| 32 | + foreach ($conditions as $value) { |
|
| 33 | + if (!in_array($value, $this->dirtFilterConditions)){ |
|
| 34 | + throw new \Exception("Invalid Dirt Filter Condition Provided: ".$value); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + $this->workingConditions = $conditions; |
|
| 38 | + } |
|
| 39 | + else { |
|
| 40 | + $this->workingConditions = $this->dirtFilterConditions; |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public function isDirty() : bool |
|
| 45 | - { |
|
| 46 | - $explodedString = explode(" ", $this->workingValue); |
|
| 47 | - foreach ($explodedString as $string){ |
|
| 48 | - foreach ($this->workingConditions as $condition){ |
|
| 49 | - $switchValue = false; |
|
| 50 | - switch ($condition) { |
|
| 51 | - case '=': |
|
| 52 | - $switchValue = strpos($string, $this->conditionValue) !== false; |
|
| 53 | - break; |
|
| 54 | - case '>': |
|
| 55 | - $string = self::turnToNumber($string); |
|
| 56 | - $_condition = self::turnToNumber($this->conditionValue); |
|
| 44 | + public function isDirty() : bool |
|
| 45 | + { |
|
| 46 | + $explodedString = explode(" ", $this->workingValue); |
|
| 47 | + foreach ($explodedString as $string){ |
|
| 48 | + foreach ($this->workingConditions as $condition){ |
|
| 49 | + $switchValue = false; |
|
| 50 | + switch ($condition) { |
|
| 51 | + case '=': |
|
| 52 | + $switchValue = strpos($string, $this->conditionValue) !== false; |
|
| 53 | + break; |
|
| 54 | + case '>': |
|
| 55 | + $string = self::turnToNumber($string); |
|
| 56 | + $_condition = self::turnToNumber($this->conditionValue); |
|
| 57 | 57 | |
| 58 | - $switchValue = $string > $_condition; |
|
| 59 | - break; |
|
| 60 | - case '<': |
|
| 61 | - $string = self::turnToNumber($string); |
|
| 62 | - $_condition = self::turnToNumber($this->conditionValue); |
|
| 58 | + $switchValue = $string > $_condition; |
|
| 59 | + break; |
|
| 60 | + case '<': |
|
| 61 | + $string = self::turnToNumber($string); |
|
| 62 | + $_condition = self::turnToNumber($this->conditionValue); |
|
| 63 | 63 | |
| 64 | - $switchValue = $string < $_condition; |
|
| 65 | - break; |
|
| 66 | - } |
|
| 64 | + $switchValue = $string < $_condition; |
|
| 65 | + break; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - if (!$switchValue) { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - } |
|
| 68 | + if (!$switchValue) { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return false; |
|
| 75 | - } |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - private function turnToNumber(string $string) : float |
|
| 78 | - { |
|
| 79 | - return (float) filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND); |
|
| 80 | - } |
|
| 77 | + private function turnToNumber(string $string) : float |
|
| 78 | + { |
|
| 79 | + return (float) filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND); |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare (strict_types=1); |
|
| 1 | +<?php declare (strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * @license MIT |
| 4 | 4 | * @author Samuel Adeshina <[email protected]> |
@@ -28,9 +28,9 @@ discard block |
||
| 28 | 28 | $this->workingValue = $value; |
| 29 | 29 | $this->conditionValue = $conditionValue; |
| 30 | 30 | |
| 31 | - if (!empty($conditions)){ |
|
| 31 | + if (!empty($conditions)) { |
|
| 32 | 32 | foreach ($conditions as $value) { |
| 33 | - if (!in_array($value, $this->dirtFilterConditions)){ |
|
| 33 | + if (!in_array($value, $this->dirtFilterConditions)) { |
|
| 34 | 34 | throw new \Exception("Invalid Dirt Filter Condition Provided: ".$value); |
| 35 | 35 | } |
| 36 | 36 | } |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | public function isDirty() : bool |
| 45 | 45 | { |
| 46 | 46 | $explodedString = explode(" ", $this->workingValue); |
| 47 | - foreach ($explodedString as $string){ |
|
| 48 | - foreach ($this->workingConditions as $condition){ |
|
| 47 | + foreach ($explodedString as $string) { |
|
| 48 | + foreach ($this->workingConditions as $condition) { |
|
| 49 | 49 | $switchValue = false; |
| 50 | 50 | switch ($condition) { |
| 51 | 51 | case '=': |
@@ -35,8 +35,7 @@ |
||
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | $this->workingConditions = $conditions; |
| 38 | - } |
|
| 39 | - else { |
|
| 38 | + } else { |
|
| 40 | 39 | $this->workingConditions = $this->dirtFilterConditions; |
| 41 | 40 | } |
| 42 | 41 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare(strict_types=1); |
|
| 1 | +<?php declare(strict_types = 1); |
|
| 2 | 2 | /** |
| 3 | 3 | * @license MIT |
| 4 | 4 | * @author Samuel Adeshina <[email protected]> |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * @throws \EmmetBlue\Core\Exception\SQLException |
| 56 | 56 | * @return void |
| 57 | 57 | */ |
| 58 | - public function connect(string $username, string $password="") |
|
| 58 | + public function connect(string $username, string $password = "") |
|
| 59 | 59 | { |
| 60 | 60 | $host = $this->dsn[0]; |
| 61 | 61 | $database = $this->dsn[1]; |
@@ -21,105 +21,105 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class MySQL implements ConnectableInterface |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var string $dsn |
|
| 26 | - */ |
|
| 27 | - protected $dsn; |
|
| 24 | + /** |
|
| 25 | + * @var string $dsn |
|
| 26 | + */ |
|
| 27 | + protected $dsn; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var \PDO $connectionObject |
|
| 31 | - */ |
|
| 32 | - protected $connectionObject; |
|
| 29 | + /** |
|
| 30 | + * @var \PDO $connectionObject |
|
| 31 | + */ |
|
| 32 | + protected $connectionObject; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var array loginData |
|
| 36 | - */ |
|
| 37 | - private $loginData = []; |
|
| 34 | + /** |
|
| 35 | + * @var array loginData |
|
| 36 | + */ |
|
| 37 | + private $loginData = []; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Setter for the DSN string {@see $dsn} |
|
| 41 | - * |
|
| 42 | - * @param array $dsnArray |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function setDsn(array $dsnArray) |
|
| 46 | - { |
|
| 47 | - $this->dsn = $dsnArray; |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * Setter for the DSN string {@see $dsn} |
|
| 41 | + * |
|
| 42 | + * @param array $dsnArray |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function setDsn(array $dsnArray) |
|
| 46 | + { |
|
| 47 | + $this->dsn = $dsnArray; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Establishes connection |
|
| 52 | - * |
|
| 53 | - * @param string $username |
|
| 54 | - * @param string $password optional |
|
| 55 | - * @throws \EmmetBlue\Core\Exception\SQLException |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function connect(string $username, string $password="") |
|
| 59 | - { |
|
| 60 | - $host = $this->dsn[0]; |
|
| 61 | - $database = $this->dsn[1]; |
|
| 50 | + /** |
|
| 51 | + * Establishes connection |
|
| 52 | + * |
|
| 53 | + * @param string $username |
|
| 54 | + * @param string $password optional |
|
| 55 | + * @throws \EmmetBlue\Core\Exception\SQLException |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function connect(string $username, string $password="") |
|
| 59 | + { |
|
| 60 | + $host = $this->dsn[0]; |
|
| 61 | + $database = $this->dsn[1]; |
|
| 62 | 62 | |
| 63 | - $this->loginData['username'] = $username; |
|
| 64 | - $this->loginData['password'] = $password; |
|
| 63 | + $this->loginData['username'] = $username; |
|
| 64 | + $this->loginData['password'] = $password; |
|
| 65 | 65 | |
| 66 | - try |
|
| 67 | - { |
|
| 68 | - $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
|
| 69 | - } |
|
| 70 | - catch (\PDOException $e) |
|
| 71 | - { |
|
| 72 | - throw new SQLException("Unable to connect to database", 400, $e); |
|
| 73 | - } |
|
| 66 | + try |
|
| 67 | + { |
|
| 68 | + $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
|
| 69 | + } |
|
| 70 | + catch (\PDOException $e) |
|
| 71 | + { |
|
| 72 | + throw new SQLException("Unable to connect to database", 400, $e); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $dsn = $this->dsn; |
|
| 76 | - unset($dsn[0], $dsn[1]); |
|
| 75 | + $dsn = $this->dsn; |
|
| 76 | + unset($dsn[0], $dsn[1]); |
|
| 77 | 77 | |
| 78 | - foreach ($dsn as $attribute) |
|
| 79 | - { |
|
| 80 | - foreach ($attribute as $key=>$value) |
|
| 81 | - { |
|
| 82 | - $this->connectionObject->setAttribute($key, $value); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } |
|
| 78 | + foreach ($dsn as $attribute) |
|
| 79 | + { |
|
| 80 | + foreach ($attribute as $key=>$value) |
|
| 81 | + { |
|
| 82 | + $this->connectionObject->setAttribute($key, $value); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Returns an instance a pdo intance of the connection object |
|
| 89 | - * |
|
| 90 | - * @return \PDO |
|
| 91 | - */ |
|
| 92 | - public function getConnection() : \PDO |
|
| 93 | - { |
|
| 94 | - if ($this->connectionObject instanceof \PDO) |
|
| 95 | - { |
|
| 96 | - return $this->connectionObject; |
|
| 97 | - } |
|
| 87 | + /** |
|
| 88 | + * Returns an instance a pdo intance of the connection object |
|
| 89 | + * |
|
| 90 | + * @return \PDO |
|
| 91 | + */ |
|
| 92 | + public function getConnection() : \PDO |
|
| 93 | + { |
|
| 94 | + if ($this->connectionObject instanceof \PDO) |
|
| 95 | + { |
|
| 96 | + return $this->connectionObject; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - $this->connect( |
|
| 100 | - $this->loginData['username'] ?? '', |
|
| 101 | - $this->loginData['password'] ?? '' |
|
| 102 | - ); |
|
| 99 | + $this->connect( |
|
| 100 | + $this->loginData['username'] ?? '', |
|
| 101 | + $this->loginData['password'] ?? '' |
|
| 102 | + ); |
|
| 103 | 103 | |
| 104 | - return $this->connectionObject; |
|
| 105 | - } |
|
| 104 | + return $this->connectionObject; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Closes connection |
|
| 109 | - */ |
|
| 110 | - public function disableConnection() |
|
| 111 | - { |
|
| 112 | - $this->connectionObject = null; |
|
| 113 | - } |
|
| 107 | + /** |
|
| 108 | + * Closes connection |
|
| 109 | + */ |
|
| 110 | + public function disableConnection() |
|
| 111 | + { |
|
| 112 | + $this->connectionObject = null; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Sets attributes for the PDO object |
|
| 117 | - * |
|
| 118 | - * @param \PDO $attribute |
|
| 119 | - * @param \PDO $value |
|
| 120 | - */ |
|
| 121 | - public function setAttribute(\PDO $attribute, \PDO $value) |
|
| 122 | - { |
|
| 123 | - $this->connectionObject->setAttribute($attribute, $value); |
|
| 124 | - } |
|
| 115 | + /** |
|
| 116 | + * Sets attributes for the PDO object |
|
| 117 | + * |
|
| 118 | + * @param \PDO $attribute |
|
| 119 | + * @param \PDO $value |
|
| 120 | + */ |
|
| 121 | + public function setAttribute(\PDO $attribute, \PDO $value) |
|
| 122 | + { |
|
| 123 | + $this->connectionObject->setAttribute($attribute, $value); |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -66,8 +66,7 @@ |
||
| 66 | 66 | try |
| 67 | 67 | { |
| 68 | 68 | $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password); |
| 69 | - } |
|
| 70 | - catch (\PDOException $e) |
|
| 69 | + } catch (\PDOException $e) |
|
| 71 | 70 | { |
| 72 | 71 | throw new SQLException("Unable to connect to database", 400, $e); |
| 73 | 72 | } |