Location: PHPKode > scripts > Scrubs > scrubs/QueryEngine.php
<?php

/**
 * Scrubs is an Object Relational Mapper for PHP.  It is not a port
 * of Hibernate.
 *
 * @version $Id: version .1$
 * @copyright 2003 
 **/

define('SCRUBS', dirname(dirname(__FILE__)));

require_once('ObjectAgent.php');
require_once(SCRUBS.'/lib/Logger.php');

class QueryEngine{

	var $dbc          = "";
	var $loadedObject = "";
	var $logger       = "";
	
	function QueryEngine() {
		$this->logger = new Logger();
	}
	
	function load($object, $primaryKey, $lazy = true){
		$sql = "SELECT * FROM `$object->table` WHERE $object->primaryKey = '$primaryKey'";
		$logString = "Database query: $sql in function load()";
		$this->logger->info($logString);
		$rs = $this->dbc->Execute($sql);
		if ($rs === false) {
			$this->logger->error($this->dbc->ErrorMsg());
			return false;
		} elseif ($rs->RecordCount() == 0) {
			$this->logger->error("No record returned for SQL Statement.");
			return false;
		} else {
			$data = $rs->fields;
			$oa = new ObjectAgent($object);
			$this->loadedObject = $oa->fill($data);
			if (!empty($this->loadedObject->oneToMany) && $lazy == false) {
				$getMethod = "get".$this->loadedObject->primaryKey;
				foreach($this->loadedObject->oneToMany as $table => $linkOn) {
					$linkObject = $this->loadedObject->$table;
					$where = "$linkOn = '".$this->loadedObject->$getMethod()."'";
					$objectList = $this->get($linkObject, $where);
					$objectArray = $this->loadedObject->objectArrayMap[$table];
					$setMethod = "set".ucfirst($objectArray);
					$this->loadedObject->$setMethod($objectList);
				}
			}
			$this->logger->info("Object properly loaded.");
			return $this->loadedObject;
		}
	}
	
	function unload($object) {
		$oa = new ObjectAgent($object);
		$unloadedObject = $oa->clear($object->fields);
		$this->loadedObject = null;
		$this->logger->info("Object properly unloaded");
		return $unloadedObject;
	}
	
	function save($object) {
		$insertArray = array();
		$getMethod = "get".ucfirst($object->primaryKey);
		$primaryKeyValue = $object->$getMethod();
		
		foreach($object->fields as $field => $property) {
			$getMethod = "get".ucfirst($property);
			$value = $object->$getMethod();
		  $insertArray[$field] = $value;
		}		
		
		if ($object->newRecord) {
			$insertFields = $object->primaryKey;
			foreach($insertArray as $fieldName => $fieldValue) {
				$insertFields .=  ", $fieldName";
			}
			if ($object->autoIncrement) {
				$insertValues = "''";
			} else {			
				$insertValues = "'$primaryKeyValue'";
			}
			foreach($insertArray as $fieldValue) {
				$insertValues .= ", '$fieldValue'";
			}
			$sql = "INSERT INTO `$object->table` ($insertFields) VALUES ($insertValues)";
		} else {
			$getMethod = "get".ucfirst($object->primaryKey);
			$primaryKeyOriginalValue = $this->loadedObject->$getMethod();
			if(!$object->autoIncrement) {
				$updatePairs = "$object->primaryKey = '$primaryKeyValue', ";
			}
			
			$i = 1;
			foreach($insertArray as $field => $value) {
				if ($i < sizeof($insertArray)) {
					$updatePairs .= "$field = '".$value."', ";
				} else {
					$updatePairs .= "$field = '".$value."' ";
				}
				$i++;
			}
			$sql = "UPDATE `$object->table` SET $updatePairs WHERE $object->primaryKey = '$primaryKeyOriginalValue'";
		}
			
		$logString = "Database query: $sql in function save()";
		$this->logger->info($logString);		
		
		if ($this->dbc->Execute($sql) === false) {
			$this->logger->error($this->dbc->ErrorMsg());
			return false;
		} else {
			$this->logger->info("Object successfully persisted.");
			if ($object->newRecord) {
				return $this->dbc->Insert_ID();
			} else {
				return true;
			}
		}
	}
	
	function delete($object, $cascade = false) {
		if ($object->newRecord) {
			return false;
		} else {
			$getMethod = "get".ucfirst($object->primaryKey);
			$primaryKeyValue = $object->$getMethod();
			$sql = "DELETE FROM `$object->table` WHERE $object->primaryKey = '$primaryKeyValue'";
			if ($object->oneToMany) {			
				if ($cascade) {
					foreach($this->loadedObject->oneToMany as $table => $linkOn) {
						$cascadeSql = "DELETE FROM `$table` WHERE $linkOn = '$primaryKeyValue'";
					}					
				}
			}
			$logString = "Database query: $sql in function delete()";
			$this->logger->info($logString);
		
			if ($this->dbc->Execute($sql) === false) {
				$this->logger->error($this->dbc->ErrorMsg());
				return false;
			} else {
				if (!empty($cascadeSql)) {
					$logString = "Database query: $cascadeSql in function delete()";
					$this->logger->info($logString);
					$this->dbc->Execute($cascadeSql);			
				}
				return true;
			}
		}
	}
	
	function get($object, $where = null) {
		$classList = array();
		if ($where != null && $where != "") {
			$sql = "SELECT * FROM `$object->table` WHERE ".$where;		
		} else {
			$sql = "SELECT * FROM `$object->table`";
		}
		$logString = "Database query: $sql in function get()";
		$this->logger->info($logString);
		$rs = $this->dbc->Execute($sql);
		if ($rs === false) {
			$this->logger->error($this->dbc->ErrorMsg());
			return false;
		} elseif ($rs->RecordCount() == 0) {
			$this->logger->error("No records returned for SQL Statement.");
			return false;
		} else {
			while(!$rs->EOF){
				$oa = new ObjectAgent($object);
				$newClass = $oa->fill($rs->fields);
				array_push($classList, $newClass);
				$rs->MoveNext();
			} 
			return $classList;
		}
	}
}
?>
Return current item: Scrubs