Skip to content

Commit bb200bb

Browse files
committed
feat: add support for deferred connections
1 parent 964e939 commit bb200bb

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/Db/Core.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,38 @@ public function __construct(
106106
}
107107
}
108108

109+
/**
110+
* Load db credentials, but defer connection until needed
111+
*
112+
* @param string|array $host Host Name or full config
113+
* @param string $dbname Database name
114+
* @param string $user Database username
115+
* @param string $password Database password
116+
* @param string $dbtype Type of database: mysql, postgres, sqlite, ...
117+
* @param array $pdoOptions Options for PDO connection
118+
*/
119+
public function load(
120+
$host = '127.0.0.1',
121+
string $dbname = '',
122+
string $user = 'root',
123+
string $password = '',
124+
string $dbtype = 'mysql',
125+
array $pdoOptions = []
126+
): Core {
127+
$this->config([
128+
'deferred' => [
129+
$host,
130+
$dbname,
131+
$user,
132+
$password,
133+
$dbtype,
134+
$pdoOptions,
135+
],
136+
]);
137+
138+
return $this;
139+
}
140+
109141
/**
110142
* Connect to database
111143
*
@@ -240,13 +272,18 @@ protected function dsn(): string
240272
/**
241273
* Return the database connection
242274
*
243-
* @param \PDO $connection Manual instance of PDO connection
275+
* @param \PDO|null $connection Manual instance of PDO connection
244276
*/
245-
public function connection(\PDO $connection = null)
277+
public function connection(?\PDO $connection = null)
246278
{
247279
if (!$connection) {
280+
if (!$this->connection && $this->config('deferred')) {
281+
$this->connect(...((array) $this->config('deferred')));
282+
}
283+
248284
return $this->connection;
249285
}
286+
250287
$this->connection = $connection;
251288
}
252289

@@ -265,7 +302,7 @@ public function close(): void
265302
*/
266303
public function lastInsertId($name = null)
267304
{
268-
return $this->connection->lastInsertId();
305+
return $this->connection()->lastInsertId();
269306
}
270307

271308
/**
@@ -357,7 +394,7 @@ public function execute()
357394
continue;
358395
}
359396

360-
if ($this->connection->query("SELECT * FROM {$state['table']} WHERE $unique='{$state['params'][$unique]}'")->fetch(\PDO::FETCH_ASSOC)) {
397+
if ($this->connection()->query("SELECT * FROM {$state['table']} WHERE $unique='{$state['params'][$unique]}'")->fetch(\PDO::FETCH_ASSOC)) {
361398
$this->errors[$unique] = "$unique already exists";
362399
}
363400
}
@@ -371,9 +408,9 @@ public function execute()
371408
}
372409

373410
if (count($state['bindings']) === 0) {
374-
$this->queryResult = $this->connection->query($state['query']);
411+
$this->queryResult = $this->connection()->query($state['query']);
375412
} else {
376-
$stmt = $this->connection->prepare($state['query']);
413+
$stmt = $this->connection()->prepare($state['query']);
377414
$stmt->execute($state['bindings']);
378415

379416
$this->queryResult = $stmt;

0 commit comments

Comments
 (0)