public static IEnumerable <dynamic> Query(this IDbConnection connection, FormattableString command, CommandConfiguration config = null)
        {
            using (var cmd = CommandFactory.CreateCommand(connection, command.Format, config))
            {
                if (command.ArgumentCount > 0)
                {
                    ParameterFactory.AddParameters(cmd, command);
                }

                using (var reader = cmd.ExecuteReader(s_defaultCommandBehavior))
                {
                    var mapper = DataMapperFactory.GetMapper(cmd, reader);
                    while (reader.Read())
                    {
                        yield return(mapper(reader));
                    }
                }
            }
        }
Example #2
0
        public static IEnumerable <T> Query <T>(this IDbConnection connection, string command, object parameters = null, CommandConfiguration config = null) where T : new()
        {
            using (var cmd = CommandFactory.CreateCommand(connection, command, config))
            {
#endif
                if (parameters != null)
                {
                    ParameterFactory.AddParameters(cmd, parameters);
                }

                using (var reader = cmd.ExecuteReader(s_defaultCommandBehavior))
                {
                    var mapper = DataMapperFactory.GetMapper <T>(cmd, reader);
                    while (reader.Read())
                    {
                        yield return((T)mapper(reader));
                    }
                }
            }
        }
        public static T QuerySingle <T>(this IDbConnection connection, FormattableString command, CommandConfiguration config = null) where T : new()
        {
            using (var cmd = CommandFactory.CreateCommand(connection, command.Format, config))
            {
                if (command.ArgumentCount > 0)
                {
                    ParameterFactory.AddParameters(cmd, command);
                }

                using (var reader = cmd.ExecuteReader(s_defaultCommandBehavior | CommandBehavior.SingleRow))
                {
                    var mapper = DataMapperFactory.GetMapper <T>(cmd, reader);
                    if (reader.Read())
                    {
                        return((T)mapper(reader));
                    }
                    return(default(T));
                }
            }
        }
Example #4
0
        public static dynamic QuerySingle(this IDbConnection connection, string command, object parameters = null, CommandConfiguration config = null)
        {
            using (var cmd = CommandFactory.CreateCommand(connection, command, config))
            {
#endif
                if (parameters != null)
                {
                    ParameterFactory.AddParameters(cmd, parameters);
                }

                using (var reader = cmd.ExecuteReader(s_defaultCommandBehavior | CommandBehavior.SingleRow))
                {
                    var mapper = DataMapperFactory.GetMapper(cmd, reader);
                    if (reader.Read())
                    {
                        return(mapper(reader));
                    }
                    return(null);
                }
            }
        }
Example #5
0
        public static async Task <dynamic> QuerySingleAsync(this IDbConnection connection, FormattableString command, CommandConfiguration config = null)
        {
            using (var cmd = (DbCommand)CommandFactory.CreateCommand(connection, command.Format, config))
            {
                var cancelToken = CommandFactory.GetCancellationToken(config);

                if (command.ArgumentCount > 0)
                {
                    ParameterFactory.AddParameters(cmd, command);
                }

                using (var reader = await cmd.ExecuteReaderAsync(s_defaultCommandBehavior | CommandBehavior.SingleRow, cancelToken).ConfigureAwait(false))
                {
                    var mapper = DataMapperFactory.GetMapper(cmd, reader);
                    if (await reader.ReadAsync(cancelToken).ConfigureAwait(false))
                    {
                        return(mapper(reader));
                    }
                    return(null);
                }
            }
        }
Example #6
0
        public static async Task <IEnumerable <T> > QueryAsync <T>(this IDbConnection connection, FormattableString command, CommandConfiguration config = null) where T : new()
        {
            using (var cmd = (DbCommand)CommandFactory.CreateCommand(connection, command.Format, config))
            {
                var cancelToken = CommandFactory.GetCancellationToken(config);

                if (command.ArgumentCount > 0)
                {
                    ParameterFactory.AddParameters(cmd, command);
                }

                using (var reader = await cmd.ExecuteReaderAsync(s_defaultCommandBehavior, cancelToken).ConfigureAwait(false))
                {
                    var results = new List <T>();
                    var mapper  = DataMapperFactory.GetMapper <T>(cmd, reader);
                    while (await reader.ReadAsync(cancelToken).ConfigureAwait(false))
                    {
                        results.Add((T)mapper(reader));
                    }
                    return(results);
                }
            }
        }
Example #7
0
        public static async Task <T> QuerySingleAsync <T>(this IDbConnection connection, string command, object parameters = null, CommandConfiguration config = null) where T : new()
        {
            using (var cmd = (DbCommand)CommandFactory.CreateCommand(connection, command, config))
            {
#endif
                var cancelToken = CommandFactory.GetCancellationToken(config);

                if (parameters != null)
                {
                    ParameterFactory.AddParameters(cmd, parameters);
                }

                using (var reader = await cmd.ExecuteReaderAsync(s_defaultCommandBehavior | CommandBehavior.SingleRow, cancelToken).ConfigureAwait(false))
                {
                    var mapper = DataMapperFactory.GetMapper <T>(cmd, reader);
                    if (await reader.ReadAsync(cancelToken).ConfigureAwait(false))
                    {
                        return((T)mapper(reader));
                    }
                    return(default(T));
                }
            }
        }
Example #8
0
        public static async Task <IEnumerable <dynamic> > QueryAsync(this IDbConnection connection, string command, object parameters = null, CommandConfiguration config = null)
        {
            using (var cmd = (DbCommand)CommandFactory.CreateCommand(connection, command, config))
            {
#endif
                var cancelToken = CommandFactory.GetCancellationToken(config);

                if (parameters != null)
                {
                    ParameterFactory.AddParameters(cmd, parameters);
                }

                using (var reader = await cmd.ExecuteReaderAsync(s_defaultCommandBehavior, cancelToken).ConfigureAwait(false))
                {
                    var results = new List <dynamic>();
                    var mapper  = DataMapperFactory.GetMapper(cmd, reader);
                    while (await reader.ReadAsync(cancelToken).ConfigureAwait(false))
                    {
                        results.Add(mapper(reader));
                    }
                    return(results);
                }
            }
        }