Skip to content

Commit

Permalink
[ENH] add db configuration call to setup database
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Aug 28, 2023
1 parent 3ba6b80 commit 1ced3c6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
7 changes: 7 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
->lang(getenv('LANG'))
->timezone(getenv('TIME_ZONE'));

(new \Wepesi\Core\Orm\DBConfig())
->db($_ENV['DB_HOST'])
->port($_ENV['DB_PORT'])
->db($_ENV['DB_NAME'])
->username($_ENV['DB_USER'])
->password($_ENV['DB_PASSWORD']);

$app = new Application($ROOT_DIR, $configuration);

require_once $app::$ROOT_DIR . '/router/route.php';
Expand Down
84 changes: 84 additions & 0 deletions src/Core/Orm/DBConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Wepesi\Core\Orm;

/**
*
*/
class DBConfig
{
/**
* @var array
*/
private static array $config = [];

/**
* @return object
*/
protected static function getConfig(): object
{
return (object)self::$config;
}

/**
* @param string $host_name
* @return $this
*/
public function host(string $host_name): DBConfig
{
self::$config['host'] = $host_name;
return $this;
}

/**
* @param string $password
* @return $this
*/
public function password(string $password): DBConfig
{
self::$config['password'] = $password;
return $this;
}

/**
* @param string $username
* @return $this
*/
public function username(string $username): DBConfig
{
self::$config['username'] = $username;
return $this;
}

/**
* @param string $port
* @return $this
*/
public function port(string $port): DBConfig
{
self::$config['port'] = $port;
return $this;
}

/**
* @param string $db_name
* @return $this
*/
public function db(string $db_name): DBConfig
{
self::$config['db'] = $db_name;
return $this;
}

/**
* @param $name
* @param $arguments
* @return mixed|void
*/
public function __call($name, $arguments)
{
if (method_exists($this, $name)) {
return call_user_func_array([$this, $name], $arguments);
}
}
}
16 changes: 8 additions & 8 deletions src/Core/Orm/Traits/QueryExecuter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Wepesi ORM
* QueryExecuter
* QueryExecute
* Ibrahim Mussa
* https://github.com/bim-g
*/
Expand All @@ -28,8 +28,8 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = []): arr
'count' => 0,
'error' => "",
];
$sql_string = explode(' ',strtolower($sql));
if($sql_string[0] == 'select' && isset($this->include_object) && count($this->include_object) > 0){
$sql_string = explode(' ', strtolower($sql));
if ($sql_string[0] == 'select' && isset($this->include_object) && count($this->include_object) > 0) {
$pdo->setAttribute(\PDO::ATTR_FETCH_TABLE_NAMES, true);
}

Expand All @@ -46,21 +46,21 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = []): arr
if ($query_result) {
$data_result['result'] = ['query_result' => true];

switch ($sql_string[0]){
switch ($sql_string[0]) {
case 'select' :
if(count($this->include_object) > 0){
if (count($this->include_object) > 0) {
$data_result['result'] = $query->fetchAll(\PDO::FETCH_GROUP);
}else{
} else {
$data_result['result'] = $query->fetchAll(\PDO::FETCH_OBJ);
}
$data_result['count'] = $query->columnCount();
break;
case 'insert' :
$last_id = $pdo->lastInsertId();
$data_result['lastID'] = $last_id;
$data_result['lastID'] = $last_id;
$data_result['count'] = $query->rowCount();
$sql = "SELECT * FROM $this->table WHERE id=?";
return $this->executeQuery($sql,[$last_id]);
return $this->executeQuery($pdo, $sql, [$last_id]);
break;
case 'update':
$data_result['count'] = $query->rowCount();
Expand Down

0 comments on commit 1ced3c6

Please sign in to comment.