Location: PHPKode > scripts > Internationalization and Localization > internationalization-and-localization/DatabaseConnection.class.php
<?php

/**
 * Project:     CWF: Cricava Web Framework
 * File:        DatabaseConnection.class.php
 *
 * @link http://www.cricava.com/
 * @copyright 2005-2009 Cricava Technologies, Inc.
 * @author Mariano Iglesias <hide@address.com>
 * @package com.cricava.cwf
 * @version 1.0
 */

require_once(dirname(__FILE__) . '/DatabaseConnectionManager.class.php');

/**
 * Class that manages database connections in a slightly database-independent
 * manner. It is currently prepared for dealing with mysql and mssql. For broader
 * database support, use ADODB layer instead (http://adodb.source-forge.net)
 *
 * @author Mariano Iglesias - hide@address.com
 * @copyright Cricava Technologies
 * @package com.cricava.cwf
 * @subpackage database
 * @since 1.0
 */
class DatabaseConnection extends DatabaseConnectionManager
{
	/**
	 * Type of driver ('mssql' or 'mysql')
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbDriver;

	/**
	 * Host where database resides
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbHost;

	/**
	 * User that connects to database
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbUser;

	/**
	 * Password for the user
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbPassword;

	/**
	 * Database to connect to
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbName;

	/**
	 * Internal resource handle to database connection
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $link;

	/**
	 * Table prefix to use
	 *
	 * @var string
	 * @access private
	 * @since 1.0
	 */
	var $dbTablePrefix;

	function & DatabaseConnection()
	{
		$this->dbDriver = DATABASE_TYPE;
		$this->dbHost = DATABASE_HOST;
		$this->dbUser = DATABASE_USER;
		$this->dbPassword = DATABASE_PASSWORD;
		$this->dbName = DATABASE_NAME;
		$this->dbTablePrefix = DATABASE_TABLE_PREFIX;
		$this->link = null;
	}

	function setDbDriver($dbd)
	{
		$this->dbDriver = $dbd;
	}

	function getDbDriver()
	{
		return $this->dbDriver;
	}

	function setDbHost($dbh)
	{
		$this->dbHost = $dbh;
	}

	function getDbHost()
	{
		return $this->dbHost;
	}

	function setDbUser($dbu)
	{
		$this->dbUser = $dbu;
	}

	function getDbUser()
	{
		return $this->dbUser;
	}

	function setDbPassword($dbp)
	{
		$this->dbPassword = $dbp;
	}

	function getDbPassword()
	{
		return $this->dbPassword;
	}

	function setDbName($dbn)
	{
		$this->dbName = $dbn;
	}

	function getDbName()
	{
		return $this->dbName;
	}

	function setDbTablePrefix($dbTablePrefix)
	{
		$this->dbTablePrefix = $dbTablePrefix;
	}

	function getDbTablePrefix()
	{
		return $this->dbTablePrefix;
	}

	function getTableName($tableName)
	{
		return (isset($this->dbTablePrefix) ? $this->dbTablePrefix : '') . $tableName;
	}

	/**
	 * Gets the ID generated from the previous INSERT operation (applicable to an auto-increment column).
	 *
	 * @return int The last inserted ID
	 */
	function last_id() {
		switch($this->dbDriver)
		{
			case 'mysql':
				return mysql_insert_id();

				break;

			case 'mssql':
				$rs = $this->execute('SELECT @@IDENTITY');
				$row = $this->fetch_array($rs);

				return $row[0];

				break;
		}

		return 0;
	}

	function escape($string)
	{
		switch($this->dbDriver)
		{
			case 'mysql':
				return str_replace("'", "\\'", $string);
				break;
			case 'mssql':
				return str_replace("'", "''", $string);
				break;
		}

		return null;
	}

	function open()
	{
		// If connection is already open, close it first
		if (isset($this->link))
		{
			$this->close();
		}

		$this->link = null;

		// Is every mandatory parameter set up ?
		if (isset($this->dbDriver) && isset($this->dbHost) && isset($this->dbUser) && isset($this->dbPassword) && isset($this->dbName))
		{
			// Which DB driver are we using ? Then, open connection, and select database
			switch($this->dbDriver)
			{
				case 'mysql':
					$this->link =& mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword);
		 	 		mysql_select_db($this->dbName, $this->link);
					break;
				case 'mssql':
					$this->link =& mssql_connect($this->dbHost, $this->dbUser, $this->dbPassword);
		 	 		mssql_select_db($this->dbName, $this->link);
					break;
			}
		}
	}

	function execute($sql)
	{
		global $_DebugSQL;

		// Is this connection properly opened ?
		if (isset($this->link))
		{
			if (isset($_DebugSQL) && $_DebugSQL == true)
			{
				echo "<textarea rows=4 cols=70>".$sql."</textarea><hr>";
			}

			// Which DB driver are we using ? Then, execute query, and immediatly return result
			switch($this->dbDriver)
			{
				case 'mysql':
					return mysql_query($sql, $this->link);
					break;
				case 'mssql':
					return mssql_query($sql, $this->link);
					break;
			}
		}

		return null;
	}

	function num_rows($result)
	{
		// Is the result properly set ?
		if (isset($result))
		{
			// Which DB driver are we using ? Then, return number of resulting rows
			switch($this->dbDriver)
			{
				case 'mysql':
					return mysql_num_rows($result);
					break;
				case 'mssql':
					return mssql_num_rows($result);
					break;
			}
		}

		return 0;
	}

	function fetch_array($result)
	{
		// Is the result properly set ?
		if (isset($result))
		{
			// Which DB driver are we using ? Then, return fetched array
			switch($this->dbDriver)
			{
				case 'mysql':
					return mysql_fetch_array($result);
					break;
				case 'mssql':
					return mssql_fetch_array($result);
					break;
			}
		}

		return null;
	}

	function fetch_row($result)
	{
		// Is the result properly set ?
		if (isset($result))
		{
			// Which DB driver are we using ? Then, return fetched row
			switch($this->dbDriver)
			{
				case 'mysql':
					return mysql_fetch_row($result);
					break;
				case 'mssql':
					return mssql_fetch_row($result);
					break;
			}
		}

		return null;
	}

	function close()
	{
		// Is this connection properly opened ?
		if (isset($this->link))
		{
			// Which DB driver are we using ? Then, close connection
			switch($this->dbDriver)
			{
				case 'mysql':
					mysql_close($this->link);
					break;
				case 'mssql':
					mssql_close($this->link);
					break;
			}

			$this->link = null;
		}
	}
}
?>
Return current item: Internationalization and Localization