Skip to content

Commit f0e5833

Browse files
authored
Merge pull request #11 from php-etl/feature/connection-management
Added multiple connections management
2 parents 79bc852 + e772811 commit f0e5833

20 files changed

+349
-156
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"symfony/expression-language": "^5.2",
1919
"php-etl/configurator-contracts": "^0.5.0",
2020
"php-etl/satellite-toolbox": "^0.3.0",
21-
"php-etl/fast-map-plugin": "^0.7.0"
21+
"php-etl/fast-map-plugin": "^0.7.0",
22+
"php-etl/packaging": "^0.1.0"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^9.5",
@@ -36,7 +37,8 @@
3637
},
3738
"autoload-dev": {
3839
"psr-4": {
39-
"functional\\Kiboko\\Plugin\\SQL\\": "tests/functional/"
40+
"functional\\Kiboko\\Plugin\\SQL\\": "tests/functional/",
41+
"functional\\Kiboko\\Plugin\\SQL\\utils\\": "tests/utils/"
4042
}
4143
},
4244
"extra": {

composer.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/ConditionalLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ConditionalLoader implements StepBuilderInterface
1818
private array $afterQueries;
1919

2020
public function __construct(
21-
private null|Node\Expr|Connection $connection = null,
21+
private null|Node\Expr|ConnectionBuilderInterface $connection = null,
2222
) {
2323
$this->logger = null;
2424
$this->alternatives = [];
@@ -43,7 +43,7 @@ public function withState(Node\Expr $state): StepBuilderInterface
4343
return $this;
4444
}
4545

46-
public function withConnection(Node\Expr|Connection $connection): StepBuilderInterface
46+
public function withConnection(Node\Expr|ConnectionBuilderInterface $connection): StepBuilderInterface
4747
{
4848
$this->connection = $connection;
4949

src/Builder/ConditionalLookup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ConditionalLookup implements StepBuilderInterface
1818
private array $afterQueries;
1919

2020
public function __construct(
21-
private null|Node\Expr|Connection $connection = null
21+
private null|Node\Expr|ConnectionBuilderInterface $connection = null
2222
) {
2323
$this->logger = null;
2424
$this->alternatives = [];
@@ -43,7 +43,7 @@ public function withState(Node\Expr $state): StepBuilderInterface
4343
return $this;
4444
}
4545

46-
public function withConnection(Node\Expr|Connection $connection): StepBuilderInterface
46+
public function withConnection(Node\Expr|ConnectionBuilderInterface $connection): StepBuilderInterface
4747
{
4848
$this->connection = $connection;
4949

src/Builder/Connection.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
namespace Kiboko\Plugin\SQL\Builder;
66

7-
use PhpParser\Builder;
87
use PhpParser\Node;
98

10-
final class Connection implements Builder
9+
final class Connection implements ConnectionBuilderInterface
1110
{
1211
private ?bool $persistentConnection;
1312

1413
public function __construct(
1514
private Node\Expr $dsn,
1615
private ?Node\Expr $username = null,
17-
private ?Node\Expr $password = null
16+
private ?Node\Expr $password = null,
17+
private string $generatedNamespace = 'GyroscopsGenerated',
1818
) {
1919
$this->persistentConnection = null;
2020
}
@@ -42,10 +42,11 @@ public function withPersistentConnection(bool $option): self
4242

4343
public function getNode(): Node\Expr
4444
{
45-
return new Node\Expr\New_(
46-
class: new Node\Name\FullyQualified('PDO'),
45+
return new Node\Expr\StaticCall(
46+
class: new Node\Name($this->generatedNamespace.'\\PDOPool'),
47+
name: new Node\Name('unique'),
4748
args: [
48-
new Node\Arg($this->dsn),
49+
new Node\Arg(value: $this->dsn),
4950
$this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')),
5051
$this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null')),
5152
new Node\Arg(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Plugin\SQL\Builder;
6+
7+
use PhpParser\Builder;
8+
9+
interface ConnectionBuilderInterface extends Builder
10+
{
11+
}

src/Builder/Extractor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Extractor implements StepBuilderInterface
1818

1919
public function __construct(
2020
private Node\Expr $query,
21-
private null|Node\Expr|Connection $connection = null,
21+
private null|Node\Expr|ConnectionBuilderInterface $connection = null,
2222
) {
2323
$this->logger = null;
2424
$this->beforeQueries = [];
@@ -43,7 +43,7 @@ public function withState(Node\Expr $state): StepBuilderInterface
4343
return $this;
4444
}
4545

46-
public function withConnection(Node\Expr|Connection $connection): StepBuilderInterface
46+
public function withConnection(Node\Expr|ConnectionBuilderInterface $connection): StepBuilderInterface
4747
{
4848
$this->connection = $connection;
4949

src/Builder/Loader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Loader implements StepBuilderInterface
1818

1919
public function __construct(
2020
private Node\Expr $query,
21-
private null|Node\Expr|Connection $connection = null,
21+
private null|Node\Expr|ConnectionBuilderInterface $connection = null,
2222
) {
2323
$this->logger = null;
2424
$this->beforeQueries = [];
@@ -43,7 +43,7 @@ public function withState(Node\Expr $state): StepBuilderInterface
4343
return $this;
4444
}
4545

46-
public function withConnection(Node\Expr|Connection $connection): StepBuilderInterface
46+
public function withConnection(Node\Expr|ConnectionBuilderInterface $connection): StepBuilderInterface
4747
{
4848
$this->connection = $connection;
4949

src/Builder/Lookup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class Lookup implements StepBuilderInterface
1717

1818
public function __construct(
1919
private AlternativeLookup $alternative,
20-
private null|Node\Expr|Connection $connection = null,
20+
private null|Node\Expr|ConnectionBuilderInterface $connection = null,
2121
) {
2222
$this->logger = null;
2323
$this->beforeQueries = [];
@@ -41,7 +41,7 @@ public function withState(Node\Expr $state): StepBuilderInterface
4141
return $this;
4242
}
4343

44-
public function withConnection(Node\Expr|Connection $connection): StepBuilderInterface
44+
public function withConnection(Node\Expr|ConnectionBuilderInterface $connection): StepBuilderInterface
4545
{
4646
$this->connection = $connection;
4747

src/Builder/SharedConnection.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Plugin\SQL\Builder;
6+
7+
use PhpParser\Node;
8+
9+
final class SharedConnection implements ConnectionBuilderInterface
10+
{
11+
private ?bool $persistentConnection;
12+
13+
public function __construct(
14+
private Node\Expr $dsn,
15+
private ?Node\Expr $username = null,
16+
private ?Node\Expr $password = null,
17+
private string $generatedNamespace = 'GyroscopsGenerated',
18+
) {
19+
$this->persistentConnection = null;
20+
}
21+
22+
public function withUsername(Node\Expr $username): self
23+
{
24+
$this->username = $username;
25+
26+
return $this;
27+
}
28+
29+
public function withPassword(Node\Expr $password): self
30+
{
31+
$this->password = $password;
32+
33+
return $this;
34+
}
35+
36+
public function withPersistentConnection(bool $option): self
37+
{
38+
$this->persistentConnection = $option;
39+
40+
return $this;
41+
}
42+
43+
public function getNode(): Node\Expr
44+
{
45+
return new Node\Expr\StaticCall(
46+
class: new Node\Name($this->generatedNamespace.'\\PDOPool'),
47+
name: new Node\Name('shared'),
48+
args: [
49+
new Node\Arg(value: $this->dsn),
50+
$this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')),
51+
$this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null')),
52+
new Node\Arg(
53+
value: new Node\Expr\Array_(
54+
items: [
55+
$this->persistentConnection ? new Node\Expr\ArrayItem(
56+
value: new Node\Expr\ConstFetch(new Node\Name("{$this->persistentConnection}")),
57+
key: new Node\Expr\ClassConstFetch(
58+
class: new Node\Name\FullyQualified('PDO'),
59+
name: new Node\Identifier('ATTR_PERSISTENT')
60+
)
61+
) : null,
62+
],
63+
attributes: [
64+
'kind' => Node\Expr\Array_::KIND_SHORT,
65+
],
66+
),
67+
),
68+
],
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)