Skip to content

Commit

Permalink
Readme and composer.json fixes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerych1984 committed Oct 6, 2021
1 parent ac4b1e3 commit ce9c392
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
80 changes: 77 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://github.com/yiisoft" target="_blank">
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px">
</a>
<h1 align="center">Yii _____</h1>
<h1 align="center">Yii Data DB</h1>
<br>
</p>

Expand All @@ -15,7 +15,7 @@
[![static analysis](https://github.com/yiisoft/_____/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/_____/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/_____/coverage.svg)](https://shepherd.dev/github/yiisoft/_____)

The package ...
The package provides `Yiisoft\Db\Query\Query` bindings for generic data abstractions.

## Requirements

Expand All @@ -26,11 +26,85 @@ The package ...
The package could be installed with composer:

```shell
composer require yiisoft/_____ --prefer-dist
composer require yiisoft/data-db --prefer-dist
```

## General usage

```php
use Yiisoft\Data\Db\Filter\All;
use Yiisoft\Data\Db\Filter\Equals;
use Yiisoft\Data\Db\QueryDataReader;

$typeId = filter_input(INPUT_GET, 'type_id', FILTER_VALIDATE_INT);
$countryId = filter_input(INPUT_GET, 'country_id', FILTER_VALIDATE_INT);
$parentId = filter_input(INPUT_GET, 'parent_id', FILTER_VALIDATE_INT);

// OR
// $typeId = $_GET['type_id'] ?? null;
// $countryId = $_GET['country_id'] ?? null;
// $parentId = $_GET['parent_id'] ?? null;

// OR
// $params = $request->getQueryParams();
// $typeId = $params['type_id'] ?? null;
// $countryId = $params['country_id'] ?? null;
// $parentId = $params['parent_id'] ?? null;

// OR same with ArrayHelper::getValue();


$query = $arFactory->createQueryTo(AR::class);

$filter = new All(
(new Equals('type_id', $typeId)),
(new Equals('country_id', $countryId)),
(new Equals('parent_id', $parentId))
);

$dataReader = (new QueryDataReader($query))
->withFilter($filter);
```

If $typeId, $countryId and $parentId equals NULL that generate SQL like:

```shell
SELECT AR::tableName().* FROM AR::tableName() WHERE type_id IS NULL AND country_id IS NULL AND parent_id IS NULL
```

If we want ignore not existing arguments (i.e. not set in $_GET/queryParams), we can use withIgnoreNull(true) method:

```php
$typeId = 1;
$countryId = null;
$parentId = null;

$filter = new All(
(new Equals('type_id', $typeId))->withIgnoreNull(true),
(new Equals('country_id', $countryId))->withIgnoreNull(true),
(new Equals('parent_id', $parentId))->withIgnoreNull(true)
);

$dataReader = (new QueryDataReader($query))
->withFilter($filter);

```

That generate SQL like:

```shell
SELECT AR::tableName().* FROM AR::tableName() WHERE type_id = 1
```

If query joins several tables with same column name, pass table name as 3-th filter arguments

```php
$equalsTableOne = (new Equals('id', 1, 'table_one'))->withIgnoreNull(true);
$equalsTableTwo = (new Equals('id', 100, 'table_two'))->withIgnoreNull(true);
```

Others filters/processors will be added at the nearest time.

## Testing

### Unit testing
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "yiisoft/replace-with-package-name",
"name": "yiisoft/data-db",
"type": "library",
"description": "_____",
"description": "Database query adapater for yiisoft/data data providers",
"keywords": [
"_____"
"db",
"data provider",
"database"
],
"homepage": "https://www.yiiframework.com/",
"license": "BSD-3-Clause",
Expand Down
4 changes: 4 additions & 0 deletions src/Filter/CompareFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function getIgnoreNull(): bool
public function toArray(): array
{
if ($this->value === null) {
if ($this->ignoreNull) {
return [];
}

return ['IS', $this->column, null];
}

Expand Down
9 changes: 0 additions & 9 deletions src/Filter/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,4 @@ public static function getOperator(): string
{
return FilterEquals::getOperator();
}

public function toArray(): array
{
if ($this->value === null && $this->ignoreNull) {
return [];
}

return parent::toArray();
}
}

0 comments on commit ce9c392

Please sign in to comment.