Skip to content

Commit

Permalink
πŸ”€ Merge pull request #4 from mychidarko/next
Browse files Browse the repository at this point in the history
Restructure of base (Bug fixes)
  • Loading branch information
mychidarko committed May 29, 2022
2 parents 52137ef + 737a22b commit 7fb2aa1
Showing 1 changed file with 63 additions and 43 deletions.
106 changes: 63 additions & 43 deletions src/Db/Core.php
Expand Up @@ -21,7 +21,7 @@ class Core
protected $config = [
'dbtype' => 'mysql',
'charset' => null,
'port' => null,
'port' => '3306',
'unixSocket' => null,
'host' => '127.0.0.1',
'username' => 'root',
Expand Down Expand Up @@ -76,14 +76,14 @@ class Core

/**
* Query result
*
*
* @var \PDOStatement
*/
protected $queryResult;

/**
* Initialize leaf db with a database connection
*
*
* @param string|array $host Host Name or full config
* @param string $dbname Database name
* @param string $user Database username
Expand All @@ -93,7 +93,7 @@ class Core
public function __construct(
$host = '',
string $dbname = '',
string $user = '',
string $user = 'root',
string $password = '',
string $dbtype = 'mysql'
) {
Expand All @@ -106,7 +106,7 @@ public function __construct(

/**
* Connect to database
*
*
* @param string|array $host Host Name or full config
* @param string $dbname Database name
* @param string $user Database username
Expand All @@ -115,21 +115,34 @@ public function __construct(
* @param array $pdoOptions Options for PDO connection
*/
public function connect(
$host = '',
$host = '127.0.0.1',
string $dbname = '',
string $user = '',
string $user = 'root',
string $password = '',
string $dbtype = '',
string $dbtype = 'mysql',
array $pdoOptions = []
): \PDO {
if (is_array($host)) {
$this->config($host);
} else {
$this->config([
'host' => $host,
'dbname' => $dbname,
'username' => $user,
'password' => $password,
'dbtype' => $dbtype,
]);
}

// response()

try {
$dbtype = $dbtype !== '' ? $dbtype : $this->config('dbtype');
$dsn = $this->dsn($host, $dbname, $dbtype);
$dbtype = $this->config('dbtype');

$connection = new \PDO(
$dsn,
$dbtype === 'sqlite' ? null : ($user !== '' ? $user : $this->config('username')),
$dbtype === 'sqlite' ? null : ($password !== '' ? $password : $this->config('password')),
$this->dsn(),
$dbtype === 'sqlite' ? null : $this->config('username'),
$dbtype === 'sqlite' ? null : $this->config('password'),
array_merge(
$this->config('pdoOptions') ?? [],
$pdoOptions
Expand All @@ -147,33 +160,39 @@ public function connect(

/**
* Connect to database using environment variables
*
*
* @param array $pdoOptions Options for PDO connection
*/
public function autoConnect(array $pdoOptions = []): \PDO
{
return $this->connect(
getenv('DB_HOST'),
getenv('DB_DATABASE'),
getenv('DB_USERNAME'),
getenv('DB_PASSWORD'),
getenv('DB_CONNECTION') ? getenv('DB_CONNECTION') : 'mysql',
$pdoOptions,
[
'dbtype' => getenv('DB_CONNECTION') ? getenv('DB_CONNECTION') : 'mysql',
'charset' => getenv('DB_CHARSET'),
'port' => getenv('DB_PORT') ? getenv('DB_PORT') : '3306',
'host' => getenv('DB_HOST') ? getenv('DB_HOST') : '127.0.0.1',
'username' => getenv('DB_USERNAME') ? getenv('DB_USERNAME') : 'root',
'password' => getenv('DB_PASSWORD') ? getenv('DB_PASSWORD') : '',
'dbname' => getenv('DB_DATABASE'),
],
'',
'',
'',
'',
$pdoOptions
);
}

protected function dsn(
$host = '',
string $dbname = '',
string $dbtype = ''
): string {
protected function dsn(): string
{
$dbtype = $this->config('dbtype');
$dbname = $this->config('dbname');
$host = $this->config('host');

if ($dbtype === 'sqlite') {
$dsn = "sqlite:$dbname";
} else {
$dbhost = $host !== '' ? $host : $this->config('host');
$dbtype = $dbtype !== '' ? $dbtype : 'mysql';

$dsn = "$dbtype:host=$dbhost";
$dsn = "$dbtype:host=$host";

if ($dbname !== '') $dsn .= ";dbname=$dbname";
if ($this->config('port')) $dsn .= ';port=' . $this->config('port');
Expand All @@ -186,7 +205,7 @@ protected function dsn(

/**
* Return the database connection
*
*
* @param \PDO $connection Manual instance of PDO connection
*/
public function connection(\PDO $connection = null)
Expand All @@ -205,7 +224,7 @@ public function close(): void

/**
* Set the current db table for operations
*
*
* @param string $table Table to perform database operations on
*/
public function table(string $table): self
Expand All @@ -221,15 +240,14 @@ public function config($name, $value = null)
{
if (class_exists('Leaf\App') && function_exists('app')) {
if (is_array($name)) {
foreach ($name as $key => $v) {
app()->config("db.$key", $v);
}
$this->config = array_merge($this->config, $name);
app()->config('db', array_merge(app()->config('db'), $this->config));
} else {
return app()->config("db.$name", $value);
}
} else {
if (is_array($name)) {
$this->config = array_merge($name, $this->config);
$this->config = array_merge($this->config, $name);
} else {
if (!$value) {
return $this->config[$name];
Expand All @@ -242,7 +260,7 @@ public function config($name, $value = null)

/**
* Manually create a database query
*
*
* @param string $sql Full db query
*/
public function query(string $sql): self
Expand All @@ -253,7 +271,7 @@ public function query(string $sql): self

/**
* Bind parameters to a query
*
*
* @param array|string $data The data to bind to string
*/
public function bind(...$bindings): self
Expand All @@ -267,8 +285,10 @@ public function bind(...$bindings): self
*/
public function execute()
{
if ($this->connection === null) trigger_error('Initialise your database first with connect()');

if ($this->connection === null) {
trigger_error('Initialise your database first with connect()');
}

$state = $this->copyState();
$this->clearState();

Expand Down Expand Up @@ -310,7 +330,7 @@ public function execute()

/**
* Get raw result of last query
*
*
* @return \PDOStatement
*/
public function result()
Expand Down Expand Up @@ -408,10 +428,10 @@ public function fetchAll($type = 'assoc')
{
$added = $this->added;
$hidden = $this->hidden;

$this->execute();

$results = array_map(function ($result) use($hidden, $added) {
$results = array_map(function ($result) use ($hidden, $added) {
if (count($hidden)) {
foreach ($hidden as $item) {
unset($result[$item]);
Expand All @@ -428,7 +448,7 @@ public function fetchAll($type = 'assoc')
if ($type == 'obj' || $type == 'object') {
$results = (object) $results;
}

return $results;
}

Expand Down

0 comments on commit 7fb2aa1

Please sign in to comment.