Skip to content

Commit f6c3ed2

Browse files
committed
add DbConnectionStringBuilder
use DbConnectionStringBuilder to parse connectionString in DbConnection
1 parent 400cb52 commit f6c3ed2

File tree

3 files changed

+157
-32
lines changed

3 files changed

+157
-32
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @copyright Copyright (c) Mohammed Moussaoui. All rights reserved.
6+
* @license MIT License. For full license information see LICENSE file in the project root.
7+
* @link https://github.com/DevNet-Framework
8+
*/
9+
10+
namespace DevNet\System\Database;
11+
12+
use ArrayAccess;
13+
use DevNet\System\Collections\Enumerator;
14+
use DevNet\System\Collections\IEnumerable;
15+
use DevNet\System\Exceptions\TypeException;
16+
use DevNet\System\PropertyTrait;
17+
18+
class DbConnectionStringBuilder implements IEnumerable, ArrayAccess
19+
{
20+
use PropertyTrait;
21+
22+
protected array $items = [];
23+
24+
public function set_ConnectionString(string $connectionString): void
25+
{
26+
preg_match_all('%;?([^=]+)\s*=\s*([^;]+)%', $connectionString, $matches);
27+
if ($matches) {
28+
foreach ($matches[1] as $index => $key) {
29+
$this->items[strtolower($key)] = $matches[2][$index];
30+
}
31+
}
32+
}
33+
34+
public function get_ConnectionString(): string
35+
{
36+
$items = [];
37+
foreach ($this->items as $key => $value) {
38+
$items[] = $key . '=' . $value;
39+
}
40+
41+
return implode(';', $items);
42+
}
43+
44+
public function get_Items(): array
45+
{
46+
return $this->Items;
47+
}
48+
49+
public function offsetSet($key, $value): void
50+
{
51+
if (!is_string($key)) {
52+
throw new TypeException("Illegal key type, the key must be of type string", 0, 1);
53+
}
54+
55+
if (!is_string($value)) {
56+
throw new TypeException("Illegal value type, the value must be of type string", 0, 1);
57+
}
58+
59+
$this->items[strtolower($key)] = $value;
60+
}
61+
62+
public function offsetExists($key): bool
63+
{
64+
if (!is_string($key)) {
65+
throw new TypeException("Illegal key type, the key must be of type string", 0, 1);
66+
}
67+
68+
return isset($this->items[strtolower($key)]);
69+
}
70+
71+
public function offsetGet($key): mixed
72+
{
73+
if (!is_string($key)) {
74+
throw new TypeException("Illegal key type, the key must be of type string", 0, 1);
75+
}
76+
77+
return $this->items[strtolower($key)] ?? null;
78+
}
79+
80+
public function offsetUnset($key): void
81+
{
82+
if (!is_string($key)) {
83+
throw new TypeException("Illegal key type, the key must be of type string", 0, 1);
84+
}
85+
86+
unset($this->items[strtolower($key)]);
87+
}
88+
89+
public function getIterator(): Enumerator
90+
{
91+
return new Enumerator($this->items);
92+
}
93+
94+
public function __toString()
95+
{
96+
return $this->ConnectionString;
97+
}
98+
}

lib/Database/MySql/MySqlConnection.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,43 @@
1010
namespace DevNet\System\Database\MySql;
1111

1212
use DevNet\System\Database\DbConnection;
13+
use DevNet\System\Database\DbConnectionStringBuilder;
1314
use PDO;
1415

1516
class MySqlConnection extends DbConnection
1617
{
1718
public function open(): void
1819
{
1920
if ($this->state == 0) {
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);
21+
$parser = new DbConnectionStringBuilder();
22+
$parser->ConnectionString = $this->connectionString;
23+
$dsn = new DbConnectionStringBuilder();
24+
25+
foreach ($parser as $key => $value) {
26+
switch (strtolower($key)) {
27+
case 'hostname':
28+
case 'host':
29+
$dsn['host'] = $value;
30+
break;
31+
case 'schema':
32+
case 'database':
33+
case 'dbname':
34+
$dsn['dbname'] = $value;
35+
break;
36+
case 'username':
37+
case 'user':
38+
$username = $value;
39+
break;
40+
case 'password':
41+
$password = $value;
42+
break;
43+
default:
44+
$dsn[$key] = $value;
45+
break;
46+
}
47+
}
48+
49+
$this->connector = new PDO('mysql:' . $dsn, $username ?? null, $password ?? null);
3650
$this->state = 1;
3751
}
3852
}

lib/Database/PgSql/PgSqlConnection.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,42 @@
1010
namespace DevNet\System\Database\PgSql;
1111

1212
use DevNet\System\Database\DbConnection;
13+
use DevNet\System\Database\DbConnectionStringBuilder;
1314
use PDO;
1415

1516
class PgSqlConnection extends DbConnection
1617
{
1718
public function open(): void
1819
{
1920
if ($this->state == 0) {
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);
21+
$parser = new DbConnectionStringBuilder();
22+
$parser->ConnectionString = $this->connectionString;
23+
$dsn = new DbConnectionStringBuilder();
24+
25+
foreach ($parser as $key => $value) {
26+
switch (strtolower($key)) {
27+
case 'hostname':
28+
case 'host':
29+
$dsn['host'] = $value;
30+
break;
31+
case 'database':
32+
case 'dbname':
33+
$dsn['dbname'] = $value;
34+
break;
35+
case 'username':
36+
case 'user':
37+
$username = $value;
38+
break;
39+
case 'password':
40+
$password = $value;
41+
break;
42+
default:
43+
$dsn[$key] = $value;
44+
break;
45+
}
46+
}
47+
48+
$this->connector = new PDO('pgsql:' . $dsn, $username ?? null, $password ?? null);
3649
$this->state = 1;
3750
}
3851
}

0 commit comments

Comments
 (0)