Skip to content

Commit

Permalink
[doc] - better empty params handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iloElias committed Oct 6, 2024
1 parent 66b1161 commit 677f6fe
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions .idea/maestro.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ilias/maestro",
"type": "library",
"version": "1.2.5",
"version": "1.2.6",
"description": "A PHP object-oriented postgres database manager",
"keywords": [
"postgres",
Expand Down
21 changes: 11 additions & 10 deletions src/Abstract/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use Ilias\Maestro\Database\Select;
use Ilias\Maestro\Utils\Utils;
use InvalidArgumentException, PDO, Exception, PDOStatement;
use function PHPUnit\Framework\isEmpty;

abstract class Query
{
public mixed $current = null;
protected array $parameters = [];
protected array $where = [];
private ?PDOStatement $stmt = null;
private bool $isBinded = false;
private bool $isBound = false;
protected string $query = '';
public const AND = 'AND';
public const OR = 'OR';
Expand All @@ -38,11 +37,10 @@ public function __construct(
/**
* Adds WHERE conditions to the SQL query.
* This method accepts an associative array of conditions where the key is the column name and the value is the condition value.
*
* @param array $conditions An associative array of conditions for the WHERE clause.
* @param string|array $conditions An associative array of conditions for the WHERE clause. Use plain text if you don't need the array resolver.
* @return $this Returns the current instance for method chaining.
*/
public function where(string|array $conditions, string $operation = Select::AND , string $compaction = Select::EQUALS, bool $group = false): static
public function where(string|array $conditions, string $operation = self::AND , string $compaction = self::EQUALS, bool $group = false): static
{
if (empty($conditions)) {
return $this;
Expand All @@ -67,7 +65,7 @@ public function where(string|array $conditions, string $operation = Select::AND
return $this;
}

public function in(string|array $conditions, string $operation = Select::AND , bool $group = false): static
public function in(string|array $conditions, string $operation = self::AND , bool $group = false): static
{
if (empty($conditions)) {
return $this;
Expand Down Expand Up @@ -108,7 +106,7 @@ protected function storeParameter(string $name, mixed $value): void
$this->parameters[$name] = $value ? Expression::TRUE : Expression::FALSE;
} elseif (is_object($value) && is_subclass_of($value, Query::class)) {
$this->parameters[$name] = "({$value})";
} elseif (is_object($value) && $value instanceof Expression) {
} elseif ($value instanceof Expression) {
$this->parameters[$name] = "{$value}";
} else {
$value = str_replace("'", "''", $value);
Expand Down Expand Up @@ -174,18 +172,21 @@ public function bindParameters(?PDO $pdo = null): Query
}
$this->query = $query;
if (!empty($this->pdo) || !empty($pdo)) {
if (!$this->isBinded) {
if (!$this->isBound) {
$stmt = $this->pdo->prepare($query);
$this->isBinded = true;
$this->isBound = true;
$this->stmt = $stmt;
}
}
return $this;
}

/**
* @throws Exception
*/
public function execute(): array
{
if (!$this->isBinded) {
if (!$this->isBound) {
$this->bindParameters();
}
if (!empty($this->stmt)) {
Expand Down
6 changes: 4 additions & 2 deletions src/Abstract/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public static function generateAlias(array $existingAlias = []): string

/**
* Fetches all rows from the table based on the given prediction, order, and limit.
@param string|array|null $prediction The prediction criteria for the query. Can be a string or an array.
*
* @param string|array|null $prediction The prediction criteria for the query. Can be a string or an array.
* @param string|array|null $orderBy The order by criteria for the query. Can be a string or an array.
* @param int|string $limit The limit for the number of rows to fetch. Default is 100.
* @return array The fetched rows as an array.
Expand Down Expand Up @@ -182,7 +183,8 @@ public static function fetchAll(string|array $prediction = null, string|array $o

/**
* Fetches a single row from the table based on the given prediction and order.
@param string|array|null $prediction The prediction criteria for the query. Can be a string or an array.
*
* @param string|array|null $prediction The prediction criteria for the query. Can be a string or an array.
* @param string|array|null $orderBy The order by criteria for the query. Can be a string or an array.
* @return mixed The fetched row or null if no row is found.
*/
Expand Down
12 changes: 1 addition & 11 deletions src/Core/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

/**
* Class Manager
*
* This class provides methods to create and manage a PostgreSQL database schema,
* including creating schemas, tables, and foreign key constraints. It also provides
* methods to insert, update, and select data from tables.
*
* @package Ilias\Maestro\Core
* This class provides methods to create and manage a PostgreSQL database schema, including creating schemas, tables, and foreign key constraints. It also provides methods to insert, update, and select data from tables.
*/
class Manager
{
Expand All @@ -39,7 +34,6 @@ public function __construct(

/**
* Create the database schema.
*
* @param Database $database
* @param bool $executeOnComplete
* @return array
Expand Down Expand Up @@ -71,7 +65,6 @@ public function createDatabase(Database $database, bool $executeOnComplete = tru

/**
* Create a schema.
*
* @param string|Schema $schema
* @return string
* @throws InvalidArgumentException
Expand All @@ -96,7 +89,6 @@ public function createSchema(string|Schema $schema): string

/**
* Create tables for a schema.
*
* @param string|Schema $schema
* @return array
*/
Expand All @@ -118,7 +110,6 @@ public function createSchemaTables(string|Schema $schema): array

/**
* Create a table.
*
* @param string $table
* @return string
* @throws NotFinalExceptions
Expand Down Expand Up @@ -180,7 +171,6 @@ public function createTable(string $table): string

/**
* Create functions for a schema.
*
* @param string|Schema $schema
* @return array
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Database/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
class Delete extends Query
{
private string $table;
/** Sets the table from which records will be deleted. @param string $table The name of the table. @return Delete Returns the current instance for method chaining./
/**
* Sets the table from which records will be deleted.
* @param string $table The name of the table.
* @return Delete Returns the current instance for method chaining.
*/
public function from(string $table): Delete
{
$this->table = $this->validateTableName($table);
Expand Down
Loading

0 comments on commit 677f6fe

Please sign in to comment.