Example #1
0
        private void SaveScriptSourceCode()
        {
            IScriptingService scripting  = Services.GetService <IScriptingService>();
            TSqlFragment      syntaxTree = scripting.ParseScript(ScriptCode, out IList <ParseError> errors);

            if (errors.Count > 0)
            {
                ShowParseErrors(errors);
                throw new InvalidOperationException("Saving script failed: incorrect syntax.");
            }

            ScriptingController controller = Services.GetService <ScriptingController>();
            string catalogName             = controller.GetScriptsCatalogName(MyServer, MyDatabase, ScriptType);

            if (controller.ScriptFileExists(catalogName, Name))
            {
                controller.SaveScriptFile(catalogName, Name, ScriptCode);
            }
            else
            {
                if (controller.GetScriptTreeNodeByName(MyServer, MyDatabase, ScriptType, Name) != null)
                {
                    throw new InvalidOperationException($"Script node \"{Name}\" already exists!");
                }
                controller.SaveScriptFile(catalogName, Name, ScriptCode);
            }

            MainWindowViewModel mainWindow = Services.GetService <MainWindowViewModel>();
            TreeNodeViewModel   treeNode   = mainWindow.GetTreeNodeByPayload(mainWindow.MainTreeRegion.TreeNodes, this);

            if (treeNode == null)
            {
                controller.CreateScriptTreeNode(this);
            }
        }
Example #2
0
        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);
        }
Example #3
0
        private void UpdateScriptCommand(object node)
        {
            if (!(node is TreeNodeViewModel treeNode))
            {
                return;
            }
            if (!(treeNode.NodePayload is MetaScript script))
            {
                return;
            }

            MessageBoxResult result = MessageBox.Show("Обновить скрипт \"" + script.Name + "\" ?",
                                                      "DaJet", MessageBoxButton.OKCancel, MessageBoxImage.Question);

            if (result != MessageBoxResult.OK)
            {
                return;
            }

            WebServer      webServer = treeNode.GetAncestorPayload <WebServer>();
            DatabaseInfo   database  = treeNode.GetAncestorPayload <DatabaseInfo>();
            DatabaseServer server    = treeNode.GetAncestorPayload <DatabaseServer>();

            string url = GetActionScriptUrl(server, database, script);

            ScriptingController controller = Services.GetService <ScriptingController>();

            byte[] bytes = controller.ReadScriptAsBytes(server, database, MetaScriptType.Script, script.Name);
            script.SourceCode = Convert.ToBase64String(bytes);

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true
            };
            string        requestJson = JsonSerializer.Serialize(script, options);
            StringContent body        = new StringContent(requestJson, Encoding.UTF8, "application/json");

            script.SourceCode = string.Empty; // we do not need source code any more

            IHttpClientFactory http   = Services.GetService <IHttpClientFactory>();
            HttpClient         client = http.CreateClient(webServer.Name);

            if (client.BaseAddress == null)
            {
                client.BaseAddress = new Uri(webServer.Address);
            }

            try
            {
                var response = client.PutAsync(url, body).Result;

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    _ = MessageBox.Show("Script has been updated successfully.", script.Name, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    ShowHttpError(script.Name + " (error)", response);
                }
            }
            catch (Exception ex)
            {
                _ = MessageBox.Show(ex.Message, script.Name);
            }
        }