예제 #1
0
        // init caller
        static void InitNameValueCollectionCaller()
        {
            Caller <NameValueCollection> .GetBuilder = (ProxyData proxyData, SqlConfig setting, NameValueCollection rawData, OperateType ot) =>
            {
                ISelectBuilder builder = new SelectBuilder();
                var            data    = new StrategyData <NameValueCollection>(proxyData, setting, rawData);

                data.CParser = new SqlServerCParser(proxyData.DateFormat, proxyData.Culture);
                InitBuilder(builder, data.Setting);

                var    orderBy          = string.IsNullOrWhiteSpace(data.RawData["orderBy"]) ? data.Setting.OrderBy.Default : data.RawData["orderBy"];
                string rowNumberOrderBy = orderBy;
                orderBy = ParseOrderBy(data.Setting, orderBy.Trim(), ref rowNumberOrderBy);

                builder.SetOrderBy(string.Format(" {0} ", orderBy));
                builder.SetRowNumberOrderBy(string.Format(" {0} ", rowNumberOrderBy));

                builder.SetPageSize(string.IsNullOrWhiteSpace(data.RawData["pageSize"]) ? data.Setting.PageSize : Convert.ToInt32(data.RawData["pageSize"]));
                builder.SetPageNumber(string.IsNullOrWhiteSpace(data.RawData["pageNumber"]) ? data.Setting.PageNumber : Convert.ToInt32(data.RawData["pageNumber"]));

                //模糊搜索
                if (!string.IsNullOrEmpty(data.RawData[data.ProxyData.FuzzySearchField] + ""))
                {
                    StringBuilder sbWhere        = new StringBuilder();
                    StringBuilder sbExpressWhere = new StringBuilder();
                    data.Setting.Where.Fields.Where(x => x.FuzzySearch && !string.IsNullOrEmpty(x.SqlExpress)).ToList().ForEach(x =>
                    {
                        if (!string.IsNullOrEmpty(x.DataType) && x.ExpressIgnoreEmpty)
                        {
                            var val = data.CParser.ParseValue(data.RawData[data.ProxyData.FuzzySearchField] + "", x.DataType);
                            if (!string.IsNullOrEmpty(val))
                            {
                                //builder.AddWhere(data.CParser.GetSql(x.Cp, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, true));
                                var retSql = data.CParser.GetSql(x.Cp, x.Name, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, true, x.IsAutoAddQuotes);
                                //sbWhere.AppendFormat((" And (1=1 " + retSql + ")"));
                                sbExpressWhere.AppendFormat(retSql);
                            }
                        }
                        else
                        {
                            //builder.AddWhere(data.CParser.GetSql(x.Cp, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, true));
                            var retSql = data.CParser.GetSql(x.Cp, x.Name, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, true, x.IsAutoAddQuotes);
                            //sbWhere.AppendFormat((" And (1=1 " + retSql + ")"));
                            sbExpressWhere.AppendFormat(retSql);
                        }
                    });
                    if (sbExpressWhere.Length > 0)
                    {
                        builder.AddWhere(sbExpressWhere.ToString());
                    }
                    data.Setting.Where.Fields.Where(x => x.FuzzySearch && string.IsNullOrEmpty(x.SqlExpress)).ToList().ForEach(x =>
                    {
                        if (x.IgnoreEmpty && !string.IsNullOrEmpty(x.DataType))
                        {
                            var val = data.CParser.ParseValue(data.RawData[data.ProxyData.FuzzySearchField] + "", x.DataType);
                            if (!string.IsNullOrEmpty(val))
                            {
                                sbWhere.AppendFormat("{0}", data.CParser.GetSql(x.Cp, x.Name, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, false, x.IsAutoAddQuotes));
                            }
                        }
                        else
                        {
                            sbWhere.AppendFormat("{0}", data.CParser.GetSql(x.Cp, x.Name, x.DbName, data.RawData[data.ProxyData.FuzzySearchField] + "", x.SqlExpress, x.DataType, false, x.IsAutoAddQuotes));
                        }
                    });
                    if (sbWhere.Length > 0)
                    {
                        builder.AddWhere(" And ( 1=2 " + sbWhere + ")");
                    }
                }

                // 单个实体查询语句
                if (ot == OperateType.SingleSelect)
                {
                    if (!string.IsNullOrEmpty(data.Setting.SingleQuery.Select))
                    {
                        builder.SetSelect(data.Setting.SingleQuery.Select);
                    }
                    if (!string.IsNullOrEmpty(data.Setting.SingleQuery.From))
                    {
                        builder.SetFrom(data.Setting.SingleQuery.From);
                    }

                    for (var i = 0; i < data.Setting.SingleQuery.Where.Fields.Count; ++i)
                    {
                        var x = data.Setting.SingleQuery.Where.Fields[i];
                        if (!string.IsNullOrWhiteSpace(data.RawData[x.Name]))
                        {
                            x.NewValue = data.RawData[x.Name];
                        }
                        builder.AddWhere(data.CParser.GetSql(x));
                    }
                }

                // where 条件
                if (ot == OperateType.MutipleSelect)
                {
                    StringBuilder sbWhere = new StringBuilder();
                    data.Setting.Where.Fields.ForEach(x =>
                    {
                        if (!string.IsNullOrWhiteSpace(data.RawData[x.Name]))
                        {
                            x.NewValue = data.RawData[x.Name];
                        }
                        if (x.IsInFrom && !string.IsNullOrEmpty(x.NewValue + ""))
                        {
                            builder.ReplaceFrom(data.CParser.ParameterPrefix + x.DbName, "'" + data.CParser.ParseValue(x.NewValue, x.DataType) + "'");
                        }
                        else if (x.IsInSelect && !string.IsNullOrEmpty(x.NewValue + ""))
                        {
                            builder.ReplaceSelect(data.CParser.ParameterPrefix + x.DbName, "'" + data.CParser.ParseValue(x.NewValue, x.DataType) + "'");
                        }
                        else
                        {
                            // ignore empty
                            if (x.IgnoreValue != null && (x.NewValue + "").Trim().ToLower() == x.IgnoreValue.Trim().ToLower())
                            {
                            }
                            else
                            {
                                if (x.IgnoreEmpty || x.ExpressIgnoreEmpty)
                                {
                                    if (!string.IsNullOrEmpty(x.DataType))
                                    {
                                        var val = data.CParser.ParseValue(x.NewValue, x.DataType);
                                        if (!string.IsNullOrEmpty(val))
                                        {
                                            builder.AddWhere(data.CParser.GetSql(x));
                                        }
                                    }
                                    else if (!string.IsNullOrEmpty(x.NewValue + "") || (!string.IsNullOrEmpty(x.SqlExpress) && !x.ExpressIgnoreEmpty))
                                    {
                                        builder.AddWhere(data.CParser.GetSql(x));
                                    }
                                }
                                else
                                {
                                    builder.AddWhere(data.CParser.GetSql(x));
                                }
                            }
                        }
                    });
                }

                //添加系统数据
                builder.AddParam("system_user_id", data.ProxyData.SystemUserId);
                builder.AddParam("system_datetime", data.ProxyData.SystemDateTime);

                return(builder);
            };
        }