Skip to content

Instantly share code, notes, and snippets.

@matheuseduardo
Created August 8, 2018 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matheuseduardo/ff04c2651c0c0a53e2eb2d35688a8427 to your computer and use it in GitHub Desktop.
Save matheuseduardo/ff04c2651c0c0a53e2eb2d35688a8427 to your computer and use it in GitHub Desktop.
classe simples de DB
<?php
class DB {
/**
* @var DB
*/
private static $instancia = NULL;
/**
* @var Mysql link identifier
*/
private $dbLink = NULL;
/**
* @var string
*/
private $user = '';
/**
* @var string
*/
private $password = '';
/**
* @var string
*/
private $dbName = '';
/**
* @var string
*/
private $host = '';
/**
* @var array
*/
private $lastResult = NULL;
/**
* @var string
*/
private $lastError = '';
/**
* @var int
*/
private $numRows = 0;
/**
* @var int
*/
private $affectedRows = 0;
/**
* @var int
*/
private $lastInsertId = 0;
/**
* @var int
*/
private $transacoes = 0;
/**
* @var bool
*/
private $forceRollback = false;
/**
* @var int
*/
private $timeInicio = 0;
/*
* @var int
*/
private $timeFim = 0;
/**
* @param string $host
* @param string $user
* @param string $password
* @param string $dbName
*/
public function __construct($host, $user, $password, $dbName) {
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->dbName = $dbName;
try {
$this->dbLink = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->dbName, $this->user, $this->password);
}
catch (PDOException $e) {
throw new Exception("Não foi possível conectar ao banco de dados. [" . $e->getCode() . "]");
}
$this->query('SET NAMES utf8');
self::$instancia = $this;
}
/**
* @param string $query
* @param array $array
* @param bool $throwError
* @return int
*/
public function query($query, $array = array(), $throwError = true) {
$this->flush();
$this->timeInicio = microtime(true);
$statement = $this->dbLink->prepare($query);
//$this->result = $statement->execute($array);
if (!$statement || !$statement->execute($array)) {
$this->flush();
$this->error($statement->errorInfo(), $throwError);
}
if (preg_match('#^ \s* (insert|delete|update|replace) \s #ix', $query)) {
$this->affectedRows = $statement->rowCount();
if (preg_match("/^\\s*(insert|replace) /i", $query)) {
$this->lastInsertId = $this->dbLink->lastInsertId();
}
$this->timeFimo = microtime();
return $this->affectedRows;
} else {
$this->lastResult = $statement->fetchAll(PDO::FETCH_ASSOC);
$this->numRows = count($this->lastResult);
$this->timeFim = microtime(true);
return $this->numRows;
}
}
/**
* @return bool
*/
public function beginTransaction() {
if ($this->transacoes == 0) {
$this->transacoes = $this->transacoes + 1;
return $this->dbLink->beginTransaction();
}
$this->transacoes = $this->transacoes + 1;
return false;
}
/**
* @return bool
*/
public function commit() {
if ($this->transacoes == 1) {
if ($this->forceRollback) {
return rollback();
}
$this->transacoes = 0;
$this->forceRollback = false;
return $this->dbLink->commit();
}
$this->transacoes = $this->transacoes - 1;
return false;
}
/**
* @return bool
*/
public function rollBack() {
$this->forceRollback = true;
if ($this->transacoes == 1) {
$this->transacoes = 0;
$this->forceRollback = false;
return $this->dbLink->rollBack();
}
$this->transacoes = $this->transacoes - 1;
return false;
}
/**
*
*/
private function flush() {
$this->lastResult = NULL;
$this->lastError = NULL;
$this->numRows = 0;
$this->affectedRows = 0;
$this->lastInsertId = 0;
$this->timeInicio = 0;
$this->timeFim = 0;
$this->lastError = '';
}
/**
* @param array $errorInfo
* @param bool $throwError
*/
private function error($errorInfo, $throwError = true) {
$this->lastError = $errorInfo[1] . ": " . $errorInfo[2];
if ($throwError)
throw new Exception($this->lastError);
}
/**
* @return DB
*/
public static function getInstancia() {
return self::$instancia;
}
/**
* @return int
*/
public function getAffectedRows() {
return $this->affectedRows;
}
/**
* @return Mysql
*/
public function getDbLink() {
return $this->dbLink;
}
/**
* @return string
*/
public function getDbName() {
return $this->dbName;
}
/**
* @return string
*/
public function getHost() {
return $this->host;
}
/**
* @return string
*/
public function getLastError() {
return $this->lastError;
}
/**
* @return int
*/
public function getLastInsertId() {
return $this->lastInsertId;
}
/**
* @return array
*/
public function getLastResult() {
return $this->lastResult;
}
/**
* @return int
*/
public function getNumRows() {
return $this->numRows;
}
/**
* @return string
*/
public function getPassword() {
return $this->password;
}
/**
* @return string
*/
public function getUser() {
return $this->user;
}
/**
* @return int
*/
public function getQueryExecTime() {
return $this->timeFim - $this->timeInicio;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment