Skip to content

Commit

Permalink
init version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Jul 28, 2023
1 parent 11e6120 commit d4865b3
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 38 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
39 changes: 19 additions & 20 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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());
}
Expand All @@ -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()
};
}
}
19 changes: 19 additions & 0 deletions src/Contracts/ConnectionInterface.php
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;
}
32 changes: 26 additions & 6 deletions src/DatabaseType/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Effectra\Database\DatabaseType;

use Effectra\Database\Contracts\DriverInterface;
use Effectra\Database\Exception\DatabaseDriverException;
use PDO;

/**
Expand All @@ -22,19 +23,38 @@ class MySql implements DriverInterface
*/
public static function setup(array $config): PDO
{
extract($config);
static::requireConfig($config);

$db = sprintf("mysql:host=%s;dbname=%s", $host, $database);
$db = sprintf("mysql:host=%s;dbname=%s", $config['host'], $config['database']);

if (count($options) == 0) {
$options = [
if (count($config['options']) == 0) {
$config['options'] = [
PDO::ATTR_ERRMODE => 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}");
}
}
}
}
65 changes: 65 additions & 0 deletions src/DatabaseType/PostgreSQL.php
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}");
}
}
}
}
11 changes: 9 additions & 2 deletions src/DatabaseType/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Effectra\Database\DatabaseType;

use Effectra\Database\Contracts\DriverInterface;
use Effectra\Database\Exception\DatabaseDriverException;
use PDO;

/**
Expand All @@ -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);
}
}
26 changes: 26 additions & 0 deletions src/Driver.php
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';
}

0 comments on commit d4865b3

Please sign in to comment.