Skip to content

Instantly share code, notes, and snippets.

@davidchc
Last active December 11, 2015 06:38
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 davidchc/4560398 to your computer and use it in GitHub Desktop.
Save davidchc/4560398 to your computer and use it in GitHub Desktop.
<?php
/***********************************************************
* Interface que defini ações de um Banco de dados
* *********************************************************
*/
Interface DataBase{
public function open();
public function execute($query);
public function close();
}
/*************************************************************
* Banco de Dados MySQL, implementando a interface DataBase
* ************************************************************
*/
class MySQLDataBase implements DataBase{
/**
* Define atributos da conexão
*/
private $host;
private $user;
private $pass;
private $dbname;
private $conn;
/**
* Passa os parametros para construtos
*/
public function __construct($host, $user, $pass, $dbname){
$this->host = $host;
$this->user - $user;
$this->pass = $pass;
$this->dbname = $dbname;
$this->open();
}
/**
* Abre a conexao
*/
public function open(){
$dsn = sprintf('mysql:host=%s;dbname=%s', $this->host, $this->dbname);
$this->conn = new PDO($dsn, $this->user, $this->pass);
}
/**
* Fecha a conexão
*/
public function close(){
$this->conn = null;
}
/**
* Executa o SQL
*/
public function execute($strSQL){
$stmt = $this->conn->prepare($strSQL);
$stmt->execute();
return $stmt;
}
}
/*******************************************************************
* Classe responsável por realizar a consulta na tabela
* no banco de dados
* *****************************************************************
*/
class Select{
/**
* Propriedades da seleção de dados em uma tabela
*/
private $conn;
private $table;
private $fields;
private $where;
private $order;
private $limit;
/**
* Passa conexãao do banco de dados
*/
public function __construct(DataBase $conn){
$this->conn = $conn;
}
/**
* Define a Nome da Tabela
*/
public function table($table){
$this->table = $table;
return $this;
}
/**
* Defina o nome dos campos
*/
public function fields($fields){
$this->fields = $fields;
return $this;
}
/**
* Define o filtro de consulta Where
*/
public function where($where){
$this->where = $where;
return $this;
}
/**
* Define o tipo de order
*/
public function order($order){
$this->order = $order;
return $this;
}
/**
* Define a quantidade de registro a retornar
*/
public function limit($limit){
$this->limit;
return $this;
}
/**
* Monta o SQL para consulta
*/
private function sql(){
$strSQL = "SELECT ";
$strSQL .= ($this->field) ? $this->field : ' * ';
$strSQL .= $this->table;
$strSQL .= ($this->where) ? ' WHERE '.$this->where : '';
$strSQL .= ($this->order) ? ' ORDER BY '.$this->order : '';
$strSQL .= ($this->limit) ? ' LIMIT '.$this->limit : '';
return $strSQL;
}
/**
* Executa o SQL montado
*/
public function execute(){
$strSQL = $this->sql();
return $this->conn->execute($strSQL);
}
/**
* Executa o select em uma linha, passando os parâmetros
*/
public function query($field = '*', $where=null, $order=null, $limit=null){
$this->table($table);
$this->field($field);
$this->where($where);
$this->order($order);
$this->limit($limit);
return $this->execute();
}
}
/***********************************************************
* Exemplo de utilização das classes
* *********************************************************
*/
/**
* Instancia a classe responsável pela conexxão com banco de dados
* MySQL
*/
$db = new MySQLDataBase('localhost', 'root', 'user', 'pass');
/**
* Instancia a classe Select, passando o banco de dados
* que irá utilizar para consulta
*/
$select = new Select($db);
/**
* Monta a consulta o SQL usando fluent interface
*/
$query = $select->table('produtos')
->field('*')
->where("status = 1")
->order('idProduto DESC')
->limit(10)
->execute();
/**
* Monta a consulta usando apenas um parametro
*/
$query2 = $select->query('produto', '*', 'status=1', 'idProduto DESC', 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment