private void TranslateCommandHandler(object parameter) { IMetadataService metadata = Services.GetService <IMetadataService>(); metadata.AttachDatabase(string.IsNullOrWhiteSpace(MyServer.Address) ? MyServer.Name : MyServer.Address, MyDatabase); IScriptingService scripting = Services.GetService <IScriptingService>(); string sql = scripting.PrepareScript(ScriptCode, out IList <ParseError> errors); string errorMessage = string.Empty; foreach (ParseError error in errors) { errorMessage += error.Message + Environment.NewLine; } MainWindowViewModel mainWindow = Services.GetService <MainWindowViewModel>(); ScriptEditorViewModel scriptEditor = Services.GetService <ScriptEditorViewModel>(); if (errors.Count > 0) { scriptEditor.Name = "Errors"; scriptEditor.ScriptCode = errorMessage; } else { scriptEditor.Name = $"{Name} (SQL)"; scriptEditor.ScriptCode = sql; } ScriptEditorView scriptView = new ScriptEditorView() { DataContext = scriptEditor }; mainWindow.AddNewTab(scriptEditor.Name, scriptView); }
private void ShowScriptUrlCommand(object node) { if (!(node is TreeNodeViewModel treeNode)) { return; } if (!(treeNode.NodePayload is MetaScript script)) { return; } WebServer webServer = treeNode.GetAncestorPayload <WebServer>(); DatabaseInfo database = treeNode.GetAncestorPayload <DatabaseInfo>(); DatabaseServer server = treeNode.GetAncestorPayload <DatabaseServer>(); string url = GetExecuteScriptUrl(webServer, server, database, script); ScriptingController controller = Services.GetService <ScriptingController>(); string sourceCode = controller.ReadScriptSourceCode(server, database, MetaScriptType.Script, script.Name); IMetadataService metadata = Services.GetService <IMetadataService>(); metadata.AttachDatabase(string.IsNullOrWhiteSpace(server.Address) ? server.Name : server.Address, database); IScriptingService scripting = Services.GetService <IScriptingService>(); TSqlFragment syntaxTree = scripting.ParseScript(sourceCode, out IList <ParseError> errors); if (errors.Count > 0) { ShowParseErrors(errors); return; } DeclareVariableStatementVisitor visitor = new DeclareVariableStatementVisitor(); syntaxTree.Accept(visitor); string jsonDTO = visitor.GenerateJsonParametersObject(); MainWindowViewModel mainWindow = Services.GetService <MainWindowViewModel>(); ScriptEditorViewModel editor = Services.GetService <ScriptEditorViewModel>(); editor.Name = $"{script.Name} (URL)"; editor.ScriptCode = url + Environment.NewLine + jsonDTO; ScriptEditorView scriptView = new ScriptEditorView() { DataContext = editor }; mainWindow.AddNewTab(editor.Name, scriptView); }
private void ExecuteCommandHandler(object parameter) { MainWindowViewModel mainWindow = Services.GetService <MainWindowViewModel>(); IMetadataService metadata = Services.GetService <IMetadataService>(); metadata.AttachDatabase(string.IsNullOrWhiteSpace(MyServer.Address) ? MyServer.Name : MyServer.Address, MyDatabase); IScriptingService scripting = Services.GetService <IScriptingService>(); string sql = scripting.PrepareScript(ScriptCode, out IList <ParseError> errors); string errorMessage = string.Empty; foreach (ParseError error in errors) { errorMessage += error.Message + Environment.NewLine; } if (errors.Count > 0) { ScriptEditorViewModel scriptEditor = Services.GetService <ScriptEditorViewModel>(); scriptEditor.Name = "Errors"; scriptEditor.ScriptCode = errorMessage; ScriptEditorView scriptView = new ScriptEditorView() { DataContext = scriptEditor }; mainWindow.AddNewTab(scriptEditor.Name, scriptView); return; } string json = "[]"; try { json = scripting.ExecuteScript(sql, out IList <ParseError> executeErrors); foreach (ParseError error in executeErrors) { errorMessage += error.Message + Environment.NewLine; } if (executeErrors.Count > 0) { ScriptEditorViewModel scriptEditor = Services.GetService <ScriptEditorViewModel>(); scriptEditor.Name = "Errors"; scriptEditor.ScriptCode = errorMessage; ScriptEditorView scriptView = new ScriptEditorView() { DataContext = scriptEditor }; mainWindow.AddNewTab(scriptEditor.Name, scriptView); return; } } catch (Exception ex) { ScriptEditorViewModel scriptEditor = Services.GetService <ScriptEditorViewModel>(); scriptEditor.Name = "Errors"; scriptEditor.ScriptCode = ex.Message; ScriptEditorView scriptView = new ScriptEditorView() { DataContext = scriptEditor }; mainWindow.AddNewTab(scriptEditor.Name, scriptView); return; } JsonSerializerOptions serializerOptions = new JsonSerializerOptions(); serializerOptions.Converters.Add(new DynamicJsonConverter()); dynamic data = JsonSerializer.Deserialize <dynamic>(json, serializerOptions); DataGrid dataView = DynamicGridCreator.CreateDynamicDataGrid(data); mainWindow.AddNewTab($"{Name} (result)", dataView); }
public ActionResult ExecuteScript([FromRoute] Guid server, [FromRoute] Guid database, [FromRoute] Guid script) { DatabaseServer srv = Metadata.Servers.Where(srv => srv.Identity == server).FirstOrDefault(); if (srv == null) { return(NotFound()); } DatabaseInfo db = srv.Databases.Where(db => db.Identity == database).FirstOrDefault(); if (db == null) { return(NotFound()); } MetaScript scr = db.Scripts.Where(scr => scr.Identity == script).FirstOrDefault(); if (scr == null) { return(NotFound()); } Dictionary <string, object> parameters = ParseParameters(HttpContext); string responseJson = "[]"; string errorMessage = string.Empty; string sourceCode = ReadScriptSourceCode(srv, db, scr); IMetadataService metadata = Services.GetService <IMetadataService>(); metadata.AttachDatabase(string.IsNullOrWhiteSpace(srv.Address) ? srv.Name : srv.Address, db); metadata.UseCredentials(db.UserName, db.Password); IScriptingService scripting = Services.GetService <IScriptingService>(); string sql = string.Empty; IList <ParseError> parseErrors = null; if (parameters.Count > 0) { sql = scripting.PrepareScript(sourceCode, parameters, out parseErrors); } else { sql = scripting.PrepareScript(sourceCode, out parseErrors); } foreach (ParseError error in parseErrors) { errorMessage += error.Message + Environment.NewLine; } if (parseErrors.Count > 0) { return(StatusCode(StatusCodes.Status500InternalServerError, errorMessage)); } try { responseJson = scripting.ExecuteJson(sql, out IList <ParseError> executeErrors); foreach (ParseError error in executeErrors) { errorMessage += error.Message + Environment.NewLine; } if (executeErrors.Count > 0) { return(StatusCode(StatusCodes.Status500InternalServerError, errorMessage)); } } catch (Exception ex) { errorMessage = ExceptionHelper.GetErrorText(ex); return(StatusCode(StatusCodes.Status500InternalServerError, errorMessage)); } return(Content(responseJson)); }