Skip to content

Commit

Permalink
Added groupBy method (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsonjohn authored Oct 9, 2024
1 parent dcf074c commit 53b4843
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities

## [5.3.0] - 2024.10.08

### Added

- Added `groupBy` method.

### Changed

- Updated documentation.

## [5.2.0] - 2024.10.08

### Added
Expand Down
39 changes: 29 additions & 10 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ $query = new Query($pdo); // $pdo as a PDO instance
- [select](#select)
- [where](#where)
- [orWhere](#orwhere)
- [groupBy](#groupby)
- [orderBy](#orderby)
- [orderByRand](#orderbyrand)
- [limit](#limit)
Expand Down Expand Up @@ -238,6 +239,22 @@ See [where](#where).

<hr />

### groupBy

**Description:**

Adds a `GROUP BY` clause.

**Parameters:**

- `$columns` (array)

**Returns:**

- (self)

<hr />

### orderBy

**Description:**
Expand Down Expand Up @@ -395,20 +412,22 @@ Returns last query parameters.

Return calculation of an aggregate function.

Available `AGGREGATE_*` constants are:
Available aggregate functions are:

- `AVG`
- `AVG_DISTINCT`
- `COUNT`
- `COUNT_DISTINCT`
- `MAX`
- `MIN`
- `SUM`
- `SUM_DISTINCT`

- `AGGREGATE_AVG`
- `AGGREGATE_AVG_DISTINCT`
- `AGGREGATE_COUNT`
- `AGGREGATE_COUNT_DISTINCT`
- `AGGREGATE_MAX`
- `AGGREGATE_MIN`
- `AGGREGATE_SUM`
- `AGGREGATE_SUM_DISTINCT`
The `AGGREGATE_*` constants can be used for this purpose.

**Parameters:**

- `$aggregate` (string): Any `AGGREGATE_*` constant
- `$aggregate` (string): Any valid aggregate function
- `$column = '*'` (string)
- `$decimals = 2` (int)

Expand Down
40 changes: 36 additions & 4 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(PDO $pdo)
private const QUERY_RIGHT_JOIN = 'right_join';
private const QUERY_COLUMNS = 'columns';
private const QUERY_WHERE = 'where';
private const QUERY_GROUP = 'group';
private const QUERY_SORT = 'sort';
private const QUERY_LIMIT = 'limit';
private const QUERY_OFFSET = 'offset';
Expand Down Expand Up @@ -596,19 +597,26 @@ private function addCondition(string $condition, string $column, string $operato
* - ge (greater than or equal to)
* - sw (starts with)
* - !sw (does not start with)
* - isw (starts with - case-insensitive)
* - !isw (does not start with - case-insensitive)
* - ew (ends with)
* - !ew (does not end with)
* - iew (ends with - case-insensitive)
* - !iew (does not end with - case-insensitive)
* - has (has)
* - !has (does not have)
* - ihas (has - case-insensitive)
* - !ihas (does not have - case-insensitive)
* - in (in)
* - !in (not in)
* - null (is or is not null)
* - null (null)
* - !null (not null)
*
* The OPERATOR_* constants can be used for this purpose.
*
* The in and !in operators accept multiple comma-separated values.
*
* The "null" operator accepts two values: true and false for is null or is not null.
* The "null" and "!null" operators accept one of two values: "true" and "false".
* The VALUE_* constants can be used for this purpose.
*
* NOTE: Some native MySQL functions can be used as the $value, however, they will be
Expand Down Expand Up @@ -643,6 +651,27 @@ public function orWhere(string $column, string $operator, mixed $value): self
return $this;
}

/**
* Adds a GROUP BY clause.
*
* @param array $columns
* @return self
*/
public function groupBy(array $columns): self
{

if (empty($columns)) {
return $this;
}

$string = ' GROUP BY ' . implode(', ', $columns);

$this->query[self::QUERY_GROUP] = rtrim($string, ', ');

return $this;

}

/**
* Adds an ORDER BY clause.
*
Expand Down Expand Up @@ -747,6 +776,7 @@ protected function getQuery(): string
. implode('', Arr::get($this->query, self::QUERY_LEFT_JOIN, []))
. implode('', Arr::get($this->query, self::QUERY_RIGHT_JOIN, []))
. Arr::get($this->query, self::QUERY_WHERE, '')
. Arr::get($this->query, self::QUERY_GROUP, '')
. Arr::get($this->query, self::QUERY_SORT, '')
. Arr::get($this->query, self::QUERY_LIMIT, '')
. Arr::get($this->query, self::QUERY_OFFSET, '');
Expand Down Expand Up @@ -899,13 +929,15 @@ public function aggregate(string $aggregate, string $column = '*', int $decimals
. implode('', Arr::get($this->query, self::QUERY_INNER_JOIN, []))
. implode('', Arr::get($this->query, self::QUERY_LEFT_JOIN, []))
. implode('', Arr::get($this->query, self::QUERY_RIGHT_JOIN, []))
. Arr::get($this->query, self::QUERY_WHERE, '');
. Arr::get($this->query, self::QUERY_WHERE, '')
. Arr::get($this->query, self::QUERY_GROUP, '')
. Arr::get($this->query, self::QUERY_SORT, '');

$stmt = $this->pdo->prepare($query);

$stmt->execute($this->placeholders);

return round($stmt->fetchColumn(), $decimals);
return round((float)$stmt->fetchColumn(), $decimals);

}

Expand Down

0 comments on commit 53b4843

Please sign in to comment.