diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index a319bb81..43a9c23b 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -129,6 +129,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent this.ServiceHost.SetRequestHandler(CreateAgentNotebookRequest.Type, HandleCreateAgentNotebookRequest); this.ServiceHost.SetRequestHandler(DeleteAgentNotebookRequest.Type, HandleDeleteAgentNotebooksRequest); this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRequest.Type, HandleUpdateAgentNotebookRequest); + this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRunPinRequest.Type, HandleUpdateAgentNotebookRunPinRequest); + this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRunNameRequest.Type, HandleUpdateAgentNotebookRunNameRequest); serviceHost.RegisterShutdownTask(async (shutdownParams, shutdownRequestContext) => @@ -1410,6 +1412,64 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent }); } + internal async Task HandleUpdateAgentNotebookRunNameRequest(UpdateAgentNotebookRunNameParams parameters, RequestContext requestContext) + { + await Task.Run(async () => + { + var result = new ResultStatus(); + try + { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + // Calling update helper function + await AgentNotebookHelper.UpdateMaterializedNotebookName( + connInfo, + parameters.MaterializedId, + parameters.TargetDatabase, + parameters.MaterializedNotebookName); + result.Success = true; + } + catch (Exception e) + { + result.Success = false; + result.ErrorMessage = e.ToString(); + + } + await requestContext.SendResult(result); + }); + } + + internal async Task HandleUpdateAgentNotebookRunPinRequest(UpdateAgentNotebookRunPinParams parameters, RequestContext requestContext) + { + await Task.Run(async () => + { + var result = new ResultStatus(); + try + { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + // Calling update helper function + await AgentNotebookHelper.UpdateMaterializedNotebookPin( + connInfo, + parameters.MaterializedId, + parameters.TargetDatabase, + parameters.MaterializedNotebookPin); + result.Success = true; + } + catch (Exception e) + { + result.Success = false; + result.ErrorMessage = e.ToString(); + + } + await requestContext.SendResult(result); + }); + } + public AgentNotebookHistoryResult GetAgentNotebookHistories( ConnectionInfo connInfo, string jobId, @@ -1475,6 +1535,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { notebookHistory.MaterializedNotebookId = (int)notebookHistoriesDict[jobRuntime]["materialized_id"]; notebookHistory.MaterializedNotebookErrorInfo = notebookHistoriesDict[jobRuntime]["notebook_error"] as string; + notebookHistory.MaterializedNotebookName = notebookHistoriesDict[jobRuntime]["notebook_name"] as string; + notebookHistory.MaterializedNotebookPin = (bool)notebookHistoriesDict[jobRuntime]["pin"]; } notebookHistories.Add(notebookHistory); } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs index 1cf230a8..637193a7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs @@ -65,6 +65,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts public class AgentNotebookHistoryInfo : AgentJobHistoryInfo { public int MaterializedNotebookId { get; set; } + public bool MaterializedNotebookPin { get; set; } + public string MaterializedNotebookName { get; set; } public int MaterializedNotebookErrorFlag { get; set; } public string MaterializedNotebookErrorInfo { get; set; } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentNotebookRequest.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentNotebookRequest.cs index 1a7c2c95..b939c149 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentNotebookRequest.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentNotebookRequest.cs @@ -212,4 +212,48 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts RequestType Type = RequestType.Create("agent/notebooktemplate"); } + + /// + /// SQL Agent Notebook templates params + /// + public class UpdateAgentNotebookRunNameParams : TaskRequestDetails + { + public string OwnerUri { get; set; } + public int MaterializedId { get; set; } + public string MaterializedNotebookName { get; set; } + public string TargetDatabase { get; set; } + + } + + /// + /// SQL Agent Notebook templates request type + /// + public class UpdateAgentNotebookRunNameRequest + { + public static readonly + RequestType Type = + RequestType.Create("agent/updatenotebookname"); + } + + /// + /// SQL Agent Notebook templates params + /// + public class UpdateAgentNotebookRunPinParams : TaskRequestDetails + { + public string OwnerUri { get; set; } + public string MaterializedId { get; set; } + public bool MaterializedNotebookPin { get; set; } + public string TargetDatabase { get; set; } + + } + + /// + /// SQL Agent Notebook templates request type + /// + public class UpdateAgentNotebookRunPinRequest + { + public static readonly + RequestType Type = + RequestType.Create("agent/updatenotebookpin"); + } } \ No newline at end of file diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentNotebookHelper.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentNotebookHelper.cs index 45fb65d4..4420a50a 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentNotebookHelper.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentNotebookHelper.cs @@ -263,7 +263,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent materialized_id, run_time, run_date, - notebook_error + notebook_error, + pin, + notebook_name FROM notebooks.nb_materialized WHERE JOB_ID = @jobId"; @@ -406,9 +408,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent string notebookDatabaseSetupQueryString = @" IF NOT EXISTS ( - SELECT schema_name - FROM information_schema.schemata - WHERE schema_name = 'notebooks' ) + SELECT SCHEMA_NAME + FROM INFORMATION_SCHEMA.SCHEMATA + WHERE SCHEMA_NAME = 'notebooks' ) BEGIN EXEC sp_executesql N'CREATE SCHEMA notebooks' END @@ -434,7 +436,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent run_time VARCHAR(100), run_date VARCHAR(100), notebook NVARCHAR(MAX), - notebook_error NVARCHAR(MAX) + notebook_error NVARCHAR(MAX), + pin BIT NOT NULL DEFAULT 0, + notebook_name NVARCHAR(MAX) NOT NULL default('') ) END USE [msdb]; @@ -569,7 +573,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent string targetDatabase, string templateFileContents) { - String getNotebookTemplateQuery = + string getNotebookTemplateQuery = @" SELECT notebook from @@ -589,5 +593,53 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent DataRow templateDataRow = templateDataTable.Rows[0]; return templateDataRow["notebook"] as string; } + + public static async Task UpdateMaterializedNotebookName( + ConnectionInfo connInfo, + int materializedId, + string targetDatabase, + string name) + { + string updateMaterializedNotebookNameQuery = + @" + UPDATE notebooks.nb_materialized + SET + notebook_name = @notebookName + WHERE + materialized_id = @materializedId + "; + List updateMaterializedNotebookNameParams = new List(); + updateMaterializedNotebookNameParams.Add(new SqlParameter("notebookName", name)); + updateMaterializedNotebookNameParams.Add(new SqlParameter("materializedId", materializedId)); + await AgentNotebookHelper.ExecuteSqlQueries( + connInfo, + updateMaterializedNotebookNameQuery, + updateMaterializedNotebookNameParams, + targetDatabase); + } + + public static async Task UpdateMaterializedNotebookPin( + ConnectionInfo connInfo, + string materializedId, + string targetDatabase, + bool pin) + { + string updateMaterializedNotebookPinQuery = + @" + UPDATE notebooks.nb_materialized + SET + pin = @notebookPin + WHERE + materialized_id = @materializedId + "; + List updateMaterializedNotebookPinParams = new List(); + updateMaterializedNotebookPinParams.Add(new SqlParameter("notebookPin", pin)); + updateMaterializedNotebookPinParams.Add(new SqlParameter("materializedId", materializedId)); + await AgentNotebookHelper.ExecuteSqlQueries( + connInfo, + updateMaterializedNotebookPinQuery, + updateMaterializedNotebookPinParams, + targetDatabase); + } } } \ No newline at end of file