Beispiel #1
0
        /// <summary>
        /// 生产查询条数的SQL
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <returns></returns>
        private DapperExecuteSql GenerateCountSql(QueryFilter filter)
        {
            var executeSql = new DapperExecuteSql();
            var whereSeg   = Context.Runtime.SqlGenerator.GenerateFilter <T>(filter, executeSql.Parameters);

            executeSql.Sql = string.IsNullOrWhiteSpace(whereSeg)
                ? $"SELECT COUNT(*) FROM {Context.Runtime.DelimitIdentifier(EntityType, _metadata.TableName)}"
                : $"SELECT COUNT(*) FROM {Context.Runtime.DelimitIdentifier(EntityType, _metadata.TableName)} WHERE {whereSeg}";
            return(executeSql);
        }
Beispiel #2
0
        /// <summary>
        /// 生成分页的SQL
        /// </summary>
        /// <param name="pageIndex">页码(从0开始)</param>
        /// <param name="pageSize">每页展示量</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sortOptions"></param>
        /// <returns></returns>
        private DapperExecuteSql GeneratePaginationSql(int pageIndex, int pageSize, QueryFilter filter, SortOptions sortOptions)
        {
            DapperExecuteSql executeSql = new DapperExecuteSql();

            sortOptions = sortOptions ?? new SortOptions(_metadata.Fields.First(f => f.IsKey).Name);
            var    segments  = Context.Runtime.GetCrudSegments(EntityType);
            string selectSeg = segments.SelectSql;
            string whereSeg  = filter != null?Context.Runtime.SqlGenerator.GenerateFilter <T>(filter, executeSql.Parameters) : null;

            string orderSeg = Context.Runtime.SqlGenerator.GenerateOrderBy <T>(sortOptions);

            executeSql.Sql = DataSource.DatabaseProvider.BuildPaginationTSql(pageIndex, pageSize, selectSeg, orderSeg, whereSeg);
            return(executeSql);
        }
Beispiel #3
0
        /// <summary>
        /// 生成In查询的SQL
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="values">需要包含的值</param>
        /// <returns></returns>
        private DapperExecuteSql GenerateQueryInSql(string field, IEnumerable <object> values)
        {
            var executeSql = new DapperExecuteSql();
            var segments   = Context.Runtime.GetCrudSegments(EntityType);

            executeSql.Sql = segments.SelectSql;
            var inSeg = Context.Runtime.SqlGenerator.GenerateInClause <T>(field, values, executeSql.Parameters);

            if (string.IsNullOrWhiteSpace(inSeg))
            {
                return(executeSql);
            }
            executeSql.Sql = $"{executeSql.Sql} WHERE {inSeg}";
            return(executeSql);
        }
Beispiel #4
0
        /// <summary>
        /// 生成查询的SQL
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <returns></returns>
        private DapperExecuteSql GenerateQuerySql(QueryFilter filter)
        {
            var executeSql = new DapperExecuteSql();
            var segments   = Context.Runtime.GetCrudSegments(EntityType);

            executeSql.Sql = segments.SelectSql;
            var whereSeg = Context.Runtime.SqlGenerator.GenerateFilter <T>(filter, executeSql.Parameters);

            if (string.IsNullOrWhiteSpace(whereSeg))
            {
                return(executeSql);
            }
            executeSql.Sql = $"{executeSql.Sql} WHERE {whereSeg}";
            return(executeSql);
        }
Beispiel #5
0
        /// <summary>
        /// 生成根据主键更新的SQL
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="fieldsToUpdate">需要更新的列</param>
        /// <returns></returns>
        private DapperExecuteSql GenerateUpdateSql(T entity, IEnumerable <KeyValuePair <String, object> > fieldsToUpdate)
        {
            var executeSql = new DapperExecuteSql();

            if (!fieldsToUpdate.Any())
            {
                throw new ArgumentException($"{typeof(IRepository<>).FullName}.{nameof(Update)} 方法的 {nameof(fieldsToUpdate)} 参数不可为 null 或空字典,如更新整个实体,考虑使用不包含 {nameof(fieldsToUpdate)} 参数的 {nameof(Update)} 重载方法。");
            }

            string tableName = Context.Runtime.GetMetadata(EntityType).TableName;
            string setSeg    = Context.Runtime.SqlGenerator.GenerateSetSegments <T>(fieldsToUpdate, executeSql.Parameters);
            string whereSeg  = Context.Runtime.SqlGenerator.GeneratePrimaryKeysWhereClause(entity, executeSql.Parameters);

            if (string.IsNullOrWhiteSpace(whereSeg))
            {
                throw new NotSupportedException("不支持没有where条件的UPDATE语句");
            }

            executeSql.Sql = $"UPDATE {Context.Runtime.DelimitIdentifier(EntityType, tableName)} SET {setSeg} WHERE {whereSeg}";
            return(executeSql);
        }