SQL模版
框架中提供了上述选择器
,条件语句
,联表
等,基本覆盖了所有sql语法,但可能还有部分生僻的用法无法被实现, 于是这里提供了一种SQL模版的使用方式,支持用户自定义SQL语句,但并不推荐用户使用
,如果一定要使用的话,请务必自己做好防SQL注入
这里提供了两种方式,select
(查询,返回数据),以及command
(执行,返回bool)
方法会自动替换:where
,:table
,:order
,:group
,:addition
字段
// select * from `DATABASE`.`TABLE` WHERE ... $result =$this ->userDAO ->select ('select * from :table WHERE ...;' );// update `DATABASE`.`TABLE` `user` set name = 'test' WHERE `user`.`id` = 10 AND type = 2 $result =$this ->userDAO ->filter (array ('id' =>10))- ->
command ("update :table set name = 'test' WHERE :where AND type = 2;" );// select id,sum(`cash`) as 'cash' from `DATABASE`.`TABLE` WHERE `id`>10 GROUP BY `type` HAVING `cash`>=100 ORDER BY `id` desc; $result =$this ->userDAO ->filter (array ('>' =>array ('id' =>10)))- ->
group (array ('type' ))->having (array ('>=' =>array ('cash' =>100)))->order (array ('id' =>'desc' ))- ->
addition (array ('sum' =>array ('cash' =>'cash' )))- ->
select ('select id,:addition from :table WHERE :where :group :order;' );
另外还可以添加一些自定义变量,这些变量会自动进行sql转义
,防止sql注入
其中键值的替换符为;
,例如;key
,值的替换符为:
,例如:value
// select `name` from `DATABASE`.`TABLE` WHERE `name`=2 $result =$this ->userDAO ->select ('select ;key from :table WHERE ;key=:value;' ,array ('key' =>'name' ,'value' =>2));
同时替换内容也可以是数组,系统会自动替换为以,
连接的字符串
// select `id`,`name` from `DATABASE`.`TABLE` WHERE `name` in (1,2,3,'test') $result =$this ->userDAO ->select ('select ;fields from :table WHERE ;key in (:value);' ,array ('key' =>'name' ,'value' =>array (1,2,3,'test' ),'fields' =>array ('id' ,'name' )));
以上替换方式都会进行SQL转义
,建议用户使用模版替换,而不要自己将变量放入SQL语句中,防止SQL注入