分类:
2009-12-30 18:52:37
/**
* 数据访问层,仅处理MYSQL
* 包括
* by:李勇
* at:2009-01-19
*/
include_once(rootpath."inc/table.class.php"); //数据表访问类
include_once(rootpath."inc/exception.class.php"); //异常类
final class DalDb{
private $config; //取数据库配置信息,因为数据库已经分布存放
private $tables; //本数据库中所有表
private $dbName; //数据库名称
private $pdo; //数据库连接参数
/**
* 构造方法
*
* @param string $dbName
*/
public function __construct($dbName){
if(!is_string($dbName)){
throw new DalException(DalException::DE_DB_CONSTRUCTOR_WRONG ,array($dbName));
}
if(!$dbName){
throw new DalException(DalException::DE_DB_CONSTRUCTOR_NULL );
}
$this->dbName=$dbName;
$this->pdo=false;
$this->config=false;
$this->tables=array();
}
/**
* 读取配置信息
*
*/
private function config(){
$file=BSPath.DIRECTORY_SEPARATOR."configs".DIRECTORY_SEPARATOR."VDBMSCONFIG.conf";
if(!file_exists($file)){
throw new DalException(DalException::DE_DB_CONFIG_FILE ,array($file));
}
$configString=file_get_contents($file);
if($configString===false){
throw new DalException(DalException::DE_DB_CONFIG_READ ,array($file));
}
if(!$configString){
throw new DalException(DalException::DE_DB_CONFIG_NULL ,array($file));
}
$json=json_decode($configString);
if(!is_object($json)){
throw new DalException(DalException::DE_DB_CONFIG_JSON ,array($configString));
}
$config=get_object_vars($json);
if(!$config){
throw new DalException(DalException::DE_DB_CONFIG_OBJ_NULL ,array($json));
}
if(!isset($config[$this->dbName])){
throw new DalException(DalException::DE_DB_CONFIG_DB ,array($this->dbName));
}
if(!isset($config[$this->dbName]->splitdb)){
throw new DalException(DalException::DE_DB_CONFIG_SPLIT ,array($this->dbName,$config));
}
if(!isset($config[$this->dbName]->splitdb[0])){
throw new DalException(DalException::DE_DB_CONFIG_SPLIT_NULL ,array($config,$this->dbName));
}
$this->config=get_object_vars($config[$this->dbName]->splitdb[0]);
}
/**
* 连接数据库
*
* @return handler PDO连接
*/
private function connect(){
if($this->pdo!==false)return $this->pdo;
if(!$this->config) $this->config();
$dsn='mysql:dbname='.$this->dbName.';host='.$this->config['ip'].';port='.$this->config['port'];
try {
$pdo=new PDO($dsn,$this->config['user'],$this->config['pass']);
}catch (PDOException $e){
throw new DalException(DalException::DE_DB_CONNECT_FAILURE ,array($dsn,$this->config['user'],$this->config['pass']));
}
$this->pdo=$pdo;
$pdo->exec("SET NAMES ".YIQI_DB_CHARSET);
$statement=$pdo->query('show tables');
$result=$statement->fetchAll();
$this->tables=array();
foreach($result as $row){
$tableName=$row[0];
$this->tables[$tableName]=false;
}
return $this->pdo;
}
/**
* 生成数据表访问对象
*
* @param string $table 表名
* @return object 数据表访问对象
*/
private function table($table){
if(isset($this->tables[$table]) and $this->tables[$table]) return $this->tables[$table];
$this->connect();
if(!isset($this->tables[$table])){
throw new DalException(DalException::DE_DB_TABLE ,array($this->dbName,$table));
}
$obj=new DalTable($this->pdo,$table);
$this->tables[$table]=$obj;
return $obj;
}
/**
* 查询数据
*
* @param string $table
请参考table类同名方法
*/
public function select($table,$fields = null,$where = null,$groupby = null,$having = null,$orderby = null,$limit = null){
$table=$this->table($table);
return $table->select($fields,$where,$groupby,$having,$orderby,$limit);
}
/**
* 查询消重后的数据
*
* @param string $table
请参考table类同名方法
*/
public function distinct($table,$fields = null,$where = null,$groupby = null,$having = null,$orderby = null,$limit = null){
$table=$this->table($table);
return $table->distinct($fields,$where,$groupby,$having,$orderby,$limit);
}
/**
* 插入数据
*
* @param string $table
请参考table类同名方法
*/
public function insert($table,$row){
$table=$this->table($table);
return $table->insert($row);
}
/**
* 删除数据
*
* @param string $table
请参考table类同名方法
*/
public function delete($table,$where){
$table=$this->table($table);
return $table->detele($where);
}
/**
* 删除表中所有数据
*
* @param string $table
请参考table类同名方法
*/
public function deleteAll($table){
$table=$this->table($table);
return $table->deleteAll();
}
/**
* 计数
*
* @param string $table
请参考table类同名方法
*/
public function count($table,$where=null){
$table=$this->table($table);
return $table->count($where);
}
/**
* 修改数据行
*
* @param string $table
请参考table类同名方法
*/
public function update($table,$row,$where){
$table=$this->table($table);
return $table->update($row,$where);
}
/**
* 执行SQL语句
*
* @param string $sql
* @return int 影响的行数
*/
public function execute($sql){
$this->connect();
if(!is_string($sql)){
throw new DalException(DalException::DE_DB_EXECUTE ,array($sql));
}
return $this->pdo->exec(trim($sql));
}
/**
* 执行查询语句
*
* @param string $sql
* @return mixed
*/
public function query($sql){
$this->connect();
if(!is_string($sql)){
throw new DalException(DalException::DE_DB_QUERY ,array($sql));
}
return $this->pdo->query(trim($sql));
}
}