Created
June 14, 2012 13:54
-
-
Save igorw/2930445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class QueryBuilder implements IteratorAggregate | |
{ | |
private $conn; | |
private $parts = array(); | |
public function __construct(Connection $conn) | |
{ | |
$this->conn = $conn; | |
} | |
public function select($fields) | |
{ | |
$this->parts['select'] = $fields; | |
} | |
public function from($table) | |
{ | |
$this->parts['from'] = $table; | |
} | |
public function where($where) | |
{ | |
$this->parts['where'] = $where; | |
} | |
public function andWhere($where) | |
{ | |
$this->parts['where'] .= ' AND '.$where; | |
} | |
public function orderBy($field, $dir = 'ASC') | |
{ | |
$this->parts['orderBy'] = "$field $dir"; | |
} | |
public function getSql() | |
{ | |
return $this->parts['select'].' '. | |
$this->parts['from'].' '. | |
$this->parts['where'].' '. | |
$this->parts['orderBy']; | |
} | |
public function execute() | |
{ | |
$sql = $this->getSql(); | |
return $this->conn->execute($sql); | |
} | |
public function getIterator() | |
{ | |
return new ArrayIterator($this->execute()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment