Skip to content

Commit

Permalink
outsource query functions. clodes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
janz93 committed Oct 13, 2015
1 parent 6c89682 commit ba8dc16
Showing 1 changed file with 5 additions and 119 deletions.
124 changes: 5 additions & 119 deletions app/model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Storyteller\app\model;
use Storyteller\core\db\Query;


class Table extends Query {
class Table {

protected $_name;

public function getAdapter() {
return new Query();
}

public function find() {
$tableInfo = $this->_getTableMeta();
var_dump($tableInfo);
Expand Down Expand Up @@ -59,130 +62,13 @@ public function fetchAll($Query) {
return null;
}
}

public function delete($table) {
$query = null;
$sql = 'DELETE FROM `' . $table . '`';
if ($this->_hasWhereCondition()) {
$query = $this->_PdoConntector->prepare($sql . 'WHERE ' . $this->_whereCondition['sql']);
$query->bindParam($this->_whereCondition['value'][0], $this->_whereCondition['value'][1]);
} else {
$query = $this->_PdoConntector->prepare($sql);
}
$query->execute();
return $query->rowCount();
}

public function update($table, $data) {
$pair = array();
$sql = 'UPDATE `' . $table .'` SET ';
foreach ($data as $column => $value) {
$pair[] = '`' . $column . '` = :' . $column;
}

$sql .= implode(', ', $pair);
$query = $this->_PdoConntector->prepare($sql . ' WHERE ' . $this->_whereCondition['sql']);
foreach ($data as $column => $value) {
$query->bindValue($column, $value);
}
$query->bindValue($this->_whereCondition['value'][0], $this->_whereCondition['value'][1]);
$query->execute();
return $query->rowCount();
}

public function insert($table, $data) {
$columns = array();
$values = array();
$sql = 'INSERT INTO `' . $table .'` (';
foreach (array_keys($data) as $column) {
$columns[] = '`' . $column . '`';
$values[] = ':' . $column;
}

$sql .= implode(', ', $columns). ') VALUES(' . implode(', ', $values) . ')';
$query = $this->_PdoConntector->prepare($sql);

foreach ($data as $column => $value) {
$query->bindColumn($column, $value);
}
if ($query->execute($data)) {
$this->setWhereCondition('`id` = ?', $this->_PdoConntector->lastInsertId());
if ($this->_hasWhereCondition()) {
$query = $this->_prepareSql($table);
$query->execute(array($this->_whereCondition['value']));
} else {
$query = $this->_prepareSql($table);
$query->execute();
}
$row = $query->fetch(\PDO::FETCH_OBJ);
if ($row){
return $row;
} else {
return null;
}
$this->_unsetParams();
} else {
return false;
}
}

public function setJoin($table, $on) {
$this->_join = array('table' => $table, 'on' => $on);
}

private function _hasJoin() {
if (!empty($this->_join)) {
return true;
} else {
return false;
}
}

private function _createJoin($sql) {
return ' JOIN `' . $this->_join['table'] . '` ON ' . $this->_join['on'];
}

/**
*
* @param string $sql
* @param mixed $value
*/
public function setWhereCondition($sql, $valueArr) {
$this->_whereCondition = array('sql' => $sql, 'value' => $valueArr);
}

public function info() {
return $this->_name;
}

/**
*
* @return boolean
*/
private function _hasWhereCondition() {
if (!empty($this->_whereCondition)) {
return true;
} else {
return false;
}
}

private function _createWhereCondition($sql) {
return ' WHERE ' . $this->_whereCondition['sql'];
}

private function _prepareSql($table, $select) {
$sql = $select . ' FROM `' . $table . '`';
if ($this->_hasJoin()) {
$sql .= $this->_createJoin($sql);
}
if ($this->_hasWhereCondition()) {
$sql .= $this->_createWhereCondition($sql);
}

return $this->_PdoConntector->prepare($sql);
}

private function _getTableMeta() {
return array('name' => $this->_name, 'primary' => $this->_primary);
}
Expand Down

0 comments on commit ba8dc16

Please sign in to comment.