Skip to content

Commit

Permalink
v5.2.0 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsonjohn committed Oct 8, 2024
1 parent ce947e7 commit 218690b
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 15 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ 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.2.0] - 2024.10.08

### Added

- Added `aggregate` method and related constants.
- Added the following operators:
- `OPERATOR_STARTS_WITH_INSENSITIVE`
- `OPERATOR_DOES_NOT_START_WITH_INSENSITIVE`
- `OPERATOR_ENDS_WITH_INSENSITIVE`
- `OPERATOR_DOES_NOT_END_WITH_INSENSITIVE`
- `OPERATOR_HAS_INSENSITIVE`
- `OPERATOR_DOES_NOT_HAVE_INSENSITIVE`
- `OPERATOR_NOT_NULL`

### Depreciated

- Depreciated `getTotalRows` method in favor of `aggregate`.

## [5.1.0] - 2024.10.05

### Added
Expand Down
43 changes: 41 additions & 2 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $query = new Query($pdo); // $pdo as a PDO instance
- [single](#single)
- [getLastQuery](#getlastquery)
- [getLastParameters](#getlastparameters)
- [aggregate](#aggregate)
- [getTotalRows](#gettotalrows)

<hr />
Expand Down Expand Up @@ -172,19 +173,26 @@ Available operators are:
- `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` (is null)
- `!null` (is 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 @@ -381,6 +389,35 @@ Returns last query parameters.

<hr />

### aggregate

**Description:**

Return calculation of an aggregate function.

Available `AGGREGATE_*` constants are:

- `AGGREGATE_AVG`
- `AGGREGATE_AVG_DISTINCT`
- `AGGREGATE_COUNT`
- `AGGREGATE_COUNT_DISTINCT`
- `AGGREGATE_MAX`
- `AGGREGATE_MIN`
- `AGGREGATE_SUM`
- `AGGREGATE_SUM_DISTINCT`

**Parameters:**

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

**Returns:**

- (float)

<hr />

### getTotalRows

**Description:**
Expand All @@ -389,6 +426,8 @@ Returns total number of rows found for the query without limit restrictions.

NOTE: To get the number of rows affected by a `DELETE`, use the [Bayfront\SimplePdo\Db->rowCount()](README.md#rowcount) method.

This method is depreciated in favor of [aggregate](#aggregate).

**Parameters:**

- (None)
Expand Down
153 changes: 140 additions & 13 deletions src/Query.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpUnused */

namespace Bayfront\SimplePdo;

Expand Down Expand Up @@ -256,13 +256,20 @@ private function parseConditionColumn(string $column): string
public const OPERATOR_GREATER_THAN_OR_EQUAL = 'ge';
public const OPERATOR_STARTS_WITH = 'sw';
public const OPERATOR_DOES_NOT_START_WITH = '!sw';
public const OPERATOR_STARTS_WITH_INSENSITIVE = 'isw';
public const OPERATOR_DOES_NOT_START_WITH_INSENSITIVE = '!isw';
public const OPERATOR_ENDS_WITH = 'ew';
public const OPERATOR_DOES_NOT_END_WITH = '!ew';
public const OPERATOR_ENDS_WITH_INSENSITIVE = 'iew';
public const OPERATOR_DOES_NOT_END_WITH_INSENSITIVE = '!iew';
public const OPERATOR_HAS = 'has';
public const OPERATOR_DOES_NOT_HAVE = '!has';
public const OPERATOR_HAS_INSENSITIVE = 'ihas';
public const OPERATOR_DOES_NOT_HAVE_INSENSITIVE = '!ihas';
public const OPERATOR_IN = 'in';
public const OPERATOR_NOT_IN = '!in';
public const OPERATOR_NULL = 'null';
public const OPERATOR_NOT_NULL = '!null';

public const VALUE_TRUE = 'true';
public const VALUE_FALSE = 'false';
Expand Down Expand Up @@ -296,13 +303,20 @@ private function addCondition(string $condition, string $column, string $operato
self::OPERATOR_GREATER_THAN_OR_EQUAL,
self::OPERATOR_STARTS_WITH,
self::OPERATOR_DOES_NOT_START_WITH,
self::OPERATOR_STARTS_WITH_INSENSITIVE,
self::OPERATOR_DOES_NOT_START_WITH_INSENSITIVE,
self::OPERATOR_ENDS_WITH,
self::OPERATOR_DOES_NOT_END_WITH,
self::OPERATOR_ENDS_WITH_INSENSITIVE,
self::OPERATOR_DOES_NOT_END_WITH_INSENSITIVE,
self::OPERATOR_HAS,
self::OPERATOR_DOES_NOT_HAVE,
self::OPERATOR_HAS_INSENSITIVE,
self::OPERATOR_DOES_NOT_HAVE_INSENSITIVE,
self::OPERATOR_IN,
self::OPERATOR_NOT_IN,
self::OPERATOR_NULL
self::OPERATOR_NULL,
self::OPERATOR_NOT_NULL
])) {
throw new QueryException('Unable to build query: invalid operator (' . $operator . ') for column (' . $column . ')');
}
Expand Down Expand Up @@ -333,6 +347,28 @@ private function addCondition(string $condition, string $column, string $operato

case self::OPERATOR_STARTS_WITH:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' LIKE ' . $value;
break;
}

$placeholders[] = $value . '%';
$condition .= 'BINARY ' . $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_START_WITH:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' NOT LIKE ' . $value;
break;
}

$placeholders[] = $value . '%';
$condition .= 'BINARY ' . $column . ' NOT LIKE ?';
break;

case self::OPERATOR_STARTS_WITH_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' LIKE ' . $value;
break;
Expand All @@ -342,7 +378,7 @@ private function addCondition(string $condition, string $column, string $operato
$condition .= $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_START_WITH:
case self::OPERATOR_DOES_NOT_START_WITH_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' NOT LIKE ' . $value;
Expand All @@ -355,6 +391,28 @@ private function addCondition(string $condition, string $column, string $operato

case self::OPERATOR_ENDS_WITH:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' LIKE ' . $value;
break;
}

$placeholders[] = '%' . $value;
$condition .= 'BINARY ' . $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_END_WITH:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' NOT LIKE ' . $value;
break;
}

$placeholders[] = '%' . $value;
$condition .= 'BINARY ' . $column . ' NOT LIKE ?';
break;

case self::OPERATOR_ENDS_WITH_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' LIKE ' . $value;
break;
Expand All @@ -364,7 +422,7 @@ private function addCondition(string $condition, string $column, string $operato
$condition .= $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_END_WITH:
case self::OPERATOR_DOES_NOT_END_WITH_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' NOT LIKE ' . $value;
Expand All @@ -377,6 +435,28 @@ private function addCondition(string $condition, string $column, string $operato

case self::OPERATOR_HAS:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' LIKE ' . $value;
break;
}

$placeholders[] = '%' . $value . '%';
$condition .= 'BINARY ' . $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_HAVE:

if ($this->is_function($value)) {
$condition .= 'BINARY ' . $column . ' NOT LIKE ' . $value;
break;
}

$placeholders[] = '%' . $value . '%';
$condition .= 'BINARY ' . $column . ' NOT LIKE ?';
break;

case self::OPERATOR_HAS_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' LIKE ' . $value;
break;
Expand All @@ -386,7 +466,7 @@ private function addCondition(string $condition, string $column, string $operato
$condition .= $column . ' LIKE ?';
break;

case self::OPERATOR_DOES_NOT_HAVE:
case self::OPERATOR_DOES_NOT_HAVE_INSENSITIVE:

if ($this->is_function($value)) {
$condition .= $column . ' NOT LIKE ' . $value;
Expand Down Expand Up @@ -450,9 +530,23 @@ private function addCondition(string $condition, string $column, string $operato
$condition .= $column . ' IS NOT NULL';

} else {

throw new QueryException('Unable to build query: invalid value (' . $value . ') for operator (' . self::OPERATOR_NULL . ')');
}

break;

case self::OPERATOR_NOT_NULL:

if ($value == self::VALUE_TRUE) {

$condition .= $column . ' IS NOT NULL';

} else if ($value == self::VALUE_FALSE) {

$condition .= $column . ' IS NULL';

} else {
throw new QueryException('Unable to build query: invalid value (' . $value . ') for operator (' . self::OPERATOR_NOT_NULL . ')');
}

break;
Expand Down Expand Up @@ -766,16 +860,35 @@ public function getLastParameters(): array
return $this->placeholders;
}

public const AGGREGATE_AVG = 'AVG';
public const AGGREGATE_AVG_DISTINCT = 'AVG_DISTINCT';
public const AGGREGATE_COUNT = 'COUNT';
public const AGGREGATE_COUNT_DISTINCT = 'COUNT_DISTINCT';
public const AGGREGATE_MAX = 'MAX';
public const AGGREGATE_MIN = 'MIN';
public const AGGREGATE_SUM = 'SUM';
public const AGGREGATE_SUM_DISTINCT = 'SUM_DISTINCT';

/**
* Returns total number of rows found for the query without limit restrictions.
* Return calculation of an aggregate function.
*
* NOTE: To get the number of rows affected by a DELETE, use the Bayfront\SimplePdo\Db->rowCount() method.
*
* @return int
* @param string $aggregate (Any AGGREGATE_* constant)
* @param string $column
* @param int $decimals
* @return float
*/
public function getTotalRows(): int
public function aggregate(string $aggregate, string $column = '*', int $decimals = 2): float
{
$query = 'SELECT COUNT(*)'

$exp = explode('_', $aggregate, 2);

if (isset($exp[1])) {
$select = $exp[0] . '(' . $exp[1] . ' ' . $column . ')';
} else {
$select = $exp[0] . '(' . $column . ')';
}

$query = 'SELECT ' . $select
. Arr::get($this->query, self::QUERY_FROM, '')
. implode('', Arr::get($this->query, self::QUERY_INNER_JOIN, []))
. implode('', Arr::get($this->query, self::QUERY_LEFT_JOIN, []))
Expand All @@ -786,7 +899,21 @@ public function getTotalRows(): int

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

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

}

/**
* Returns total number of rows found for the query without limit restrictions.
*
* NOTE: To get the number of rows affected by a DELETE, use the Bayfront\SimplePdo\Db->rowCount() method.
*
* @deprecated Depreciated in favor of aggregate()
* @return int
*/
public function getTotalRows(): int
{
return (int)$this->aggregate(self::AGGREGATE_COUNT);
}

}

0 comments on commit 218690b

Please sign in to comment.