setTablePrefix (Database::instance ()->table_prefix ()); $this->setBody (<<init (); } function init () { } function render () { $conditions = ($this->hasWheres ()) ? ' WHERE ' . join (' AND ', $this->getWheres ()) : ''; $joins = ($this->hasJoins ()) ? join ("\n\t", $this->getJoins ()) : ''; $result = $this->getBody (); $result = str_replace (':from:', $this->getFrom (), $result); $result = str_replace (':fields:', ($this->getFields ()) ? join (", ", $this->getFields ()) : ' * ', $result); $result = str_replace (':where:', $conditions, $result); $result = str_replace (':join:', $joins, $result); $result = str_replace (':order:', ($this->hasOrder ()) ? ' ORDER BY ' . $this->getOrder () : '', $result); $result = str_replace (':limit:', $this->renderLimit (), $result); $result = str_replace (':prefix:', $this->getTablePrefix (), $result); return $result; } function __toString () { return $this->render (); } function addWhere ($where) { $this->_wheres [] = $where; } function getWheres () { return $this->_wheres; } function hasWheres () { return count ($this->_wheres) > 0; } function addJoin ($join, $position = self::POSITION_APPEND) { if ($position == self::POSITION_APPEND) { $this->_joins [] = $join; } else { array_unshift ($this->_joins, $join); } } function getJoins () { return $this->_joins; } function hasJoins () { return count ($this->_joins) > 0; } function setTablePrefix ($_table_prefix) { $this->_table_prefix = $_table_prefix; } function getTablePrefix () { return $this->_table_prefix; } function setOrder ($_order) { $this->_order = $_order; } function getOrder () { return $this->_order; } function hasOrder () { return isset ($this->_order); } function clearOrder () { unset ($this->_order); } function setLimit ($_limit) { $this->_limit = $_limit; } function getLimit () { return $this->_limit; } function hasLimit () { return isset ($this->_limit); } protected function renderLimit () { $result = ""; if ($this->hasLimit () || $this->hasOffset ()) { $result = " LIMIT "; $result .= ($this->hasOffset ()) ? $this->getOffset () : '0'; $result = "$result, " . ($this->hasLimit () ? $this->getLimit () : '0'); } return $result; } function setOffset ($_offset) { $this->_offset = $_offset; } function getOffset () { return $this->_offset; } function hasOffset () { return isset ($this->_offset); } function setBody ($_body) { $this->_body = $_body; } function getBody () { return $this->_body; } function addField ($_field) { $this->_fields[] = $_field; } function clearFields () { $this->_fields = array (); } function getFields () { return $this->_fields; } function setFrom ($_from) { $this->_from = $_from; } function getFrom () { return $this->_from; } }