Location: PHPKode > scripts > Database metadata > class.sqlBuilder.inc.php
<?php
/**
 * See class DocBlock
 *
 * @author H.F.N. den Boer <hide@address.com>
 * @copyright Copyright 2005 - 2006, IMS - the Netherlands <http://www.denboer-ims.nl>
 * @version 1.0.0
 * @package nl.denboer-ims.imslib
 */

global $imslib_Backbone;

//	First check if access of this page is allowed
if (!isset($imslib_Backbone))
	die("In order to call this file, the backbone must be included first...");

/**
 * This class offers a generic way to build SQL inserts and SQL updates
 *
 * @package nl.denboer-ims.imslib
 */
class imslib_sqlBuilder
{
	/**
	 * @var bool, indicating this is intended as a INSERT
	 * @access public
	 */
	public $isInsert;

	/**
	 * @var array, used as a hashlist, contains fields and values
	 * @access private
	 */
	private $_fieldList;

	/**
	 * @var string
	 * @access private
	 */
	private $_tableName;

	/**
	 * @var string
	 * @access private
	 */
	private $_keyfieldName;

	/**
	 * @var string
	 * @access private
	 */
	private $_keyfieldValue;

	/**
	 * Method to get a INSERT string
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access private
	 * @return string
	 */
	private function getInsert()
	{
		$isFirst = true;
		$fields = "INSERT INTO " . $this->_tableName . " (";
		$values = " VALUES (";

		reset($this->_fieldList);
		$value = current($this->_fieldList);
		$key = key($this->_fieldList);
		while ($key)
		{
			if (!$isFirst)
			{
				$fields .= ", ";
				$values .= ", ";
			}
			$fields .= "$key";
			$values .= "$value";
			$isFirst = false;
			$value = next($this->_fieldList);
			$key = key($this->_fieldList);
		}

		return "$fields) $values)";
	}

	/**
	 * Method to get a UPDATE string
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access private
	 * @return string
	 */
	private function getUpdate()
	{
		$isFirst = true;
		$retVal = "UPDATE " . $this->_tableName . " SET ";
		reset($this->_fieldList);
		$value = current($this->_fieldList);
		$key = key($this->_fieldList);
		while ($key)
		{
			if (!$isFirst)
				$retVal .= ", ";
			$retVal .= "$key = $value";
			$isFirst = false;
			$value = next($this->_fieldList);
			$key = key($this->_fieldList);
		}
		$retVal .= " WHERE " . $this->_keyfieldName . " = " . $this->_keyfieldValue;
		return $retVal;
	}

	/**
	 * Reset instance, always use this one...
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @param string $tableName
	 * @access public
	 * @return void
	 */
	public function reset($tableName)
	{
		$this->_fieldList = array();
		$this->_tableName = $tableName;
		$this->isInsert = true;
	}

	/**
	 * Method to switch to UPDATE mode
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @param string $keyfieldName
	 * @param int $id of keyfield
	 * @access public
	 * @return void
	 */
	public function setUpdateModus($keyfieldName, $id)
	{
		$this->_keyfieldName = $keyfieldName;
		$this->_keyfieldValue = $id;
		$this->isInsert = false;
	}

	/**
	 * Method to add a field/value pair
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param string $fieldName
	 * @param mixed $value
	 * @return void
	 */
	public function add($fieldName, $value)
	{
		global $imslib_Backbone;
		$this->_fieldList[$fieldName] = $imslib_Backbone->getSqlParameter($value);
	}

	/**
	 * Method to get the composed SQL string
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @return string
	 */
	public function getSql()
	{
		if (!is_array($this->_fieldList) || count($this->_fieldList) == 0)
			return "";

		global $imslib_Backbone;
		if ($this->isInsert)
			return $this->getInsert();
		else
			return $this->getUpdate();
	}
}

?>
Return current item: Database metadata