Skip to content

Commit 400cb52

Browse files
committed
refactor DbConnection
- move Connector property the the abstract class - add constructor to the abstract class - parse the connection uri in the open() method of the child class
1 parent e2f169d commit 400cb52

File tree

4 files changed

+52
-78
lines changed

4 files changed

+52
-78
lines changed

lib/Database/DbConnection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,31 @@
1010
namespace DevNet\System\Database;
1111

1212
use DevNet\System\PropertyTrait;
13+
use PDO;
1314

1415
abstract class DbConnection
1516
{
1617
use PropertyTrait;
1718

19+
protected string $connectionString;
20+
protected ?PDO $connector = null;
1821
protected int $state = 0;
1922

23+
public function __construct(string $connectionString)
24+
{
25+
$this->connectionString = $connectionString;
26+
}
27+
28+
public function get_ConnectionString(): string
29+
{
30+
return $this->connectionString;
31+
}
32+
33+
public function get_Connector(): ?PDO
34+
{
35+
return $this->connector;
36+
}
37+
2038
public function get_State(): int
2139
{
2240
return $this->state;

lib/Database/MySql/MySqlConnection.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,25 @@
1414

1515
class MySqlConnection extends DbConnection
1616
{
17-
private string $datasource;
18-
private string $username;
19-
private string $password;
20-
private ?PDO $connector;
21-
22-
public function __construct(string $connection)
23-
{
24-
$username = parse_url($connection, PHP_URL_USER);
25-
$password = parse_url($connection, PHP_URL_PASS);
26-
$host = parse_url($connection, PHP_URL_HOST);
27-
$port = parse_url($connection, PHP_URL_PORT);
28-
$path = parse_url($connection, PHP_URL_PATH);
29-
$query = parse_url($connection, PHP_URL_QUERY);
30-
31-
$username = $username ? $username : "";
32-
$password = $password ? $password : "";
33-
$host = $host ? "host=" . $host : "";
34-
$port = $port ? ":" . $port : "";
35-
$database = $path ? substr(strrchr($path, "/"), 1) : "";
36-
$options = $query ? str_replace("&", ";", $query) . ";" : "";
37-
38-
$this->datasource = "mysql:" . $host . $port . ";" . "dbname=" . $database . ";" . $options;
39-
$this->username = $username;
40-
$this->password = $password;
41-
}
42-
43-
public function get_Connector(): PDO
44-
{
45-
return $this->connector;
46-
}
47-
4817
public function open(): void
4918
{
5019
if ($this->state == 0) {
51-
$this->connector = new PDO($this->datasource, $this->username, $this->password);
20+
$username = parse_url($this->connectionString, PHP_URL_USER);
21+
$password = parse_url($this->connectionString, PHP_URL_PASS);
22+
$host = parse_url($this->connectionString, PHP_URL_HOST);
23+
$port = parse_url($this->connectionString, PHP_URL_PORT);
24+
$path = parse_url($this->connectionString, PHP_URL_PATH);
25+
$query = parse_url($this->connectionString, PHP_URL_QUERY);
26+
27+
$username = $username ? $username : "";
28+
$password = $password ? $password : "";
29+
$host = $host ? "host=" . $host : "";
30+
$port = $port ? ":" . $port : "";
31+
$database = $path ? substr(strrchr($path, "/"), 1) : "";
32+
$options = $query ? str_replace("&", ";", $query) . ";" : "";
33+
34+
$dsn = "mysql:" . $host . $port . ";" . "dbname=" . $database . ";" . $options;
35+
$this->connector = new PDO($dsn, $username, $password);
5236
$this->state = 1;
5337
}
5438
}

lib/Database/PgSql/PgSqlConnection.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,25 @@
1414

1515
class PgSqlConnection extends DbConnection
1616
{
17-
private string $datasource;
18-
private string $username;
19-
private string $password;
20-
private ?PDO $connector;
21-
22-
public function __construct(string $connection)
23-
{
24-
$username = parse_url($connection, PHP_URL_USER);
25-
$password = parse_url($connection, PHP_URL_PASS);
26-
$host = parse_url($connection, PHP_URL_HOST);
27-
$port = parse_url($connection, PHP_URL_PORT);
28-
$path = parse_url($connection, PHP_URL_PATH);
29-
$query = parse_url($connection, PHP_URL_QUERY);
30-
31-
$username = $username ? $username : "";
32-
$password = $password ? $password : "";
33-
$host = $host ? "host=" . $host : "";
34-
$port = $port ? "," . $port : "";
35-
$database = $path ? substr(strrchr($path, "/"), 1) : "";
36-
$options = $query ? str_replace("&", ";", $query) . ";" : "";
37-
38-
$this->datasource = "pgsql:" . $host . $port . ";" . "dbname=" . $database . ";" . $options;
39-
$this->username = $username;
40-
$this->password = $password;
41-
}
42-
43-
public function get_Connector(): PDO
44-
{
45-
return $this->connector;
46-
}
47-
4817
public function open(): void
4918
{
5019
if ($this->state == 0) {
51-
$this->connector = new PDO($this->datasource, $this->username, $this->password);
20+
$username = parse_url($this->connectionString, PHP_URL_USER);
21+
$password = parse_url($this->connectionString, PHP_URL_PASS);
22+
$host = parse_url($this->connectionString, PHP_URL_HOST);
23+
$port = parse_url($this->connectionString, PHP_URL_PORT);
24+
$path = parse_url($this->connectionString, PHP_URL_PATH);
25+
$query = parse_url($this->connectionString, PHP_URL_QUERY);
26+
27+
$username = $username ? $username : "";
28+
$password = $password ? $password : "";
29+
$host = $host ? "host=" . $host : "";
30+
$port = $port ? "," . $port : "";
31+
$database = $path ? substr(strrchr($path, "/"), 1) : "";
32+
$options = $query ? str_replace("&", ";", $query) . ";" : "";
33+
34+
$dsn = "pgsql:" . $host . $port . ";" . "dbname=" . $database . ";" . $options;
35+
$this->connector = new PDO($dsn, $username, $password);
5236
$this->state = 1;
5337
}
5438
}

lib/Database/Sqlite/SqliteConnection.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,11 @@
1414

1515
class SqliteConnection extends DbConnection
1616
{
17-
private string $datasource;
18-
private ?PDO $connector;
19-
20-
public function __construct(string $connection)
21-
{
22-
$this->datasource = "sqlite:" . $connection;
23-
}
24-
25-
public function get_Connector(): PDO
26-
{
27-
return $this->connector;
28-
}
29-
3017
public function open(): void
3118
{
3219
if ($this->state == 0) {
33-
$this->connector = new PDO($this->datasource);
20+
$dsn = "sqlite:" . $this->ConnectionString;
21+
$this->connector = new PDO($dsn);
3422
$this->state = 1;
3523
}
3624
}

0 commit comments

Comments
 (0)