Inheritance: IConfigureCommand
        public static int Execute(this DbConnection cnx, Action <IConfigureCommand> cfg)
        {
            var cmd = new CommandConfiguration();

            cfg(cmd);
            return(cnx.Execute(cmd));
        }
        public static Task <int> ExecuteAsync(this DbConnection cnx, Action <IConfigureCommand> cfg, CancellationToken token)
        {
            var cmd = new CommandConfiguration();

            cfg(cmd);
            return(cnx.ExecuteAsync(cmd, token));
        }
 internal static async Task <T> GetValueAsync <T>(this DbConnection db, CommandConfiguration cfg, CancellationToken token)
 {
     using (var cmd = db.CreateAndSetupCommand(cfg))
     {
         return(await cmd.GetValueAsync <T>(token).ConfigureAwait(false));
     }
 }
        public static T GetValue <T>(this DbConnection db, Action <IConfigureCommand> cfg)
        {
            var cmd = new CommandConfiguration();

            cfg(cmd);
            return(db.GetValue <T>(cmd));
        }
 internal static T GetValue <T>(this DbConnection db, CommandConfiguration cfg)
 {
     using (var cmd = db.CreateAndSetupCommand(cfg))
     {
         return(cmd.GetValue <T>());
     }
 }
 internal static async Task <int> ExecuteAsync(this DbConnection cnx, CommandConfiguration cfg, CancellationToken token)
 {
     using (var cmd = cnx.CreateAndSetupCommand(cfg))
     {
         return(await cmd.ExecuteAsync(token).ConfigureAwait(false));
     }
 }
 internal static int Execute(this DbConnection cnx, CommandConfiguration cfg)
 {
     using (var cmd = cnx.CreateAndSetupCommand(cfg))
     {
         return(cmd.Execute());
     }
 }
        public static Task <T> GetValueAsync <T>(this DbConnection db, Action <IConfigureCommand> cfg, CancellationToken token)
        {
            var cmd = new CommandConfiguration();

            cfg(cmd);
            return(GetValueAsync <T>(db, cmd, token));
        }
 internal static void QueryAndProcess <T>(this DbConnection cnx, CommandConfiguration cfg, Func <T, bool> processor, T anonModel = default(T),
                                          bool firstRowOnly = false)
 {
     using (var cmd = cnx.CreateAndSetupCommand(cfg))
     {
         cmd.QueryAndProcess(processor, firstRowOnly: firstRowOnly);
     }
 }
 internal static async Task QueryAndProcessAsync <T>(this DbConnection db, CommandConfiguration cfg, Func <T, bool> processor, CancellationToken token, T anonModel = default(T),
                                                     bool firstRowOnly = false)
 {
     using (var cmd = db.CreateAndSetupCommand(cfg))
     {
         await cmd.QueryAndProcessAsync(token, processor, firstRowOnly : firstRowOnly).ConfigureAwait(false);
     }
 }
        /// <summary>
        /// Processes each row (as a strongly typed poco) as it is read.
        /// Used to process a big stream of results.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cnx"></param>
        /// <param name="cfg">Sql and command arguments</param>
        /// <param name="processor">True/False to continue processing</param>
        /// <param name="anonModel">Map to anonymous</param>
        /// <param name="firstRowOnly"></param>
        /// <returns></returns>
        public static void QueryAndProcess <T>(this DbConnection cnx, Action <IConfigureCommand> cfg,
                                               Func <T, bool> processor, T anonModel = default(T),
                                               bool firstRowOnly = false)
        {
            var cmdConfig = new CommandConfiguration();

            cfg(cmdConfig);
            cnx.QueryAndProcess(cmdConfig, processor, anonModel, firstRowOnly);
        }
        /// <summary>
        /// Processes each row (as a strongly typed poco) as it is read.
        /// Used to process a big stream of results.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cnx"></param>
        /// <param name="cfg">Sql and command arguments</param>
        /// <param name="processor">True/False to continue processing</param>
        /// <param name="token"></param>
        /// <param name="anonModel">Map to anonymous</param>
        /// <param name="firstRowOnly"></param>
        /// <returns></returns>
        public static Task QueryAndProcessAsync <T>(this DbConnection cnx, Action <IConfigureCommand> cfg,
                                                    Func <T, bool> processor, CancellationToken token, T anonModel = default(T),
                                                    bool firstRowOnly = false)
        {
            var cmdConfig = new CommandConfiguration();

            cfg(cmdConfig);
            return(cnx.QueryAndProcessAsync(cmdConfig, processor, token, anonModel, firstRowOnly));
        }
