Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LazyPostgresqlAdapter #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}