Skip to content

Commit

Permalink
Update console runner
Browse files Browse the repository at this point in the history
  • Loading branch information
taddison committed Dec 20, 2017
1 parent 9ef19a2 commit 8f641e7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/SQLSpelunker.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <connection string> <sql script>");
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public SQLDatabaseDefinitionService(string connectionString)

_connectionString = connectionString;
_definitions = new Dictionary<StoredProcedure, string>();

// Test connection during construction
using(var conn = new SqlConnection(connectionString))
{
conn.Open();
}
}

public string GetStoredProcedureDefinition(StoredProcedure storedProcedure)
Expand Down
2 changes: 2 additions & 0 deletions src/SQLSpelunker.Core/SQLSpelunker.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>NU1701</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom" Version="14.0.3811.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
</ItemGroup>


</Project>

0 comments on commit 8f641e7

Please sign in to comment.