Example #1
0
        static T ExecuteElement <T>(
            Query query,
            IDataContextEx dataContext,
            Mapper <T> mapper,
            Expression expression,
            object[]       ps)
        {
            using (var runner = dataContext.GetQueryRunner(query, 0, expression, ps))
                try
                {
                    mapper.QueryRunner = runner;

                    using (var dr = runner.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var value = mapper.Map(runner, dr);
                            runner.RowsCount++;
                            return(value);
                        }
                    }

                    return(Array <T> .Empty.First());
                }
                finally
                {
                    mapper.QueryRunner = null;
                }
        }
Example #2
0
        static IEnumerable <T> ExecuteQuery <T>(
            Query query,
            IDataContextEx dataContext,
            Mapper <T> mapper,
            Expression expression,
            object[]       ps,
            int queryNumber)
        {
            using (var runner = dataContext.GetQueryRunner(query, queryNumber, expression, ps))
                try
                {
                    mapper.QueryRunner = runner;

                    using (var dr = runner.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            yield return(mapper.Map(runner, dr));

                            runner.RowsCount++;
                        }
                    }
                }
                finally
                {
                    mapper.QueryRunner = null;
                }
        }
Example #3
0
 static async Task <object> NonQueryQueryAsync(
     Query query,
     IDataContextEx dataContext,
     Expression expression,
     object[]          ps,
     CancellationToken cancellationToken)
 {
     using (var runner = dataContext.GetQueryRunner(query, 0, expression, ps))
         return(await runner.ExecuteNonQueryAsync(cancellationToken));
 }
Example #4
0
        static int QueryQuery2(Query query, IDataContextEx dataContext, Expression expr, object[] parameters)
        {
            using (var runner = dataContext.GetQueryRunner(query, 0, expr, parameters))
            {
                var n = runner.ExecuteScalar();

                if (n != null)
                {
                    return(0);
                }

                runner.QueryNumber = 1;

                return(runner.ExecuteNonQuery());
            }
        }
Example #5
0
        static async Task <T> ExecuteElementAsync <T>(
            Query query,
            IDataContextEx dataContext,
            Mapper <object> mapper,
            Expression expression,
            object[]          ps,
            CancellationToken cancellationToken)
        {
            using (var runner = dataContext.GetQueryRunner(query, 0, expression, ps))
            {
                Func <IDataReader, object> m = dr => mapper.Map(runner, dr);

                try
                {
                    mapper.QueryRunner = runner;

                    var dr = await runner.ExecuteReaderAsync(cancellationToken);

                    var item = default(T);
                    var read = false;

                    await dr.QueryForEachAsync(
                        m,
                        r =>
                    {
                        read = true;
                        item = dataContext.MappingSchema.ChangeTypeTo <T>(r);
                        runner.RowsCount++;
                        return(false);
                    },
                        cancellationToken);

                    if (read)
                    {
                        return(item);
                    }

                    return(Array <T> .Empty.First());
                }
                finally
                {
                    mapper.QueryRunner = null;
                }
            }
        }
Example #6
0
        static async Task <object> QueryQuery2Async(
            Query query,
            IDataContextEx dataContext,
            Expression expr,
            object[]          parameters,
            CancellationToken cancellationToken)
        {
            using (var runner = dataContext.GetQueryRunner(query, 0, expr, parameters))
            {
                var n = await runner.ExecuteScalarAsync(cancellationToken);

                if (n != null)
                {
                    return(0);
                }

                runner.QueryNumber = 1;

                return(await runner.ExecuteNonQueryAsync(cancellationToken));
            }
        }
Example #7
0
        static async Task ExecuteQueryAsync <T>(
            Query query,
            IDataContextEx dataContext,
            Mapper <T> mapper,
            Expression expression,
            object[]                      ps,
            int queryNumber,
            Func <T, bool> func,
            Func <Expression, object[], int> skipAction,
            Func <Expression, object[], int> takeAction,
            CancellationToken cancellationToken)
        {
            using (var runner = dataContext.GetQueryRunner(query, queryNumber, expression, ps))
            {
                Func <IDataReader, T> m = dr => mapper.Map(runner, dr);

                runner.SkipAction = skipAction != null ? () => skipAction(expression, ps) : null as Func <int>;
                runner.TakeAction = takeAction != null ? () => takeAction(expression, ps) : null as Func <int>;

                try
                {
                    mapper.QueryRunner = runner;

                    var dr = await runner.ExecuteReaderAsync(cancellationToken);

                    await dr.QueryForEachAsync(m, r =>
                    {
                        var b = func(r);
                        runner.RowsCount++;
                        return(b);
                    }, cancellationToken);
                }
                finally
                {
                    mapper.QueryRunner = null;
                }
            }
        }
Example #8
0
 static int NonQueryQuery(Query query, IDataContextEx dataContext, Expression expr, object[] parameters)
 {
     using (var runner = dataContext.GetQueryRunner(query, 0, expr, parameters))
         return(runner.ExecuteNonQuery());
 }