/// <summary>
 /// Adds a parameter to the call (for all types other than strings)
 /// </summary>
 /// <param name="ID">Name of the parameter</param>
 /// <param name="Value">Value to add</param>
 /// <param name="Type">SQL type of the parameter</param>
 /// <param name="Direction">Parameter direction (defaults to input)</param>
 public virtual void AddParameter(string ID, DbType Type, object Value = null, ParameterDirection Direction = ParameterDirection.Input)
 {
     if (ExecutableCommand != null)
     {
         ExecutableCommand.AddParameter(ID, Type, Value, Direction);
     }
 }
Example #2
0
 /// <summary>
 ///     Hlavná slučka hry
 /// </summary>
 public void Run()
 {
     GameRunning = true;
     while (GameRunning)
     {
         string            consolePromt = ConsolePromt();
         ExecutableCommand cmd          = _parser.ParseLine(consolePromt, State);
         if (cmd != null)
         {
             try
             {
                 string result = cmd.Execute(this);
                 if (!string.IsNullOrEmpty(result))
                 {
                     Console.WriteLine(result);
                 }
             }
             catch (EndGameException ex)
             {
                 State = GameState.End;
                 Console.WriteLine(ex.Message);
                 EndGame();
             }
         }
         else
         {
             var suggestion = TextTools.GetSuggestion(_parser.GetCommands(State), consolePromt);
             Console.WriteLine("Invallid command." + (suggestion != "" ? " Did you mean " + suggestion + "?" : ""));
         }
     }
 }
Example #3
0
        // ReSharper disable once InconsistentNaming
        protected void Throws <T>(ExecutableCommand When) where T : Exception
        {
            Exception exceptionThrown = null;

            try
            {
                this.When(When);
            }
            catch (Exception e)
            {
                exceptionThrown = e;
            }

            formatter.Block("Then:");

            Assert(
                exceptionThrown is T,
                () => formatter.Write("It throws " + typeof(T).Name).NewLine(),
                () =>
            {
                if (exceptionThrown == null)
                {
                    formatter.Write("But it did not.");
                    return;
                }

                formatter.Write("But got " + exceptionThrown.GetType().Name).NewLine();
            });


            // consume all events
            results = Enumerable.Empty <DomainEvent>();
        }
 /// <summary>
 /// Adds a parameter to the call (for strings only)
 /// </summary>
 /// <param name="ID">Name of the parameter</param>
 /// <param name="Value">Value to add</param>
 /// <param name="Length">Size of the string(either -1 or greater than 4000 should be used to indicate nvarchar(max))</param>
 /// <param name="Direction">Parameter direction (defaults to input)</param>
 public virtual void AddParameter(string ID, int Length, string Value = "", ParameterDirection Direction = ParameterDirection.Input)
 {
     if (ExecutableCommand != null)
     {
         ExecutableCommand.AddParameter(ID, Length, Value, Direction);
     }
 }
 /// <summary>
 /// Executes the stored procedure and returns a reader object
 /// </summary>
 public virtual void ExecuteReader()
 {
     Open();
     if (ExecutableCommand != null)
     {
         Reader = ExecutableCommand.ExecuteReader();
     }
 }
 /// <summary>
 /// Returns a parameter's value
 /// </summary>
 /// <typeparam name="DataType">Data type of the object</typeparam>
 /// <param name="ID">Parameter name</param>
 /// <param name="Default">Default value for the parameter</param>
 /// <param name="Direction">Parameter direction (defaults to input)</param>
 /// <returns>if the parameter exists (and isn't null or empty), it returns the parameter's value. Otherwise the default value is returned.</returns>
 public virtual DataType GetParameter <DataType>(string ID, DataType Default = default(DataType), ParameterDirection Direction = ParameterDirection.Input)
 {
     if (Direction == ParameterDirection.Output)
     {
         return(ExecutableCommand.GetOutputParameter <DataType>(ID, Default));
     }
     return(Reader.GetParameter <DataType>(ID, Default));
 }
 /// <summary>
 /// Adds a parameter to the call (for all types other than strings)
 /// </summary>
 /// <param name="ID">Name of the parameter</param>
 /// <param name="Value">Value to add</param>
 /// <param name="Type">SQL type of the parameter</param>
 /// <param name="Direction">Parameter direction (defaults to input)</param>
 /// <returns>This</returns>
 public virtual SQLHelper AddParameter(string ID, DbType Type, object Value = null, ParameterDirection Direction = ParameterDirection.Input)
 {
     if (ExecutableCommand != null)
     {
         ExecutableCommand.AddParameter(ID, Type, Value, Direction);
     }
     return(this);
 }
 /// <summary>
 /// Adds a parameter to the call (for strings only)
 /// </summary>
 /// <param name="ID">Name of the parameter</param>
 /// <param name="Value">Value to add</param>
 /// <param name="Length">Size of the string(either -1 or greater than 4000 should be used to indicate nvarchar(max))</param>
 /// <param name="Direction">Parameter direction (defaults to input)</param>
 /// <returns>This</returns>
 public virtual SQLHelper AddParameter(string ID, int Length, string Value = "", ParameterDirection Direction = ParameterDirection.Input)
 {
     if (ExecutableCommand != null)
     {
         ExecutableCommand.AddParameter(ID, Length, Value, Direction);
     }
     return(this);
 }
 /// <summary>
 /// Executes the stored procedure and returns a reader object
 /// </summary>
 public virtual SQLHelper ExecuteReader()
 {
     Open();
     if (ExecutableCommand != null)
     {
         Reader = ExecutableCommand.ExecuteReader();
     }
     return(this);
 }
Example #10
0
        /// <summary>
        /// Creates a command from its properties
        /// </summary>
        /// <param name="name">The name of the executed command, such as 'plugins' for '/plugins'</param>
        /// <param name="description">The description of the command given by /help</param>
        /// <param name="help">The help given by doing /help [command] or when a command fails</param>
        /// <param name="method">The method executed when calling the method</param>
        internal Command(string name, string description, string help, ExecutableCommand method)
        {
            this.Name        = name;
            this.Description = description;
            this.Help        = help;
            this._method     = method;

            _commands.Add(this);
        }
Example #11
0
        protected void When(ExecutableCommand command)
        {
            BeforeExecute(command);

            formatter
            .Block("When users:")
            .Write(command, new EventFormatter(formatter));

            results = Context.ProcessCommand(command);
        }
Example #12
0
        protected void When(ExecutableCommand command)
        {
            //SetupAuthenticationMetadata(command.Meta);
            //command.IdGenerator = new FakeIdGenerator(this);

            formatter
            .Block("When users:")
            .Write(command, new EventFormatter(formatter));

            results = Context.ProcessCommand(command);
        }
Example #13
0
        public RunnerViewModel()
        {
            Specifications = new ObservableCollection <Test>();
            FooterVM       = new FooterViewModel();

            RunSuiteCommand = new ExecutableCommand(
                () => RunSuite(),
                () => AllowTestRun);

            ExportResultsCommand = new ExecutableCommand(
                () => ViewBinder.Instance.DisplayActive <TestActiveViewModel>(),
                () => AllowTestRun && FooterVM.AllTestsPassed.HasValue);
        }
        internal static async Task <int> Main(string[] args)
        {
            using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
            ILogger logger = loggerFactory.CreateLogger <Program>();

            RootCommand rootCommand = new();

            rootCommand.Name = "dotnet-template-localizer";

            foreach (Func <ILoggerFactory, ExecutableCommand> commandCreator in CommandCreators)
            {
                ExecutableCommand command = commandCreator(loggerFactory);
                rootCommand.AddCommand(command.CreateCommand());
            }

            return(await rootCommand.InvokeAsync(args).ConfigureAwait(false));
        }
 private void RecreateConnection()
 {
     if (Reader != null)
     {
         Reader.Close();
         Reader.Dispose();
         Reader = null;
     }
     if (ExecutableCommand != null)
     {
         ExecutableCommand.Dispose();
         ExecutableCommand = null;
     }
     ExecutableCommand             = Factory.CreateCommand();
     ExecutableCommand.CommandText = _Command;
     ExecutableCommand.Connection  = Connection;
     ExecutableCommand.CommandType = CommandType;
     if (Transaction != null)
     {
         ExecutableCommand.Transaction = Transaction;
     }
 }
 public virtual void Dispose()
 {
     Close();
     if (Connection != null)
     {
         Connection.Dispose();
         Connection = null;
     }
     if (Transaction != null)
     {
         Transaction.Dispose();
         Transaction = null;
     }
     if (ExecutableCommand != null)
     {
         ExecutableCommand.Dispose();
         ExecutableCommand = null;
     }
     if (Reader != null)
     {
         Reader.Dispose();
         Reader = null;
     }
 }
 /// <summary>
 /// Opens the connection
 /// </summary>
 public virtual void Open()
 {
     ExecutableCommand.Open();
 }
 /// <summary>
 /// Clears the parameters
 /// </summary>
 public virtual SQLHelper ClearParameters()
 {
     ExecutableCommand.ClearParameters();
     return(this);
 }
 /// <summary>
 /// Executes the stored procedure as a scalar query
 /// </summary>
 /// <typeparam name="DataType">Data type to return</typeparam>
 /// <returns>The object of the first row and first column</returns>
 public virtual DataType ExecuteScalar <DataType>()
 {
     return(ExecutableCommand.ExecuteScalar <DataType>());
 }
 /// <summary>
 /// Commits a transaction
 /// </summary>
 public virtual SQLHelper Commit()
 {
     ExecutableCommand.Commit();
     return(this);
 }
 /// <summary>
 /// Executes the stored procedure as a non query
 /// </summary>
 /// <returns>Number of rows effected</returns>
 public virtual int ExecuteNonQuery()
 {
     Open();
     return((ExecutableCommand != null) ? ExecutableCommand.ExecuteNonQuery() : 0);
 }
Example #22
0
 public void EmitCommand( ExecutableCommand cmd )
 {
     cmd.BuildAssembly( il );
 }
 /// <summary>
 /// Commits a transaction
 /// </summary>
 public virtual void Commit()
 {
     ExecutableCommand.Commit();
 }
 /// <summary>
 /// Closes the connection
 /// </summary>
 public virtual void Close()
 {
     ExecutableCommand.Close();
 }
 /// <summary>
 /// Clears the parameters
 /// </summary>
 public virtual void ClearParameters()
 {
     ExecutableCommand.ClearParameters();
 }
 /// <summary>
 /// Begins a transaction
 /// </summary>
 public virtual void BeginTransaction()
 {
     Transaction = ExecutableCommand.BeginTransaction();
     Command     = _Command;
 }
 /// <summary>
 /// Opens the connection
 /// </summary>
 public virtual SQLHelper Open()
 {
     ExecutableCommand.Open();
     return(this);
 }
 /// <summary>
 /// Rolls back a transaction
 /// </summary>
 public virtual SQLHelper Rollback()
 {
     ExecutableCommand.Rollback();
     return(this);
 }
 /// <summary>
 /// Rolls back a transaction
 /// </summary>
 public virtual void Rollback()
 {
     ExecutableCommand.Rollback();
 }
 /// <summary>
 /// Executes the query and returns a data set
 /// </summary>
 /// <returns>A dataset filled with the results of the query</returns>
 public virtual DataSet ExecuteDataSet()
 {
     return(ExecutableCommand.ExecuteDataSet(Factory));
 }
        public TransferFile ExecuteCommand(string command, string session)
        {
            bool executeasync = false;

            CleanUp();
            if (command.ToLower().StartsWith("start "))
            {
                executeasync = true;
                command      = command.Substring(6);
            }
            var commands = command.Split('|').Where(p => p != "").ToArray();
            List <ExecutableCommand> ecommands = new List <ExecutableCommand>();

            foreach (var cmds in commands)
            {
                ExecutableCommand ecmd = new ExecutableCommand();

                var parts = SplitArguments(cmds).Where(p => p != "").ToArray(); //TODO: Better arguments handling (support quotes)
                ecmd.Parameters = parts.Skip(1).ToArray();
                //Support piping

                if (Commands.ContainsKey(parts.First().ToLower()))
                {
                    var cmdd = Commands[parts.First().ToLower()];

                    //Create command object
                    var cmd = cmdd.CreateNew <IConsoleCommand>();
                    ecmd.Command = cmd;
                    //Map parameters

                    for (int i = 1; i < parts.Length; i += 2)
                    {
                        if (parts[i].StartsWith("-"))
                        {
                            try
                            {
                                //Parameter
                                if (cmdd.Parameters.ContainsKey(parts[i].ToLower().TrimStart('-')))
                                {
                                    var pi = cmdd.Parameters[parts[i].ToLower().TrimStart('-')];
                                    if (pi.PropertyType == typeof(string))
                                    {
                                        pi.SetValue(cmd, parts[i + 1]); //TODO: Support other types than string
                                    }
                                    else if (pi.PropertyType == typeof(bool))
                                    {
                                        pi.SetValue(cmd, bool.Parse(parts[i + 1]));
                                    }
                                    else if (pi.PropertyType == typeof(int))
                                    {
                                        pi.SetValue(cmd, int.Parse(parts[i + 1]));
                                    }
                                    else if (pi.PropertyType.IsEnum)
                                    {
                                        pi.SetValue(cmd, Enum.Parse(pi.PropertyType, parts[i + 1], true));
                                    }
                                }
                                else
                                {
                                    AddLogToSession(session, new CommandLog("System", "Unrecognized parameter"));
                                }
                            } catch (Exception exc)
                            {
                                AddLogToSession(session, new CommandLog("System", $"Error in parameters for {cmdd}: {exc.Message}"));
                            }
                        }
                        else
                        {
                            //Log.Add("Unknown parameter: " + parts[i]);
                        }
                    }
                }
                else
                {
                    AddLogToSession(session, new CommandLog("System", $"Unknown Command: {parts.First()}"));
                }

                if (ecmd.Command is IConsoleOutputCommand)
                {
                    ((IConsoleOutputCommand)ecmd.Command).OutputToConsole += new OutputToConsoleHandler((c, s) => { if (s != null)
                                                                                                                    {
                                                                                                                        AddLogToSession(session, new CommandLog(c.GetType().Name, s));
                                                                                                                    }
                                                                                                        });
                }
                if (ecmd.Command is ILogAwareCommand)
                {
                    ((ILogAwareCommand)ecmd.Command).Log = NewSession(session);
                }

                if (ecmd.Command is IInputCommand && ecommands.Any() && (ecommands.Last().Command is IOutputCommand))
                {
                    (ecmd.Command as IInputCommand).Initialize(ecommands.Last().Command as IOutputCommand, ecmd.Parameters);
                }

                if (ecommands.Count > 0 && !(ecmd.Command is IInputCommand))
                {
                    AddLogToSession(session, new CommandLog("System", "You cannot pipe content to that command"));
                    return(null);
                }
                else if (commands.Length > 1 && ecommands.Count == 0 && !(ecmd.Command is IOutputCommand))
                {
                    AddLogToSession(session, new CommandLog("System", "You cannot pipe content from that command"));
                    return(null);
                }
                ecommands.Add(ecmd);
            }

            TransferFile df = null;
            //Execute

            Action ExecuteCommands = () => {
                foreach (var ec in ecommands)
                {
                    try
                    {
                        var exec = ec.Command.Execute(ec.Parameters);
                        if (!string.IsNullOrEmpty(exec))
                        {
                            AddLogToSession(session, new CommandLog(ec.Command.GetType().Name, exec));
                        }
                        if (ec.Command is IReturnsFile)
                        {
                            df = (ec.Command as IReturnsFile).File;
                        }
                    }
                    catch (Exception exc)
                    {
                        AddLogToSession(session, new CommandLog(ec.Command.GetType().Name, exc.Message));
                        //TODO: Logging?
                    }
                }
            };

            if (executeasync)
            {
                //Queue task
                AddLogToSession(session, new CommandLog("System", "Starting Task"));
                System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Run(ExecuteCommands);
                Tasks.Add(t);
            }
            else
            {
                ExecuteCommands();
            }

            return(df);
        }