Created
March 15, 2017 14:27
-
-
Save boliveirasilva/e5d84170dacd04cfdc0b41b640a3b2f2 to your computer and use it in GitHub Desktop.
PDO DataAccess Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PDO_DataAccess | |
{ | |
/** | |
* Database connection link | |
* @var \PDO | |
*/ | |
protected $link; | |
/** | |
* Connection data | |
* | |
* @var DbConfig | |
*/ | |
protected $db_conf; | |
public function __construct(PDO $pdo = null, DbConfig $config = null) | |
{ | |
if ($pdo !== null) { | |
$this->link = $pdo; | |
} elseif ($config !== null) { | |
self::getConnection($config); | |
} else { | |
throw new Exception(__METHOD__ . '::Configure your connection, please!'); | |
} | |
} | |
private function getConnection(DbConfig $config) | |
{ | |
$pdo_dns = 'mysql:host=' . $config->getDbHost() . ';'; | |
$pdo_dns .= ($config->getDbPort() ? 'port=' . $config->getDbPort() . ';' : ''); | |
if ($config->getSocket()) { | |
$pdo_dns = 'mysql:unix_socket='.$config->getSocket().';'; | |
} | |
$pdo_dns .= 'dbname=' . $config->getDbName(); | |
$pdo_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); | |
if ($config->isPersistent()) { | |
$pdo_options[PDO::ATTR_PERSISTENT] = true; | |
} | |
$this->db_conf = $config; | |
$this->link = new PDO($pdo_dns, $config->getDbUser(), $config->getDbPass(), $pdo_options); | |
} | |
} | |
class DbConfig | |
{ | |
/** | |
* @var string | |
*/ | |
private $db_host; | |
/** | |
* @var string | |
*/ | |
private $db_name; | |
/** | |
* @var string | |
*/ | |
private $db_user; | |
/** | |
* @var string | |
*/ | |
private $db_pass; | |
/** | |
* @var string | |
*/ | |
private $table_prefix; | |
/** | |
* @var integer | |
*/ | |
private $db_port; | |
/** | |
* @var string | |
*/ | |
private $socket; | |
/** | |
* @var boolean | |
*/ | |
private $persistent; | |
public function __construct($host, $database, $user, $pass) | |
{ | |
$this->db_port = null; // ini_get("mysqli.default_port") | |
// $socket = ini_get("mysqli.default_socket"); | |
$this->setDbHost($host); | |
$this->setDbName($database); | |
$this->setDbUser($user); | |
$this->setDbPass($pass); | |
} | |
public function getDbHost() | |
{ | |
return $this->db_host; | |
} | |
/** | |
* @param string $db_host | |
* @return dbConfig | |
*/ | |
public function setDbHost($db_host) | |
{ | |
$this->persistent = stripos($db_host, 'p:') === 0; | |
$host = $this->persistent ? substr($db_host, 2) : $db_host; | |
if (strpos($host, ':') !== false) { | |
list($host, $port) = explode(':', $host); | |
// PHP may not honor the port number if connecting to 'localhost' | |
if ($port && is_numeric($port)) { | |
if (!strcasecmp($host, 'localhost')) | |
// XXX: Looks like PHP gethostbyname() is IPv4 only | |
$host = gethostbyname($host); | |
$this->setDbPort($port); | |
} elseif ($port) { | |
$this->setSocket($port); | |
} | |
} | |
$this->db_host = $host; | |
return $this; | |
} | |
public function getDbName() | |
{ | |
return $this->db_name; | |
} | |
/** | |
* @param string $db_name | |
* @return dbConfig | |
*/ | |
public function setDbName($db_name) | |
{ | |
$this->db_name = $db_name; | |
return $this; | |
} | |
public function getDbUser() | |
{ | |
return $this->db_user; | |
} | |
/** | |
* @param string $db_user | |
* @return dbConfig | |
*/ | |
public function setDbUser($db_user) | |
{ | |
$this->db_user = $db_user; | |
return $this; | |
} | |
public function getDbPass() | |
{ | |
return $this->db_pass; | |
} | |
/** | |
* @param string $db_pass | |
* @return dbConfig | |
*/ | |
public function setDbPass($db_pass) | |
{ | |
$this->db_pass = $db_pass; | |
return $this; | |
} | |
public function getTablePrefix() | |
{ | |
return $this->table_prefix; | |
} | |
/** | |
* @param string $table_prefix | |
* @return DbConfig | |
*/ | |
public function setTablePrefix($table_prefix) | |
{ | |
$this->table_prefix = $table_prefix; | |
return $this; | |
} | |
public function getDbPort() | |
{ | |
return $this->db_port; | |
} | |
/** | |
* @param int $db_port | |
* @return DbConfig | |
*/ | |
public function setDbPort($db_port) | |
{ | |
$this->db_port = (int) $db_port; | |
return $this; | |
} | |
public function getSocket() | |
{ | |
return $this->socket; | |
} | |
/** | |
* @param string $socket | |
* @return DbConfig | |
*/ | |
public function setSocket($socket) | |
{ | |
$this->socket = $socket; | |
return $this; | |
} | |
public function isPersistent() | |
{ | |
return $this->persistent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment