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.
243 lines
6.8 KiB
243 lines
6.8 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;
|
||
|
|
||
3 years ago
|
use Phacil\Framework\Interfaces\Databases;
|
||
|
|
||
11 months ago
|
class MySQL_PDO implements Databases
|
||
6 years ago
|
{
|
||
|
/**
|
||
|
* Link to the database connection
|
||
|
*
|
||
|
* @var \PDO
|
||
|
*/
|
||
|
private $dbh;
|
||
|
/**
|
||
|
* List of connection settings
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
|
private $options = array(
|
||
3 years ago
|
'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT
|
||
6 years ago
|
);
|
||
|
/**
|
||
|
* The number of rows affected by the last operation
|
||
|
*
|
||
|
* @var int
|
||
|
*/
|
||
|
private $affectedRows = 0;
|
||
|
/**
|
||
|
* The data for the database connection
|
||
|
*
|
||
|
* @var \stdClass
|
||
|
*/
|
||
|
private $params = array();
|
||
|
/**
|
||
|
* Sets the connection and connects to the database
|
||
|
*
|
||
|
* @param string $host server Address
|
||
|
* @param string $user Username
|
||
|
* @param string $pass Password
|
||
|
* @param string $name The database name
|
||
|
* @param string $charset Encoding connection
|
||
|
*/
|
||
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
|
|
||
|
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
|
}
|
||
|
}
|
||
2 years ago
|
|
||
6 years ago
|
/**
|
||
|
* Query the database
|
||
2 years ago
|
* @param string $sql
|
||
|
* @return \Phacil\Framework\Databases\Object\ResultInterface|true
|
||
|
* @throws \PDOException
|
||
6 years ago
|
*/
|
||
1 year ago
|
public function query($sql)
|
||
6 years ago
|
{
|
||
|
if ($this->dbh) {
|
||
2 years ago
|
$data = new \Phacil\Framework\Databases\Object\Result();
|
||
6 years ago
|
|
||
|
$sth=$this->dbh->prepare($sql);
|
||
|
$sth->execute();
|
||
|
//$sth= $this->dbh->query($sql);
|
||
|
$this->affectedRows = $sth->rowCount();
|
||
11 months ago
|
$data->rows = $sth ? $sth->fetchAll(\PDO::FETCH_ASSOC) : array();
|
||
6 years ago
|
$data->row = isset($data->rows[0]) ? $data->rows[0] : null;
|
||
|
$data->num_rows = $this->affectedRows;
|
||
|
return $data;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
public function escape($string = null)
|
||
|
{
|
||
|
return $this->dbh ? str_replace("'", "", $this->dbh->quote($string)) : null;
|
||
|
}
|
||
|
/**
|
||
|
* Gets the number of rows affected by the last operation
|
||
|
*
|
||
|
* @return int
|
||
|
*/
|
||
|
public function countAffected()
|
||
|
{
|
||
|
return $this->affectedRows;
|
||
|
}
|
||
|
/**
|
||
|
* Gets the ID of the last inserted row or sequence of values
|
||
|
*
|
||
|
* @return int
|
||
|
*/
|
||
|
public function getLastId()
|
||
|
{
|
||
|
return $this->dbh ? $this->dbh->lastInsertId() : 0;
|
||
|
}
|
||
|
/**
|
||
|
* 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
|
}
|
||
|
/**
|
||
|
* 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
|
}
|
||
|
/**
|
||
|
* Closing a database connection
|
||
|
*/
|
||
|
public function close()
|
||
|
{
|
||
|
$this->dbh = null;
|
||
|
}
|
||
|
public function __destruct()
|
||
|
{
|
||
|
$this->close();
|
||
|
}
|
||
11 months ago
|
|
||
|
/**
|
||
|
* Execute a prepared statement with parameters
|
||
|
*
|
||
|
* @param string $sql SQL query with named placeholders
|
||
|
* @param array $params Associative array of parameters
|
||
|
* @return \Phacil\Framework\Databases\Object\ResultInterface|true
|
||
|
* @throws \Phacil\Framework\Exception
|
||
|
*/
|
||
|
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()) {
|
||
|
$data = new \Phacil\Framework\Databases\Object\Result();
|
||
|
$data->setNumRows($stmt->rowCount());
|
||
11 months ago
|
$data->setRows($stmt->fetchAll(\PDO::FETCH_ASSOC));
|
||
11 months ago
|
//$data->setRow(isset($data->rows[0]) ? $data->rows[0] : null);
|
||
|
$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;
|
||
|
}
|
||
6 years ago
|
}
|