Beispiel #13
0
        private static SProcResult HandleProc(this DbConnection db, Action <SProcInput> input, Func <DbCommand, SProcResult> exec)
        {
            var data = new SProcInput();

            input(data);

            var output   = new List <DbParameter>();
            var provider = db.Provider();

            DbParameter par         = null;
            DbParameter returnValue = null;

            var cmdConfig = new CommandConfiguration();

            cmdConfig.IsStoredProcedure = true;
            cmdConfig.SqlText           = data.ProcName;
            cmdConfig.ApplyOptions      = data.Apply;
            SProcResult result;

            using (var cmd = db.CreateAndSetupCommand(cmdConfig))
            {
                var args = PrepareStatement.CreateParamsDictionary(data.Arguments);
                args.ForEach(kv =>
                {
                    par = cmd.AddParam(provider, kv.Key, kv.Value);

                    if (IsOutput(kv.Key))
                    {
                        par.ParameterName = par.ParameterName[0] + kv.Key.Substring(1);
                        par.Direction     = ParameterDirection.Output;
                        output.Add(par);
                    }
                });

                returnValue           = cmd.AddParam(provider, "ReturnValue", 0);
                returnValue.Direction = ParameterDirection.ReturnValue;

                result = exec(cmd);
            }

            result.ReturnValue = returnValue.Value.ConvertTo <int>();
            var outv = (IDictionary <string, object>)result.OutputValues;

            foreach (var param in output)
            {
                //first char of parameter name is db specific param prefix
                outv[param.ParameterName.Substring(1)] = param.Value;
            }
            return(result);
        }
Beispiel #14
0
        /// <summary>
        /// Sets the commands sql and parameters, takes care of provider specific functionality. At the end, the command is ready to execute
        /// </summary>
        /// <param name="cnx"></param>
        /// <param name="cfg"></param>
        /// <returns></returns>
        public static DbCommand CreateAndSetupCommand(this DbConnection cnx, CommandConfiguration cfg)
        {
            var provider = cnx.Provider();
            var cmd      = cnx.CreateCommand();

            cmd.CommandText = cfg.SqlText;
            if (cfg.IsStoredProcedure)
            {
                cmd.CommandType = CommandType.StoredProcedure;
            }
            var paramNames = cmd.SetupParameters(provider, cfg.Args);

            cmd.CommandText = provider.FormatParameters(cmd.CommandText, paramNames);
            provider.OnCommandExecuting(cmd);
            cfg.ApplyOptions(cmd);
            return(cmd);
        }
        public static PagedResult <T> FetchPaged <T>(this DbConnection db, Action <IConfigureCommand> cfg, Pagination page, T anonModel = null) where T : class
        {
            typeof(T).Must(t => t != typeof(object), "Dynamic types are not supported");
            var cmd = new CommandConfiguration();

            cfg(cmd);
            var builder = new PagedQueryBuilder(db.GetPocoInfo <T>(), db.Provider());
            var data    = builder.Build(cmd.SqlText, cmd.Args, page);

            var result = new PagedResult <T>();

            result.LongCount =
                db.GetValue <long>(c => c.Sql(data.CountSql, cmd.Args).WithCommandOptions(cmd.ApplyOptions));
            if (result.Count == 0)
            {
                return(result);
            }

            result.Items = db.Fetch <T>(c => c.Sql(data.PagedSql, data.Args).WithCommandOptions(cmd.ApplyOptions));
            return(result);
        }
Beispiel #16
0
 public void Import(CommandConfiguration cfg)
 {
     Sql(cfg.SqlText, cfg.Args);
     ApplyOptions = cfg.ApplyOptions;
 }
Beispiel #17
0
 public Task<int> ExecuteAsync(CommandConfiguration cfg, CancellationToken cancel)
 {
     throw new System.NotImplementedException();
 }
Beispiel #18
0
 public int Execute(CommandConfiguration cfg)
 {
     Result = cfg;
     return 0;
 }
Beispiel #19
0
 public void Import(CommandConfiguration cfg)
 {
     Sql(cfg.SqlText, cfg.Args);
     ApplyOptions = cfg.ApplyOptions;
 }