Skip to content

Instantly share code, notes, and snippets.

@Dziuperman
Last active December 7, 2019 12:30
Show Gist options
  • Select an option

  • Save Dziuperman/d7255f1a81224c39184a2f4d537eb4b6 to your computer and use it in GitHub Desktop.

Select an option

Save Dziuperman/d7255f1a81224c39184a2f4d537eb4b6 to your computer and use it in GitHub Desktop.
Query builder #php #OOP #MVC
<?php
namespace Engine\Core\Database;
use \PDO;
use Engine\Core\Config\Config;
class Connection
{
private $link;
/**
* Connection constructor.
*/
public function __construct()
{
$this->connect();
}
private function connect()
{
$config = Config::file('database');
$dsn = 'mysql:host='.$config['host'].';dbname='.$config['db_name'].';charset='.$config['charset'];
$this->link = new PDO($dsn, $config['username'], $config['password']);
return $this;
}
/**
* @param $sql
* @param array $values
* @return mixed
*/
public function execute($sql, $values = [])
{
$sth = $this->link->prepare($sql);
return $sth->execute($values);
}
/**
* @param $sql
* @param array $values
* @return array
*/
public function query($sql, $values = [])
{
$sth = $this->link->prepare($sql);
$sth->execute($values);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
if ($result === false) {
return [];
}
return $result;
}
}
<?php
namespace Engine\Core\Database;
class QueryBuilder
{
/**
* @var array
*/
protected $sql = [];
/**
* @var array
*/
public $values = [];
/**
* @param string $fields
* @return $this
*/
public function select($fields = '*')
{
$this->reset();
$this->sql['select'] = "SELECT {$fields} ";
return $this;
}
/**
* @param $table
* @return $this
*/
public function from($table)
{
$this->sql['from'] = "FROM {$table} ";
return $this;
}
/**
* @param string $column
* @param string $value
* @param string $operator
* @return $this
*/
public function where($column, $value, $operator = '=')
{
$this->sql['where'][] = "{$column} {$operator} ?";
$this->values[] = $value;
return $this;
}
/**
* @param $field
* @param $order
* @return $this
*/
public function orderBy($field, $order)
{
$this->sql['order_by'] = "ORDER BY {$field} {$order}";
return $this;
}
/**
* @param $number
* @return $this
*/
public function limit($number)
{
$this->sql['limit'] = " LIMIT {$number}";
return $this;
}
/**
* @param $table
* @return $this
*/
public function update($table)
{
$this->reset();
$this->sql['update'] = "UPDATE {$table} ";
return $this;
}
/**
* @param $table
* @return $this
*/
public function insert($table)
{
$this->reset();
$this->sql['insert'] = "INSERT INTO {$table} ";
return $this;
}
/**
* @param array $data
* @return $this
*/
public function set($data = [])
{
$this->sql['set'] .= "SET ";
if(!empty($data)) {
foreach ($data as $key => $value) {
$this->sql['set'] .= "{$key} = ?";
if (next($data)) {
$this->sql['set'] .= ", ";
}
$this->values[] = $value;
}
}
return $this;
}
/**
* @return string
*/
public function sql()
{
$sql = '';
if(!empty($this->sql)) {
foreach ($this->sql as $key => $value) {
if ($key == 'where') {
$sql .= ' WHERE ';
foreach ($value as $where) {
$sql .= $where;
if (count($value) > 1 and next($value)) {
$sql .= ' AND ';
}
}
} else {
$sql .= $value;
}
}
}
return $sql;
}
/**
* Reset Builder
*/
public function reset()
{
$this->sql = [];
$this->values = [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment