Skip to content

Commit

Permalink
outsource the query functions. close #14
Browse files Browse the repository at this point in the history
  • Loading branch information
janz93 committed Sep 16, 2015
1 parent 4856cc0 commit a3d0057
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 73 deletions.
16 changes: 16 additions & 0 deletions core/db/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Storyteller\core\db;

class Delete extends Query {

public function __construct($pdo) {
$this->PDOConntector = $pdo;
$this->stm = self::SQL_DELETE . ' ';
}

public function finalQuery() {
return parent::finalQuery($this->PDOConntector);
}

}
15 changes: 15 additions & 0 deletions core/db/Insert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Storyteller\core\db;

class Insert extends Query {

public function __construct($pdo, $data) {
$this->PDOConntector = $pdo;
$this->stm = self::SQL_INSERT . ' ';
}

public function finalQuery() {
return parent::finalQuery($this->PDOConntector);
}
}
79 changes: 71 additions & 8 deletions core/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,96 @@

use Storyteller\core\db\Select;

class Query {
class Query implements Mysql {

public $PDOConntector = null;
public $stm = '';
private $_bind = array();
private $isWhereExsists = false;


private $_PDOConntector = null;
private $_finalQuery = '';

public function __construct() {
try {
$this->_PDOConntector = new \PDO('mysql:' . DB_HOST . '=localhost;dbname=' . DB_DATABASE, DB_USER, DB_PASS);
$this->_PDOConntector->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->PDOConntector = new \PDO('mysql:' . DB_HOST . '=localhost;dbname=' . DB_DATABASE, DB_USER, DB_PASS);
$this->PDOConntector->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $e) {
echo 'Error' . $e->getMessage();
}
}

public function select() {
return new Select($this->_PDOConntector);
return new Select($this->PDOConntector);
}

public function insert(array $data) {
public function insert($table, array $data) {
$this->_finalQuery = 'INSERT ';
}

public function update(array $data) {
public function update($table, array $data) {
$this->_finalQuery = 'UPDATE ';
}

public function delete() {
$this->_finalQuery = 'DELETE ';
}

public function from($table, $columns = array()) {
$this->stm .= self::SQL_FROM . ' `' . $table->info() . '` ';
if (!empty($columns)) {
$this->columns($table, $columns);
}
return $this;
}



public function where($sql, $value = null) {
if (! $this->isWhereExsists) {
$this->stm .= self::SQL_WHERE . ' ' . $sql . ' ';
$this->isWhereExsists = true;
} else {
$this->stm .= self::SQL_AND . ' ' . $sql . ' ';
}
if (!is_null($value)) {
$this->_addBindValue($sql, $value);
}
return $this;
}

public function orWhere($sql, $value = null) {
if (!$this->isWhereExsists) {
$this->stm .= self::SQL_WHERE . ' ' . $sql . ' ';
$this->isWhereExsists = true;
} else {
$this->stm .= self::SQL_OR . ' ' . $sql . ' ';
}
if (!is_null($value)) {
$this->_addBindValue($sql, $value);
}
return $this;
}

public function finalQuery() {
$query = $this->PDOConntector->prepare($this->stm);
if (! empty($this->_bind)) {
foreach ($this->_bind as $parameter => $value) {
$query->bindValue($parameter, $value);
}
}
return $query;
}

private function _addBindValue($sql, $value) {
if (preg_match ('/:\w+/', $sql, $match)) {
$this->_bind += array (
$match [0] => $value
);
} elseif (preg_match ('/\?/', $sql, $match)) {
$this->_bind += array (
count($this->_bind) + 1 => $value
);
}
}

}
108 changes: 43 additions & 65 deletions core/db/Select.php
Original file line number Diff line number Diff line change
@@ -1,99 +1,77 @@
<?php

namespace Storyteller\core\db;

use Storyteller\core\db;

class Select {

private $_db = null;
private $_stm = '';
private $_bind = array();
private $isWhereExsists = false;
class Select extends Query {

public function __construct(\PDO $db) {
$this->_db = $db;
$this->_stm = 'SELECT ';
public function __construct($pdo) {
$this->PDOConntector = $pdo;
$this->stm = self::SQL_SELECT . ' %s ';
}

public function from($table, $columns = array()) {
if (empty($columns)) {
$this->_stm .= '* ';
} else {
$this->_stm .= implode(', ' . $table->info() . '.', $columns) . ' ';
}
$this->_stm .= 'FROM ' . $table->info() . ' ';
return $this;
}
private $_columns = array();

public function where($sql, $value = null) {
if (!$this->isWhereExsists) {
$this->_stm .= 'WHERE ' . $sql . ' ';
$this->isWhereExsists = true;
} else {
$this->_stm .= 'AND ' . $sql . ' ';
}
if (!is_null($value)) {
$this->_addBindValue($sql, $value);
}
public function columns($table, $columns) {
$this->_columns += array($table->info() => $columns);
return $this;
}

public function orWhere($sql, $value = null) {
if (!$this->isWhereExsists) {
$this->_stm .= 'WHERE ' . $sql . ' ';
$this->isWhereExsists = true;
} else {
$this->_stm .= 'OR ' . $sql . ' ';
}
if (!is_null($value)) {
$this->_addBindValue($sql, $value);
public function limit($count, $offset = 0) {
if (is_numeric($count) && $count > 0) {
$this->stm .= self::SQL_LIMIT . ' ' . $count . ' ';
if ($offset > 0) {
$this->stm .= self::SQL_OFFSET . ' ' . $offset . ' ';
}
}
return $this;
}

public function order($arg) {
$argsAmount = count($arg);
if (is_array($arg) && $argsAmount > 1) {
$this->_stm .= 'ORDER BY ';
for ($i = 0; $i < $argsAmount; $i++) {
$this->_stm .= $this->_checkOrder($arg[$i]) . ', ';
$this->stm .= self::SQL_ORDER_BY;
for ($i = 0; $i < $argsAmount; $i ++) {
$this->stm .= $this->_checkOrder($arg [$i]) . ', ';
}
$this->_stm .= ' ';
$this->stm .= ' ';
} else {
$this->_stm .= 'ORDER BY ' . $this->_checkOrder($arg) . ' ';
}
return $this;
}

public function limit($count, $offset = 0) {
if (is_numeric($count) && $count > 0) {
$this->_stm .= 'LIMIT ' . $count . ' ';
if ($offset > 0) {
$this->_stm .= ' OFFSET ' . $offset . ' ';
}
$this->stm .= self::SQL_ORDER_BY . ' ' . $this->_checkOrder($arg) . ' ';
}
return $this;
}

public function finalQuery() {
$query = $this->_db->prepare($this->_stm);
if (!empty($this->_bind)) {
foreach ($this->_bind as $parameter => $value) {
$query->bindValue($parameter, $value);
$finalColumns = '';
if (empty($this->_columns)) {
$finalColumns = self::SQL_WILDCARD;
} else {
$numColumnsTables = count($this->_columns);
$j = 1;
foreach ($this->_columns as $table => $columnsArr) {
$numColums = count($columnsArr);
for ($i = 0; $i < $numColums; $i++) {
$finalColumns .= '`' . $table . '`.`' . $columnsArr[$i] . '`';
if ($j == $numColumnsTables && $i == ($numColums -1)) {
$finalColumns .= '';
} else {
$finalColumns .= ', ';
}
}
$j++;
}
}
return $query;
}

private function _addBindValue($sql, $value) {
if (preg_match('/:\w+/', $sql, $match)) {
$this->_bind += array($match[0] => $value);
} elseif (preg_match('/\?/', $sql, $match)) {
$this->_bind += array(count($this->_bind) + 1 => $value);
}
$this->stm = sprintf($this->stm, $finalColumns);

return parent::finalQuery($this->PDOConntector);
}


private function _checkOrder($sql) {
if (!is_string($sql)) {
throw new \InvalidArgumentException('$sql argument must be a string');
}
if (preg_match('/(A|DE)SC/', $sql)) {
return $sql;
} else {
Expand Down
15 changes: 15 additions & 0 deletions core/db/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Storyteller\core\db;

class Update extends Query {

public function __construct($pdo, $data) {
$this->PDOConntector = $pdo;
$this->stm = self::SQL_UPDATE . ' ';
}

public function finalQuery() {
return parent::finalQuery($this->PDOConntector);
}
}

0 comments on commit a3d0057

Please sign in to comment.