//创建一个查询对象构造器 private ISelectBuilder CreateBuilder(DbContext db, ExecuteModel executeModel) { //获取当前类名 var tableName = GetTableName(executeModel.Entity.GetType()); //查询器 ISelectBuilder selectBuilder = db.Select(tableName); //获取条件sql var whereSql = GetConitionsql(executeModel.Conditions); //添加SQL条件 selectBuilder.Where(whereSql); executeModel.Conditions?.ForEach(p => { //先设置参数,组装SQL if (p.Oper == Operational.In || p.Oper == Operational.NotIn) { //In 或 not in 通过设置参数方式 GetParams(p.Key, p.Value, selectBuilder); } else { selectBuilder.Parameters(p.Key, p.Value); } }); if (executeModel.OrderColumn.IsNotNullOrEmpty()) {//如果包括了排序字段,则使用排序 selectBuilder.OrderBy(executeModel.OrderColumn); }//end if //返回组装的对象 return(selectBuilder); }
public void SqlBuilderSample(FeiniuBus.DynamicQuery dynamicQuery, ISelectBuilder selectBuilder) { var sb = new StringBuilder(); var mappings = new SqlFieldMappings(); mappings.Map("Guest", "t1.Guest") .Map("Address", "t1.Address") .Map("Disabled", "t1.Disabled") .Map("Amout", "t1.Amout") .Map("Price", "t1.Price") .Map("Drink", "t1.Drink") .Map("Count", "t1.Count") .Map("Total", "t1.Total") .Map("Url", "t1.Url"); selectBuilder.Mapping(mappings); selectBuilder.Where(dynamicQuery.ParamGroup); var whereClause = selectBuilder.BuildWhere(); var orderbyClause = selectBuilder.OrderBy(dynamicQuery.Order); System.Console.WriteLine(whereClause); System.Console.WriteLine(orderbyClause); }