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.
158 lines
4.2 KiB
158 lines
4.2 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 Exception;
|
||
|
use \PDO as PDONative;
|
||
|
use stdClass;
|
||
6 years ago
|
|
||
|
final class sqlsrvPDO {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var PDONative
|
||
|
*/
|
||
6 years ago
|
private $connection = null;
|
||
3 years ago
|
|
||
|
/**
|
||
|
*
|
||
|
* @var PDONative
|
||
|
*/
|
||
6 years ago
|
private $statement = null;
|
||
|
|
||
|
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) {
|
||
|
throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\'');
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
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
|
/**
|
||
|
* @return never
|
||
|
* @throws Exception
|
||
|
*/
|
||
6 years ago
|
public function execute() {
|
||
|
try {
|
||
|
if ($this->statement && $this->statement->execute()) {
|
||
|
$data = array();
|
||
|
|
||
|
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
|
||
|
$data[] = $row;
|
||
|
}
|
||
|
|
||
|
$result = new \stdClass();
|
||
|
$result->row = (isset($data[0])) ? $data[0] : array();
|
||
|
$result->rows = $data;
|
||
|
$result->num_rows = $this->statement->rowCount();
|
||
|
}
|
||
|
} catch(\PDOException $e) {
|
||
|
throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode());
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $sql
|
||
|
* @param array $params
|
||
|
* @return stdClass
|
||
|
* @throws Exception
|
||
|
*/
|
||
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;
|
||
|
}
|
||
|
|
||
|
$result = new \stdClass();
|
||
|
$result->row = (isset($data[0]) ? $data[0] : array());
|
||
|
$result->rows = $data;
|
||
|
$result->num_rows = $this->statement->rowCount();
|
||
|
}
|
||
|
} catch (\PDOException $e) {
|
||
|
throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
|
||
|
}
|
||
|
|
||
|
if ($result) {
|
||
|
return $result;
|
||
|
} else {
|
||
|
$result = new \stdClass();
|
||
|
$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 {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
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
|
}
|
||
|
}
|