/Framework/Db/Mysql.php
PHP | 311 lines | 145 code | 46 blank | 120 comment | 19 complexity | ac2fa33f7f86f936a8bf8051d93bd1d8 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /**
- * [ActiveRecord] Mysql???
- *
- * @version $Id: Mysql.php 338 2012-04-16 09:12:33Z linsir123 $
- * @package Db
- */
- class DbMysql
- {
- /**
- * @var ??????
- */
- public $queries = array();
-
- /**
- * @var ????
- */
- public $numQuery = 0;
-
- /**
- * @var ?????????????ID
- */
- public $insertId = 0;
-
- /**
- * @var ??????????????
- */
- public $error = '';
-
- /**
- * @var ????????????
- */
- private $_result;
-
- /**
- * @var ??????
- */
- private $_debug;
-
- /**
- * @var ????
- */
- private $_dbh;
-
- /**
- * @var ?????
- */
- private $_dbname;
-
- /**
- * ????
- *
- * @param int $dsnNumber ???????,???"main.dsn"???
- */
- public function __construct($dsnNumber = 0)
- {
- $this->_debug = App::O('main.debug');
-
- ///
- list($host, $user, $pwd, $this->_dbname, $charset) = App::O('main.dsn.'.$dsnNumber);
- if ( ! $this->_dbh = @mysql_connect($host, $user, $pwd, true))
- throw new Exception("?????[Mysql]???({$host})?????({$user})?");
-
- if ( ! @mysql_select_db($this->_dbname, $this->_dbh))
- throw new Exception("???({$this->_dbname})????");
-
- ///
- if ($this->_supportCollation()) {
- if ( ! isset($charset))
- $charset = App::O('main.charset.db');
-
- ///
- if ( ! empty($charset))
- @mysql_query("SET NAMES '{$charset}'", $this->_dbh);
- }
- }
-
- /**
- * ????
- */
- public function __destruct()
- {
- @mysql_close($this->_dbh);
- }
-
- /**
- * ????,??
- *
- * @param string $query ?????SQL
- * @return mixed
- */
- public function query($query)
- {
- $returnVal = 0;
- $this->insertId = 0;
- $this->_result = array();
-
- if ($this->_debug)
- $this->_timerStart();
-
- $res = @mysql_query($query, $this->_dbh);
- ++$this->numQuery;
-
- if ($this->_debug)
- $this->queries[] = array($query, $this->_timerStop());
-
- if ($this->error = mysql_error($this->_dbh)) {
- $this->_bail($query);
- return false;
- }
-
- if (preg_match("/^\\s*(insert|delete|update|replace) /i", $query)) {
- $returnVal = mysql_affected_rows($this->_dbh);
- if (preg_match("/^\\s*(insert|replace) /i", $query))
- $this->insertId = mysql_insert_id($this->_dbh);
-
- } else {
- $numRows = 0;
- while ($row = @mysql_fetch_object($res)) {
- $this->_result[$numRows] = $row;
- $numRows++;
- }
-
- @mysql_free_result($res);
- $returnVal = $numRows;
- }
-
- return $returnVal;
- }
-
- /**
- * ??????
- *
- * @param string $query ??????
- * @return resource
- */
- public function operate($query)
- {
- return mysql_query($query, $this->_dbh);
- }
-
- /**
- * ?????
- *
- * @param string $query ??SQL
- * @param int $x ??????
- * @param int $y ??????
- * @return string
- */
- public function getVar($query, $x = 0, $y = 0)
- {
- $this->query($query);
- if ( ! isset($this->_result[$y]))
- return null;
-
- ///
- $row = array_values(get_object_vars($this->_result[$y]));
- return isset($row[$x]) ? $row[$x] : null;
- }
-
- /**
- * ??????
- *
- * @param string $query ??SQL
- * @param int $y ??????
- * @return array
- */
- public function getRow($query, $y = 0)
- {
- $this->query($query);
- if ( ! isset($this->_result[$y]))
- return null;
-
- ///
- return get_object_vars($this->_result[$y]);
- }
-
- /**
- * ???????
- *
- * @param string $query ??SQL
- * @param int $x ??????
- * @return array
- */
- public function getCol($query, $x = 0)
- {
- $array = array();
- $this->query($query);
-
- ///
- if ($result = $this->_result) {
- $i = 0;
- foreach($result as $row) {
- $row = array_values(get_object_vars($row));
- $array[$i++] = $row[$x];
- }
- }
-
- return $array;
- }
-
- /**
- * ?????????
- *
- * @param string $query ??SQL
- * @return array
- */
- public function getResults($query)
- {
- $array = array();
- $this->query($query);
-
- ///
- if ($result = $this->_result) {
- $i = 0;
- foreach($result as $row)
- $array[$i++] = get_object_vars($row);
- }
-
- return $array;
- }
-
- /**
- * ???????????
- *
- * @return array
- */
- public function getTables()
- {
- $array = array();
- if ($result = mysql_list_tables($this->_dbname, $this->_dbh)) {
- while ($row = mysql_fetch_row($result))
- array_push($array, $row[0]);
-
- mysql_free_result($result);
- }
-
- return $array;
- }
-
- /**
- * ?????????(?????????)
- *
- * @param string $table ????????
- * @return bool
- */
- public function existsTable($table)
- {
- $query = "SELECT 1 FROM `{$table}` LIMIT 0,1";
- $result = mysql_query($query, $this->_dbh);
- if (empty($result))
- return false;
-
- return true;
- }
-
- /**
- * ??Mysql????
- *
- * @return string
- */
- public function version()
- {
- return mysql_get_server_info($this->_dbh);
- }
-
-
- /// ??????? ///
-
-
- /**
- * ?????
- */
- private function _timerStart()
- {
- $mtime = explode(' ', microtime());
- $this->timeStart = $mtime[1] + $mtime[0];
- }
-
- /**
- * ?????,???????
- *
- * @return int
- */
- private function _timerStop()
- {
- $mtime = explode(' ', microtime());
- return $mtime[1] + $mtime[0] - $this->timeStart;
- }
-
- /**
- * ??Mysql???????,???4.1.0????
- *
- * @return bool
- */
- private function _supportCollation()
- {
- return (version_compare(mysql_get_server_info($this->_dbh), '4.1.0', '>='));
- }
-
- /**
- * ????????
- *
- * @param string $query ???SQL
- */
- private function _bail($query)
- {
- $content = $query.'('.mysql_error().' ['.mysql_errno().'])';
- App::log($content);
- }
- }