Address warnings and (some) nullables (#2013)

This commit is contained in:
Cheena Malhotra
2023-04-18 20:57:13 -07:00
committed by GitHub
parent d56f2309da
commit 648d7dbd3c
83 changed files with 674 additions and 588 deletions

View File

@@ -1100,7 +1100,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
}
const string UrnFormatStr = "Server/JobServer[@Name='{0}']/Job[@Name='{1}']/Schedule[@Name='{2}']";
string serverName = dataContainer.Server.Name.ToUpper();
string serverName = dataContainer.Server.Name.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
string scheduleUrn = string.Format(UrnFormatStr, serverName, jobData.Job.Name, schedule.Name);
STParameters param = new STParameters(dataContainer.Document);
@@ -1132,7 +1132,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo);
dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
XmlDocument jobDoc = CreateJobXmlDocument(dataContainer.Server.Name.ToUpper(), jobName);
XmlDocument jobDoc = CreateJobXmlDocument(dataContainer.Server.Name.ToUpper(System.Globalization.CultureInfo.InvariantCulture), jobName);
dataContainer.Init(jobDoc.InnerXml);
STParameters param = new STParameters(dataContainer.Document);
@@ -1487,9 +1487,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
{
string jobRuntime = jobHistory.RunDate.ToString("yyyyMMddHHmmss");
AgentNotebookHistoryInfo notebookHistory = jobHistory;
if (notebookHistoriesDict.ContainsKey(jobRuntime))
if (notebookHistoriesDict.TryGetValue(jobRuntime, out DataRow dataRow))
{
notebookHistory.MaterializedNotebookId = (int)notebookHistoriesDict[jobRuntime]["materialized_id"];
notebookHistory.MaterializedNotebookId = (int)dataRow["materialized_id"];
notebookHistory.MaterializedNotebookErrorInfo = notebookHistoriesDict[jobRuntime]["notebook_error"] as string;
notebookHistory.MaterializedNotebookName = notebookHistoriesDict[jobRuntime]["notebook_name"] as string;
notebookHistory.MaterializedNotebookPin = (bool)notebookHistoriesDict[jobRuntime]["pin"];

View File

@@ -3,8 +3,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -32,54 +30,56 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
{
this.data = data;
var availableSystems =
dataContainer.Server.JobServer.EnumSubSystems()
dataContainer.Server?.JobServer.EnumSubSystems()
.Rows.OfType<DataRow>()
.Select(r => (AgentSubSystem)Convert.ToInt32(r["subsystem_id"]));
foreach (var agentSubSystemId in availableSystems)
if (availableSystems != null)
{
var agentSubSystem = CreateJobStepSubSystem(agentSubSystemId, dataContainer, data);
// The server might have some new subsystem we don't know about, just ignore it.
if (agentSubSystem != null)
foreach (var agentSubSystemId in availableSystems)
{
subSystems[agentSubSystemId] = agentSubSystem;
var agentSubSystem = CreateJobStepSubSystem(agentSubSystemId, dataContainer, data);
// The server might have some new subsystem we don't know about, just ignore it.
if (agentSubSystem != null)
{
subSystems[agentSubSystemId] = agentSubSystem;
}
}
}
}
}
public JobStepSubSystem[] AvailableSubSystems
{
get { return this.subSystems.Keys.OrderBy(k => (int) k).Select(k => this.subSystems[k]).ToArray(); }
get { return this.subSystems.Keys.OrderBy(k => (int)k).Select(k => this.subSystems[k]).ToArray(); }
}
public JobStepSubSystem Lookup(AgentSubSystem key)
{
JobStepSubSystem rv = null;
if (this.subSystems.ContainsKey(key))
if (this.subSystems.TryGetValue(key, out JobStepSubSystem? value))
{
return this.subSystems[key];
return value;
}
return rv;
}
private static TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof (AgentSubSystem));
private static TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(AgentSubSystem));
// Returns name of the subsystem for a given enum value
public static string LookupFriendlyName(AgentSubSystem key)
{
return (string)typeConverter.ConvertToString((Enum)key);
}
// Returns name of the subsystem for a given enum value
public static string LookupName(AgentSubSystem key)
{
// Have to subtract first enum value to bring the
// index to 0-based index
return typeConverter.ConvertToInvariantString((Enum) key);
return typeConverter.ConvertToInvariantString((Enum)key);
}
private static JobStepSubSystem CreateJobStepSubSystem(
AgentSubSystem agentSubSystem,
CDataContainer dataContainer,
AgentSubSystem agentSubSystem,
CDataContainer dataContainer,
JobStepData data)
{
switch (agentSubSystem)

View File

@@ -95,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
if (jobData.Job != null)
{
const string UrnFormatStr = "Server/JobServer[@Name='{0}']/Job[@Name='{1}']/Step[@Name='{2}']";
string serverName = this.DataContainer.Server.Name.ToUpper();
string serverName = this.DataContainer.Server.Name.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
string urn = string.Format(UrnFormatStr, serverName, jobData.Job.Name, stepName);
jobStep = jobData.Job.Parent.Parent.GetSmoObject(urn) as JobStep;
}

View File

@@ -24,7 +24,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
/// simple job schedule structure.
/// </summary>
public struct SimpleJobSchedule
public partial struct SimpleJobSchedule
{
#region consts
private const int EndOfDay = 235959;
@@ -44,7 +44,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
private FrequencyTypes frequencyTypes;
private FrequencySubDayTypes frequencySubDayTypes;
private FrequencyRelativeIntervals frequencyRelativeIntervals;
private System.Boolean isEnabled;
private bool isEnabled;
#endregion
#region Init
@@ -100,7 +100,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
public static DateTime ConvertIntToDateTime(int source)
{
return new DateTime(source / 10000
, (source / 100) % 100
, source / 100 % 100
, source % 100);
}
/// <summary>
@@ -126,7 +126,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
public static TimeSpan ConvertIntToTimeSpan(int source)
{
return new TimeSpan(source / 10000
, (source / 100) % 100
, source / 100 % 100
, source % 100);
}
/// <summary>
@@ -150,13 +150,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
/// <returns>JobScheduleData object</returns>
public JobScheduleData ToJobScheduleData()
{
JobScheduleData data = new JobScheduleData();
var data = new JobScheduleData();
data.Name = this.Name;
data.Enabled = this.IsEnabled;
data.ActiveStartDate = SimpleJobSchedule.ConvertIntToDateLocalized(this.ActiveStartDate);
data.ActiveStartTime = SimpleJobSchedule.ConvertIntToTimeSpan(this.ActiveStartTimeOfDay);
data.ActiveEndDate = SimpleJobSchedule.ConvertIntToDateLocalized(this.ActiveEndDate);
data.ActiveEndTime = SimpleJobSchedule.ConvertIntToTimeSpan(this.ActiveEndTimeOfDay);
data.ActiveStartDate = ConvertIntToDateLocalized(this.ActiveStartDate);
data.ActiveStartTime = ConvertIntToTimeSpan(this.ActiveStartTimeOfDay);
data.ActiveEndDate = ConvertIntToDateLocalized(this.ActiveEndDate);
data.ActiveEndTime = ConvertIntToTimeSpan(this.ActiveEndTimeOfDay);
data.FrequencyTypes = this.FrequencyTypes;
data.FrequencyInterval = this.FrequencyInterval;
data.FrequencyRecurranceFactor = this.FrequencyRecurrenceFactor;
@@ -173,15 +173,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
/// <returns>new SimpleJobSchedule</returns>
public static SimpleJobSchedule FromJobScheduleData(JobScheduleData source)
{
SimpleJobSchedule schedule = new SimpleJobSchedule();
var schedule = new SimpleJobSchedule();
schedule.Name = source.Name;
schedule.ID = source.ID;
schedule.IsEnabled = source.Enabled;
schedule.ActiveStartDate = SimpleJobSchedule.ConvertDateTimeToInt(source.ActiveStartDate);
schedule.ActiveStartTimeOfDay = SimpleJobSchedule.ConvertTimeSpanToInt(source.ActiveStartTime);
schedule.ActiveEndDate = SimpleJobSchedule.ConvertDateTimeToInt(source.ActiveEndDate);
schedule.ActiveEndTimeOfDay = SimpleJobSchedule.ConvertTimeSpanToInt(source.ActiveEndTime);
schedule.ActiveStartDate = ConvertDateTimeToInt(source.ActiveStartDate);
schedule.ActiveStartTimeOfDay = ConvertTimeSpanToInt(source.ActiveStartTime);
schedule.ActiveEndDate = ConvertDateTimeToInt(source.ActiveEndDate);
schedule.ActiveEndTimeOfDay = ConvertTimeSpanToInt(source.ActiveEndTime);
schedule.FrequencyTypes = source.FrequencyTypes;
schedule.FrequencyInterval = source.FrequencyInterval;
schedule.FrequencyRecurrenceFactor = source.FrequencyRecurranceFactor;
@@ -293,7 +293,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return string.Empty;
}
StringBuilder daysOfWeek = new StringBuilder();
var daysOfWeek = new StringBuilder();
// Start matching with Monday. GetLocalizedDaysOfWeek() must start with Monday too.
WeekDays dayOfWeek = WeekDays.Monday;
foreach (string localizedDayOfWeek in GetLocalizedDaysOfWeek())
@@ -309,7 +309,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
// There's no easy way to advance to the next enum value, so since we know
// it's a bitfield mask we do a left shift ourselves.
int nextDay = ((int)dayOfWeek) << 1;
int nextDay = (int)dayOfWeek << 1;
dayOfWeek = (WeekDays)nextDay;
if (dayOfWeek > WeekDays.Saturday)
{
@@ -449,7 +449,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
{
get
{
MonthlyRelativeWeekDays relativeDays = (MonthlyRelativeWeekDays) this.FrequencyInterval;
var relativeDays = (MonthlyRelativeWeekDays) this.FrequencyInterval;
switch (relativeDays)
{
@@ -671,10 +671,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
/// <returns></returns>
string ExpandFormatString(string format)
{
StringBuilder stringBuilder = new StringBuilder();
var stringBuilder = new StringBuilder();
int lastIndex = 0;
MatchCollection matches = Regex.Matches(format, @"\{(?<property>\w+)\}");
MatchCollection matches = GetPropertyDescriptorRegex().Matches(format);
if (matches.Count > 0)
{
@@ -687,7 +687,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
if (property != null)
{
object propertyValue = property.GetValue(this);
propertyValue = propertyValue != null ? propertyValue.ToString() : String.Empty;
propertyValue = propertyValue != null ? propertyValue.ToString() : string.Empty;
stringBuilder.Append(format.Substring(lastIndex, match.Index - lastIndex));
stringBuilder.Append(propertyValue as string);
@@ -705,6 +705,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
return new string[] { "SR.Monday", "SR.Tuesday", "SR.Wednesday", "SR.Thursday", "SR.Friday", "SR.Saturday", "SR.Sunday" };
}
[GeneratedRegex("\\{(?<property>\\w+)\\}")]
private static partial Regex GetPropertyDescriptorRegex();
#endregion
}