public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) throw ShellExpcetion.NoDatabase(); var direction = s.Scan(@"[><]\s*").Trim(); var filename = s.Scan(@".+").Trim(); //dump import if(direction == "<") { using (var reader = new StreamReader(filename, Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { engine.Run(line, new Display()); // no output } } } // dump export else { using (var writer = new StreamWriter(filename, false, Encoding.UTF8, 65536)) { writer.AutoFlush = true; writer.WriteLine("-- LiteDB v{0}.{1}.{2} dump file @ {3}", engine.Version.Major, engine.Version.Minor, engine.Version.Build, DateTime.Now); engine.Dump(writer); writer.Flush(); } } }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (s.Scan("false|off").Length > 0 && _writer != null) { display.TextWriters.Remove(_writer); input.OnWrite = null; _writer.Flush(); _writer.Dispose(); _writer = null; } else if (_writer == null) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } var path = Path.GetFullPath(string.Format("LiteDB-spool-{0:yyyy-MM-dd-HH-mm}.txt", DateTime.Now)); _writer = File.CreateText(path); display.TextWriters.Add(_writer); input.OnWrite = (t) => _writer.Write(t); } }
public IShellEngine DetectEngine(string filename) { var engines = new IShellEngine[] { new ShellEngine_090(), new ShellEngine_104(), new ShellEngine_200() }; // new files use always lastest version if (!File.Exists(filename)) { return(new ShellEngine_200()); } foreach (var engine in engines) { if (engine.Detect(filename)) { return(engine); } } throw new ShellExpcetion("Invalid dabatase format"); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) throw ShellExpcetion.NoDatabase(); engine.Dispose(); engine = null; }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine != null) { engine.Dispose(); } input.Running = false; }
public override void Execute(ref IShellEngine engine, StringScanner s, Display d, InputCommand input) { var sb = new StringBuilder(); var enabled = !(s.Scan(@"off\s*").Length > 0); if(engine == null) throw ShellExpcetion.NoDatabase(); engine.Debug(enabled); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } engine.Dispose(); engine = null; }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) throw ShellExpcetion.NoDatabase(); var filename = s.Scan(@".+").Trim(); foreach (var line in File.ReadAllLines(filename)) { input.Queue.Enqueue(line); } }
public override void Execute(ref IShellEngine engine, StringScanner s, Display d, InputCommand input) { var sb = new StringBuilder(); var enabled = !(s.Scan(@"off\s*").Length > 0); if (engine == null) { throw ShellExpcetion.NoDatabase(); } engine.Debug(enabled); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } var filename = s.Scan(@".+").Trim(); foreach (var line in File.ReadAllLines(filename)) { input.Queue.Enqueue(line); } }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } var ver = engine.Version; display.WriteInfo(string.Format("v{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build)); }
/// <summary> /// If command is a console command, execute and returns true - if not, just returns false /// </summary> public static bool TryExecute(string command, ref IShellEngine engine, Display display, InputCommand input) { var s = new StringScanner(command); foreach (var cmd in _commands) { if (cmd.IsCommand(s)) { cmd.Execute(ref engine, s, display, input); return true; } } return false; }
/// <summary> /// If command is a console command, execute and returns true - if not, just returns false /// </summary> public static bool TryExecute(string command, ref IShellEngine engine, Display display, InputCommand input) { var s = new StringScanner(command); foreach (var cmd in _commands) { if (cmd.IsCommand(s)) { cmd.Execute(ref engine, s, display, input); return(true); } } return(false); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { var connectionString = s.Scan(@".+"); if (engine != null) { engine.Dispose(); } // get filename, detect engine and open var filename = this.GetFilename(connectionString); engine = this.DetectEngine(filename); engine.Open(connectionString); // get engine version and display info var ver = engine.Version; display.WriteLine(ConsoleColor.DarkCyan, string.Format("open \"{0}\" (v{1}.{2}.{3})", Path.GetFileName(filename), ver.Major, ver.Minor, ver.Build)); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { var temp = Path.GetTempPath() + "LiteDB.Shell.txt"; // remove "ed" command from history input.History.RemoveAt(input.History.Count - 1); var last = input.History.Count > 0 ? input.History[input.History.Count - 1] : ""; File.WriteAllText(temp, last.Replace("\n", Environment.NewLine)); Process.Start("notepad.exe", temp).WaitForExit(); var text = File.ReadAllText(temp); if (text == last) return; input.Queue.Enqueue(text); }
public static void Start(InputCommand input, Display display) { IShellEngine engine = null; 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 isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref engine, display, input); if (isConsoleCommand == false) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } engine.Run(cmd, display); } } catch (Exception ex) { display.WriteError(ex.Message); } } }
public IShellEngine DetectEngine(string filename) { var engines = new IShellEngine[] { new ShellEngine_090(), new ShellEngine_104(), new ShellEngine_200() }; // new files use always lastest version if (!File.Exists(filename)) { return new ShellEngine_200(); } foreach (var engine in engines) { if (engine.Detect(filename)) return engine; } throw new ShellExpcetion("Invalid dabatase format"); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { var temp = Path.GetTempPath() + "LiteDB.Shell.txt"; // remove "ed" command from history input.History.RemoveAt(input.History.Count - 1); var last = input.History.Count > 0 ? input.History[input.History.Count - 1] : ""; File.WriteAllText(temp, last.Replace("\n", Environment.NewLine)); Process.Start("notepad.exe", temp).WaitForExit(); var text = File.ReadAllText(temp); if (text == last) { return; } input.Queue.Enqueue(text); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (s.Scan("false|off").Length > 0 && _writer != null) { display.TextWriters.Remove(_writer); input.OnWrite = null; _writer.Flush(); _writer.Dispose(); _writer = null; } else if (_writer == null) { if (engine == null) throw ShellExpcetion.NoDatabase(); var path = Path.GetFullPath(string.Format("LiteDB-spool-{0:yyyy-MM-dd-HH-mm}.txt", DateTime.Now)); _writer = File.CreateText(path); display.TextWriters.Add(_writer); input.OnWrite = (t) => _writer.Write(t); } }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if (engine == null) { throw ShellExpcetion.NoDatabase(); } var direction = s.Scan(@"[><]\s*").Trim(); var filename = s.Scan(@".+").Trim(); //dump import if (direction == "<") { using (var reader = new StreamReader(filename, Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { engine.Run(line, new Display()); // no output } } } // dump export else { using (var writer = new StreamWriter(filename, false, Encoding.UTF8, 65536)) { writer.AutoFlush = true; writer.WriteLine("-- LiteDB v{0}.{1}.{2} dump file @ {3}", engine.Version.Major, engine.Version.Minor, engine.Version.Build, DateTime.Now); engine.Dump(writer); writer.Flush(); } } }
public abstract void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input);
public override void Execute(ref IShellEngine engine, StringScanner s, Display d, InputCommand input) { var sb = new StringBuilder(); var full = s.Match("full"); if (!full) { d.WriteHelp("Basic Shell Commands - try `help full` for all commands"); d.WriteHelp("======================================================="); d.WriteHelp("> show collections", "List all collections inside database"); d.WriteHelp("> db.<collection>.insert <jsonDoc>", "Insert a new document into collection"); d.WriteHelp("> db.<collection>.update <jsonDoc>", "Update a document inside collection"); d.WriteHelp("> db.<collection>.delete <filter>", "Delete documents using a filter clausule (see find)"); d.WriteHelp("> db.<collection>.find <filter> [skip N][limit N]", "Show filtered documents based on index search"); d.WriteHelp("> db.<collection>.count <filter>", "Show count rows according query filter"); d.WriteHelp("> db.<collection>.ensureIndex <field> [true|{options}]", "Create a new index document field. For unique key, use true"); d.WriteHelp("> db.<collection>.indexes", "List all indexes in this collection"); d.WriteHelp("<filter> = <field> [=|>|>=|<|<=|!=|like|between] <jsonValue>", "Filter query syntax"); d.WriteHelp("<filter> = (<filter> [and|or] <filter> [and|or] ...)", "Multi queries syntax"); d.WriteHelp("Try:"); d.WriteHelp(" > db.customers.insert { _id:1, name:\"John Doe\", age: 37 }"); d.WriteHelp(" > db.customers.ensureIndex name"); d.WriteHelp(" > db.customers.find name like \"John\""); d.WriteHelp(" > db.customers.find name like \"John\" and _id between [0, 100] limit 10"); } else { d.WriteHelp("Shell commands"); d.WriteHelp("=============="); d.WriteHelp("> open <filename>", "Open a new database"); d.WriteHelp("> close", "Close current database"); d.WriteHelp("> run <filename>", "Run commands inside filename"); d.WriteHelp("> pretty on|off", "Turns on/off pretty json format"); d.WriteHelp("> dump > <filename.dmp>", "Export all documents to an external file script"); d.WriteHelp("> dump < <filename.dmp>", "Import all documents inside a script dump file"); d.WriteHelp("> timer", "Show timer before prompt"); d.WriteHelp("> ed", "Open nodepad with last command to edit and execute"); d.WriteHelp("> spool on|off", "Spool all output in a spool file"); d.WriteHelp("> -- comment", "Do nothing, its just a comment"); d.WriteHelp("> /<command>/", "Support for multi line command"); d.WriteHelp("> debug on|off", "Enabled debug messages from dbengine"); d.WriteHelp("> version", "Show LiteDB version"); d.WriteHelp("> exit", "Close LiteDB shell"); d.WriteHelp(); d.WriteHelp("Collections commands"); d.WriteHelp("===================="); d.WriteHelp("> show collections", "List all collections inside database"); d.WriteHelp("> db.<collection>.insert <jsonDoc>", "Insert a new document into collection"); d.WriteHelp("> db.<collection>.update <jsonDoc>", "Update a document inside collection"); d.WriteHelp("> db.<collection>.delete <filter>", "Delete documents using a filter clausule (see find)"); d.WriteHelp("> db.<collection>.bulk <filename>", "Bulk insert a json file as documents"); d.WriteHelp("> db.<collection>.find [skip N][limit N]", "Show all documents. Can limit/skip results"); d.WriteHelp("> db.<collection>.find <filter> [skip N][limit N]", "Show filtered documents based on index search. See <filter> syntax below"); d.WriteHelp("> db.<collection>.count <filter>", "Show count rows according query filter"); d.WriteHelp("> db.<collection>.ensureIndex <field> [unique]", "Create a new index document field"); d.WriteHelp("> db.<collection>.indexes", "List all indexes in this collection"); d.WriteHelp("> db.<collection>.drop", "Drop collection and destroy all documents inside"); d.WriteHelp("> db.<collection>.dropIndex <field>", "Drop a index and make index area free to use with another index"); d.WriteHelp("> db.<collection>.rename <newCollectionName>", "Rename a collection"); d.WriteHelp("> db.<collection>.min <field>", "Returns min/first value from collection using index field"); d.WriteHelp("> db.<collection>.max <field>", "Returns max/last value from collection using index field"); d.WriteHelp("> db.<collection>.stats", "Display statistics about a collection"); d.WriteHelp("<filter> = <field> [=|>|>=|<|<=|!=|like|contains|in|between] <jsonValue>", "Filter query syntax"); d.WriteHelp("<filter> = (<filter> [and|or] <filter> [and|or] ...)", "Multi queries syntax"); d.WriteHelp("<jsonDoc> = {_id: ... , key: value, key1: value1 }", "Represent a json (extended version) for a BsonDocument. See special data types"); d.WriteHelp("Json Date", "{ field: { $date :\"2015-01-01T23:59:59Z\"} }"); d.WriteHelp("Json Guid", "{ field: { $guid :\"3a1c34b3-9f66-4d8e-975a-d545d898a4ba\"} }"); d.WriteHelp("Json Binary", "{ field: { $binary :\"base64 byte array\"} }"); d.WriteHelp(); d.WriteHelp("File storage commands"); d.WriteHelp("====================="); d.WriteHelp("> fs.find", "List all files on database"); d.WriteHelp("> fs.find <fileId>", "List file info from a key. Supports * for starts with key"); d.WriteHelp("> fs.upload <fileId> <filename>", "Insert a new file inside database"); d.WriteHelp("> fs.download <fileId> <filename>", "Save a file to disk passing a file key and filename"); d.WriteHelp("> fs.update <fileId> {key:value}", "Update metadata file"); d.WriteHelp("> fs.delete <fileId>", "Remove a file inside database"); d.WriteHelp(); d.WriteHelp("Other commands"); d.WriteHelp("=============="); d.WriteHelp("> begin", "Begin an exclusive transaction"); d.WriteHelp("> commit", "Commit transaction"); d.WriteHelp("> rollback", "Rollback a transaction"); d.WriteHelp("> dbversion [N]", "Get/Set user database file version"); d.WriteHelp("> shrink", "Reduce database removing empty pages"); d.WriteHelp("> diskdump [n m]", "Display database disk pages strucutre (from N page to M page)"); } }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { display.Pretty = !(s.Scan(@"off\s*").Length > 0); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { if(engine != null) engine.Dispose(); input.Running = false; }
public override void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input) { input.Timer.Start(); }
public override void Execute(ref IShellEngine engine, StringScanner s, Display d, InputCommand input) { var sb = new StringBuilder(); var full = s.Match("full"); if (!full) { d.WriteHelp("Basic Shell Commands - try `help full` for all commands"); d.WriteHelp("======================================================="); d.WriteHelp("> show collections", "List all collections inside database"); d.WriteHelp("> db.<collection>.insert <jsonDoc>", "Insert a new document into collection"); d.WriteHelp("> db.<collection>.update <jsonDoc>", "Update a document inside collection"); d.WriteHelp("> db.<collection>.delete <filter>", "Delete documents using a filter clausule (see find)"); d.WriteHelp("> db.<collection>.find <filter> [skip N][limit N]", "Show filtered documents based on index search"); d.WriteHelp("> db.<collection>.count <filter>", "Show count rows according query filter"); d.WriteHelp("> db.<collection>.ensureIndex <field> [true|{options}]", "Create a new index document field. For unique key, use true"); d.WriteHelp("> db.<collection>.indexes", "List all indexes in this collection"); d.WriteHelp("<filter> = <field> [=|>|>=|<|<=|!=|like|between] <jsonValue>", "Filter query syntax"); d.WriteHelp("<filter> = (<filter> [and|or] <filter> [and|or] ...)", "Multi queries syntax"); d.WriteHelp("Try:"); d.WriteHelp(" > db.customers.insert { _id:1, name:\"John Doe\", age: 37 }"); d.WriteHelp(" > db.customers.ensureIndex name"); d.WriteHelp(" > db.customers.find name like \"John\""); d.WriteHelp(" > db.customers.find name like \"John\" and _id between [0, 100] limit 10"); } else { d.WriteHelp("Shell commands"); d.WriteHelp("=============="); d.WriteHelp("> open <filename>", "Open a new database"); d.WriteHelp("> close", "Close current database"); d.WriteHelp("> run <filename>", "Run commands inside filename"); d.WriteHelp("> pretty on|off", "Turns on/off pretty json format"); d.WriteHelp("> dump > <filename.dmp>", "Export all documents to an external file script"); d.WriteHelp("> dump < <filename.dmp>", "Import all documents inside a script dump file"); d.WriteHelp("> timer", "Show timer before prompt"); d.WriteHelp("> ed", "Open nodepad with last command to edit and execute"); d.WriteHelp("> spool on|off", "Spool all output in a spool file"); d.WriteHelp("> -- comment", "Do nothing, its just a comment"); d.WriteHelp("> /<command>/", "Support for multi line command"); d.WriteHelp("> debug on|off", "Enabled debug messages from dbengine"); d.WriteHelp("> version", "Show LiteDB version"); d.WriteHelp("> exit", "Close LiteDB shell"); d.WriteHelp(); d.WriteHelp("Collections commands"); d.WriteHelp("===================="); d.WriteHelp("> show collections", "List all collections inside database"); d.WriteHelp("> db.<collection>.insert <jsonDoc>", "Insert a new document into collection"); d.WriteHelp("> db.<collection>.update <jsonDoc>", "Update a document inside collection"); d.WriteHelp("> db.<collection>.delete <filter>", "Delete documents using a filter clausule (see find)"); d.WriteHelp("> db.<collection>.bulk <filename>", "Bulk insert a json file as documents"); d.WriteHelp("> db.<collection>.find [skip N][limit N]", "Show all documents. Can limit/skip results"); d.WriteHelp("> db.<collection>.find <filter> [skip N][limit N]", "Show filtered documents based on index search. See <filter> syntax below"); d.WriteHelp("> db.<collection>.count <filter>", "Show count rows according query filter"); d.WriteHelp("> db.<collection>.ensureIndex <field> [unique]", "Create a new index document field"); d.WriteHelp("> db.<collection>.indexes", "List all indexes in this collection"); d.WriteHelp("> db.<collection>.drop", "Drop collection and destroy all documents inside"); d.WriteHelp("> db.<collection>.dropIndex <field>", "Drop a index and make index area free to use with another index"); d.WriteHelp("> db.<collection>.rename <newCollectionName>", "Rename a collection"); d.WriteHelp("> db.<collection>.min <field>", "Returns min/first value from collection using index field"); d.WriteHelp("> db.<collection>.max <field>", "Returns max/last value from collection using index field"); d.WriteHelp("> db.<collection>.stats", "Display statistics about a collection"); d.WriteHelp("<filter> = <field> [=|>|>=|<|<=|!=|like|contains|in|between] <jsonValue>", "Filter query syntax"); d.WriteHelp("<filter> = (<filter> [and|or] <filter> [and|or] ...)", "Multi queries syntax"); d.WriteHelp("<jsonDoc> = {_id: ... , key: value, key1: value1 }", "Represent a json (extended version) for a BsonDocument. See special data types"); d.WriteHelp("Json Date", "{ field: { $date :\"2015-01-01T23:59:59Z\"} }"); d.WriteHelp("Json Guid", "{ field: { $guid :\"3a1c34b3-9f66-4d8e-975a-d545d898a4ba\"} }"); d.WriteHelp("Json Binary", "{ field: { $binary :\"base64 byte array\"} }"); d.WriteHelp(); d.WriteHelp("File storage commands"); d.WriteHelp("====================="); d.WriteHelp("> fs.find", "List all files on database"); d.WriteHelp("> fs.find <fileId>", "List file info from a key. Supports * for starts with key"); d.WriteHelp("> fs.upload <fileId> <filename>", "Insert a new file inside database"); d.WriteHelp("> fs.download <fileId> <filename>", "Save a file to disk passing a file key and filename"); d.WriteHelp("> fs.update <fileId> {key:value}", "Update metadata file"); d.WriteHelp("> fs.delete <fileId>", "Remove a file inside database"); d.WriteHelp(); d.WriteHelp("Other commands"); d.WriteHelp("=============="); d.WriteHelp("> shrink", "Reduce database removing empty pages"); d.WriteHelp("> diskdump [n m]", "Display database disk pages strucutre (from N page to M page)"); } }