Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Last active November 10, 2016 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilcorrea/4e264c60b449ce7fcc11060828d71fe4 to your computer and use it in GitHub Desktop.
Save wilcorrea/4e264c60b449ce7fcc11060828d71fe4 to your computer and use it in GitHub Desktop.
CREATE TABLE `clientes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
<?php
require_once 'config/config.php';
/**
* Class dbConnection
*
* @method $this table(string $table)
* @method $this collection(string $collection)
*
* @method $this fields(array|string $fields)
*
* @method $this where(array $where)
* @method $this group(array $group)
* @method $this order(array $order)
*/
class dbConnection extends Config
{
/**
* @var string
*/
private $host = 'localhost';
/**
* @var string
*/
private $user = 'root';
/**
* @var string
*/
private $password = 'root';
/**
* @var string
*/
private $database = 'db';
/**
* @var PDO
*/
private $pdo = null;
/**
* @var array
*/
private $clausules = [];
/**
* @var array
*/
private $errors = [];
/**
* @param $name
* @param $arguments
* @return $this
*/
public function __call($name, $arguments)
{
$this->clausules[$name] = $arguments[0];
return $this;
}
/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* @return PDO
*/
private function connect()
{
// singleton to use one connection to all queries
// useful to transactions
if (!$this->pdo) {
$this->pdo = new PDO("mysql:host={$this->host};dbname={$this->database}", $this->user, $this->password);
}
return $this->pdo;
}
/**
* @param $sql
* @return PDOStatement
*/
private function prepare($sql)
{
return $this->connect()->prepare($sql);
}
/**
* @param $sql
* @param array $parameters
* @return mixed
*/
private function execute($sql, array $parameters = [])
{
$statement = $this->prepare($sql);
if ($statement->execute($parameters)) {
return $statement->rowCount();
}
$this->errors = $statement->errorInfo();
return null;
}
/**
* @param $sql
* @param array $parameters
* @param null $fetch
* @return array
*/
private function fetch($sql, array $parameters = [], $fetch = null)
{
$statement = $this->prepare($sql);
if ($statement and $statement->execute($parameters)) {
$fetch = $fetch ? $fetch : PDO::FETCH_ASSOC;
return $statement->fetchAll($fetch);
}
$this->errors = $statement->errorInfo();
return null;
}
/**
* @param array $parameters
* @param bool $debug
* @return null|string
*/
public function create($parameters, $debug = false)
{
$collection = isset($this->clausules['table']) ? $this->clausules['table'] : (isset($this->clausules['collection']) ? $this->clausules['collection'] : '');
$fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '/*FIELDS*/';
$fields = is_array($fields) ? $fields : [$fields];
$parameters = is_array($parameters) ? $parameters : [$parameters];
$commands = [];
$commands[] = 'INSERT INTO';
$commands[] = $collection;
$commands[] = '(' . (implode(', ', $fields)) . ')';
$commands[] = 'VALUES';
$commands[] = '(' . (str_repeat('?,', count($fields) - 2) . '?') . ')';
$sql = implode(' ', $commands);
if ($debug) {
var_dump($sql);
}
if (!$this->execute($sql, $parameters)) {
return null;
}
return $this->connect()->lastInsertId();
}
/**
* @param array|null $parameters
* @param bool $debug
* @return array
*/
public function read($parameters = null, $debug = false)
{
$collection = isset($this->clausules['table']) ? $this->clausules['table'] : (isset($this->clausules['collection']) ? $this->clausules['collection'] : '');
$fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '*';
$where = isset($this->clausules['where']) ? $this->clausules['where'] : null;
$group = isset($this->clausules['group']) ? $this->clausules['group'] : null;
$order = isset($this->clausules['order']) ? $this->clausules['order'] : null;
$fields = is_array($fields) ? $fields : [$fields];
$parameters = is_array($parameters) ? $parameters : [$parameters];
$commands = [];
$commands[] = 'SELECT';
$commands[] = is_array($fields) ? implode(', ', $fields) : $fields;
$commands[] = 'FROM ' . $collection;
if ($where) {
$where = is_array($where) ? $where : [$where];
$commands[] = 'WHERE (' . implode(') AND (', $where) . ')';
}
if ($group) {
$group = is_array($group) ? $group : [$group];
$commands[] = 'GROUP ' . implode(', ', $group);
}
if ($order) {
$order = is_array($order) ? $order : [$order];
$commands[] = 'ORDER ' . implode(', ', $order);
}
$sql = implode(' ', $commands);
if ($debug) {
var_dump($sql);
}
return $this->fetch($sql, $parameters);
}
/**
* @param array $set
* @param array $parameters
* @param bool $debug
* @return null|string
*/
public function update($set, $parameters = null, $debug = false)
{
$collection = isset($this->clausules['table']) ? $this->clausules['table'] : (isset($this->clausules['collection']) ? $this->clausules['collection'] : '');
$where = isset($this->clausules['where']) ? $this->clausules['where'] : null;
$fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '/*FIELDS*/';
$fields = is_array($fields) ? $fields : [$fields];
$set = is_array($set) ? $set : [$set];
$parameters = is_array($parameters) ? $parameters : [$parameters];
$commands = [];
$commands[] = 'UPDATE';
$commands[] = $collection;
$commands[] = 'SET';
$commands[] = is_array($fields) ? implode(', ', $fields) : $fields;
if ($where) {
$where = is_array($where) ? $where : [$where];
$commands[] = 'WHERE (' . implode(') AND (', $where) . ')';
}
$sql = implode(' ', $commands);
if ($debug) {
var_dump($sql);
}
if ($parameters) {
$parameters = array_merge($set, $parameters);
}
return $this->execute($sql, $parameters);
}
/**
* @param array $parameters
* @param bool $debug
* @return mixed
*/
public function destroy($parameters, $debug = false)
{
$collection = isset($this->clausules['table']) ? $this->clausules['table'] : (isset($this->clausules['collection']) ? $this->clausules['collection'] : '');
$where = isset($this->clausules['where']) ? $this->clausules['where'] : null;
$parameters = is_array($parameters) ? $parameters : [$parameters];
$commands = [];
$commands[] = 'DELETE FROM';
$commands[] = $collection;
if ($where) {
$where = is_array($where) ? $where : [$where];
$commands[] = 'WHERE (' . implode(') AND (', $where) . ')';
}
$sql = implode(' ', $commands);
if ($debug) {
var_dump($sql);
}
return $this->execute($sql, $parameters);
}
}
<?php
try {
$connection = new dbConnection();
$connection->table('clientes');
$create = $connection
//->table('clientes') dont need repeat
->fields('nome') // or ->fields(['nome'])
->create(['William Correa'], true); //debugging
$read = $connection
//->table('clientes') dont need repeat
->where(['nome = ?']) // or ->where('nome = ?')
->fields(['id', 'nome']) // or ->fields('id, nome')
->read(['William Correa'], true); //debugging
$update = $connection
//->table('clientes') dont need repeat
->where(['id = ?']) // or ->where('id = ?')
->fields(['nome = ?']) // or ->fields('nome = ?')
->update(['William'], $read[count($read) - 1]['id'], true); //debugging
$destroy = $connection
//->table('clientes') dont need repeat
->where('id = ?') // or ->where(['id = ?'])
->destroy([$read[count($read) - 1]['id']], true); //debugging
var_dump([
'create' => $create,
'read' => count($read),
'update' => $update,
'destroy' => $destroy,
]);
var_dump($connection->getErrors());
} catch (Exception $e) {
var_dump($e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment