-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare for v1.0.1 release with join and experimental sqlite support
- Loading branch information
Showing
19 changed files
with
517 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,5 +120,4 @@ MIT, see [LICENSE file](LICENSE). | |
|
||
- Write more examples | ||
- Write having | ||
- Write joins | ||
- Schema manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
$config = new Blrf\Dbal\Config('mysql://user:pass@localhost/bookstore'); | ||
|
||
$config->create()->then( | ||
function (Blrf\Dbal\Connection $db) { | ||
// start query builder | ||
$qb = $db->query() | ||
->select('*') | ||
->from('customer_address', 'address') | ||
->join('address_status', 'address.status_id = status.status_id', 'status') | ||
->where( | ||
fn(Blrf\Dbal\Query\ConditionBuilder $cb) => $cb->and( | ||
$cb->eq('address.customer_id'), | ||
$cb->like('status.address_status') | ||
) | ||
) | ||
->setParameters([3, 'Inac%']) | ||
->limit(4); | ||
echo "sql: " . $qb->getSql() . "\n"; | ||
// sql: SELECT * FROM customer_address AS address | ||
// INNER JOIN address_status AS status ON address.status_id = status.status_id | ||
// WHERE (address.customer_id = ? AND status.address_status LIKE ?) LIMIT 4 | ||
return $qb->execute(); | ||
} | ||
)->then( | ||
function (Blrf\Dbal\Result $result) { | ||
print_r($result->rows); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Blrf\Dbal\Driver\Sqlite; | ||
|
||
use Blrf\Dbal\Config; | ||
use Blrf\Dbal\Result; | ||
use Blrf\Dbal\ResultStream; | ||
use Blrf\Dbal\Driver\Connection as DriverConnection; | ||
use Clue\React\SQLite\Factory; | ||
use Clue\React\SQLite\DatabaseInterface; | ||
use Clue\React\SQLite\Result as SqliteResult; | ||
use React\Promise\PromiseInterface; | ||
|
||
use function React\Promise\resolve; | ||
|
||
/** | ||
* React-Php Sqlite driver | ||
* | ||
* @see https://github.com/clue/reactphp-sqlite | ||
*/ | ||
class Connection extends DriverConnection | ||
{ | ||
public function connect(): PromiseInterface | ||
{ | ||
$factory = new Factory(); | ||
return $factory->open('/' . $this->config->getDb(), SQLITE3_OPEN_READWRITE)->then( | ||
function (DatabaseInterface $db) { | ||
return $this->setNativeConnection($db); | ||
} | ||
); | ||
} | ||
|
||
public function query(): QueryBuilder | ||
{ | ||
return new QueryBuilder($this); | ||
} | ||
|
||
/** | ||
* Execute sql query | ||
* | ||
* @param array<int, string> $params | ||
* @return PromiseInterface<Result> | ||
*/ | ||
public function execute(string $sql, array $params = []): PromiseInterface | ||
{ | ||
// @phpstan-ignore-next-line | ||
return $this->getNativeConnection()->query($sql, $params)->then( | ||
function (SqliteResult $res) { | ||
return new Result( | ||
$res->rows ?? [], | ||
$res->insertId, | ||
$res->changed ?? 0, | ||
0 | ||
); | ||
} | ||
); | ||
} | ||
|
||
public function stream(string $sql, array $params = []): ResultStream | ||
{ | ||
throw new \Exception('Stream not yet supported on sqlite'); | ||
} | ||
|
||
public function quit(): PromiseInterface | ||
{ | ||
return $this->getNativeConnection()->quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Blrf\Dbal\Driver\Sqlite; | ||
|
||
use Blrf\Dbal\Config; | ||
use Blrf\Dbal\Driver as DriverInterface; | ||
use Blrf\Dbal\Connection as ConnectionInterface; | ||
use React\Promise\PromiseInterface; | ||
use SensitiveParameter; | ||
|
||
class Driver implements DriverInterface | ||
{ | ||
/** | ||
* Connect | ||
* | ||
* @return PromiseInterface<ConnectionInterface> | ||
*/ | ||
public function connect( | ||
#[SensitiveParameter] | ||
Config $config | ||
): PromiseInterface { | ||
return (new Connection($config))->connect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Blrf\Dbal\Driver\Sqlite; | ||
|
||
use Blrf\Dbal\Result; | ||
use Blrf\Dbal\ResultStream; | ||
use Blrf\Dbal\Driver\QueryBuilder as DriverQueryBuilder; | ||
use React\Promise\PromiseInterface; | ||
|
||
class QueryBuilder extends DriverQueryBuilder | ||
{ | ||
/** | ||
* Execute query | ||
* | ||
* @return PromiseInterface<Result> | ||
*/ | ||
public function execute(): PromiseInterface | ||
{ | ||
return $this->connection->execute($this->getSql(), $this->getParameters()); | ||
} | ||
|
||
public function stream(): ResultStream | ||
{ | ||
return $this->connection->stream($this->getSql(), $this->getParameters()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Blrf\Dbal\Query; | ||
|
||
use ValueError; | ||
|
||
/** | ||
* @phpstan-type JoinFromArray array{ | ||
* table: string, | ||
* on: string, | ||
* alias?: string|null, | ||
* type?: string|JoinType | ||
* } | ||
*/ | ||
class JoinExpression extends Expression | ||
{ | ||
public readonly JoinType $type; | ||
|
||
/** | ||
* @param JoinFromArray $data | ||
*/ | ||
public static function fromArray(array $data): static | ||
{ | ||
$table = $data['table'] ?? ''; | ||
$on = $data['on'] ?? ''; | ||
$alias = $data['alias'] ?? null; | ||
$type = $data['type'] ?? JoinType::INNER; | ||
return new static($type, $table, $on, $alias); | ||
} | ||
|
||
public static function fromString(string $join): static | ||
{ | ||
throw new \Exception('Not implemented'); | ||
} | ||
|
||
final public function __construct( | ||
JoinType|string $type, | ||
public readonly string $table, | ||
public readonly string $on, | ||
public readonly ?string $alias = null | ||
) { | ||
if (is_string($type)) { | ||
$type = empty($type) ? JoinType::INNER : JoinType::from(strtoupper($type)); | ||
} | ||
if (strlen($table) == 0) { | ||
throw new ValueError('Join expression table cannot be empty'); | ||
} | ||
if (strlen($on) == 0) { | ||
throw new ValueError('Join expression ON cannot be empty'); | ||
} | ||
$this->type = $type; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->type->value . ' JOIN ' . $this->table . | ||
($this->alias === null ? '' : ' AS ' . $this->alias) . | ||
' ON ' . $this->on; | ||
} | ||
|
||
/** @return array{ | ||
* table: string, | ||
* on: string, | ||
* alias: string|null, | ||
* type: string | ||
* } | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'type' => $this->type->value, | ||
'table' => $this->table, | ||
'on' => $this->on, | ||
'alias' => $this->alias | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Blrf\Dbal\Query; | ||
|
||
enum JoinType: string | ||
{ | ||
case INNER = 'INNER'; | ||
case LEFT = 'LEFT'; | ||
case RIGHT = 'RIGHT'; | ||
case FULL = 'FULL'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.