Skip to content

Commit eb4f080

Browse files
committed
Prepare for v1.0.1 release with join and experimental sqlite support
1 parent 0f28f97 commit eb4f080

19 files changed

+517
-24
lines changed

CHANGELOG.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
# Changelog
22

3-
## main-dev (2024-03-17)
4-
5-
- QueryBuilder and Condition/ConditionGroup now have `fromArray()` and `toArray()` methods.
3+
## v1.0.1 (2024-04-21)
64

7-
## main-dev (2024-03-25)
8-
9-
- Offical docs page: https://blrf.net/dbal/
10-
- Full coverage
11-
- Condtion/ConditionBuilder support for more operators (lg, lge, like, isNull, ...)
12-
13-
## main-dev (2024-04-04)
14-
15-
- PHPStan on max
5+
- QueryBuilder::join() added
6+
- sqlite driver added EXPERIMENTAL (rather old version, not production ready)
7+
- Connection::quit() method added
168

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

@@ -23,3 +15,17 @@
2315
- Update QueryBuilderInterface
2416
- Fix Result $rows param
2517
- Test QueryBuilder::select() with SelectExpression
18+
19+
## main-dev (2024-04-04)
20+
21+
- PHPStan on max
22+
23+
## main-dev (2024-03-25)
24+
25+
- Offical docs page: https://blrf.net/dbal/
26+
- Full coverage
27+
- Condtion/ConditionBuilder support for more operators (lg, lge, like, isNull, ...)
28+
29+
## main-dev (2024-03-17)
30+
31+
- QueryBuilder and Condition/ConditionGroup now have `fromArray()` and `toArray()` methods.

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,4 @@ MIT, see [LICENSE file](LICENSE).
120120

121121
- Write more examples
122122
- Write having
123-
- Write joins
124123
- Schema manager

examples/select.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ function (Blrf\Dbal\Connection $db) {
1818
$cb->eq('title')
1919
)
2020
)
21+
->orderBy('publication_date', 'DESC')
2122
->setParameters(['9789998691568', 1, 'Moby Dick'])
2223
->limit(3);
23-
// sql: SELECT * FROM book WHERE ((isbn13 = ? AND language_id = ?) OR title = ?) LIMIT 3
24+
// sql: SELECT * FROM book WHERE ((isbn13 = ? AND language_id = ?) OR title = ?)
25+
// ORDER BY publication_date DESC LIMIT 3
2426
return $qb->execute();
2527
}
2628
)->then(

examples/selectJoin.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
$config = new Blrf\Dbal\Config('mysql://user:pass@localhost/bookstore');
5+
6+
$config->create()->then(
7+
function (Blrf\Dbal\Connection $db) {
8+
// start query builder
9+
$qb = $db->query()
10+
->select('*')
11+
->from('customer_address', 'address')
12+
->join('address_status', 'address.status_id = status.status_id', 'status')
13+
->where(
14+
fn(Blrf\Dbal\Query\ConditionBuilder $cb) => $cb->and(
15+
$cb->eq('address.customer_id'),
16+
$cb->like('status.address_status')
17+
)
18+
)
19+
->setParameters([3, 'Inac%'])
20+
->limit(4);
21+
echo "sql: " . $qb->getSql() . "\n";
22+
// sql: SELECT * FROM customer_address AS address
23+
// INNER JOIN address_status AS status ON address.status_id = status.status_id
24+
// WHERE (address.customer_id = ? AND status.address_status LIKE ?) LIMIT 4
25+
return $qb->execute();
26+
}
27+
)->then(
28+
function (Blrf\Dbal\Result $result) {
29+
print_r($result->rows);
30+
}
31+
);

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
# Condition::fromArray() will return Condition or ConditionGroup based on $data keys
66
# don't know how to tell that to phpstan
77
- tests/Query/ConditionTest.php
8+
#
9+
# SQlite driver requires additional extensions and SQLite driver
10+
# is rather experimental. So we'll skip it for now.
11+
#
12+
- src/Driver/Sqlite
813

914
paths:
1015
- src/

src/Config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Config implements Stringable
3232
* @var array<string,string>
3333
*/
3434
protected static array $driverMap = [
35-
'mysql' => \Blrf\Dbal\Driver\Mysql\Driver::class
35+
'mysql' => \Blrf\Dbal\Driver\Mysql\Driver::class,
36+
'sqlite' => \Blrf\Dbal\Driver\Sqlite\Driver::class
3637
];
3738
/**
3839
* Dbal driver
@@ -219,7 +220,7 @@ public function createDriver(): Driver
219220
{
220221
$driver = $this->driver;
221222
if (!class_exists($driver)) {
222-
$class = self::$driverMap[$driver];
223+
$class = self::$driverMap[$driver] ?? null;
223224
if ($class === null) {
224225
throw new RuntimeException('No such driver: ' . $driver);
225226
}

src/Connection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function execute(string $sql, array $params = []): PromiseInterface;
4242
*/
4343
public function stream(string $sql, array $params = []): ResultStream;
4444

45+
/**
46+
* Quit (soft-close) the connection
47+
*
48+
* @return PromiseInterface<void>
49+
*/
50+
public function quit(): PromiseInterface;
51+
4552
/**
4653
* Get native connection
4754
*/

src/Driver/Connection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ abstract public function execute(string $sql, array $params = []): PromiseInterf
5353
*/
5454
abstract public function stream(string $sql, array $params = []): ResultStream;
5555

56+
/**
57+
* Quit (soft-close) the connection
58+
*
59+
* @return PromiseInterface<void>
60+
*/
61+
abstract public function quit(): PromiseInterface;
62+
5663
/**
5764
* Set underlying native connection
5865
*/

src/Driver/Mysql/Connection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ public function stream(string $sql, array $params = []): ResultStream
5757
// @phpstan-ignore-next-line
5858
return new ResultStream($this->getNativeConnection()->queryStream($sql, $params));
5959
}
60+
61+
public function quit(): PromiseInterface
62+
{
63+
// @phpstan-ignore-next-line
64+
return $this->getNativeConnection()->quit();
65+
}
6066
}

