清如许工作室 PHP框架DB源码分享
<?phpif(!defined('QRX')) exit('Request Error!');
/**
* 数据库类
*/
class Db {
//连接ID
private $linkID;
//调试模式
private $debugMode;
//缓存管理
private $globalCache;
//调试模式SQL
public $sqlTree = array();
/**
* 构造函数
* @param 配置信息 $config
*/
function __construct($config)
{
//是否调试模式
$this->debugMode = $config['DebugMode'];
//缓存方式
if('file'==$config['CacheType'])
{
$this->globalCache = new FileCache($config['DataCachePath']);;
}
else
{
$this->globalCache = new MemcacheCache($config['MemcacheHost'],$config['MemcachePort']);
}
//连接数据库
$this->linkID = mysql_connect($config['DbHost'],$config['DbUser'],$config['DbPass']) or exit('Can\'t Connect MySQL Server');
mysql_query("set sql_mode=''",$this->linkID);
mysql_query('set names utf8',$this->linkID);
mysql_select_db($config['DbName'],$this->linkID) or exit('Can\'t select MySQL database');
}
/**
* 执行查询
* @param sql语句 $sql
* @return res结果集
*/
function Execute($sql)
{
$res = mysql_query($sql,$this->linkID) or exit(mysql_error());
//调试模式记录sql
if($this->debugMode)
{
$this->sqlTree[] = $sql;
}
if(false!=$res)
{
return $res;
}
return false;
}
/**
* 获得上次插入的自增ID
* @return id
*/
function GetInsertID()
{
$id = mysql_insert_id($this->linkID);
return $id;
}
/**
* 执行update
* @param sql $sql
* @return 影响行数
*/
function ExecuteUpdate($sql)
{
mysql_query($sql,$this->linkID);
return mysql_affected_rows($this->linkID);
}
/**
* 查询全部
* @param sql $sql
* @param 查询行数 $limit
* @param 缓存 $cache
* @return list
*/
function GetAll($sql,$limit='',$cache=false)
{
$sql = empty($limit) ? $sql : $sql.' '.$limit;
$sql = $this->SafeCheck($sql);
$cacheDataEmpty = true;
$cacheKey = '';
if($cache)
{
$cacheKey = md5($sql);
$cacheData = $this->globalCache->Get($cacheKey);
if(!empty($cacheData))
{
$cacheDataEmpty = false;
return $cacheData;
}
}
if($cacheDataEmpty)
{
$res = $this->Execute($sql);
$arr = array();
while (false !==($row = mysql_fetch_array($res)))
{
$arr[] = $row;
}
if($cache && !$this->debugMode)
{
$this->globalCache->Set($cacheKey,$arr);
}
return $arr;
}
}
/**
* 查询返回一行
* @param sql $sql
* @param 缓存 $cache
* @return row
*/
function GetRow($sql,$cache=false)
{
$sql = $this->SafeCheck($sql);
$cacheDataEmpty = true;
$cacheKey = '';
if($cache)
{
$cacheKey = md5($sql);
$cacheData = $this->globalCache->Get($cacheKey);
if(!empty($cacheData))
{
$cacheDataEmpty = false;
return $cacheData;
}
}
if($cacheDataEmpty)
{
$res = $this->Execute($sql);
$row = mysql_fetch_array($res);
if($cache && !$this->debugMode)
{
$this->globalCache->Set($cacheKey,$row);
}
return $row;
}
}
/**
* 查询返回一行第一列
* @param sql $sql
* @param 缓存 $cache
* @return rol
*/
function GetOne($sql,$cache=false)
{
$sql = $this->SafeCheck($sql);
$cacheDataEmpty = true;
$cacheKey = '';
if($cache)
{
$cacheKey = md5($sql);
$cacheData = $this->globalCache->Get($cacheKey);
if(!empty($cacheData))
{
$cacheDataEmpty = false;
return $cacheData;
}
}
if($cacheDataEmpty)
{
$res = $this->Execute($sql);
$row = mysql_fetch_array($res);
if($cache && !$this->debugMode)
{
$this->globalCache->Set($cacheKey,$row[0]);
}
return $row[0];
}
}
/**
* SQL查询安全检测
* @param sql $sql
* @return sql
*/
private function SafeCheck($sql)
{
$oldSql = $sql;
$sql = str_replace(array('\\\\', '\\\'', '\\"', '\'\''), '', $sql);
$pattern = '/union|sleep|benchmark|load_file|outfile|#|-|\/\*|0x/i';
if(preg_match($pattern,$sql))
{
$errorMsg = GetIp().'-'.GetSelf().'-'.$oldSql."\r\n";
fputs(fopen(ROOTPATH.'/sql-safe.log','a+'),$errorMsg);
exit('Request Error!');
}
unset($oldSql);
return $sql;
}
}
?>