Giter Site home page Giter Site logo

data-db's Introduction

Yii

Yii Data DB


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge static analysis type-coverage

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

Requirements

  • PHP 8.1 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/data-db

General usage

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:

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:

$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:

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

$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

$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 ofYiisoft\Db\Query\Query

Filter "Not"

Takes only one argument with type ofYiisoft\Data\Reader\Filter\FilterInterface

Group

  • All - and
  • Any - or

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Data DB is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

data-db's People

Contributors

arogachev avatar dependabot[bot] avatar devanych avatar gerych1984 avatar luizcmarin avatar samdark avatar sankaest avatar stylecibot avatar terabytesoftw avatar vjik avatar xepozz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

data-db's Issues

`QueryDataReader::createItem()` shold be realized in `Db\Query` class

QueryDataReader::createItem() method should be removed

protected function createItem(array|object $row): array|object
{
/** @psalm-var TValue */
return $row;
}

BatchQueryResult already has property Closure $populateMethod to prepare row result

https://github.com/yiisoft/db/blob/e380ae2ffb6f3a466e6f168fafa93548e50f0d8c/src/Query/BatchQueryResult.php#L109

Instead of these, Db\Query class can contain property Closure $callback (and withCallback() method) which will be called for each row from result.

The $callback will be specified in one place (Query) instead of many different realizations. And this will allow to use Query directly to get prepared result (without any data reader).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.