Exemplo n.º 1
0
        /// <summary>
        /// 生成 In 从句。
        /// </summary>
        /// <param name="fieldName">字段名称。</param>
        /// <param name="values">字段值的集合。</param>
        /// <param name="parameters">查询参数</param>
        /// <returns></returns>
        public string GenerateInClause <TEntity>(string fieldName, IEnumerable <object> values, DynamicParameters parameters)
        {
            if (fieldName.IsNullOrWhiteSpace() || values.IsNullOrEmpty())
            {
                return(null);
            }

            string parameterName = $"p{parameters.ParameterNames.Count() + 1}";

            parameters.Add(parameterName, values);
            return($"{_runtime.DelimitIdentifier(typeof(TEntity), MappingStrategyParser.Parse(fieldName))} IN {_runtime.DelimitParameter(typeof(TEntity), parameterName)}");
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据字段名和值生成 SET 子句(不包含 SET 字符)。
        /// </summary>
        /// <param name="fieldsAndValues">要生成 SET 子句的字段名和字段值。</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public string GenerateSetSegments <TEntity>(IEnumerable <KeyValuePair <string, object> > fieldsAndValues, DynamicParameters parameters)
        {
            Guard.ArgumentNotNull(fieldsAndValues, nameof(fieldsAndValues));

            List <string> setList = new List <string>();

            foreach (var kp in fieldsAndValues)
            {
                string parameterName = $"p{parameters.ParameterNames.Count() + 1}";
                setList.Add($"{_runtime.DelimitIdentifier(typeof(TEntity), MappingStrategyParser.Parse(kp.Key))} = {_runtime.DelimitParameter(typeof(TEntity), parameterName)}");
                parameters.Add($"{parameterName}", kp.Value);
            }
            return(setList.ToArrayString(", "));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 生成实体主键的  Where 从句(不包含 Where 字符)。
        /// </summary>
        /// <param name="entity">实体类型</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public string GeneratePrimaryKeysWhereClause <TEntity>(TEntity entity, DynamicParameters parameters)
        {
            Guard.ArgumentNotNull(parameters, nameof(parameters));
            Guard.ArgumentNotNull(parameters, nameof(parameters));

            var           keys      = _runtime.GetMetadata(typeof(TEntity)).Fields.Where(f => f.IsKey).ToArray();
            List <string> whereList = new List <string>();

            foreach (var k in keys)
            {
                string parameterName = $"p{parameters.ParameterNames.Count() + 1}";
                whereList.Add($"{_runtime.DelimitIdentifier(typeof(TEntity), MappingStrategyParser.Parse(k.Name))} = {_runtime.DelimitParameter(typeof(TEntity), parameterName)}");
                parameters.Add(parameterName, k.Field.GetValue(entity));
            }
            return(whereList.ToArrayString(" AND "));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 生成 Order By 子句。
        /// </summary>
        /// <param name="sortOptions">排序选项。</param>
        /// <returns></returns>
        public string GenerateOrderBy <TEntity>(SortOptions sortOptions)
        {
            Guard.ArgumentNotNull(sortOptions, nameof(sortOptions));

            string fields = sortOptions.FieldSortDictionary
                            .Select(fieldSort => $"{_runtime.DelimitIdentifier(typeof(TEntity), MappingStrategyParser.Parse(fieldSort.Key))} " + (fieldSort.Value == SortOrder.Descending ? "desc" : "asc"))
                            .ToArrayString(", ");
            string sql = $" ORDER BY {fields}";

            return(sql);
        }