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.
227 lines
6.4 KiB
227 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
|
||
|
*/
|
||
|
|
||
|
namespace Phacil\Framework\Databases;
|
||
|
|
||
|
use \PDO as PDONative;
|
||
3 years ago
|
use Phacil\Framework\Interfaces\Databases;
|
||
6 years ago
|
|
||
11 months ago
|
class sqlsrvPDO implements Databases {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var PDONative
|
||
|
*/
|
||
6 years ago
|
private $connection = null;
|
||
3 years ago
|
|
||
|
/**
|
||
|
*
|
||
11 months ago
|
* @var PDOStatement
|
||
3 years ago
|
*/
|
||
6 years ago
|
private $statement = null;
|
||
|
|
||
11 months ago
|
/**
|
||
|
*
|
||
|
* @var int
|
||
|
*/
|
||
|
private $affectedRows = 0;
|
||
|
|
||
1 year ago
|
/**
|
||
|
*
|
||
|
* @param string $hostname
|
||
|
* @param string $username
|
||
|
* @param string $password
|
||
|
* @param string $database
|
||
|
* @param string $port
|
||
|
* @param string $charset
|
||
|
* @return void
|
||
|
* @throws \Phacil\Framework\Exception
|
||
|
*/
|
||
6 years ago
|
public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') {
|
||
|
try {
|
||
3 years ago
|
$this->connection = new PDONative("sqlsrv:Server=" . $hostname . ";Database=" . $database, $username, $password, array(\PDO::SQLSRV_ATTR_DIRECT_QUERY => true));
|
||
6 years ago
|
} catch(\PDOException $e) {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\'');
|
||
6 years ago
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $sql
|
||
|
* @return void
|
||
|
*/
|
||
6 years ago
|
public function prepare($sql) {
|
||
|
$this->statement = $this->connection->prepare($sql);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param mixed $parameter
|
||
|
* @param mixed $variable
|
||
|
* @param int $data_type
|
||
|
* @param int $length
|
||
|
* @return void
|
||
|
*/
|
||
6 years ago
|
public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) {
|
||
|
if ($length) {
|
||
|
$this->statement->bindParam($parameter, $variable, $data_type, $length);
|
||
|
} else {
|
||
|
$this->statement->bindParam($parameter, $variable, $data_type);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
2 years ago
|
*
|
||
3 years ago
|
* @param string $sql
|
||
|
* @param array $params
|
||
2 years ago
|
* @return \Phacil\Framework\Databases\Object\ResultInterface|true
|
||
1 year ago
|
* @throws \Phacil\Framework\Exception
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function query($sql, $params = array()) {
|
||
|
$this->statement = $this->connection->prepare($sql);
|
||
|
|
||
|
$result = false;
|
||
|
|
||
|
try {
|
||
|
if ($this->statement && $this->statement->execute($params)) {
|
||
|
$data = array();
|
||
|
|
||
|
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
|
||
|
$data[] = $row;
|
||
|
}
|
||
|
|
||
2 years ago
|
$result = new \Phacil\Framework\Databases\Object\Result();
|
||
|
$result->setRow((isset($data[0])) ? $data[0] : array());
|
||
|
$result->setRows($data);
|
||
|
$result->setNumRows($this->statement->rowCount());
|
||
6 years ago
|
}
|
||
|
} catch (\PDOException $e) {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
|
||
6 years ago
|
}
|
||
|
|
||
|
if ($result) {
|
||
|
return $result;
|
||
|
} else {
|
||
2 years ago
|
$result = new \Phacil\Framework\Databases\Object\Result();
|
||
6 years ago
|
$result->row = array();
|
||
|
$result->rows = array();
|
||
|
$result->num_rows = 0;
|
||
|
return $result;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $value
|
||
|
* @return string
|
||
|
*/
|
||
6 years ago
|
public function escape($value) {
|
||
|
return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value);
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return int */
|
||
6 years ago
|
public function countAffected() {
|
||
|
if ($this->statement) {
|
||
|
return $this->statement->rowCount();
|
||
|
} else {
|
||
11 months ago
|
return $this->affectedRows;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return string */
|
||
6 years ago
|
public function getLastId() {
|
||
|
return $this->connection->lastInsertId();
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return bool */
|
||
6 years ago
|
public function isConnected() {
|
||
|
if ($this->connection) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return void */
|
||
6 years ago
|
public function __destruct() {
|
||
3 years ago
|
unset($this->connection);
|
||
6 years ago
|
}
|
||
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 {
|
||
|
$this->statement = $this->connection->prepare($sql);
|
||
|
|
||
|
if ($this->statement === false) {
|
||
|
throw new \Phacil\Framework\Exception('Error preparing query: ' . implode(', ', $this->connection->errorInfo()));
|
||
|
}
|
||
|
|
||
|
// Bind parameters if there are any
|
||
|
if (!empty($params)) {
|
||
|
foreach ($params as $placeholder => &$param) {
|
||
|
$this->statement->bindParam($placeholder, $param, $this->getParamType($param));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$this->statement->execute();
|
||
|
|
||
|
if ($this->statement->columnCount()) {
|
||
|
$data = new \Phacil\Framework\Databases\Object\Result();
|
||
|
$data->setNumRows($this->statement->rowCount());
|
||
|
$data->setRows($this->statement->fetchAll(\PDO::FETCH_ASSOC));
|
||
|
$this->statement->closeCursor();
|
||
|
|
||
|
return $data;
|
||
|
} else {
|
||
|
$this->affectedRows = $this->statement->rowCount();
|
||
|
$this->statement->closeCursor();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
} catch (\PDOException $exception) {
|
||
|
throw new \Phacil\Framework\Exception($exception->getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @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
|
}
|