diff --git a/src/SQLSpelunker.Console/Program.cs b/src/SQLSpelunker.Console/Program.cs index 32ddb50..88f0f6b 100644 --- a/src/SQLSpelunker.Console/Program.cs +++ b/src/SQLSpelunker.Console/Program.cs @@ -8,21 +8,59 @@ public class Program { static void Main(string[] args) { - var connectionString = "server=localhost;initial catalog=tsqlscheduler;integrated security=sspi"; - var sqlScript = "exec scheduler.UpsertJobsForAllTasks"; + var connectionString = string.Empty; + var sqlScript = string.Empty; - if(args.Length > 0) + if(args.Length == 2) { connectionString = args[0]; sqlScript = args[1]; } + else + { + WriteError("Invalid arguments"); + WriteError("dotnet run "); + return; + } - var builder = new SqlConnectionStringBuilder(connectionString); + SqlConnectionStringBuilder builder = null; + try + { + builder = new SqlConnectionStringBuilder(connectionString); + } + catch (ArgumentException ae) + { + WriteError($"Connection string is not valid: {ae.Message}"); + return; + } var database = builder.InitialCatalog; - var sw = new ScriptWalker(new SQLDatabaseDefinitionService(connectionString)); + if(string.IsNullOrWhiteSpace(database)) + { + System.Console.WriteLine("Connection string does not specify database, defaulting to master"); + database = "master"; + } + + // Establish connection + ScriptWalker sw = null; + try + { + sw = new ScriptWalker(new SQLDatabaseDefinitionService(connectionString)); + } + catch (SqlException sx) + { + WriteError($"Unable to connect to database: {sx.Message}"); + return; + } var callChain = sw.GetCalledProcedures(sqlScript, database); System.Console.WriteLine(callChain.GetCallHierarchy()); } + + private static void WriteError(string error) + { + var originalColour = System.Console.ForegroundColor; + System.Console.Error.WriteLine(error); + System.Console.ForegroundColor = originalColour; + } } } diff --git a/src/SQLSpelunker.Core/Definitions/SQLDatabaseDefinitionService.cs b/src/SQLSpelunker.Core/Definitions/SQLDatabaseDefinitionService.cs index cb3a40f..6099c38 100644 --- a/src/SQLSpelunker.Core/Definitions/SQLDatabaseDefinitionService.cs +++ b/src/SQLSpelunker.Core/Definitions/SQLDatabaseDefinitionService.cs @@ -19,6 +19,12 @@ public SQLDatabaseDefinitionService(string connectionString) _connectionString = connectionString; _definitions = new Dictionary(); + + // Test connection during construction + using(var conn = new SqlConnection(connectionString)) + { + conn.Open(); + } } public string GetStoredProcedureDefinition(StoredProcedure storedProcedure) diff --git a/src/SQLSpelunker.Core/SQLSpelunker.Core.csproj b/src/SQLSpelunker.Core/SQLSpelunker.Core.csproj index 8af5947..4bad15f 100644 --- a/src/SQLSpelunker.Core/SQLSpelunker.Core.csproj +++ b/src/SQLSpelunker.Core/SQLSpelunker.Core.csproj @@ -2,6 +2,7 @@ netstandard2.0 + NU1701 @@ -9,4 +10,5 @@ +