src/Driver/Sqlite/Connection.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Blrf\Dbal\Driver\Sqlite;
6+
7+
use Blrf\Dbal\Config;
8+
use Blrf\Dbal\Result;
9+
use Blrf\Dbal\ResultStream;
10+
use Blrf\Dbal\Driver\Connection as DriverConnection;
11+
use Clue\React\SQLite\Factory;
12+
use Clue\React\SQLite\DatabaseInterface;
13+
use Clue\React\SQLite\Result as SqliteResult;
14+
use React\Promise\PromiseInterface;
15+
16+
use function React\Promise\resolve;
17+
18+
/**
19+
* React-Php Sqlite driver
20+
*
21+
* @see https://github.com/clue/reactphp-sqlite
22+
*/
23+
class Connection extends DriverConnection
24+
{
25+
public function connect(): PromiseInterface
26+
{
27+
$factory = new Factory();
28+
return $factory->open('/' . $this->config->getDb(), SQLITE3_OPEN_READWRITE)->then(
29+
function (DatabaseInterface $db) {
30+
return $this->setNativeConnection($db);
31+
}
32+
);
33+
}
34+
35+
public function query(): QueryBuilder
36+
{
37+
return new QueryBuilder($this);
38+
}
39+
40+
/**
41+
* Execute sql query
42+
*
43+
* @param array<int, string> $params
44+
* @return PromiseInterface<Result>
45+
*/
46+
public function execute(string $sql, array $params = []): PromiseInterface
47+
{
48+
// @phpstan-ignore-next-line
49+
return $this->getNativeConnection()->query($sql, $params)->then(
50+
function (SqliteResult $res) {
51+
return new Result(
52+
$res->rows ?? [],
53+
$res->insertId,
54+
$res->changed ?? 0,
55+
0
56+
);
57+
}
58+
);
59+
}
60+
61+
public function stream(string $sql, array $params = []): ResultStream
62+
{
63+
throw new \Exception('Stream not yet supported on sqlite');
64+
}
65+
66+
public function quit(): PromiseInterface
67+
{
68+
return $this->getNativeConnection()->quit();
69+
}
70+
}

0 commit comments

Comments
 (0)