Skip to content

Commit 025835d

Browse files
committed
lazyload pdo wip
1 parent 68ee089 commit 025835d

File tree

4 files changed

+131
-6
lines changed

4 files changed

+131
-6
lines changed

api.php

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,7 +5226,7 @@ public function __construct(string $driver, string $address, int $port, string $
52265226
$this->database = $database;
52275227
$dsn = $this->getDsn($address, $port, $database);
52285228
$options = $this->getOptions();
5229-
$this->pdo = new \PDO($dsn, $username, $password, $options);
5229+
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
52305230
$commands = $this->getCommands();
52315231
foreach ($commands as $command) {
52325232
$this->pdo->query($command);
@@ -5420,6 +5420,7 @@ private function query(string $sql, array $parameters): \PDOStatement
54205420

54215421
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
54225422
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
5423+
use Tqdev\PhpCrudApi\Database\LazyPdo;
54235424

54245425
class GenericDefinition
54255426
{
@@ -5429,7 +5430,7 @@ class GenericDefinition
54295430
private $typeConverter;
54305431
private $reflection;
54315432

5432-
public function __construct(\PDO $pdo, string $driver, string $database)
5433+
public function __construct(LazyPdo $pdo, string $driver, string $database)
54335434
{
54345435
$this->pdo = $pdo;
54355436
$this->driver = $driver;
@@ -5842,14 +5843,16 @@ private function query(string $sql): bool
58425843
// file: src/Tqdev/PhpCrudApi/Database/GenericReflection.php
58435844
namespace Tqdev\PhpCrudApi\Database {
58445845

5846+
use Tqdev\PhpCrudApi\Database\LazyPdo;
5847+
58455848
class GenericReflection
58465849
{
58475850
private $pdo;
58485851
private $driver;
58495852
private $database;
58505853
private $typeConverter;
58515854

5852-
public function __construct(\PDO $pdo, string $driver, string $database)
5855+
public function __construct(LazyPdo $pdo, string $driver, string $database)
58535856
{
58545857
$this->pdo = $pdo;
58555858
$this->driver = $driver;
@@ -5979,6 +5982,125 @@ private function query(string $sql, array $parameters): array
59795982
}
59805983
}
59815984

5985+
// file: src/Tqdev/PhpCrudApi/Database/LazyPdo.php
5986+
namespace Tqdev\PhpCrudApi\Database {
5987+
5988+
class LazyPdo extends \PDO
5989+
{
5990+
private $dsn;
5991+
private $user;
5992+
private $password;
5993+
private $options = array();
5994+
5995+
private $pdo = null;
5996+
5997+
public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array())
5998+
{
5999+
$this->dsn = $dsn;
6000+
$this->user = $user;
6001+
$this->password = $password;
6002+
$this->options = $options;
6003+
// explicitly NOT calling super::__construct
6004+
}
6005+
6006+
private function pdo()
6007+
{
6008+
if (!$this->pdo) {
6009+
$this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
6010+
}
6011+
return $this->pdo;
6012+
}
6013+
6014+
public function setUser(/*?string*/ $user): bool
6015+
{
6016+
if ($this->pdo) {
6017+
return false;
6018+
}
6019+
$this->user = $user;
6020+
return true;
6021+
}
6022+
6023+
public function setPassword(/*?string*/ $password): bool
6024+
{
6025+
if ($this->pdo) {
6026+
return false;
6027+
}
6028+
$this->password = $password;
6029+
return true;
6030+
}
6031+
6032+
public function inTransaction(): bool
6033+
{
6034+
// Do not call parent method if there is no pdo object
6035+
return $this->pdo && parent::inTransaction();
6036+
}
6037+
6038+
public function setAttribute($attribute, $value): bool
6039+
{
6040+
if ($this->pdo) {
6041+
return $this->pdo()->setAttribute($attribute, $value);
6042+
}
6043+
$this->options[$attribute] = $value;
6044+
return true;
6045+
}
6046+
6047+
public function getAttribute($attribute): mixed
6048+
{
6049+
return $this->pdo()->getAttribute($attribute);
6050+
}
6051+
6052+
public function beginTransaction(): bool
6053+
{
6054+
return $this->pdo()->beginTransaction();
6055+
}
6056+
6057+
public function commit(): bool
6058+
{
6059+
return $this->pdo()->commit();
6060+
}
6061+
6062+
public function rollBack(): bool
6063+
{
6064+
return $this->pdo()->rollBack();
6065+
}
6066+
6067+
public function errorCode(): mixed
6068+
{
6069+
return $this->pdo()->errorCode();
6070+
}
6071+
6072+
public function errorInfo(): array
6073+
{
6074+
return $this->pdo()->errorInfo();
6075+
}
6076+
6077+
public function exec($query): int
6078+
{
6079+
return $this->pdo()->exec($query);
6080+
}
6081+
6082+
public function prepare($statement, $options = array())
6083+
{
6084+
return $this->pdo()->prepare($statement, $options);
6085+
}
6086+
6087+
public function quote($string, $parameter_type = null): string
6088+
{
6089+
return $this->pdo()->quote($string, $parameter_type);
6090+
}
6091+
6092+
public function lastInsertId(/* ?string */$name = null): string
6093+
{
6094+
return $this->pdo()->lastInsertId($name);
6095+
}
6096+
6097+
public function query(string $statement): \PDOStatement
6098+
{
6099+
return call_user_func_array(array($this->pdo(), __FUNCTION__), func_get_args());
6100+
}
6101+
}
6102+
}
6103+
59826104
// file: src/Tqdev/PhpCrudApi/Database/TypeConverter.php
59836105
namespace Tqdev\PhpCrudApi\Database {
59846106

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(string $driver, string $address, int $port, string $
7171
$this->database = $database;
7272
$dsn = $this->getDsn($address, $port, $database);
7373
$options = $this->getOptions();
74-
$this->pdo = new \PDO($dsn, $username, $password, $options);
74+
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
7575
$commands = $this->getCommands();
7676
foreach ($commands as $command) {
7777
$this->pdo->query($command);

src/Tqdev/PhpCrudApi/Database/GenericDefinition.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
55
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
6+
use Tqdev\PhpCrudApi\Database\LazyPdo;
67

78
class GenericDefinition
89
{
@@ -12,7 +13,7 @@ class GenericDefinition
1213
private $typeConverter;
1314
private $reflection;
1415

15-
public function __construct(\PDO $pdo, string $driver, string $database)
16+
public function __construct(LazyPdo $pdo, string $driver, string $database)
1617
{
1718
$this->pdo = $pdo;
1819
$this->driver = $driver;

src/Tqdev/PhpCrudApi/Database/GenericReflection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Database;
33

4+
use Tqdev\PhpCrudApi\Database\LazyPdo;
5+
46
class GenericReflection
57
{
68
private $pdo;
79
private $driver;
810
private $database;
911
private $typeConverter;
1012

11-
public function __construct(\PDO $pdo, string $driver, string $database)
13+
public function __construct(LazyPdo $pdo, string $driver, string $database)
1214
{
1315
$this->pdo = $pdo;
1416
$this->driver = $driver;

0 commit comments

Comments
 (0)