https://github.com/yiisoft/data-db
Database adapter for data interfaces
https://github.com/yiisoft/data-db
data database db hacktoberfest yii3
Last synced: 4 months ago
JSON representation
Database adapter for data interfaces
- Host: GitHub
- URL: https://github.com/yiisoft/data-db
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2021-10-01T13:02:52.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T15:09:16.000Z (over 1 year ago)
- Last Synced: 2024-05-23T01:06:45.072Z (over 1 year ago)
- Topics: data, database, db, hacktoberfest, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 129 KB
- Stars: 14
- Watchers: 18
- Forks: 4
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Data DB
[](https://packagist.org/packages/yiisoft/data-db)
[](https://packagist.org/packages/yiisoft/data-db)
[](https://codecov.io/gh/yiisoft/data-db)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/data-db/master)
[](https://github.com/yiisoft/data-db/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/data-db)
The package provides `Yiisoft\Db\Query\Query` bindings for generic data abstractions.
Detailed build statuses:
| RDBMS | Status |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| SQLite | [](https://github.com/yiisoft/data-db/actions?query=workflow%3Asqlite) |
| MySQL | [](https://github.com/yiisoft/data-db/actions?query=workflow%3Amysql) |
| PostgreSQL | [](https://github.com/yiisoft/data-db/actions?query=workflow%3Apgsql) |
| Microsoft SQL Server | [](https://github.com/yiisoft/data-db/actions?query=workflow%3Amssql) |
| Oracle | [](https://github.com/yiisoft/data-db/actions?query=workflow%3Aoracle) |
## Requirements
- PHP 8.1 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/data-db
```
## 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:
```sql
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:
```sql
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);
```
## Current filters/processors
### Compare
- Equals - =
- NotEquals - !=
- GreaterThan - >
- GreaterThanOrEqual - >=
- In
- LessThan - <
- LessThanOrEqual - <=
- Not
- Like\ILIke
- Exists
- Between
#### Filter "Like" or "ILike"
This filters has methods `withBoth`, `withoutBoth`, `withStart`, `withoutStart`, `withEnd`, `withoutEnd`
```php
$filter = new Like('column', 'value');
$dataReader = (new QueryDataReader($query))->withFilter($filter);
//column LIKE '%value%'
$filter = (new Like('column', 'value'))->withoutStart();
$dataReader = (new QueryDataReader($query))->withFilter($filter);
//column LIKE 'value%'
$filter = (new Like('column', 'value'))->withoutEnd();
$dataReader = (new QueryDataReader($query))->withFilter($filter);
//column LIKE '%value'
```
#### Filter "Exists"
Takes only one argument with type of`Yiisoft\Db\Query\Query`
#### Filter "Not"
Takes only one argument with type of`Yiisoft\Data\Reader\Filter\FilterInterface`
### Group
- All - and
- Any - or
## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for
that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
## License
The Yii Data DB is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.
Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)