_op = $op; $this->_lparen = $lparen; $this->_rparen = $rparen; $this->assign($expr); } function value () { return $this->_orig; } function count () { $tally = 0; foreach ($this->_expr as $operand): if (is_a($operand, "OOM_Expr")): $tally = $tally + $operand->count(); else: $tally = $tally + count($operand); endif; endforeach; return $tally; } function assign ($expr) { $this->_orig = $expr; if (is_array($expr)): $this->_expr = $expr; elseif (is_a($expr, "OOM_Expr") or is_numeric($expr) or is_string($expr)): $this->_expr = array($expr); else: $this->_expr = array(); endif; } function merge ($expr) { if (is_array($expr)): $this->_expr = array_merge($this->_expr, $expr); elseif (is_a($expr, "OOM_Expr") or is_numeric($expr) or is_string($expr)): array_push($this->_expr, $expr); endif; } function evaluate ($operand) { $ret = NULL; if (is_numeric($operand) or is_string($operand)): $ret = $operand; elseif (is_a($operand, "OOM_Expr") and $operand->count() > 0): $ret = $operand->to_string(); endif; return $ret; } function to_string () { $ret = NULL; foreach ($this->_expr as $operand): $o = $this->evaluate($operand); if (!is_null($o)): $ret = (is_null($ret) ? '' : $ret.' '.$this->_op.' ') .$this->_lparen.$o.$this->_rparen; endif; endforeach; return $ret; } } class OOM_And extends OOM_Expr { function OOM_And ($expr = NULL) { OOM_Expr::OOM_Expr($expr, 'AND'); } } class OOM_Or extends OOM_Expr { function OOM_Or ($expr = NULL) { OOM_Expr::OOM_Expr($expr, 'OR'); } } class OOM_Literal extends OOM_Expr { function OOM_Literal ($expr = NULL) { OOM_Expr::OOM_Expr($expr, NULL); } function assign ($expr) { $this->_orig = $expr; if (is_null($expr)): $this->_expr = array("NULL"); elseif (is_bool($expr)): $this->_expr = array(($expr) ? 1 : 0); elseif (is_numeric($expr)): $this->_expr = array($expr); elseif (is_string($expr)): $this->_expr = array("'".mysql_escape_string($expr)."'"); elseif (is_a($expr, "OOM_Expr")): $this->_expr = array($expr); else: $this->_expr = array(); endif; } } class OOM_List extends OOM_Expr { function OOM_List ($expr, $lparen = '', $rparen = '') { OOM_Expr::OOM_Expr($expr, ',', $lparen, $rparen); } } class OOM_Literal_Row extends OOM_Expr { var $_raw = NULL; function OOM_Literal_Row ($expr, $op, $glue, $lparen = '(', $rparen = ')') { $arr = NULL; if (is_a($expr, "OOM_Literal_Row")): $this->_raw = $expr->raw(); elseif (is_array($expr)): $this->_raw = $expr; endif; if (is_array($this->_raw)): foreach ($this->_raw as $key => $value): $lit =& new OOM_Literal($value); $arr[] = $key . $glue . $lit->to_string(); unset($lit); endforeach; endif; OOM_Expr::OOM_Expr($arr, $op, $lparen, $rparen); } function raw () { return $this->_raw; } } class OOM_Pick_Out_Row extends OOM_Literal_Row { function OOM_Pick_Out_Row ($expr) { OOM_Literal_Row::OOM_Literal_Row($expr, 'AND', '<=>'); } } class OOM_Set_Row extends OOM_Literal_Row { function OOM_Set_Row ($expr) { OOM_Literal_Row::OOM_Literal_Row($expr, ',', '=', '', ''); } } class OOM_Condition_Clause extends OOM_And { var $_word; function OOM_Condition_Clause ($word, $expr = NULL) { $this->_word = $word; OOM_And::OOM_And($expr); } function to_clause () { if ($this->count() > 0): $ret = $this->_word.' '.$this->to_string(); else: $ret = ''; endif; return $ret; } } class OOM_Where extends OOM_Condition_Clause { function OOM_Where ($expr = NULL) { OOM_Condition_Clause::OOM_Condition_Clause("WHERE", $expr); } } class OOM_Having extends OOM_Condition_Clause { function OOM_Having ($expr = NULL) { OOM_Condition_Clause::OOM_Condition_Clause("HAVING", $expr); } } ?>