deb =& Debugger::plug_in(__FILE__, $this->_ugd); $this->_query = $query; if (method_exists($db, "issue") and method_exists($db, "result") and method_exists($db, "is_empty") and method_exists($db, "error")): $this->_db =& $db; else: $this->_error[TRUE] = -1; $this->_error[FALSE] = "Invalid class passed to constructor!"; endif; } /* OOM_Query::OOM_Query () */ function __destructor () { Debugger::unplug($this->deb, $this->_ugd); } function query () { return $this->_query; } function error ($numeric = FALSE) { return (is_bool($numeric) ? $this->_error[$numeric] : $this->_error); } function is_empty () { return $this->_db->is_empty(); } function issue () { $this->deb->log("MySQL << " . $this->_query, __FILE__, __FUNCTION__, __LINE__); $ret = $this->_db->issue($this->_query); if (is_null($ret)): $this->_error = $this->_db->error(NULL); $this->deb->log("MySQL >> ERROR: " . $this->_db->error(), __FILE__, __FUNCTION__, __LINE__); else: $this->deb->log("MySQL >> query successful, $ret rows affected", __FILE__, __FUNCTION__, __LINE__); endif; return $ret; } /* OOM_Query::issue () */ function result ($row = NULL, $key = NULL) { $table = $this->guarantee(); if (is_null($row) and is_null($key)): $ret = $table; elseif (is_null($key)): $ret = $table[$row]; elseif (is_null($row)): if (is_array($table)): foreach ($table as $index => $trow): $ret[$index] = $trow[$key]; endforeach; endif; else: $ret = $table[$row][$key]; endif; return $ret; } function guarantee () { return $this->_db->result(); } function count () { return count($this->guarantee()); } } /* OOM_Query */ class OOM_Select extends OOM_Query { var $_cols, $_table, $_rest; var $_where = NULL; var $_innocent, $_cache; function OOM_Select (&$db, $table, $cols = '*', $where = NULL, $rest = NULL, $init = NULL) { $this->_cols = (is_array($cols) ? implode(",", $cols) : $cols); $this->_table = (is_array($table) ? implode(",", $table) : $table); $this->_where =& new OOM_Where($where); $this->_rest = $rest; OOM_Query::OOM_Query($db, "SELECT ".$this->cols()." FROM ".$this->table()." ".$this->conditions()." ".$this->rest()); $this->_cache = $init; $this->_innocent = is_null($this->_cache); } /* OOM_Select::OOM_Select () */ function issue () { $this->_innocent = FALSE; $ret = OOM_Query::issue(); $this->_cache = OOM_Query::guarantee(); return $ret; } function is_empty () { $this->guarantee(); return OOM_Query::is_empty(); } function guarantee () { if ($this->_innocent) $this->issue(); return $this->_cache; } function cols () { return (is_array($this->_cols) ? implode(", ", $this->_cols) : $this->_cols); } function table () { return $this->_table; } function rest () { return $this->_rest; } function conditions ($with = NULL) { $qc = $this->_where; $qc->merge($with); return $qc->to_clause(); } function where ($conditions = NULL, $rest = NULL) { $qc = $this->_where; $qc->merge($conditions); return new OOM_Select($this->_db, $this->_table, $this->_cols, $qc, (is_null($rest) ? $this->_rest : $rest)); } } /* OOM_Select */ class OOM_Update extends OOM_Query { var $_table, $_set, $_where, $_rest, $_fore; # OOM_Update: Construct an OOM Update query # $db: A class front-end for an SQL database. It needs methods issue() and result() # $table: The name of the table (this can include joined tables) to update # $set: An associative array from field names to objects of class OOM_Expr, specifying the value for each field # $where: Conditions to pick out the row(s) on which update is performed. Used to construct an object of class OOM_Where. # $rest: Any flags for the tail end of the command. # $fore: Any flags for the front of the command (LOW_PRIORITY, IGNORE) function OOM_Update (&$db, $table, $set, $where = NULL, $rest = NULL, $fore = NULL) { $this->_table = (is_array($table) ? implode(",", $table) : $table); $this->_set =& new OOM_Set_Row($set); $this->_where =& new OOM_Where($where); $this->_rest = $rest; $this->_fore = $fore; OOM_Query::OOM_Query($db, "UPDATE ".$this->_fore.$this->_table." SET ".$this->_set->to_string()." ".$this->_where->to_clause()." ".$this->_rest); } } class OOM_Insert extends OOM_Query { var $_cmd = array("INSERT", "REPLACE"); function OOM_Insert (&$db, $table, $what, $into = NULL, $shove = FALSE, $fore = '') { $dw = ''; if (is_array($into)) $dw = '('.implode(",", $into).') '; if (is_a($what, "OOM_Select")): $dw .= $what->query(); elseif (is_array($into)): $values =& new OOM_List($what); $dw .= 'VALUES ('.$values->to_string().')'; else: $sr =& new OOM_Set_Row($what); $dw .= 'SET '.$sr->to_string(); endif; $rest = ''; if (is_a($shove, "OOM_Set_Row") or is_array($shove)): $s =& new OOM_Set_Row($shove); $rest = "ON DUPLICATE KEY UPDATE ".$s->to_string(); endif; OOM_Query::OOM_Query($db, $this->_cmd[$shove === TRUE]." INTO $table $dw $rest"); } } class OOM_Delete extends OOM_Query { function OOM_Delete (&$db, $table, $where = NULL, $using = NULL, $rest = NULL, $fore = NULL) { if (is_array($table)) $table = implode(",", $table); if (is_array($using)) $using = implode(",", $using); $qc =& new OOM_Where($where); OOM_Query::OOM_Query($db, "DELETE $fore FROM $table ".(is_null($using) ? ' ' : "USING $using ").$qc->to_clause().' '.$rest); } } ?>