Skip to content

Instantly share code, notes, and snippets.

@csalajan
Created July 23, 2015 02:50
Show Gist options
  • Save csalajan/be9e399fffc745e77cf3 to your computer and use it in GitHub Desktop.
Save csalajan/be9e399fffc745e77cf3 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: Craig
* Date: 10/17/2014
* Time: 8:36 PM
*/
class DB {
private $_con;
private $_method;
private $_table;
private $_query;
private $_fields;
private $_orderBy;
private $_result;
private $_data;
private $_limit = 0;
private $_where = [];
private $_params = [];
public function __construct($connection) {
$this->_con = $connection;
return true;
}
public function get($table) {
$this->_method = 'select';
$this->_table = $table;
$this->buildQuery();
return $this->query();
}
public function create($table, array $data) {
$this->_method = 'insert';
$this->_table = $table;
$this->_data = $data;
$this->buildQuery();
return $this->query();
}
public function update($table, array $data, array $key) {
$this->_method = 'update';
$this->_table = $table;
$this->_data = $data;
$this->where($key);
$this->buildQuery();
return $this->query();
}
public function destroy($table, array $key) {
$this->_method = 'delete';
$this->_table = $table;
$this->where($key);
$this->buildQuery();
return $this->query();
}
public function fields($fields) {
$this->_fields = $fields;
return $this;
}
public function where($key, $value = null) {
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->_where[$k] = $v;
}
} else {
$this->_where[$key] = $value;
}
return $this;
}
public function orderBy($key, $order) {
$this->_orderBy = $key . ' ' . $order;
return $this;
}
public function limit($num) {
$this->_limit = $num;
return $this;
}
public function group() {
}
public function like() {
}
public function join() {}
private function buildQuery() {
switch ($this->_method) {
case 'select':
$this->buildSelect();
break;
case 'insert':
$this->buildInsert();
break;
case 'update':
$this->buildUpdate();
break;
case 'delete':
$this->buildDelete();
break;
}
}
private function buildSelect() {
$query = 'SELECT ';
$query .= ($this->_fields === null) ? '*' : $this->_fields;
$query .= ' FROM ' . $this->_table;
$query .= $this->buildWhere();
$query .= $this->buildOrder();
$query .= $this->buildLimit();
$this->_query = $query;
}
private function buildInsert() {
$query = 'INSERT INTO ' . $this->_table;
$query .= ' (' . implode(', ', array_keys($this->_data)) . ')';
$query .= ' values (';
foreach ($this->_data as $key => $value) {
$query .= ':' . $key;
$this->_params[':' . $key] = $value;
end($this->_data);
if ($key !== key($this->_data)) {
$query .= ', ';
}
}
$query .= ')';
$this->_query = $query;
}
private function buildUpdate() {
$query = 'UPDATE ' . $this->_table . ' SET ';
foreach ($this->_data as $key => $value) {
$query .= $key . ' = :' . $key;
$this->_params[':'.$key] = $value;
end($this->_data);
if ($key !== key($this->_data)) {
$query .= ', ';
}
}
$query .= $this->buildWhere();
$this->_query = $query;
}
private function buildDelete() {
$query = 'DELETE FROM ' . $this->_table;
$query .= $this->buildWhere();
$this->_query = $query;
}
private function buildWhere() {
$query = '';
if (count($this->_where) > 0) {
$query = ' WHERE ';
foreach ($this->_where as $key => $value) {
$query .= $key . ' = :' . $key;
$this->_params[':' . $key] = $value;
end($this->_where);
if ($key !== key($this->_where)) {
$query .= ' AND ';
}
}
}
return $query;
}
private function buildOrder() {
if ($this->_orderBy !== null) {
return ' ORDER BY ' . $this->_orderBy;
}
}
private function buildLimit() {
if ($this->_limit > 0) {
if ($this->_con->getAttribute("PDO::ATTR_ DRIVER_NAME") === 'mysql') {
return ' LIMIT ' . $this->_limit;
} else {
return ' FETCH FIRST ' . $this->_limit . ' ROWS ONLY';
}
}
return '';
}
private function query() {
$this->_result = $this->_con->prepare($this->_query);
$id = $this->_result->execute($this->_params);
if ($this->_method === 'select') {
return $this->_result->fetchAll();
} else {
return $id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment