From f75b2a54d5cbb5c55452042dee90cbb3f112b340 Mon Sep 17 00:00:00 2001 From: Roboxon <145359440+roboxon@users.noreply.github.com> Date: Mon, 23 Oct 2023 23:37:10 +0200 Subject: [PATCH] traits/table --- src/Database/SqlEnumCondition.php | 38 ++++++ src/core/BaseTable.php | 23 +++- src/test | 0 src/traits/table/RemoveTrait.php | 29 +++++ src/traits/table/SafeDeleteTrait.php | 63 ++++++++++ src/traits/table/SelectTrait.php | 165 ++++++++++++++++++++++++--- src/traits/table/UpdateTrait.php | 48 +++++++- 7 files changed, 342 insertions(+), 24 deletions(-) create mode 100644 src/Database/SqlEnumCondition.php delete mode 100644 src/test create mode 100644 src/traits/table/SafeDeleteTrait.php diff --git a/src/Database/SqlEnumCondition.php b/src/Database/SqlEnumCondition.php new file mode 100644 index 0000000..ef52acc --- /dev/null +++ b/src/Database/SqlEnumCondition.php @@ -0,0 +1,38 @@ + + * Dariusz RumiƄski + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +enum SqlEnumCondition: string +{ + case Equal = ' = '; + + case Bigger = ' > '; + + case Less = ' < '; + + case BiggerEqual = ' >= '; + + case LessEqual = ' =< '; + + case IsNull = ' IS NULL '; + + case NotNull = 'IS NOT NULL'; + + case Not = ' != '; + + case Like = ' LIKE '; + + case Descending = ' DESC '; + + case Ascending = ' ASC '; + + case Between = ' BETWEEN '; +} diff --git a/src/core/BaseTable.php b/src/core/BaseTable.php index 73cc00c..2078439 100644 --- a/src/core/BaseTable.php +++ b/src/core/BaseTable.php @@ -10,15 +10,26 @@ public function __construct(?string $connectionName = null) { parent::__construct(PdoConnManager::connect($connectionName)); } - public static function safeAssociArrayToObject(array $array, object $TargetClass): object + + private function fetchObject(array $row) { - $instance = new $TargetClass(); - foreach ($instance as $key => $value) { - if (isset($array[$key])) { - $instance->$key = $array[$key]; + foreach($row as $key => $value) + { + if(property_exists($this, $key)){ + $this->$key = $value; } } - return $instance; + } + + private function fetchAllObjects(array $rows):array + { + $objects = []; + foreach($rows as $row) + { + $obj = new $this(); + $objects[] = $obj->fetchObject($row); + } + return $objects; } } \ No newline at end of file diff --git a/src/test b/src/test deleted file mode 100644 index e69de29..0000000 diff --git a/src/traits/table/RemoveTrait.php b/src/traits/table/RemoveTrait.php index c9a55f5..7bb0a59 100644 --- a/src/traits/table/RemoveTrait.php +++ b/src/traits/table/RemoveTrait.php @@ -1,5 +1,10 @@ setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return null; + } + $query = "DELETE FROM {$table} WHERE {$whereColumn} = :{$whereColumn}"; + if ($secondWhereColumn) { + $query .= " AND {$secondWhereColumn} = :{$secondWhereColumn}"; + } + $arrayBind[':'.$whereColumn] = $whereValue; + if ($secondWhereColumn) { + $arrayBind[':'.$secondWhereColumn] = $secondWhereValue; + } + return $this->deleteQuery($query, $arrayBind); + } } \ No newline at end of file diff --git a/src/traits/table/SafeDeleteTrait.php b/src/traits/table/SafeDeleteTrait.php new file mode 100644 index 0000000..4fa4e19 --- /dev/null +++ b/src/traits/table/SafeDeleteTrait.php @@ -0,0 +1,63 @@ +id + * return 1 if successful and null if not + */ + public function delete(int $id = null): int|null + { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return null; + } + if (!$id) { + $id = $this->id; + } + if(!$id || $id < 1) + { + $this->setError('property id does existed or not setted in object'); + return null; + } + $query = "UPDATE $table SET deleted_at = NOW() WHERE id = :id"; + + return $this->updateQuery($query, [':id' => $id]); + } + + /** + * @param int|null $id + * @return int|null + * @description : if id = null then use $this->id + * return 1 if successful and null if not + */ + public function restore(int $id = null): int|null + { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return null; + } + if (!$id) { + $id = $this->id; + } + if(!$id || $id < 1) + { + $this->setError('property id does existed or not setted in object'); + return null; + } + $query = "UPDATE {$this->table} SET deleted_at = NULL WHERE id = :id"; + + return $this->updateQuery($query, []); + } +} \ No newline at end of file diff --git a/src/traits/table/SelectTrait.php b/src/traits/table/SelectTrait.php index 8ce5a6a..0c43344 100644 --- a/src/traits/table/SelectTrait.php +++ b/src/traits/table/SelectTrait.php @@ -1,37 +1,59 @@ setTable(); if(!$table) { $this->setError('table is not setted in function setTable'); + return false; } $row = $this->selectQuery("SELECT * FROM $table WHERE id = :id", [':id' => $id]); if (null !== $row && isset($row[0]) && \is_array($row[0])) { $this->fetchObject($row[0]); + return true; } else { $this->setError('Object with given id dose not Existed'); } + return false; } /** * @param array $ids - * * @return null|array<$this> + * in case of failure return null */ public function ids(array $ids): array|null { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return false; + } $stringIds = ''; foreach ($ids as $id) { $stringIds .= $id.','; } $stringIds = rtrim($stringIds, ','); - $query = "SELECT * FROM {$this->table} WHERE id IN ({$stringIds})"; + $query = "SELECT * FROM {$table} WHERE id IN ({$stringIds})"; $queryResult = $this->selectQuery($query, []); if(is_array($queryResult)) { @@ -41,25 +63,136 @@ public function ids(array $ids): array|null } - private function fetchObject(array $row) - { - foreach($row as $key => $value) + /** + * @return null|array<$this> + */ + public function firstRows( + int $countRows, + string $whereColumn, + \SqlEnumCondition $whereCondition, + mixed $whereValue, + ?string $orderByColumnName = null + ): null|array { + $table = $this->setTable(); + if(!$table) { - if(property_exists($this, $key)){ - $this->$key = $value; - } + $this->setError('table is not setted in function setTable'); + return false; + } + $arrayBindValue = []; + $where = " WHERE {$whereColumn} ".$whereCondition->value." :{$whereColumn}"; + $arrayBindValue[':'.$whereColumn] = $whereValue; + $query = ($orderByColumnName) ? + "SELECT * FROM {$table} ORDER BY {$orderByColumnName} {$where} LIMIT {$countRows}" : + "SELECT * FROM {$table} {$where} LIMIT {$countRows}"; + $queryResult = $this->selectQuery($query, $arrayBindValue); + if(is_array($queryResult)) + { + return $this->fetchAllObjects($queryResult); } + return null; } - private function fetchAllObjects(array $rows):array + + /** + * @return null|array<$this> + */ + public function lastRows(int $countRows, string $orderByColumnName, ?string $whereColumn = null, ?\SqlEnumCondition $whereCondition = null, int|string|bool $whereValue = null): null|array { - $objects = []; - foreach($rows as $row) - { - $obj = new $this; - $objects[] = $obj->fetchObject($row); + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return false; + } + $arrayBindValue = []; + $where = ''; + if (null !== $whereColumn && null !== $whereCondition) { + $where = " WHERE {$whereColumn} ".$whereCondition->value; + } + if (null !== $whereValue) { + $where .= " :{$whereValue}"; + $arrayBindValue[':'.$whereValue] = $whereValue; + } + + $query = "SELECT * FROM {$table} ORDER BY {$orderByColumnName} DESC {$where} LIMIT {$countRows}"; + + $queryResult = $this->selectQuery($query, $arrayBindValue); + if(is_array($queryResult)) + { + return $this->fetchAllObjects($queryResult); + } + return null; + } + + + /** + * @return null|array<$this> + */ + public function columnSelect( + ?string $firstColumn = null, + ?\SqlEnumCondition $firstCondition = null, + mixed $firstValue = null, + ?string $secondColumn = null, + ?\SqlEnumCondition $secondCondition = null, + mixed $secondValue = null, + ?string $orderBy = null, + ?string $ASC_DES = null, + ?int $limit_count = null, + ?int $limit_offset = null, + ?bool $isDel = null, + ?bool $deactives = null, + ?bool $actives = null + + ): null|array { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return false; + } + $limit = ''; + $arrayBindValue = []; + + $isDel ? ' AND deleted_at IS NOT NULL ' : ''; + $actives ? ' AND isActive = 1 ' : ''; + + $deactives ? ' AND isActive IS NOT 1 ' : ''; + if ($orderBy) { + $orderBy = " ORDER BY {$orderBy} {$ASC_DES}"; + } + if ($limit_count) { + $limit = " LIMIT {$limit_count}"; + if ($limit_offset) { + $limit = " LIMIT {$limit_offset} , {$limit_count}"; } - return $objects; + } + + $query = "SELECT * FROM {$table} "; + $firstColumnQuery = null; + $secondColumnQuery = null; + + if(null !== $firstColumn && null !== $firstCondition) + { + $firstValue = (' LIKE ' === (string) $firstCondition->value) ? '%'.$firstValue : $firstValue; + $firstColumnQuery = " {$firstColumn} {$firstCondition->value} :{$firstColumn}"; + $arrayBindValue[':'.$firstColumn] = $firstValue; + } + + if (null !== $secondColumn && null !== $secondCondition) { + $secondColumnQuery = " AND {$secondColumn} {$secondCondition->value} :{$secondColumn}"; + $secondValue = (' LIKE ' === $secondCondition->value) ? '%'.$secondValue : $secondValue; + $arrayBindValue[':'.$secondColumn] = $secondValue; + } + $query .= "WHERE {$firstColumnQuery} {$secondColumnQuery} {$isDel} {$actives} {$deactives} {$orderBy} {$limit}"; + $query = trim($query); + //echo $query; + $queryResult = $this->selectQuery($query, $arrayBindValue); + if(is_array($queryResult)) + { + return $this->fetchAllObjects($queryResult); + } + return null; } } \ No newline at end of file diff --git a/src/traits/table/UpdateTrait.php b/src/traits/table/UpdateTrait.php index 0c78a11..8a4b706 100644 --- a/src/traits/table/UpdateTrait.php +++ b/src/traits/table/UpdateTrait.php @@ -3,9 +3,12 @@ namespace GemFramework\Traits\Table; /** - * insert New Object into Database + * @method update() + * @method setNull() + * @method setTimeNow() + * Update current Object into Database */ -trait InsertTrait +trait UpdateTrait { /** * @insert current instance into Database @@ -45,4 +48,45 @@ public function update(): int|null } return null; } + + + /** + * @set a specific column to null based on condition whereColumn = $whereValue + * + * @exampel $this->setNull('deleted_at,'id',$this->id); + * + * @explain: set deleted_at to null where id = $this->id + */ + public function setNull(string $columnNameSetToNull, string $whereColumn, mixed $whereValue): int|null + { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return null; + } + $query = "UPDATE {$table} SET {$columnNameSetToNull} = NULL WHERE {$whereColumn} = :whereValue"; + + return $this->updateQuery($query, [':whereValue' => $whereValue]); + } + + /** + * @set a specific column to time now based on condition whereColumn = $whereValue + * + * @exampel $order->setTimeNow('paid_at','id',$this->id); + * + * @explain: set paid_at to 18-08-2022 12:45:13 where id = $this->id + */ + public function setTimeNow(string $columnNameSetToNowTomeStamp, string $whereColumn, mixed $whereValue): int|null + { + $table = $this->setTable(); + if(!$table) + { + $this->setError('table is not setted in function setTable'); + return null; + } + $query = "UPDATE {$table} SET {$columnNameSetToNowTomeStamp} = NOW() WHERE {$whereColumn} = :whereValue"; + + return $this->updateQuery($query, [':whereValue' => $whereValue]); + } }