分类:
2009-12-30 18:55:03
/**
* 数据库访问层的异常类
*
*/
class DalException extends Exception {
//table类的异常
const DE_TABLE_CONSTRUCTOR_TABLE_WRONG = 1001;
const DE_TABLE_CONSTRUCTOR_TABLE = 1002;
const DE_TABLE_CONSTRUCTOR_PDO = 1003;
const DE_TABLE_META_NULL = 1004;
const DE_TABLE_INSERT_NULL = 1005;
const DE_TABLE_INSERT_WRONG = 1006;
const DE_TABLE_INSERT_FAILURE = 1007;
const DE_TABLE_DELETE_NO_WHERE = 1008;
const DE_TABLE_UPDATE_NULL = 1009;
const DE_TABLE_UPDATE_WRONG = 1010;
const DE_TABLE_UPDATE_INVALID = 1011;
const DE_TABLE_UPDATE_NO_WHERE = 1012;
const DE_TABLE_FIELD_WRONG = 1013;
const DE_TABLE_SORT_WRONG = 1014;
const DE_TABLE_LIMIT_WRONG = 1015;
const DE_TABLE_LIMIT_COUNT = 1016;
const DE_TABLE_CONDITION_PRIMARYKEY = 1017;
const DE_TABLE_CONDITION_WRONG = 1018;
const DE_TABLE_CONDITION_NO_PRIMARYKEY = 1019;
const DE_TABLE_EXECUTE_STRING = 1020;
const DE_TABLE_QUERY_STRING = 1021;
//db类的异常
const DE_DB_CONSTRUCTOR_WRONG = 2001;
const DE_DB_CONSTRUCTOR_NULL = 2002;
const DE_DB_CONFIG_FILE = 2003;
const DE_DB_CONFIG_READ = 2004;
const DE_DB_CONFIG_NULL = 2005;
const DE_DB_CONFIG_JSON = 2006;
const DE_DB_CONFIG_OBJ_NULL = 2007;
const DE_DB_CONFIG_DB = 2008;
const DE_DB_CONFIG_SPLIT = 2009;
const DE_DB_CONFIG_SPLIT_NULL = 2010;
const DE_DB_CONNECT_FAILURE = 2011;
const DE_DB_TABLE = 2012;
const DE_DB_EXECUTE = 2013;
const DE_DB_QUERY = 2014;
const DE_UNKNOWN = 5000;
//所有异常情况的文字表达
static private $msgs = array(
//db类的异常消息
self::DE_DB_CONSTRUCTOR_WRONG => '构造数据库对象时名称参数必须是字符串,现为<%s>',
self::DE_DB_CONSTRUCTOR_NULL => '构造数据库对象时必须提供名称参数',
self::DE_DB_CONFIG_FILE => '数据库配置文件不存在:<%s>',
self::DE_DB_CONFIG_READ => '数据库配置文件读取错误:<%s>',
self::DE_DB_CONFIG_NULL => '数据库配置文件内容为空:<%s>',
self::DE_DB_CONFIG_JSON => '数据库配置文件内容不是正确的JSON对象:<%s>',
self::DE_DB_CONFIG_OBJ_NULL => '数据库配置对象内容为空:<%s>',
self::DE_DB_CONFIG_DB => '配置文件中缺少此数据库的信息:<%s>',
self::DE_DB_CONFIG_SPLIT => '配置文件中缺少数据库<%s>的SPLIT信息:<%s>',
self::DE_DB_CONFIG_SPLIT_NULL => '配置文件<%s>中数据库<%s>的SPLIT信息为空',
self::DE_DB_CONNECT_FAILURE => '连接数据库失败,DSN:<%s>,USER:<%s>,PASS:<%s>',
self::DE_DB_TABLE => '数据库<%s>中没有找到表<%s>',
self::DE_DB_EXECUTE => 'execute方法的参数必须是字符串,现为<%s>',
self::DE_DB_QUERY => 'query方法的参数必须是字符串,现为<%s>',
//table类的异常消息
self::DE_TABLE_CONSTRUCTOR_TABLE_WRONG => '构造表对象时表名参数必须是字符串,现为<%s>',
self::DE_TABLE_CONSTRUCTOR_TABLE => '构造表对象时缺少参数 表名',
self::DE_TABLE_CONSTRUCTOR_PDO => '构造表对象时缺少参数 数据库连接',
self::DE_TABLE_META_NULL => '表对象:<%s>无法获取表结构信息',
self::DE_TABLE_INSERT_NULL => '表对象:<%s>插入行:<%s>时数据不正确',
self::DE_TABLE_INSERT_WRONG => '表对象:<%s>插入行:<%s>时,数据格式不识别',
self::DE_TABLE_INSERT_FAILURE => '表对象:<%s>插入行:<%s>时,插入失败,SQL:<%s>,请检查表约束',
self::DE_TABLE_DELETE_NO_WHERE => '表对象:<%s>删除时必须指定条件,现参数为:<%s>',
self::DE_TABLE_UPDATE_NULL => '表对象:<%s>修改时必须指明修改值,现参数为:<%s>',
self::DE_TABLE_UPDATE_WRONG => '表对象:<%s>修改时修改值格式错误,现参数为:<%s>',
self::DE_TABLE_UPDATE_INVALID => '表对象:<%s>修改时修改值与列不符合,现参数为:<%s>',
self::DE_TABLE_UPDATE_NO_WHERE => '表对象:<%s>修改时没有正确指定条件,现参数为:<%s>',
self::DE_TABLE_FIELD_WRONG => '为表对象:<%s>指定的字段列表格式错误,现参数为:<%s>',
self::DE_TABLE_SORT_WRONG => '表对象:<%s>的排序依据错误:<%s>',
self::DE_TABLE_LIMIT_WRONG => '表对象:<%s>分页错误:<%s>',
self::DE_TABLE_LIMIT_COUNT => '表对象:<%s>分页参数的个数错误:<%s>',
self::DE_TABLE_CONDITION_PRIMARYKEY => '表对象<%s>条件中指定了一个单值<%s>,但表缺少单一主键',
self::DE_TABLE_CONDITION_WRONG => '表对象<%s>条件格式错误<%s>',
self::DE_TABLE_CONDITION_NO_PRIMARYKEY => '表对象<%s>指定了主键条件<%s>,但表结构中缺少单一主键',
self::DE_TABLE_EXECUTE_STRING => '表对象<%s>的执行语句<%s>必须是字符串',
self::DE_TABLE_QUERY_STRING => '表对象<%s>的查询语句<%s>必须是字符串',
self::DE_UNKNOWN => '未知错误'
);
public function __construct($code = 0,$msg = null) {
if(isset(self::$msgs[$code])){
if(is_string($msg)){
$message = $msg;
}else if(is_array($msg)){
foreach ($msg as $k=>$v){
if(!is_string($v)&&!is_numeric($v)){
$msg[$k]=$this->dump($v);
}
}
$message = vsprintf(self::$msgs[$code],$msg);
}else{
$message = self::$msgs[$code];
}
}else{
$code = self::DE_UNKNOWN ;
if(is_string($msg)){
$message = $msg;
}else{
$message = '错误代码:'.$code;
}
}
parent::__construct($message, $code);
}
private function dump($mixed){
return var_export($mixed,true);
}
}