示例#1
0
        public static IParamBuilder CreateInSql(TkDbContext context, IFieldInfo fieldName,
                                                IEnumerable <string> values)
        {
            TkDebug.AssertArgumentNull(fieldName, "fieldName", null);
            TkDebug.AssertArgumentNull(values, "values", null);

            string[] valueArray = values.ToArray();
            if (valueArray.Length == 0)
            {
                return(ParamBuilder.NoResult);
            }

            string seperator = string.Empty;

            switch (fieldName.DataType)
            {
            case TkDataType.String:
                seperator = "\'";
                break;

            case TkDataType.Int:
            case TkDataType.Byte:
            case TkDataType.Short:
            case TkDataType.Double:
            case TkDataType.Long:
                break;

            default:
                TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                            "生成IN SQL语句的字段只支持字符串、整形、浮点三种类型,现在字段{0}类型是{1},不在支持范围",
                                                            fieldName.FieldName, fieldName.DataType), null);
                break;
            }
            var items = from value in valueArray
                        select seperator + StringUtil.EscapeAposString(value) + seperator;
            string insql = string.Join(",", items);
            string field;

            if (context != null)
            {
                field = context.EscapeName(fieldName.FieldName);
            }
            else
            {
                field = fieldName.FieldName;
            }
            string sql = string.Format(ObjectUtil.SysCulture, "{0} IN ({1})",
                                       field, insql);
            InternalParamBuilder builder = new InternalParamBuilder {
                Sql = sql
            };

            return(builder);
        }
示例#2
0
        internal static IParamBuilder InternalCreateSingleSql(TkDbContext context, string fieldName,
                                                              TkDataType type, string oper, string paramName, object fieldValue)
        {
            TkDebug.AssertArgumentNull(context, "context", null);
            TkDebug.AssertArgumentNullOrEmpty(fieldName, "fieldName", null);
            TkDebug.AssertArgumentNullOrEmpty(oper, "oper", null);
            TkDebug.AssertArgumentNullOrEmpty(paramName, "paramName", null);
            TkDebug.AssertArgumentNull(fieldValue, "fieldValue", null);

            InternalParamBuilder builder = new InternalParamBuilder
            {
                Sql = CreateSingleSql(context, fieldName, oper, paramName)
            };

            builder.Parameters.Add(paramName, type, fieldValue);
            return(builder);
        }
示例#3
0
        private static IParamBuilder LinkParamBuilder(IEnumerable <IParamBuilder> builders,
                                                      string linkOperator)
        {
            int j = 0;
            InternalParamBuilder result  = new InternalParamBuilder();
            StringBuilder        builder = new StringBuilder();

            foreach (IParamBuilder item in builders)
            {
                string sql = item.Sql;
                TkDebug.AssertNotNullOrEmpty(sql, string.Format(ObjectUtil.SysCulture,
                                                                "builders参数第{0}个IParamBuilder生成的Sql是空,这是不允许的", j), item);
                if (j > 0)
                {
                    builder.Append(linkOperator);
                }
                builder.Append("(").Append(sql).Append(")");
                //StringUtil.JoinStringItem(builder, i++, "(" + sql + ")", linkOperator);
                DbParameterList itemParams = item.Parameters;
                TkDebug.AssertNotNull(itemParams, string.Format(ObjectUtil.SysCulture,
                                                                "builders参数第{0}个IParamBuilder调用的Parameters返回是空,这是不允许的",
                                                                j), item);
                result.Parameters.Add(itemParams);
                ++j;
            }
            //result.Sql = builder.ToString();
            if (j == 0)
            {
                return(null);
            }
            else if (j == 1)
            {
                return(builders.First());
            }
            else
            {
                result.Sql = builder.ToString();
                return(result);
            }
        }