|
|
@@ -0,0 +1,350 @@ |
|
|
|
<?php |
|
|
|
/** |
|
|
|
* 数据库连接类 |
|
|
|
* ============================================================================ |
|
|
|
* * 版权所有 蜘蛛出行 * * |
|
|
|
* 网站地址: http://www.zhizhuchuxing.com |
|
|
|
* ---------------------------------------------------------------------------- |
|
|
|
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和 |
|
|
|
* 使用;不允许对程序代码以任何形式任何目的的再发布。 |
|
|
|
* ============================================================================ |
|
|
|
* Author By: 倪宗锋 |
|
|
|
* PhpStorm mysql.php |
|
|
|
* Create By 2016/11/8 9:01 $ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
namespace Util\Util; |
|
|
|
|
|
|
|
|
|
|
|
class MyPDO |
|
|
|
{ |
|
|
|
protected static $_instance = null; |
|
|
|
private $dbh; |
|
|
|
private $_db; |
|
|
|
|
|
|
|
public function __construct($db) |
|
|
|
{ |
|
|
|
$this->_db = $db; |
|
|
|
$config = Util::getDbConfig(); |
|
|
|
$dbHost = $config[$db]['host']; |
|
|
|
$dbName = $config[$db]['db']; |
|
|
|
$dbUser = $config[$db]['user']; |
|
|
|
$dbPassword = $config[$db]['pwd']; |
|
|
|
$dbPort = $config[$db]['port']; |
|
|
|
try { |
|
|
|
$this->dsn = 'mysql:host=' . $dbHost . ':' . $dbPort . ';dbname=' . $dbName; |
|
|
|
$this->dbh = new \PDO($this->dsn, $dbUser, $dbPassword); |
|
|
|
$this->dbh->query("set names 'utf8'"); |
|
|
|
} catch (\PDOException $e) { |
|
|
|
$this->outputError($e->getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:防止克隆 |
|
|
|
* Function Name: __clone |
|
|
|
* |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
private function __clone() |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:Singleton instance |
|
|
|
* Function Name: getInstance |
|
|
|
* @param $db string |
|
|
|
* |
|
|
|
* @return null|MyPDO |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public static function getInstance($db) |
|
|
|
{ |
|
|
|
$instance = self::$_instance; |
|
|
|
if (empty($instance[$db])) { |
|
|
|
$instance[$db] = new self($db); |
|
|
|
self::$_instance = $instance; |
|
|
|
} |
|
|
|
return $instance[$db]; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:获取一条记录 |
|
|
|
* Function Name: fetchRow |
|
|
|
* @param $strSql |
|
|
|
* |
|
|
|
* @return mixed|string |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function fetchRow($strSql) |
|
|
|
{ |
|
|
|
$recordSet = $this->dbh->query($strSql); |
|
|
|
if ($recordSet) { |
|
|
|
$recordSet->setFetchMode(\PDO::FETCH_ASSOC); |
|
|
|
$result = $recordSet->fetch(); |
|
|
|
} else { |
|
|
|
$result = ''; |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:获取多条记录 |
|
|
|
* Function Name: fetchAll |
|
|
|
* @param $strSql |
|
|
|
* |
|
|
|
* @return array|mixed|string |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function fetchAll($strSql) |
|
|
|
{ |
|
|
|
$recordSet = $this->dbh->query($strSql); |
|
|
|
if ($recordSet) { |
|
|
|
$recordSet->setFetchMode(\PDO::FETCH_ASSOC); |
|
|
|
$result = $recordSet->fetchAll(); |
|
|
|
} else { |
|
|
|
$result = ''; |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:获取第一条记录中的第一个字段 |
|
|
|
* Function Name: fetchOne |
|
|
|
* @param $strSql |
|
|
|
* |
|
|
|
* @return string |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function fetchOne($strSql) |
|
|
|
{ |
|
|
|
$recordSet = $this->dbh->query($strSql); |
|
|
|
if ($recordSet) { |
|
|
|
$result = $recordSet->fetch(); |
|
|
|
$return = $result[0]; |
|
|
|
} else { |
|
|
|
$return = ''; |
|
|
|
} |
|
|
|
return $return; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:更新 |
|
|
|
* Function Name: update |
|
|
|
* @param $table string 表名 |
|
|
|
* @param $arrayDataValue array 字段与值 |
|
|
|
* @param string $where string where条件 |
|
|
|
* |
|
|
|
* @return int |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function update($table, $arrayDataValue, $where = '') |
|
|
|
{ |
|
|
|
$checkFields = $this->checkFields($table, $arrayDataValue);//校验参数是否存在 |
|
|
|
if($checkFields['flag'] == false) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
if ($where) { |
|
|
|
$strSql = ''; |
|
|
|
foreach ($arrayDataValue as $key => $value) { |
|
|
|
$strSql .= ", `$key`='$value'"; |
|
|
|
} |
|
|
|
$strSql = substr($strSql, 1); |
|
|
|
$strSql = "UPDATE `{$table}` SET {$strSql} WHERE {$where}"; |
|
|
|
} else { |
|
|
|
$keyArr = implode('`,`', array_keys($arrayDataValue)); |
|
|
|
$strSql = "REPLACE INTO `$table` (`{$keyArr}`) VALUES ('" . implode("','", $arrayDataValue) . "')"; |
|
|
|
} |
|
|
|
return $this->dbh->exec($strSql); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:插入 |
|
|
|
* Function Name: insert |
|
|
|
* @param $table string 表名 |
|
|
|
* @param $arrayDataValue array 字段与值 |
|
|
|
* |
|
|
|
* @return int |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function insert($table, $arrayDataValue) |
|
|
|
{ |
|
|
|
$checkFields = $this->checkFields($table, $arrayDataValue); |
|
|
|
if($checkFields['flag']) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
$keyArray = implode('`,`', array_keys($arrayDataValue)); |
|
|
|
$valArray = implode("','", $arrayDataValue); |
|
|
|
$strSql = "INSERT INTO `$table` (`{$keyArray}`) VALUES ('{$valArray}')"; |
|
|
|
$result = $this->dbh->exec($strSql); |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Replace 覆盖方式插入 |
|
|
|
* |
|
|
|
* @param String $table 表名 |
|
|
|
* @param Array $arrayDataValue 字段与值 |
|
|
|
* @return Int |
|
|
|
*/ |
|
|
|
public function replace($table, $arrayDataValue) |
|
|
|
{ |
|
|
|
$checkFields = $this->checkFields($table, $arrayDataValue); |
|
|
|
if($checkFields['flag']) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
$keyArray = implode('`,`', array_keys($arrayDataValue)); |
|
|
|
$valArray = implode("','", $arrayDataValue); |
|
|
|
$strSql = "REPLACE INTO `$table`(`{$keyArray}`) VALUES ('{$valArray}')"; |
|
|
|
$result = $this->dbh->exec($strSql); |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Delete 删除 |
|
|
|
* |
|
|
|
* @param String $table 表名 |
|
|
|
* @param String $where 条件 |
|
|
|
* @return Int |
|
|
|
*/ |
|
|
|
public function delete($table, $where = '') |
|
|
|
{ |
|
|
|
$strSql = "DELETE FROM `$table` WHERE $where"; |
|
|
|
$result = $this->dbh->exec($strSql); |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:execSql 执行SQL语句 |
|
|
|
* Function Name: execSql |
|
|
|
* @param $strSql |
|
|
|
* |
|
|
|
* @return int |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
public function execSql($strSql) |
|
|
|
{ |
|
|
|
$result = $this->dbh->exec($strSql); |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* beginTransaction 事务开始 |
|
|
|
*/ |
|
|
|
public function beginTransaction() |
|
|
|
{ |
|
|
|
$this->dbh->beginTransaction(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* commit 事务提交 |
|
|
|
*/ |
|
|
|
public function commit() |
|
|
|
{ |
|
|
|
$this->dbh->commit(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* rollback 事务回滚 |
|
|
|
*/ |
|
|
|
public function rollback() |
|
|
|
{ |
|
|
|
$this->dbh->rollback(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* transaction 通过事务处理多条SQL语句 |
|
|
|
* 调用前需通过getTableEngine判断表引擎是否支持事务 |
|
|
|
* |
|
|
|
* @param array $arraySql |
|
|
|
* @return Boolean |
|
|
|
*/ |
|
|
|
public function execTransaction($arraySql) |
|
|
|
{ |
|
|
|
$retVal = 1; |
|
|
|
$this->beginTransaction(); |
|
|
|
foreach ($arraySql as $strSql) { |
|
|
|
if ($this->execSql($strSql) == 0) $retVal = 0; |
|
|
|
} |
|
|
|
if ($retVal == 0) { |
|
|
|
$this->rollback(); |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
$this->commit(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:检查指定字段是否在指定数据表中存在 |
|
|
|
* Function Name: checkFields |
|
|
|
* @param $table string 表名 |
|
|
|
* @param $arrayFields array 字段 |
|
|
|
* |
|
|
|
* @return array |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
private function checkFields($table, $arrayFields) |
|
|
|
{ |
|
|
|
$fields = $this->getFields($table); |
|
|
|
foreach ($arrayFields as $key => $value) { |
|
|
|
if (!in_array($key, $fields)) { |
|
|
|
return Util::returnArrEr("Unknown column `$key` in field list."); |
|
|
|
} |
|
|
|
} |
|
|
|
return Util::returnArrSu(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* getFields 获取指定数据表中的全部字段名 |
|
|
|
* |
|
|
|
* @param String $table 表名 |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
private function getFields($table) |
|
|
|
{ |
|
|
|
$fields = array(); |
|
|
|
$recordSet = $this->dbh->query("SHOW COLUMNS FROM $table"); |
|
|
|
$recordSet->setFetchMode(\PDO::FETCH_ASSOC); |
|
|
|
$result = $recordSet->fetchAll(); |
|
|
|
foreach ($result as $rows) { |
|
|
|
$fields[] = $rows['Field']; |
|
|
|
} |
|
|
|
return $fields; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function Description:输出错误信息 |
|
|
|
* Function Name: outputError |
|
|
|
* @param $strErrMsg |
|
|
|
* |
|
|
|
* @throws \Exception |
|
|
|
* |
|
|
|
* @author 倪宗锋 |
|
|
|
*/ |
|
|
|
private function outputError($strErrMsg) |
|
|
|
{ |
|
|
|
throw new \Exception('MySQL Error: ' . $strErrMsg); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* destruct 关闭数据库连接 |
|
|
|
*/ |
|
|
|
public function __destruct() |
|
|
|
{ |
|
|
|
$this->dbh = null; |
|
|
|
} |
|
|
|
} |