Skip to content

Instantly share code, notes, and snippets.

@igorw
Created June 14, 2012 13:54
Show Gist options
  • Save igorw/2930445 to your computer and use it in GitHub Desktop.
Save igorw/2930445 to your computer and use it in GitHub Desktop.
<?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