动态创建ActiveRecord条件的查询 MyQuery

 在CMS中。我们经常会按一定的条件来进行搜索。如果用户没有选择这个条件的话,我们就不能将它放到sql中
也许我们可以用自己拼装sql语句的方式很好的实现这种查询。然后再前面过滤掉一些危险的参数
但是参数过滤有一个不好的地方是。会把一些信息给过滤掉了。

在castle ActiveRecord里面我们最基本的查询都是靠传参的形式了。
ScalarQuery<xxInfo> query = new ScalarQuery<xxInfo>(typeof(xxInfo), hql,ID);
如果用传参的话我们感觉在 ActiveRecord里会比较麻烦。写起来不顺

于是我们自己写了一简单的类来处下这种情况(不清楚它是否提供类似的处理类,方法)也修正了一下bug
/**/ ///风云 lovebanyi.cnblogs.com 
public   class  MyQuery < T >  : SimpleQuery < T >
    
{
        
public MyQuery(string query)
            : 
base(query)
        
{

        }

        
private int i = 0;
        
public void AddCondition(string porperty, string @operator, object parm)
        
{
            
if (i == 0)
            
{
                
base.Query += " where " + porperty + " " + @operator + " ?";
            }

            
else
            
{
                
base.Query +=  " and " + porperty + " " + @operator + " ?";
            }

            
base.AddModifier(new Castle.ActiveRecord.Queries.Modifiers.QueryParameter(i++, parm));
        }

        
public void AddCondition(string condition)
        
{
            
if (i == 0)
            
{
                
base.Query += " where " + condition;
            }

            
else
            

                
base.Query += " and "+ condition;
            }

        }

        
public void AddCondition(string condition, object parm)
        
{
            AddCondition(condition);
            
base.AddModifier(new Castle.ActiveRecord.Queries.Modifiers.QueryParameter(i++, parm));
        }


        
public void AddCondition(string condition, List<object> parms)
        
{
            AddCondition(condition);
            
for (int j = 0; j < parms.Count; j++)
            
{
                
base.AddModifier(new Castle.ActiveRecord.Queries.Modifiers.QueryParameter(i++, parms[j]));
            }

        }

        
private System.Text.RegularExpressions.Regex regCount = new System.Text.RegularExpressions.Regex("^select(.*?)from", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
        
        
protected override string PrepareQueryForCount(string countQuery)
        
{
            
if (regCount.IsMatch(countQuery))
            
{
                countQuery 
= regCount.Replace(countQuery, "select count(*) from");
            }

            
else
            
{
                countQuery 
= "select count(*) " + countQuery;
            }

            
return countQuery;
            
        }
V2 新加一个代码。这样你在返回MyQuery<int>的时候不会出错
         public  MyQuery(Type targetType,  string  query)
            : 
base (targetType, query)
        

        }

使用 (写在entiy的类中)
  string  hql  =   " from Supplier " ;
            MyQuery
< Supplier >  query  =   new  MyQuery < Supplier > (hql);
            query.SetQueryRange(start, maxResults);
            query.AddCondition(
" Name " , " like " , " % " + name + " % " );
            query.AddCondition(
" Number " , " = " , " 0592 " );
return  query.Execute();
当然你可以对操作符再次进行一些处理。更好的防止写错和加快速度


另一个小例子 /Files/lovebanyi/MyQueryExample.txt
v0.2 http://files.cnblogs.com/lovebanyi/myqueryV0.2.txt

转载于:https://www.cnblogs.com/lovebanyi/archive/2007/07/24/829654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值