-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11e6120
commit d4865b3
Showing
8 changed files
with
181 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
|
||
declare(strict_types=1); | ||
|
||
namespace Effectra\Database\Contracts; | ||
|
||
use PDO; | ||
|
||
interface ConnectionInterface | ||
{ | ||
/** | ||
* Setups the SQLite database connection based on the provided configuration. | ||
* | ||
* @param array $config The configuration array for the SQLite database connection. | ||
* @return PDO The PDO instance representing the SQLite database connection. | ||
*/ | ||
public static function setup(array $config): PDO; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Effectra\Database\DatabaseType; | ||
|
||
use Effectra\Database\Contracts\DriverInterface; | ||
use Effectra\Database\Exception\DatabaseDriverException; | ||
use PDO; | ||
|
||
/** | ||
* Class PostgreSQL | ||
* | ||
* Represents a PostgreSQL database driver implementation. | ||
*/ | ||
class PostgreSQL implements DriverInterface | ||
{ | ||
/** | ||
* Setups the PostgreSQL database connection based on the provided configuration. | ||
* | ||
* @param array $config The configuration array for the PostgreSQL database connection. | ||
* @return PDO The PDO instance representing the PostgreSQL database connection. | ||
*/ | ||
public static function setup(array $config): PDO | ||
{ | ||
static::requireConfig($config); | ||
|
||
$dsn = sprintf( | ||
"pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s", | ||
$config['host'], | ||
$config['port'], | ||
$config['database'], | ||
$config['username'], | ||
$config['password'] | ||
); | ||
|
||
if (count($config['options']) == 0) { | ||
$config['options'] = [ | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, | ||
]; | ||
} | ||
|
||
return new PDO($dsn, $config['username'], $config['password'], $config['options']); | ||
} | ||
|
||
/** | ||
* Ensures that all required configurations are present in the given array. | ||
* | ||
* @param array $configs The array of configurations to check. | ||
* @throws DatabaseDriverException Thrown if any required configuration is missing. | ||
*/ | ||
private static function requireConfig(array $configs): void | ||
{ | ||
$requiredConfig = [ | ||
'username', 'password', 'host', 'port', 'database', | ||
]; | ||
|
||
foreach ($requiredConfig as $c) { | ||
if (!array_key_exists($c, $configs)) { | ||
throw new DatabaseDriverException("Missed required configuration: {$c}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Effectra\Database; | ||
|
||
/** | ||
* Represents the available database driver options. | ||
*/ | ||
class Driver { | ||
|
||
/** | ||
* The SQLite database driver. | ||
*/ | ||
const SQLITE = 'sqlite'; | ||
|
||
/** | ||
* The MySQL database driver. | ||
*/ | ||
const MYSQL = 'mysql'; | ||
|
||
/** | ||
* The postGreSql database driver. | ||
*/ | ||
const POSTGRESQL = 'postgresql'; | ||
} |