* @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Db */ /** * MySQL connection and query handlers. Provides some * abstraction for querying a mysql database. * * @todo Make charset accessible to code and ini * * @author Ben Borowski * @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @link http://typeoneerror.com/asra * @category Asra * @package Db */ class Asra_Db_MySQL { //--------------------------------------- // PROTECTED VARIABLES //--------------------------------------- /** * Connect by default * * @var bool */ protected $_autoConnect = true; /** * Which character set to use to connect * * @var string */ protected $_charset = 'utf8'; /** * MySQL host * * @var string */ protected $_host = 'localhost'; /** * Link to connected database * * @var resource */ protected $_link = null; /** * MySQL password * * @var string */ protected $_pass = 'root'; /** * MySQL username * * @var string */ protected $_user = 'root'; //--------------------------------------- // PUBLIC VARIABLES //--------------------------------------- /** * Connected to mysql * * @var bool */ public $connected = false; /** * Database name * * @var string */ public $db = null; /** * Last query run * * @var string */ public $last = null; /** * Class has connected and database has been selected * * @var bool */ public $ready = false; //--------------------------------------- // CONSTRUCTOR //--------------------------------------- /** * Create a MySQL object. * * @param array $config Database config. * @param bool $autoConnect Automatically open link to mysql. * @return Asra_Db_Mysql Reference for fluent interface. */ public function __construct($config = null, $autoConnect = true) { $this->_autoConnect = $autoConnect; if ($config != null) { $this->config( $config->connection['database.params.host'], $config->connection['database.params.username'], $config->connection['database.params.password'], $config->connection['database.params.dbname'] ); if ($autoConnect) { $this->_connect(); } } return $this; } //--------------------------------------- // METHODS //--------------------------------------- /** * Closes the mysql connected explicitly. * * @return bool */ public function close() { $this->connected = false; return mysql_close($this->_link); } /** * Sets the connection strings. * * @return Asra_Db_Mysql */ public function config($host, $user, $pass, $db) { $this->_host = $host; $this->_user = $user; $this->_pass = $pass; $this->db = $db; return $this; } /** * Connects to the database. * * @return Asra_Db_Mysql */ public function connect() { $this->_connect(); return $this; } /** * Connects to mysql database. * * @return resource or boolean false on failure. */ protected function _connect() { if (!$this->connected) { $this->_link = @mysql_connect($this->_host, $this->_user, $this->_pass); @mysql_set_charset($this->_charset, $this->_link); } if (!$this->_link) { $this->_error("Could not connect to `" . $this->_host . "` as `" . $this->_user . "`"); } else { $this->connected = true; if (!$this->ready) { if ($this->selectdb()) { $this->ready = true; } } } return $this->_link; } /** * Document an error and then die. * * @return void */ protected function _error($text = null) { //trigger_error($text, E_USER_ERROR); //exit; require_once "Asra/Db/Exception.php"; throw new Asra_Db_Exception($text); } /** * Checks whether a table exists in a database. * * @param string $table Name of table. * @return bool True if table exists. */ public function exists($table) { if ($this->ready) { $tables = array(); $query = @mysql_query("SHOW TABLES"); while ($result = @mysql_fetch_array($query)) { $tables[] = $result[0]; } @mysql_free_result($query); if (in_array($table, $tables)) return true; else return false; } else { return false; } } /** * Get the database name. * * @return string The database name. */ public function getDb() { return $this->db; } /** * Get the hostname. * * @return string The hostname. */ public function getHost() { return $this->_host; } /** * Get user name. * * @return string The user name. */ public function getUser() { return $this->_user; } /** * Runs a query and returns the result as an object array * * @param string $query * @return array of objects or false if no results */ public function obj($query) { return $this->query($query, true); } /** * Run a query on the MySQL connection. * * @param string $query Query to run. * @return array or false if no results. */ public function query($query, $object = false) { $this->last = $query; $result = mysql_query($query); if (!$result) { $this->_error(mysql_error()); } if (mysql_num_rows($result)) { $rows = array(); $func = $object ? 'mysql_fetch_object' : 'mysql_fetch_assoc'; while ($row = $func($result)) { $rows[] = $row; } return $rows; } else { return false; } } /** * The same as query() but returns a single row. Thus no wrapper array. * * @param string $query The query to run. * @return array A single array row result. */ public function row($query) { // -- add an auto LIMIT 1 to it if (stripos($query, 'limit') === false) $query .= ' LIMIT 1'; // -- run the query like normal $result = $this->query($query); // -- return an empty array if no results if ($result === false) return false; else return $result[0]; } /** * Run query and return effected rows if asked. * * @param string $query Query to run. * @param bool $effected Return effected rows. * @return bool or int if effected is true. */ public function run($query, $effected = false) { $this->last = $query; $result = mysql_query($query); if ($result) { if ($effected) return mysql_affected_rows(); return true; } else { return false; } } /** * Search for rows based on a simple condition. * * @param string $table Database table to search. * @param string $search Search params for WHERE. * @param string $fields Fields to select in the query. * @return array or false if no results. */ public function search($table, $search = '', $fields = ' * ') { $query = "SELECT "; if (notnull($fields)) { $query .= $fields; } else { $query .= " * "; } $query .= " FROM " . $table; if (notnull($search)) { $query .= " WHERE " . $search; } $this->last = $query; $result = mysql_query($query); if (!$result) { $this->_error(mysql_error()); } if (mysql_num_rows($result)) { $rows = array(); while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } return $rows; } else { return false; } } /** * Select a database. * * @param string $db Name of database * @return bool True if database could be selected. */ public function selectdb($db = null) { if (is_null($db)) $db = $this->db; return (mysql_select_db($db)); } }