Skip to content

Commit

Permalink
Update DB.php
Browse files Browse the repository at this point in the history
  • Loading branch information
riverside committed Nov 15, 2020
1 parent ae11e56 commit ad80779
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class DB
{
protected $attributes = array();

private static $connection = null;
private static $config = 'database.php';

protected $connection = 'default';

private $data = null;

Expand All @@ -25,6 +27,8 @@ class DB

private $params = array();

private static $pool = array();

private $rowCount = null;

private $select = null;
Expand All @@ -37,7 +41,7 @@ class DB

public function __construct()
{
$this->connect();
$this->mount();
}

protected function buildInsert(array $data, $modifiers=null, $upsert=false): string
Expand Down Expand Up @@ -164,28 +168,9 @@ protected function buildWhere(): string
return join(' AND ', $tmp);
}

protected function connect(): void
{
if (is_object(self::$connection))
public static function config(string $filename)
{
$this->dbh = self::$connection;
return;
}

$dsn = $_ENV['PHP_ORM_DSN'];
$user = $_ENV['PHP_ORM_USER'];
$password = $_ENV['PHP_ORM_PSWD'];

try {
$this->dbh = new \PDO($dsn, $user, $password);
$this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES,true);

self::$connection = $this->dbh;

} catch (\PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
self::$config = $filename;
}

public function count(): ?int
Expand Down Expand Up @@ -395,21 +380,44 @@ public function leftJoin(string $table, string $conditions): DB
return $this;
}

public function rightJoin(string $table, string $conditions): DB
public function limit(int $rowCount=null, int $offset=null): DB
{
$this->join[] = array(
'type' => 'RIGHT',
'table' => $table,
'conditions' => $conditions,
);
$this->rowCount = $rowCount;
$this->offset = $offset;

return $this;
}

public function limit(int $rowCount=null, int $offset=null): DB
protected function mount(): DB
{
$this->rowCount = $rowCount;
$this->offset = $offset;
if (array_key_exists($this->connection, self::$pool)
&& is_object(self::$pool[$this->connection])
&& self::$pool[$this->connection] instanceof \PDO
)
{
$this->dbh = self::$pool[$this->connection];
return $this;
}

$database = include self::$config;
if (!isset($database[$this->connection]))
{
throw new \Exception("Connection not found.");
}
$opts = $database[$this->connection];

$configuration = new Configuration(
$opts['username'],
$opts['password'],
$opts['database'],
$opts['host'],
$opts['port'],
$opts['driver'],
$opts['charset'],
$opts['collation']);
$connection = new Connection($configuration);
$this->dbh = $connection->getDbh();
self::$pool[$this->connection] = $this->dbh;

return $this;
}
Expand Down Expand Up @@ -451,6 +459,17 @@ public function reset(): DB
return $this;
}

public function rightJoin(string $table, string $conditions): DB
{
$this->join[] = array(
'type' => 'RIGHT',
'table' => $table,
'conditions' => $conditions,
);

return $this;
}

public function select(string $value=null): DB
{
$this->select = $value;
Expand Down

0 comments on commit ad80779

Please sign in to comment.