public static void Start(InputCommand input, Display display)
        {
            var commands = new List <IShellCommand>();
            var env      = new Env {
                Input = input, Display = display
            };

            // register commands
            RegisterCommands(commands);

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var s = new StringScanner(cmd);

                    var found = false;

                    // first test all shell app commands
                    foreach (var command in commands)
                    {
                        if (!command.IsCommand(s))
                        {
                            continue;
                        }

                        command.Execute(s, env, out var a, out var b);

                        found = true;
                        break;
                    }

                    // if not found, try database command
                    if (!found)
                    {
                        display.WriteResult(env.Engine.Run(cmd));
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            LiteDatabase db      = null;
            var          input   = new InputCommand();
            var          display = new Display();

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    db = new LiteDatabase(args[0]);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref db, display, input);

                    if (isConsoleCommand == false)
                    {
                        if (db == null)
                        {
                            throw LiteException.NoDatabase();
                        }

                        var result = db.Run(cmd);

                        display.WriteResult(result);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            LiteDatabase db = null;
            var input = new InputCommand();
            var display = new Display();

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    db = new LiteDatabase(args[0]);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd)) continue;

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref db, display, input);

                    if (isConsoleCommand == false)
                    {
                        if(db == null) throw LiteException.NoDatabase();

                        var result = db.Run(cmd);

                        display.WriteResult(result);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Example #4
0
        public static void Start(InputCommand input, Display display)
        {
            var env = new Env {
                Input = input, Display = display
            };

            // show welcome message
            display.WriteWelcome();

            Console.CancelKeyPress += (o, e) => { e.Cancel = true; env.Running = false; };

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var scmd = GetCommand(cmd);

                    if (scmd != null)
                    {
                        scmd(env);
                        continue;
                    }

                    // if string is not a shell command, try execute as sql command
                    if (env.Database == null)
                    {
                        throw new Exception("Database not connected");
                    }

                    env.Running = true;

                    display.WriteResult(env.Database.Execute(cmd), env);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex);
                }
            }
        }