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.
177 lines
4.7 KiB
177 lines
4.7 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;
|
||
|
|
||
9 months ago
|
use Phacil\Framework\Databases\Api\DriverInterface;
|
||
3 years ago
|
|
||
9 months ago
|
/** @package Phacil\Framework\Databases */
|
||
|
class SQLSRV implements DriverInterface {
|
||
10 months ago
|
|
||
|
const DB_TYPE = 'Microsoft SQL Server Database';
|
||
|
|
||
|
const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MSSQL'];
|
||
|
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var resource
|
||
|
*/
|
||
6 years ago
|
private $link;
|
||
|
|
||
3 years ago
|
/**
|
||
9 months ago
|
* {@inheritdoc}
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') {
|
||
|
/*
|
||
|
* Argument 2 passed to sqlsrv_connect() must be an array, string given
|
||
|
*/
|
||
|
$connectionInfo = array (
|
||
|
"UID" => $username,
|
||
|
"PWD" => $password,
|
||
|
"Database" => $database
|
||
|
);
|
||
|
|
||
|
if (!$this->link = \sqlsrv_connect($hostname, $connectionInfo)) {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname);
|
||
6 years ago
|
}
|
||
|
|
||
|
/*
|
||
|
if (!mssql_select_db($database, $this->link)) {
|
||
|
exit('Error: Could not connect to database ' . $database);
|
||
|
}
|
||
|
*/
|
||
|
|
||
2 years ago
|
\sqlsrv_query($this->link,"SET NAMES 'utf8'");
|
||
|
\sqlsrv_query($this->link, "SET CHARACTER SET utf8");
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
public function isConnected() {
|
||
|
return ($this->link) ? true : false;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
9 months ago
|
* {@inheritdoc}
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function query($sql) {
|
||
4 years ago
|
$resource = \sqlsrv_query($this->link, $sql);
|
||
6 years ago
|
|
||
|
if ($resource) {
|
||
|
if (is_resource($resource)) {
|
||
|
$i = 0;
|
||
|
|
||
|
$data = array();
|
||
|
|
||
3 years ago
|
while ($result = \sqlsrv_fetch_array($resource, \SQLSRV_FETCH_ASSOC)) {
|
||
6 years ago
|
$data[$i] = $result;
|
||
|
|
||
|
$i++;
|
||
|
}
|
||
|
|
||
|
\sqlsrv_free_stmt($resource);
|
||
|
|
||
10 months ago
|
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
|
||
10 months ago
|
$query = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]);
|
||
10 months ago
|
$query->setNumRows($i);
|
||
6 years ago
|
|
||
|
unset($data);
|
||
|
|
||
|
return $query;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
} else {
|
||
3 years ago
|
throw new \Phacil\Framework\Exception('Error: <br />' . $sql);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
9 months ago
|
* @inheritdoc
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function escape($value) {
|
||
|
$unpacked = unpack('H*hex', $value);
|
||
|
|
||
|
return '0x' . $unpacked['hex'];
|
||
|
}
|
||
|
|
||
9 months ago
|
/** @inheritdoc */
|
||
6 years ago
|
public function countAffected() {
|
||
|
return \sqlsrv_rows_affected($this->link);
|
||
|
}
|
||
|
|
||
9 months ago
|
/** @inheritdoc */
|
||
6 years ago
|
public function getLastId() {
|
||
|
$last_id = false;
|
||
|
|
||
4 years ago
|
$resource = \sqlsrv_query($this->link, "SELECT @@identity AS id");
|
||
6 years ago
|
|
||
|
if ($row = \sqlsrv_fetch($resource)) {
|
||
|
$last_id = trim($row[0]);
|
||
|
}
|
||
|
|
||
|
sqlsrv_free_stmt($resource);
|
||
|
|
||
|
return $last_id;
|
||
|
}
|
||
|
|
||
10 months ago
|
/**
|
||
9 months ago
|
* {@inheritdoc}
|
||
10 months ago
|
*/
|
||
|
public function execute($sql, array $params = [])
|
||
|
{
|
||
|
if (!empty($params)) {
|
||
|
// Bind parameters if there are any
|
||
|
$stmt = \sqlsrv_prepare($this->link, $sql, $params);
|
||
|
|
||
|
if ($stmt === false) {
|
||
|
throw new \Phacil\Framework\Exception('Error preparing query: ' . \sqlsrv_errors());
|
||
|
}
|
||
|
|
||
|
$result = \sqlsrv_execute($stmt);
|
||
|
|
||
|
if ($result === false) {
|
||
|
throw new \Phacil\Framework\Exception('Error executing query: ' . \sqlsrv_errors());
|
||
|
}
|
||
|
|
||
|
// Processar resultados se for um SELECT
|
||
|
if (\sqlsrv_has_rows($stmt)) {
|
||
|
$data = [];
|
||
|
while ($row = \sqlsrv_fetch_array($stmt, \SQLSRV_FETCH_ASSOC)) {
|
||
|
$data[] = $row;
|
||
|
}
|
||
|
|
||
10 months ago
|
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
|
||
10 months ago
|
$resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]);
|
||
10 months ago
|
$resultObj->setNumRows(\sqlsrv_num_rows($stmt));
|
||
|
|
||
|
return $resultObj;
|
||
|
}
|
||
|
|
||
|
// Se não for um SELECT, apenas retornar verdadeiro
|
||
|
return true;
|
||
|
} else {
|
||
|
// Se não há parâmetros, executar diretamente sem consulta preparada
|
||
|
return $this->query($sql);
|
||
|
}
|
||
|
}
|
||
10 months ago
|
|
||
|
/**
|
||
|
*
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function getDBType() {
|
||
|
return self::DB_TYPE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function getDBTypeId() {
|
||
|
return self::DB_TYPE_ID;
|
||
|
}
|
||
6 years ago
|
}
|