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.1 KiB
158 lines
4.1 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;
|
||
|
|
||
3 years ago
|
use Phacil\Framework\Interfaces\Databases;
|
||
|
|
||
11 months ago
|
class Postgre implements Databases {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var resource|false
|
||
|
*/
|
||
6 years ago
|
private $link;
|
||
3 years ago
|
|
||
|
/**
|
||
|
*
|
||
|
* @param string $hostname
|
||
|
* @param string $username
|
||
|
* @param string $password
|
||
|
* @param string $database
|
||
|
* @param string $port
|
||
|
* @param string $charset
|
||
|
* @return void
|
||
1 year ago
|
* @throws \Phacil\Framework\Exception
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function __construct($hostname, $username, $password, $database, $port = '5432', $charset = 'UTF8') {
|
||
6 years ago
|
if (!$this->link = pg_connect('host=' . $hostname . ' port=' . $port . ' user=' . $username . ' password=' . $password . ' dbname=' . $database)) {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Error: Could not make a database link using ' . $username . '@' . $hostname);
|
||
6 years ago
|
}
|
||
|
if (!pg_ping($this->link)) {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Error: Could not connect to database ' . $database);
|
||
6 years ago
|
}
|
||
6 years ago
|
pg_query($this->link, "SET CLIENT_ENCODING TO '".$charset."'");
|
||
6 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
public function isConnected() {
|
||
|
return ($this->link) ? true : false;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
2 years ago
|
*
|
||
3 years ago
|
* @param string $sql
|
||
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) {
|
||
|
$resource = pg_query($this->link, $sql);
|
||
|
if ($resource) {
|
||
|
if (is_resource($resource)) {
|
||
|
$i = 0;
|
||
|
$data = array();
|
||
|
while ($result = pg_fetch_assoc($resource)) {
|
||
|
$data[$i] = $result;
|
||
|
$i++;
|
||
|
}
|
||
|
pg_free_result($resource);
|
||
2 years ago
|
$query = new \Phacil\Framework\Databases\Object\Result();
|
||
|
$query->setRow(isset($data[0]) ? $data[0] : array());
|
||
|
$query->setRows($data);
|
||
|
$query->setNumRows($i);
|
||
6 years ago
|
unset($data);
|
||
|
return $query;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
} else {
|
||
1 year ago
|
throw new \Phacil\Framework\Exception('Error: ' . pg_result_error($this->link) . '<br />' . $sql);
|
||
6 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @param string $value
|
||
|
* @return string
|
||
|
*/
|
||
6 years ago
|
public function escape($value) {
|
||
|
return pg_escape_string($this->link, $value);
|
||
|
}
|
||
3 years ago
|
|
||
|
/** @return int */
|
||
6 years ago
|
public function countAffected() {
|
||
|
return pg_affected_rows($this->link);
|
||
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @return mixed
|
||
|
* @throws Exception
|
||
|
*/
|
||
6 years ago
|
public function getLastId() {
|
||
|
$query = $this->query("SELECT LASTVAL() AS `id`");
|
||
|
return $query->row['id'];
|
||
|
}
|
||
3 years ago
|
|
||
|
/** @return void */
|
||
6 years ago
|
public function __destruct() {
|
||
|
pg_close($this->link);
|
||
|
}
|
||
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 = [])
|
||
|
{
|
||
|
// Verificar se há parâmetros e fazer o bind
|
||
|
if (!empty($params)) {
|
||
|
$sql = $this->replacePlaceholders($sql, array_keys($params));
|
||
|
$result = pg_query_params($this->link, $sql, array_values($params));
|
||
|
|
||
|
if ($result === false) {
|
||
|
throw new \Phacil\Framework\Exception('Error executing query: ' . pg_last_error($this->link));
|
||
|
}
|
||
|
|
||
|
// Processar resultados se for um SELECT
|
||
|
$i = 0;
|
||
|
$data = [];
|
||
|
while ($row = pg_fetch_assoc($result)) {
|
||
|
$data[$i] = $row;
|
||
|
$i++;
|
||
|
}
|
||
|
|
||
|
$resultObj = new \Phacil\Framework\Databases\Object\Result();
|
||
|
$resultObj->setNumRows($i);
|
||
|
$resultObj->setRow(isset($data[0]) ? $data[0] : []);
|
||
|
$resultObj->setRows($data);
|
||
|
|
||
|
return $resultObj;
|
||
|
} else {
|
||
|
// Se não há parâmetros, executar diretamente sem consulta preparada
|
||
|
return $this->query($sql);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Replace placeholders in the SQL query with named placeholders
|
||
|
*
|
||
|
* @param string $sql SQL query with named placeholders
|
||
|
* @param array $placeholders Array of named placeholders
|
||
|
* @return string SQL query with named placeholders
|
||
|
*/
|
||
|
private function replacePlaceholders($sql, $placeholders)
|
||
|
{
|
||
|
foreach ($placeholders as $placeholder) {
|
||
|
$sql = str_replace($placeholder, '$' . ($placeholders[$placeholder] + 1), $sql);
|
||
|
}
|
||
|
|
||
|
return $sql;
|
||
|
}
|
||
6 years ago
|
}
|