Skip to content

Commit

Permalink
v1.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkic committed Apr 10, 2024
1 parent 2b742a6 commit b3de1d1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@

## main-dev (2024-04-04)

- PHPStan on max
- PHPStan on max

## v1.0.0 (2024-04-10)

- QueryBuilder::fromArray() will probably be deprecated
- QueryBuilder::select() accepts SelectExpression
- QueryBuilder::from() accepts FromExpression
- QueryBuilder::createSelectException() correctly calls SelectExpression::fromString()
- Update QueryBuilderInterface
- Fix Result $rows param
- Test QueryBuilder::select() with SelectExpression
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

Async database abstraction layer for [ReactPHP](https://reactphp.org/).

> **Development version**: This project is currently in development.
> This is a proof-of-concept for [ReactPHP ORM](https://github.com/dmarkic/orm) that uses this DBAL.
Full example is available in [Bookstore respository](https://github.com/dmarkic/orm-bookstore-example).
Bookstore example uses [blrf/dbal](https://github.com/dmarkic/dbal), [blrf/orm](https://github.com/dmarkic/orm) and [framework-x](https://github.com/reactphp-framework/framework-x) to showcase current DBAL/ORM development.

Expand Down
3 changes: 2 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use React\Promise\PromiseInterface;
use Blrf\Dbal\ResultStream;
use Blrf\Dbal\Driver\QueryBuilder;

/**
* Connection interface
Expand All @@ -25,7 +26,7 @@ public function connect(): PromiseInterface;
* Start query builder
*
*/
public function query(): QueryBuilderInterface;
public function query(): QueryBuilder;

/**
* Execute query on connection
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Condition implements Stringable
'is null', 'is not null'
];

/**
* Value will be null if operator is in noValueOperators
*/
public readonly ?string $value;
/**
* Create condition from array
Expand Down
3 changes: 2 additions & 1 deletion src/Query/FromExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use strlen;

/**
* SELECT [expression]
* FROM [expression]
*/
class FromExpression extends Expression
{
Expand Down Expand Up @@ -46,6 +46,7 @@ public static function fromArray(array $data): static
*
* @note Currently does not support subquery as QueryBuilder
* But could probably be done with `(...) AS x` match.
* Very basic regexp.
*/
public static function fromString(string $from): static
{
Expand Down
19 changes: 14 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Blrf\Dbal\Query\OrderByExpression;
use Blrf\Dbal\Query\OrderByType;
use Blrf\Dbal\Query\Type;
use Blrf\Dbal\Driver\QueryBuilder as DriverQueryBuilder;
use TypeError;
use array_map;
use is_array;
Expand Down Expand Up @@ -125,8 +126,9 @@ class QueryBuilder implements QueryBuilderInterface
* offset?:int
* } $data
* @param mixed $arguments Arguments to QueryBuilder constructor
* @deprecated Most probably
*/
public static function fromArray(array $data, mixed ...$arguments): QueryBuilder|QueryBuilderInterface
public static function fromArray(array $data, mixed ...$arguments): QueryBuilder|DriverQueryBuilder
{
$class = $data['class'] ?? static::class;
$qb = new $class(...$arguments);
Expand Down Expand Up @@ -250,11 +252,15 @@ public function toArray(): array
*
* @see self::createSelectExpression
*/
public function select(string ...$exprs): static
public function select(string|SelectExpression ...$exprs): static
{
$this->setType(Type::SELECT);
foreach ($exprs as $expr) {
$this->addSelectExpression($this->createSelectExpression($expr));
if ($expr instanceof SelectExpression) {
$this->addSelectExpression($expr);
} else {
$this->addSelectExpression($this->createSelectExpression($expr));
}
}
return $this;
}
Expand Down Expand Up @@ -303,8 +309,11 @@ public function delete(string|QueryBuilderInterface $from, string $as = null): s
*
* @see self::createFromExpression()
*/
public function from(string|QueryBuilderInterface $from, string $as = null): static
public function from(string|QueryBuilderInterface|FromExpression $from, string $as = null): static
{
if ($from instanceof FromExpression) {
return $this->addFromExpression($from);
}
return $this->addFromExpression($this->createFromExpression($from, $as));
}

Expand Down Expand Up @@ -633,7 +642,7 @@ protected function reset(): static
*/
protected function createSelectExpression(string $expr): SelectExpression
{
return new SelectExpression($expr);
return SelectExpression::fromString($expr);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Blrf\Dbal\Query\Condition;
use Blrf\Dbal\Query\ConditionGroup;
use Blrf\Dbal\Query\FromExpression;
use Blrf\Dbal\Query\OrderByExpression;
use Blrf\Dbal\Query\SelectExpression;

interface QueryBuilderInterface
{
Expand All @@ -16,15 +19,17 @@ interface QueryBuilderInterface
*/
public static function fromArray(array $data, mixed ...$arguments): self;

public function select(string ...$exprs): static;
public function select(string|SelectExpression ...$exprs): static;

public function update(string|self $from): static;

public function insert(string $into): static;

public function delete(string|self $from): static;

public function from(string|self $from, string $as = null): static;
public function from(string|FromExpression|self $from, string $as = null): static;

public function addFromExpression(FromExpression $expr): static;

public function value(string $column, mixed $value): static;

Expand All @@ -43,6 +48,8 @@ public function orWhere(Condition|ConditionGroup|callable $condition): static;

public function orderBy(string $orderBy, string $type = 'ASC'): static;

public function addOrderByExpression(OrderByExpression $expr): static;

public function limit(?int $offset = null, ?int $limit = null): static;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Result implements Countable
/**
* Constructor
*
* @param array<mixed> $rows
* @param array<array<mixed>> $rows Result rows
*/
public function __construct(
public readonly array $rows = [],
Expand Down
11 changes: 11 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Blrf\Dbal\QueryBuilder;
use Blrf\Dbal\Query\Condition;
use Blrf\Dbal\Query\FromExpression;
use Blrf\Dbal\Query\OrderByExpression;
use Blrf\Dbal\Query\SelectExpression;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(QueryBuilder::class)]
Expand Down Expand Up @@ -55,6 +57,15 @@ public function testSelect(): void
$this->assertSame(['f'], $nqb->getParameters());
}

public function testSelectWithExpression(): void
{
$qb = new QueryBuilder();
$qb->select(new SelectExpression('a', 'b'));
$qb->from(new FromExpression('c', 'd'));
$exp = 'SELECT a AS b FROM c AS d';
$this->assertSame($exp, $qb->getSql());
}

public function testSelectWithAddWhereWithoutPreviousWhere(): void
{
$exp = 'SELECT a,b FROM c WHERE d = ? ORDER BY e ASC LIMIT 1 OFFSET 2';
Expand Down

0 comments on commit b3de1d1

Please sign in to comment.