All pastes #112161 Raw Edit

k4st

public php v1 · immutable
·published 2006-08-02 19:04 UTC
rendered paste body
<?phpclass FARecordFinder {	var $_dba;	var $_def;	var $_cache = array();	var $_temp_from = "";		function FARecordFinder(&$dba, &$def, &$model) 	{		$this->_dba = &$dba;		$this->_def = &$def;		$this->_model = &$model;	}		function &createRecord() 	{		$class = $this->_def->getRecordClass();		$ret = &new $class($this->_dba, $this->_def, $this, $this->_model, array());		return $ret;	}		function &find($id) 	{		if (!isset($this->_cache[$id])) 		{			//geoffrey: removed $this->_def->getField( because it's also used in findBy			$this->_cache[$id] = &$this->findBy($this->_def->getPrimaryKey(), $id);		}				return $this->_cache[$id];	}		function &findAll($order = '', $groupby = '', $limit = 0, $end = -1) 	{		return $this->findAllWhere(NULL, array(), $order, $groupby, $limit, $end);	}		function &findAllByArray($pairs, $order = '', $groupby = '', $limit = 0, $end = -1) 	{		// Generate the WHERE portion		$where = '';		$sep = '';		foreach ($pairs as $key => $value) 		{			$where = $sep . $this->_def->getField($key) . "=?";			$sep = ' AND ';		}				return $this->findAllWhere($where, array_values($pairs), $order, $groupby, $limit, $end);	}		function &selectAllWhere($select, $where, $values = array(), $order = '', $groupby = '', $limit = 0, $end = -1) 	{		// Put the query together		$select = "SELECT " . ((!$select)			? $this->_def->getSelectFields()			: ($this->_def->getSelectFields()				? $this->_def->getSelectFields() .", ". $select				: $select)		);		$from = " FROM " . $this->_def->getSelectTables() . $this->getTempFrom();		$where = ($where) 			? " WHERE " . $where . (($this->_def->getSelectWhere()) 				? " AND " . $this->_def->getSelectWhere() 				: '')			: (($this->_def->getSelectWhere() 				? " WHERE " . $this->_def->getSelectWhere() 				: '')		);		$order = ($order) 			? " ORDER BY " . $order 			: ($this->_def->getSelectOrderby()				? " ORDER BY " . $this->_def->getSelectOrderby()				: ''		);		$groupby = ($groupby) 			? " GROUP BY " . (($this->_def->getSelectGroupby()) 				? $this->_def->getSelectGroupby() . (substr_utf($groupby, 0, 6) != "HAVING" ? ", " : " "). $groupby 				: $groupby)			: ($this->_def->getSelectGroupby()				? " GROUP BY " . $this->_def->getSelectGroupby() 				: ''		);		$limit = ($limit) ? " LIMIT " . $limit : '';				$sql = $select.$from.$where.$groupby.$order.$limit;				//echo $sql .'<hr>';				// Run the query		$result = &$this->_dba->executeQuery($sql, $values);				$class = $this->_def->getIteratorClass();		$ret = &new $class($this->_dba, $this->_def, $this, $this->_model, $result);				return $ret;	}		function &findAllWhere($where, $values = array(), $order = '', $groupby = '', $limit = 0, $end = -1) 	{		return $this->selectAllWhere(FALSE, $where, $values, $order, $groupby, $limit, $end);	}		function &findAllBy($col, $value) 	{		return $this->findAllWhere($this->_def->getField($col) ."=?", array($value), '', '', 0, -1);	}		function &searchAllWhere($fields, $where, $values = array(), $order = '', $groupby = '', $limit = 0, $end = -1)	{		foreach($fields as $key => $field)		{			$fields[$key] = $this->_def->getField($field);		}				$fields = implode(",", $fields);		$select = "MATCH(". $fields .") AGAINST (? IN BOOLEAN MODE) AS search_score";				$values[0] = !isset($values[0]) ? "" : $values[0];		$values[0] = preg_replace("~(\s|\t|\n|\r|\n)~", " ", $values[0]);		$values[0] = htmlentities($values[0], ENT_QUOTES, K4_CHARSET);		$values[0] = str_replace("* ", " ", $values[0]); // don't want to end up with **'s in there				$len = strlen_utf($values[0]);		if(substr_utf($values[0], $len-1) == '*')		{			$values[0] = substr_utf($values[0], 0, $len-1);		}				$values[0] = implode("* ", explode(" ", $values[0])) .'*';				return $this->selectAllWhere($select, $where, $values, $order, $groupby, $limit, $end);	}		function &findBy($field, $value) 	{		return $this->findByArray(array($field => $value));	}		function &findByArray($pairs) 	{		if (empty($pairs)) 		{			return NULL;		}				// Generate the WHERE portion		$where = '';		$sep = '';		foreach ($pairs as $key => $value) 		{			$where = $sep . $this->_def->getField($key) . "=?";			$sep = ' AND ';		}				return $this->findWhere($where, array_values($pairs));	}		// common function for findWhere and selectWhere	function &selectWhere($select, $where, $values = array(), $order = '', $limit = 1) 	{		// Put the query together		$select = "SELECT " . ((!$select)			? $this->_def->getSelectFields()			: ($this->_def->getSelectFields()				? $this->_def->getSelectFields() .", ". $select				: $select)		);		$from = " FROM " . $this->_def->getSelectTables() . $this->getTempFrom();		$where = ($where) 			? " WHERE " . $where . (($this->_def->getSelectWhere()) 				? " AND " . $this->_def->getSelectWhere() 				: '')			: (($this->_def->getSelectWhere()) 				? " WHERE " . $this->_def->getSelectWhere() 				: ''		);		$order = ($order) 			? " ORDER BY " . $order 			: ($this->_def->getSelectOrderby()				? " ORDER BY " . $this->_def->getSelectOrderby()				: ''		);		$groupby = (($this->_def->getSelectGroupby())				? " GROUP BY " . $this->_def->getSelectGroupby() 				: ''		);		$limit = ($limit) ? " LIMIT " . $limit : '';				$sql = $select.$from.$where.$groupby.$order.$limit;				// Run the query		$result = &$this->_dba->executeQuery($sql, $values);				$ret = NULL;				// Does the resultset have any records?		if ($result->next()) 		{			if ($result->hasNext()) 			{				trigger_error("Selecting a single record returned multiple records", E_USER_WARNING);			}						$class = $this->_def->getRecordClass();			$ret = &new $class($this->_dba, $this->_def, $this, $this->_model, $result->current());		}				return $ret;	}		function &findWhere($where, $values = array(), $order = '', $limit = 0) 	{		return $this->selectWhere(FALSE, $where, $values, $order, $limit);	}		function getCountWhere($where, $values = array())	{		$count = ($this->_def->getPrimaryKey() ? $this->_def->getField($this->_def->getPrimaryKey()) : "*");		$select = "SELECT COUNT(". $count .") AS row_count FROM ". $this->_def->getSelectTables();		$where = ($where && $where != '')			? " WHERE " . $where . (($this->_def->getSelectWhere()) 				? " AND " . $this->_def->getSelectWhere() 				: '')			: (($this->_def->getSelectWhere()) 				? " WHERE " . $this->_def->getSelectWhere() 				: ''		);				$sql = $select.$where;				$result = &$this->_dba->executeQuery($sql, $values);				$count = 0;				if($result->next())		{			$row = &$result->current();						$count = $row['row_count'];		}				$result->free();				return $count;	}		function deleteWhere($where, $values = array())	{		$from = "DELETE FROM " . $this->_def->getTable();		$where = ($where && $where != '') ? " WHERE ". $where : '';				return $this->_dba->executeUpdate($from . $where, $values);	}		function deleteBy($column, $value)	{		return $this->deleteWhere($column .'=?', array($value));	}		function delete($key_value)	{		if(!$this->_def->getPrimaryKey())		{			trigger_error("Deleting from a table without a primary key.");		}				return $this->deleteWhere($this->_def->getPrimaryKey() .'=?', array($key_value));	}		function revive(&$record) 	{		$record->revive($this->_dba, $this, $this->_model);	}		function addTempFrom($from)	{		$this->_temp_from = ", ". $from ." ";	}		function getTempFrom()	{		$ret = "";		$ret .= $this->_temp_from;				$this->_temp_from = "";				return $ret;	}}?>