A super easy PHP Framework for web development!
https://github.com/exacti/phacil-framework
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
6.4 KiB
253 lines
6.4 KiB
6 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
|
||
|
* GPLv3 General License.
|
||
|
* https://exacti.com.br
|
||
|
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
|
||
6 years ago
|
*/
|
||
3 years ago
|
|
||
|
namespace Phacil\Framework\Databases;
|
||
|
|
||
10 months ago
|
use Phacil\Framework\Databases\Api\DriverInterface as DatabasesDriver;
|
||
3 years ago
|
|
||
11 months ago
|
/** @package Phacil\Framework\Databases */
|
||
10 months ago
|
class MySQL_PDO implements DatabasesDriver
|
||
6 years ago
|
{
|
||
11 months ago
|
|
||
|
const DB_TYPE = 'MySQL';
|
||
|
|
||
|
const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MYSQL'];
|
||
|
|
||
6 years ago
|
/**
|
||
|
* Link to the database connection
|
||
|
*
|
||
|
* @var \PDO
|
||
|
*/
|
||
|
private $dbh;
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* List of connection settings
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
|
private $options = array(
|
||
3 years ago
|
'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT
|
||
6 years ago
|
);
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* The number of rows affected by the last operation
|
||
|
*
|
||
|
* @var int
|
||
|
*/
|
||
|
private $affectedRows = 0;
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* The data for the database connection
|
||
|
*
|
||
|
* @var \stdClass
|
||
|
*/
|
||
|
private $params = array();
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
10 months ago
|
* {@inheritdoc}
|
||
6 years ago
|
*/
|
||
6 years ago
|
public function __construct($host, $user, $pass, $name, $port = '3306', $charset = 'utf8mb4')
|
||
6 years ago
|
{
|
||
3 years ago
|
$this->params = new \stdClass;
|
||
6 years ago
|
# keep connection data
|
||
|
$this->params->host = $host;
|
||
|
$this->params->user = $user;
|
||
|
$this->params->pass = $pass;
|
||
|
$this->params->name = $name;
|
||
|
$this->params->charset = $charset;
|
||
6 years ago
|
$this->params->connstr = "mysql:host={$host};port={$port};dbname={$name};charset={$charset}";
|
||
6 years ago
|
# add the connection parameters
|
||
|
$this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES '{$charset}'";
|
||
|
$this->connect();
|
||
|
}
|
||
3 years ago
|
|
||
10 months ago
|
/** @inheritdoc */
|
||
3 years ago
|
public function isConnected() {
|
||
|
if ($this->dbh) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
/**
|
||
|
* Connect to database
|
||
|
*/
|
||
|
public function connect()
|
||
|
{
|
||
|
try {
|
||
3 years ago
|
$this->dbh = new \PDO($this->params->connstr, $this->params->user, $this->params->pass, $this->options);
|
||
6 years ago
|
if (version_compare(PHP_VERSION, '5.3.6', '<=')) {
|
||
|
$this->dbh->exec($this->options['PDO::MYSQL_ATTR_INIT_COMMAND']);
|
||
|
}
|
||
3 years ago
|
} catch (\PDOException $exception) {
|
||
3 years ago
|
throw new \Phacil\Framework\Exception($exception->getMessage());
|
||
6 years ago
|
}
|
||
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
10 months ago
|
* {@inheritdoc}
|
||
6 years ago
|
*/
|
||
1 year ago
|
public function query($sql)
|
||
6 years ago
|
{
|
||
11 months ago
|
if ($this->dbh) {
|
||
6 years ago
|
$sth=$this->dbh->prepare($sql);
|
||
|
$sth->execute();
|
||
|
//$sth= $this->dbh->query($sql);
|
||
|
$this->affectedRows = $sth->rowCount();
|
||
11 months ago
|
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
|
||
11 months ago
|
$data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$sth ? $sth->fetchAll(\PDO::FETCH_ASSOC) : array()]);
|
||
11 months ago
|
$data->setNumRows($this->affectedRows);
|
||
6 years ago
|
return $data;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* Concludes the string in quotation marks to be used in the query
|
||
|
*
|
||
|
* @param mixed $string shielded line
|
||
|
* @return string Returns shielded line or to FALSE , if the driver does not support screening
|
||
10 months ago
|
* @inheritdoc
|
||
6 years ago
|
*/
|
||
|
public function escape($string = null)
|
||
|
{
|
||
|
return $this->dbh ? str_replace("'", "", $this->dbh->quote($string)) : null;
|
||
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
10 months ago
|
* {@inheritdoc}
|
||
6 years ago
|
*/
|
||
|
public function countAffected()
|
||
|
{
|
||
|
return $this->affectedRows;
|
||
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
10 months ago
|
* {@inheritdoc}
|
||
6 years ago
|
*/
|
||
|
public function getLastId()
|
||
|
{
|
||
|
return $this->dbh ? $this->dbh->lastInsertId() : 0;
|
||
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* Gets the name of the driver
|
||
|
*
|
||
|
* @return string|null
|
||
|
*/
|
||
|
public function getDriverName()
|
||
|
{
|
||
3 years ago
|
return $this->dbh ? $this->dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) : null;
|
||
6 years ago
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* Get information about the version of the client libraries that are used by the PDO driver
|
||
|
*
|
||
|
* @return string|null
|
||
|
*/
|
||
|
public function getVersion()
|
||
|
{
|
||
3 years ago
|
return $this->dbh ? $this->dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION) : null;
|
||
6 years ago
|
}
|
||
10 months ago
|
|
||
6 years ago
|
/**
|
||
|
* Closing a database connection
|
||
|
*/
|
||
|
public function close()
|
||
|
{
|
||
|
$this->dbh = null;
|
||
|
}
|
||
11 months ago
|
|
||
|
/**
|
||
10 months ago
|
* {@inheritdoc}
|
||
11 months ago
|
*/
|
||
|
public function execute($sql, array $params = [])
|
||
|
{
|
||
|
try {
|
||
|
$stmt = $this->dbh->prepare($sql);
|
||
|
|
||
|
if ($stmt === false) {
|
||
|
throw new \Phacil\Framework\Exception('Error preparing query: ' . implode(', ', $this->dbh->errorInfo()));
|
||
|
}
|
||
|
|
||
|
// Bind parameters if there are any
|
||
|
if (!empty($params)) {
|
||
|
foreach ($params as $placeholder => &$param) {
|
||
11 months ago
|
$stmt->bindParam($placeholder, $param, $this->getParamType($param));
|
||
11 months ago
|
}
|
||
|
}
|
||
|
|
||
|
$stmt->execute();
|
||
|
|
||
|
if ($stmt->columnCount()) {
|
||
11 months ago
|
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
|
||
11 months ago
|
$data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$stmt->fetchAll(\PDO::FETCH_ASSOC)]);
|
||
11 months ago
|
$data->setNumRows($stmt->rowCount());
|
||
11 months ago
|
|
||
11 months ago
|
$stmt->closeCursor();
|
||
|
|
||
|
return $data;
|
||
|
} else {
|
||
|
$this->affectedRows = $stmt->rowCount();
|
||
|
$stmt->closeCursor();
|
||
|
|
||
|
return true;
|
||
|
|
||
|
}
|
||
|
} catch (\PDOException $exception) {
|
||
|
throw new \Phacil\Framework\Exception($exception->getMessage());
|
||
|
}
|
||
|
}
|
||
11 months ago
|
|
||
|
/**
|
||
|
*
|
||
|
* @param mixed $param
|
||
|
* @return int
|
||
|
*/
|
||
|
private function getParamType(&$param)
|
||
|
{
|
||
|
$paramType = gettype($param);
|
||
|
|
||
|
switch ($paramType) {
|
||
|
case 'boolean':
|
||
|
$paramType = \PDO::PARAM_BOOL;
|
||
|
break;
|
||
|
case 'integer':
|
||
|
$paramType = \PDO::PARAM_INT;
|
||
|
break;
|
||
|
case 'double':
|
||
|
case 'float':
|
||
|
$paramType = \PDO::PARAM_STR;
|
||
|
break;
|
||
|
case 'NULL':
|
||
|
$paramType = \PDO::PARAM_NULL;
|
||
|
break;
|
||
|
default:
|
||
|
$paramType = \PDO::PARAM_STR;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return $paramType;
|
||
|
}
|
||
11 months ago
|
|
||
|
/**
|
||
|
*
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function getDBType() {
|
||
|
return self::DB_TYPE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function getDBTypeId() {
|
||
|
return self::DB_TYPE_ID;
|
||
|
}
|
||
6 years ago
|
}
|