db = Pluf::db(); if (strlen($base) > 0) { $this->Q($base, $args); } } /** * Returns the where clause. * * @return string Where clause without the WHERE */ function gen() { return implode(' AND ', $this->ands); } /** * Add a condition. * * @param string String to 'interpolate' * @param mixed String or array of parameters (array()) */ function Q($base, $args=array()) { $escaped = array(); if (!is_array($args)) { $args = array($args); } foreach ($args as $arg) { $escaped[] = $this->db->esc($arg); } $this->ands[] = vsprintf($base, $escaped); return $this; } /** * Add another SQL as a AND. * * @param Pluf_SQL Other object to add to the current. */ function SAnd($sql) { return $this->SDef($sql); } /** * Add another SQL as a OR * * @param Pluf_SQL Other object to add to the current. */ function SOr($sql) { return $this->SDef($sql, 'OR'); } /** * Add another SQL to the current * * @param Pluf_SQL Other object to add to the current. * @param string Type of addition */ function SDef($sql, $k='AND') { if (empty($this->ands)) { $this->ands = $sql->ands; } else { $othersql = $sql->gen(); $current = $this->gen(); if (strlen($othersql)) { $this->ands = array(); $this->ands[] = '('.$current.') '.$k.' ('.$othersql.')'; } } return $this; } /** * Get keywords. * * Considering a query string, explode the query string in * keywords given a defined delimiter. * * @param string Query string * @param string delimiter (' ') * @return array Array of keywords */ function keywords($string, $del=' ') { $keys = array(); $args = explode($del, $string); foreach ($args as $arg) { $arg = trim($arg); if (strlen($arg) > 0) { $keys[] = $arg; } } return $keys; } }