icej
(icej)
新手上路

一般会员
UID 66066
精华
0
积分 36
帖子 34
金钱 36 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2005-8-21
状态 离线
|
偷懒方式 拼装SQL语句类 请大家指点
我是个新手,希望大家能多指点,提出更好的方式.
一直觉得ROR方式不错,不过说实话,ROR对我最有吸引力的是写SQL语句那里,自己不用写什么,只要放个数组进去,就自动产生需要的SQL语句提交执行,觉得太爽了,特别是遇到象USER表这样的动辄10-20个字段提交表单POST,然后Insert Update的话,语句可省大发了.
但是ROR那样的要想用这种方便的方式,就必须全盘接受,所以我写了一个类来实现自动拼装SQL语句.不过由于我还没在实际工作中实验,还不知道效果如何,而且实现方式也很土,希望大家能多指点.
这个SQL类支持Insert Update 多表联合Select 3种方式,并且在使用时针对Update和Insert分根据表实际情况检查使用类型和根据字段别名来组装SQL语句两种情况.多表Select支持传递2个包含表名和字段名的数组组装 和 传递表名 根据表的实际情况组装SQL语句.
第一种Insert SQL语句组装 向类传递一个包含字段名(字段名做区分处理,字符型前加s_,整形前加i_)的和值的数组 然后返回SQL语句
include("helpsql.php");
$hs = new HelpSql();
//使用简单组合方式 insert语句 根据字段名的前缀来决定是否加‘’
$hs->initTableName = "bbs"; //设置表名
$table = array("i_bbs_id"=>"1","s_bbs_title"=>"sss","s_bbs_content"=>1,"i_bbs_user_id"=>"1"); //设置字段对应的值 KEY是字段别名 s_字符 i_ 数字 缺省属性是字符
$sql = $hs->get_Insert($table,"bbs_id=1"); //组装insert语句 参数:表数组 Where条件
if($sql)
{
echo $sql; //打印结果 "insert into bbs(bbs_id,bbs_title,bbs_content,bbs_user_id) values('1','sss','1',1) where bbs_id=1"
}
else
{
echo $hs->reErrorInfo; //打印错误信息
} 第二种 设置数据库连接方式和表名 类会获得该表的字段属性并生成数组文件,执行时会根据数组文件来判断插入值的属性 如果不符合会报错误
$hs->initTableName = "bbs"; //设置表名
$table = array("bbs_id"=>"1Q","bbs_title"=>"sss","bbs_content"=>1,"bbs_user_id"=>"1"); //设置字段对应的值 KEY是真实字段名 值是要更新的值 缺省属性是字符
$hs->initdBParam = array("dbname"=>"bbs","dbuser"=>"root","dbpass"=>"123456","dbhost"=>"localhost");//设置数据库连接方式
$hs->initdBInfoPath = dirname(__FILE__);//设置表数组脚本存放位置 名字是 表名_table.php
$sql = $hs->get_Update($table,"bbs_id=1",true); //组装insert语句 参数:表数组 Where条件
if($sql)
{
echo $sql; //打印结果 "update bbs set bbs_id=1,bbs_title='sss',bbs_content='1',bbs_user_id=1 where bbs_id=1"
}
else
{
echo $hs->reErrorInfo; //如果属性不匹配 则返回错误 "字段 bbs_id 值 1Q 不是要求的 int 属性"
} 多表联合Select 第一种方式 传递包含表名和字段的数组进行组装
$tables = array(
"bbs"=>array("bbs_id"=>"","bbs_title"=>"","bbs_content"=>"","bbs_user_id"=>""), //表bbs 字段
"reply"=>array("reply_id"=>"","reply_content"=>""),//表reply 字段
);
echo $hs->get_ManyTableSelect($tables,"bbs.id=1"); //获得多表Select语句 参数:多表数组 条件 返回字段别名为 表名_字段名 "select bbs.bbs_id as bbs_bbs_id,bbs.bbs_title as bbs_bbs_title,bbs.bbs_content as bbs_bbs_content,bbs.bbs_user_id as bbs_bbs_user_id,reply.reply_id as reply_reply_id,reply.reply_content as reply_reply_content from bbs,reply where bbs.id=1" 多表联合Select 另一种形式 根据传递包含表名的数组 通过连接数据库获得表包含的字段 然后组装SQL语句
$tables = array(
"bbs"=>""), //表bbs
"reply"=>""),//表reply
);
echo $hs->get_ManyTableSelect($c,"bbs.id=1",true);
//select bbs.bbs_id as bbs_bbs_id,bbs.bbs_title as bbs_bbs_title,bbs.bbs_content as bbs_bbs_content,bbs.bbs_user_id as bbs_bbs_user_id,bbs.bbs_user_name as bbs_bbs_user_name,bbs.bbs_date as bbs_bbs_date,reply.reply_id as reply_reply_id,reply.reply_content as reply_reply_content,reply.reply_user_id as reply_reply_user_id,reply.reply_user_name as reply_reply_user_name,reply.reply_bbs_id as reply_reply_bbs_id,reply.reply_date as reply_reply_date from bbs,reply where bbs.id=1 类代码如下,做的很粗糙请多指教
<?php
class HelpSql
{
var $ischeckDbInfo = false; //是否从数据库获得字段类型
var $initdBParam = array( //数据库连接参数
"dbname"=>"",
"dbpass"=>"",
"dbuser"=>"",
"dbhost"=>"",
);
var $initTableName = "";//操作表名
var $initData = array(); //数据库使用数据 KEY字段名=>数据
var $initdBInfoPath = ""; //指定路径
var $initdBInfoTBPath = "";//表文件路径
var $initdBInfoExp = "_table.php";//存放数据表信息后缀
var $tableInfo = array(); //表的信息
var $reErrorCode = "";//返回错误代码 001数据库连接错误 002表错误 003字段名不存在 004字段属性错误
var $reErrorInfo = "";
//设置获得表信息的路径
function _set_DbTableFile()
{
//数据库表信息存放路径
//echo "set db path";
$this->initdBInfoTBPath = $this->initdBInfoPath."\\".$this->initTableName.$this->initdBInfoExp;
}
//获得表信息 成功返回TRUE失败返回FALSE
function _get_DbTableInfo()
{
//echo $this->initdBInfoTBPath;
if (is_readable($this->initdBInfoTBPath))
{
echo "include array";
$this->tableInfo = include($this->initdBInfoTBPath);
}
else
{
//从数据库获得表信息
echo "make array";
$dblink = mysql_connect($this->initdBParam["dbhost"], $this->initdBParam["dbuser"], $this->initdBParam["dbpass"]);
if (!$dblink)
{
$this->reErrorCode = 001;
$this->reErrorInfo = mysql_error();
return false;
}
else
{
if (!mysql_select_db($this->initdBParam["dbname"]))
{
$this->reErrorCode = 002;
$this->reErrorInfo = mysql_error();
return false;
}
else
{
$result = mysql_query("Select * FROM ".$this->initTableName);
$fields = mysql_num_fields($result);
$f = fopen($this->initdBInfoTBPath,"w");
$c = "<?php\n return array (\n";
for ($i=0; $i < $fields; $i++)
{
$type = mysql_field_type($result, $i);
$name = mysql_field_name($result, $i);
$tmptype = explode(" ",$type);
$this->tableInfo[$name] = $tmptype[0];
$c.='"'.$name.'"=>'.'"'.$tmptype[0].'",'."\n";
}
$c.=");\n?>";
fwrite($f,$c);
fclose($f);
}
}
}
return true;
}
//组装Insert SQL语句 数据数组 条件 是否检查字段类型
function get_Insert($indata,$where="",$ischeck="")
{
$this->_set_DbTableFile();
if (!is_bool($ischeck))
{
//echo "user table info ";
$ischeck = $this->ischeckDbInfo;
}
if($ischeck) //需要检查字段属性
{
$sql1 = "insert into ".$this->initTableName."(";
$sql2 = " values(";
//print_r($data);
while(list($key,$val)=each($indata))
{
$re = $this->_check_Data($key,$val);
if(!$re)
{
return false;
}
else
{
$sql1.=$key.",";
if($re=="s")
{
$sql2.="'".$val."',";
}
else
{
$sql2.=$val.",";
}
}
}
$sql1 = substr($sql1,0,-1);
$sql1.=")";
$sql2 = substr($sql2,0,-1);
$sql2.=")";
}
else
{
//组装SQL语句
$sql1 = "insert into ".$this->initTableName."(";
$sql2 = " values(";
while(list($key,$val)=each($indata))
{
$sql1.=$this->_getFieldName($key).",";
$sql2.=$this->_get_FieldValues($key,$val).",";
}
$sql1 = substr($sql1,0,-1);
$sql1.=")";
$sql2 = substr($sql2,0,-1);
$sql2.=")";
}
if ($where!="")
{
return $sql1.$sql2." where ".$where;
}
else
{
return $sql1.$sql2;
}
}
//组装Update
function get_Update($indata,$where="",$ischeck="")
{
$this->_set_DbTableFile();
if (!is_bool($ischeck))
{
//echo "user table info ";
$ischeck = $this->ischeckDbInfo;
}
if($ischeck) //需要检查字段属性
{
$sql1 = "update ".$this->initTableName." set ";
//print_r($data);
while(list($key,$val)=each($indata))
{
$re = $this->_check_Data($key,$val);
if(!$re)
{
return false;
}
else
{
$sql1.=$key."=";
if($re=="s")
{
$sql1.="'".$val."',";
}
else
{
$sql1.=$val.",";
}
}
}
$sql1 = substr($sql1,0,-1);;
}
else
{
//组装SQL语句
$sql1 = "update ".$this->initTableName." set";
while(list($key,$val)=each($indata))
{
$sql1.=$this->_getFieldName($key)."=";
$sql1.=$this->_get_FieldValues($key,$val).",";
}
$sql1 = substr($sql1,0,-1);
}
if ($where!="")
{
return $sql1." where ".$where;
}
else
{
return $sql1;
}
}
//不根据表真实属性分析时 拆字段名 i是数字 s是字符 返回VALUES
function _get_FieldValues($key,$val)
{
$key = substr($key,0,1);
switch ($key)
{
case "i":
return $val;
break;
case "s":
return "'".$val."'";
break;
default:
return "'".$val."'";
break;
}
}
//不根据表真实属性分析时 返回真实字段名
function _getFieldName($key)
{
return substr($key,2);
}
//检查数据是否符合字段要求 成功返回TRUE失败返回FALSE
function _check_Data($key,$val)
{
if(!$this->_get_DbTableInfo())
{
return false;
}
else
{
if (!isset($this->tableInfo[$key]))
{
$this->reErrorCode = 003;
$this->reErrorInfo = "字段名不存在";
return false;
}
else
{
switch ($this->tableInfo[$key])
{
case "string":
if(!is_string($val))
{
$this->reErrorCode = 004;
$this->reErrorInfo = "字段 $key 值 $val 不是要求的 ".$this->tableInfo[$key]."属性";
return false;
}
return "s";
break;
case "int":
if(ereg ("[^0-9]",$val))
{
$this->reErrorCode = 004;
$this->reErrorInfo = "字段 $key 值 $val 不是要求的 ".$this->tableInfo[$key]." 属性";
return false;
}
return "i";
break;
default:
return "s";
break;
}
}
}
}
//多表联合Select
function get_ManyTableSelect($indata,$where="",$type=false)
{
$sql1 = "select ";
$sql2 =" from ";
if (!$type) //简单方式
{
while(list($table,$fields)=each($indata))
{
$sql2.=$table.",";
while(list($field,$val)=each($fields))
{
$sql1.="$table.$field as ".$table."_".$field.",";
}
}
}
else
{
while(list($table,$val)=each($indata))
{
$this->initTableName = $table;
$sql2.="$table,";
$this->_set_DbTableFile();
echo $this->initdBInfoTBPath;
if($this->_get_DbTableInfo())
{
//print_r($this->tableInfo);
while(list($field,$val)=each($this->tableInfo))
{
$sql1.=$this->initTableName.".$field as ".$this->initTableName."_".$field.",";
}
}
}
}
$sql1 = substr($sql1,0,-1);
$sql2 = substr($sql2,0,-1);
if ($where!="")
{
return $sql1.$sql2." where ".$where;
}
else
{
return $sql1.$sql2;
}
}
}
?>
|
|