Skip to content

Commit

Permalink
Merge pull request #7 from phramework/xs-1.x-LazyPostgresqlAdapter
Browse files Browse the repository at this point in the history
Added LazyPostgresqlAdapter
  • Loading branch information
nohponex authored Jul 19, 2019
2 parents e6fdd50 + f9306d4 commit 8f49142
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions src/LazyPostgresqlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
declare(strict_types=1);

namespace Phramework\Database;

use Phramework\Exceptions\DatabaseException;

/**
* @author Xenofon Spafaridis <[email protected]>
*/
class LazyPostgresqlAdapter implements IAdapter
{
/** @var IAdapter */
private $adapter;

/** @var array */
private $settingsDb;

public function __construct(
array $databaseSettings
) {
$this->settingsDb = $databaseSettings;
}

/**
* @throws DatabaseException
*/
private function getAdapter(): IAdapter
{
if ($this->adapter === null) {
$this->adapter = new PostgreSQL($this->settingsDb);
}

return $this->adapter;
}

public function getAdapterName()
{
return 'postgresql';
}

public function execute($query, $parameters = [])
{
return $this
->getAdapter()
->execute(
$query,
$parameters
);
}

public function executeLastInsertId($query, $parameters = [])
{
return $this
->getAdapter()
->executeLastInsertId(
$query,
$parameters
);
}

public function executeAndFetch($query, $parameters = [], $castModel = null)
{
return $this
->getAdapter()
->executeAndFetch(
$query,
$parameters,
$castModel
);
}

public function executeAndFetchAll($query, $parameters = [], $castModel = null)
{
return $this
->getAdapter()
->executeAndFetchAll(
$query,
$parameters,
$castModel
);
}

public function executeAndFetchArray($query, $parameters = [])
{
return $this
->getAdapter()
->executeAndFetchArray(
$query,
$parameters
);
}

public function executeAndFetchAllArray($query, $parameters = [])
{
return $this
->getAdapter()
->executeAndFetchAllArray(
$query,
$parameters
);
}

public function bindExecuteLastInsertId($query, $parameters = [])
{
return $this
->getAdapter()
->bindExecuteLastInsertId(
$query,
$parameters
);
}

public function bindExecute($query, $parameters = [])
{
return $this
->getAdapter()
->bindExecute(
$query,
$parameters
);
}

public function bindExecuteAndFetch($query, $parameters = [], $castModel = null)
{
return $this
->getAdapter()
->bindExecuteAndFetch(
$query,
$parameters,
$castModel
);
}

public function bindExecuteAndFetchAll($query, $parameters = [], $castModel = null)
{
return $this
->getAdapter()
->bindExecuteAndFetchAll(
$query,
$parameters,
$castModel
);
}

public function close()
{
if ($this->adapter !== null) {
$this
->adapter
->close();
}

$this->adapter = null;
}
}

0 comments on commit 8f49142

Please sign in to comment.