示例#1
0
        public async Task <T> GetAsync()
        {
            SqlProvider.FormatGet(this.FieldAnyExpression);
            SetSql();
            var result = await QueryFirstAsync(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            return(result);
        }
示例#2
0
 public TReturn Get <TReturn>(bool where, Expression <Func <T, TReturn> > trueSelect, Expression <Func <T, TReturn> > falseSelect)
 {
     if (where)
     {
         SqlProvider.Context.Set.SelectExpression = trueSelect;
     }
     else
     {
         SqlProvider.Context.Set.SelectExpression = falseSelect;
     }
     SqlProvider.FormatGet <T>();
     return(DbCon.QueryFirst_1 <TReturn>(SqlProvider, DbTransaction));
 }
        /// <summary>
        /// 反射执行需要指向T类型的函数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlProvider"></param>
        /// <param name="methodName"></param>
        public void FormatSend <T>(SqlProvider sqlProvider, string methodName)
        {
            switch (methodName)
            {
            case "Count":
            {
                sqlProvider.FormatCount();
            }
            break;

            case "Sum":
            {
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                sqlProvider.FormatSum(lambda);
            }
            break;

            case "Get":
            {
                //加上自定义实体返回
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                this.ReturnType = lambda.ReturnType;
                sqlProvider.Context.Set.SelectExpression = lambda;
                sqlProvider.FormatGet <T>();
            }
            break;

            case "ToList":
            {
                //加上自定义实体返回
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                this.ReturnType = lambda.ReturnType;
                sqlProvider.Context.Set.SelectExpression = lambda;
                sqlProvider.FormatToList <T>();
            }
            break;

            default:
                throw new DapperExtensionException("the expression is no support this function");
            }
            //得到解析的sql和param对象
            string sql   = sqlProvider.SqlString;
            var    param = ToSubqueryParam(sqlProvider.Params, ref sql);

            _sqlCmd.Append(sql);
            this.Param.AddDynamicParams(param);
        }
示例#4
0
 public T Get()
 {
     SqlProvider.FormatGet <T>();
     return(DbCon.QueryFirstOrDefaults <T>(SqlProvider.SqlString, SqlProvider.Params, DbTransaction, SqlProvider.ProviderOption));
 }
示例#5
0
 public async Task <T> GetAsync()
 {
     SqlProvider.FormatGet <T>();
     return(await DbCon.QueryFirstOrDefaultAsyncs <T>(SqlProvider, DbTransaction));
 }
示例#6
0
 public TReturn Get <TReturn>(Expression <Func <T, TReturn> > select)
 {
     SqlProvider.Context.Set.SelectExpression = select;
     SqlProvider.FormatGet <T>();
     return(DbCon.QueryFirst_1 <TReturn>(SqlProvider, DbTransaction));
 }
示例#7
0
 public TSource Get <TSource>()
 {
     SqlProvider.FormatGet <T>();
     return(DbCon.QueryFirstOrDefaults <TSource>(SqlProvider, DbTransaction));
 }
示例#8
0
 public async Task <TReturn> GetAsync <TReturn>(Expression <Func <T, TReturn> > select)
 {
     SqlProvider.Context.Set.SelectExpression = select;
     SqlProvider.FormatGet <T>();
     return(await DbCon.QueryFirst_1Async <TReturn>(SqlProvider, DbTransaction));
 }
示例#9
0
        /// <summary>
        /// 反射执行需要指向T类型的函数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlProvider"></param>
        /// <param name="methodName"></param>
        public void FormatSend <T>(QuerySet <T> querySet, string methodName)
        {
            SqlProvider sqlProvider = querySet.SqlProvider;

            //写入重新生成后的条件
            if (WhereExpression != null && WhereExpression.Any())
            {
                querySet.WhereExpressionList.AddRange(WhereExpression);
            }
            //因为表达式的原因,递归获取连表默认会倒序
            if (sqlProvider.JoinList.Any())
            {
                sqlProvider.JoinList.Reverse();
            }
            switch (methodName)
            {
            case "Count":
            {
                sqlProvider.FormatCount();
            }
            break;

            case "Sum":
            {
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                sqlProvider.FormatSum(lambda);
            }
            break;

            case "Min":
            {
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                sqlProvider.FormatMin(lambda);
            }
            break;

            case "Max":
            {
                var lambda = this.expression.Arguments[0].GetLambdaExpression();
                sqlProvider.FormatMax(lambda);
            }
            break;

            case "Get":
            {
                LambdaExpression lambda = default(LambdaExpression);
                if (this.expression.Arguments.Count == 1)
                {
                    lambda          = this.expression.Arguments[0].GetLambdaExpression();
                    this.ReturnType = lambda.ReturnType;
                }
                else if (this.expression.Arguments.Count == 0)                                //无自定义列表返回
                {
                    lambda          = null;
                    this.ReturnType = this.expression.Method.ReturnType;
                }
                else
                {
                    //带if判断
                    if (this.expression.Arguments[0].ToConvertAndGetValue().Equals(true))
                    {
                        lambda = this.expression.Arguments[1].GetLambdaExpression();
                    }
                    else
                    {
                        lambda = this.expression.Arguments[2].GetLambdaExpression();
                    }
                    this.ReturnType = lambda.ReturnType;
                }
                sqlProvider.Context.Set.SelectExpression = lambda;
                sqlProvider.FormatGet <T>();
            }
            break;

            case "ToList":
            {
                LambdaExpression lambda = default(LambdaExpression);
                if (this.expression.Arguments.Count == 1)
                {
                    lambda          = this.expression.Arguments[0].GetLambdaExpression();
                    this.ReturnType = lambda.ReturnType;
                }
                else if (this.expression.Arguments.Count == 0)                                //无自定义列表返回
                {
                    lambda          = null;
                    this.ReturnType = this.expression.Method.ReturnType.GenericTypeArguments[0];
                }
                else
                {
                    //带if判断
                    if (this.expression.Arguments[0].ToConvertAndGetValue().Equals(true))
                    {
                        lambda = this.expression.Arguments[1].GetLambdaExpression();
                    }
                    else
                    {
                        lambda = this.expression.Arguments[2].GetLambdaExpression();
                    }
                    this.ReturnType = lambda.ReturnType;
                }
                sqlProvider.Context.Set.SelectExpression = lambda;
                sqlProvider.FormatToList <T>();
            }
            break;

            default:
                throw new DapperExtensionException("Kogel.Dapper.Extension中子查询不支持的扩展函数");
            }
            //得到解析的sql和param对象
            string sql   = sqlProvider.SqlString;
            var    param = ToSubqueryParam(sqlProvider.Params, ref sql);

            _sqlCmd.Append(sql);
            this.Param.AddDynamicParams(param);
        }
示例#10
0
文件: Query.cs 项目: Nilker/Account
 public T Get()
 {
     SqlProvider.FormatGet(this.FieldAnyExpression);
     SetSql();
     return(QrFd(SqlProvider.SqlString, SqlProvider.Params, DbTransaction));
 }
        public T Get()
        {
            SqlProvider.FormatGet();

            return(DbCon.QuerySingle <T>(SqlProvider.SqlString, SqlProvider.Params));
        }