Skip to content

Commit

Permalink
Introduce fetch* methods in QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-demb authored and morozov committed Feb 20, 2021
1 parent cab102f commit 0f9efe3
Show file tree
Hide file tree
Showing 3 changed files with 459 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,9 @@ public function fetchAllKeyValue(string $query, array $params = [], array $types
* to the first column and the values being an associative array representing the rest of the columns
* and their values.
*
* @param string $query SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string>|array<string, int|string> $types Parameter types
* @param string $query SQL query
* @param list<mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return array<mixed,array<string,mixed>>
*
Expand Down
102 changes: 102 additions & 0 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,108 @@ public function getState()
return $this->state;
}

/**
* Prepares and executes an SQL query and returns the first row of the result
* as an associative array.
*
* @return array<string, mixed>|false False is returned if no rows are found.
*
* @throws Exception
*/
public function fetchAssociative()
{
return $this->connection->fetchAssociative($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the first row of the result
* as a numerically indexed array.
*
* @return array<int, mixed>|false False is returned if no rows are found.
*
* @throws Exception
*/
public function fetchNumeric()
{
return $this->connection->fetchNumeric($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the value of a single column
* of the first row of the result.
*
* @return mixed|false False is returned if no rows are found.
*
* @throws Exception
*/
public function fetchOne()
{
return $this->connection->fetchOne($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the result as an array of numeric arrays.
*
* @return array<int,array<int,mixed>>
*
* @throws Exception
*/
public function fetchAllNumeric(): array
{
return $this->connection->fetchAllNumeric($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the result as an array of associative arrays.
*
* @return array<int,array<string,mixed>>
*
* @throws Exception
*/
public function fetchAllAssociative(): array
{
return $this->connection->fetchAllAssociative($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the result as an associative array with the keys
* mapped to the first column and the values mapped to the second column.
*
* @return array<mixed,mixed>
*
* @throws Exception
*/
public function fetchAllKeyValue(): array
{
return $this->connection->fetchAllKeyValue($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the result as an associative array with the keys mapped
* to the first column and the values being an associative array representing the rest of the columns
* and their values.
*
* @return array<mixed,array<string,mixed>>
*
* @throws Exception
*/
public function fetchAllAssociativeIndexed(): array
{
return $this->connection->fetchAllAssociativeIndexed($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Prepares and executes an SQL query and returns the result as an array of the first column values.
*
* @return array<int,mixed>
*
* @throws Exception
*/
public function fetchFirstColumn(): array
{
return $this->connection->fetchFirstColumn($this->getSQL(), $this->params, $this->paramTypes);
}

/**
* Executes this query using the bound parameters and their types.
*
Expand Down
Loading

0 comments on commit 0f9efe3

Please sign in to comment.