diff --git a/README.md b/README.md index 716626d..6bbb281 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Effectra\Database is a PHP package that provides database connection and query e You can install the Effectra\Database package via Composer. Simply run the following command: ```bash -composer require effectra/database +composer require effectra/db ``` ## Usage @@ -18,17 +18,25 @@ To establish a database connection, you need to create an instance of the `Conne ```php use Effectra\Database\Connection; +use Effectra\Database\Diver; use Effectra\Config\ConfigDB; // Create a new instance of the Connection class +$mysqlConfig = [ + 'driver' => 'mysql', + 'host' => 'localhost', + 'database' => 'your_database_name', + 'username' => 'your_mysql_username', + 'password' => 'your_mysql_password', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + // Add any additional options if needed +]; + $connection = new Connection( - driver: $driver, - host: $host, - port: $port, - username: $username, - password: $password, - database: $database, + Driver::MYSQL, + $mysqlConfig ); // Establish the database connection $pdo = $connection->connect(); diff --git a/composer.json b/composer.json index e95ea7d..e72b031 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,7 @@ } ], "require": { - "effectra/config":"1.0", - "effectra/sql-query":"1.0" + "effectra/sql-query":"^1.0" }, - "minimum-stability": "dev" + "minimum-stability": "stable" } diff --git a/src/Connection.php b/src/Connection.php index ee434bc..04d8cc7 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -4,9 +4,9 @@ namespace Effectra\Database; -use Effectra\Config\ConfigDB; use Effectra\Database\Contracts\DriverInterface; use Effectra\Database\DatabaseType\MySql; +use Effectra\Database\DatabaseType\PostgreSQL; use Effectra\Database\DatabaseType\Sqlite; use Effectra\Database\Exception\DatabaseDriverException; use Effectra\Database\Exception\DatabaseException; @@ -18,8 +18,14 @@ * * Represents a database connection manager. */ -class Connection extends ConfigDB +class Connection { + + public function __construct( + protected string $driver, + protected array $config + ) { + } /** * Establishes a database connection based on the configuration. * @@ -28,10 +34,13 @@ class Connection extends ConfigDB */ public function connect(): PDO { - $config = $this->getConfig(); + $driver = $this->getDatabaseDriver(); + if (!$driver instanceof DriverInterface) { + throw new DatabaseDriverException("Error Processing Driver"); + } try { - return $this->getDatabaseDriver($config['driver'])->setup($config); + return $driver->setup($this->config); } catch (PDOException $e) { throw new DatabaseException($e->getMessage()); } @@ -40,25 +49,15 @@ public function connect(): PDO /** * Retrieves the appropriate database driver based on the given driver string. * - * @param string|DriverInterface $driver The driver string or an instance of DriverInterface. * @return DriverInterface The instance of the DriverInterface implementation for the specified driver. * @throws DatabaseDriverException If the driver is not supported or does not exist. */ - public function getDatabaseDriver(string $driver): DriverInterface + public function getDatabaseDriver(): DriverInterface { - $drivers = [ - 'mysql' => MySql::class, - 'sqlite' => Sqlite::class, - ]; - - if (isset($drivers[$driver])) { - return new $drivers[$driver](); - } - - if ($driver instanceof DriverInterface) { - return $driver; - } - - throw new DatabaseDriverException("This driver '$driver' does not exist."); + return match($this->driver){ + 'mysql'=> new MySql(), + 'sqlite' => new Sqlite(), + 'postgresql' =>new PostgreSQL() + }; } } diff --git a/src/Contracts/ConnectionInterface.php b/src/Contracts/ConnectionInterface.php new file mode 100644 index 0000000..7c1cc5b --- /dev/null +++ b/src/Contracts/ConnectionInterface.php @@ -0,0 +1,19 @@ + PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_PERSISTENT => false, - PDO::MYSQL_ATTR_INIT_COMMAND => sprintf('SET NAMES %s COLLATE %s', $charset, $collation) + PDO::MYSQL_ATTR_INIT_COMMAND => sprintf('SET NAMES %s COLLATE %s', $config['charset'], $config['collation']) ]; } - return new PDO($db, $username, $password, $options); + return new PDO($db, $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', 'database', 'charset', 'collation', + ]; + + foreach ($requiredConfig as $c) { + if (!array_key_exists($c, $configs)) { + throw new DatabaseDriverException("Missed required configuration: {$c}"); + } + } } } diff --git a/src/DatabaseType/PostgreSQL.php b/src/DatabaseType/PostgreSQL.php new file mode 100644 index 0000000..a8f2d5c --- /dev/null +++ b/src/DatabaseType/PostgreSQL.php @@ -0,0 +1,65 @@ + 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}"); + } + } + } +} diff --git a/src/DatabaseType/Sqlite.php b/src/DatabaseType/Sqlite.php index 061ccd7..51aac23 100644 --- a/src/DatabaseType/Sqlite.php +++ b/src/DatabaseType/Sqlite.php @@ -5,6 +5,7 @@ namespace Effectra\Database\DatabaseType; use Effectra\Database\Contracts\DriverInterface; +use Effectra\Database\Exception\DatabaseDriverException; use PDO; /** @@ -19,13 +20,19 @@ class Sqlite implements DriverInterface * * @param array $config The configuration array for the SQLite database connection. * @return PDO The PDO instance representing the SQLite database connection. + * @throws DatabaseDriverException When required configuration is missing. */ public static function setup(array $config): PDO { - extract($config); + // Check if the required 'database' configuration key is set + if (!isset($config['database'])) { + throw new DatabaseDriverException("Required configuration 'database' is missing."); + } - $db = sprintf("sqlite:%s", $database); + // Create the database connection string + $db = sprintf("sqlite:%s", $config['database']); + // Create and return the PDO instance representing the SQLite database connection return new PDO($db); } } diff --git a/src/Driver.php b/src/Driver.php new file mode 100644 index 0000000..4259dc0 --- /dev/null +++ b/src/Driver.php @@ -0,0 +1,26 @@ +