Skip to content

Commit 96fe8e6

Browse files
mauvoGoEddie
authored andcommitted
Make some internal state accessible. (GoEddie#22)
* Can check to see if CodeCoverage is running. * Can access exceptions handled internally by SQLCover. * Results object reports the data source the results came from.
1 parent 20180c5 commit 96fe8e6

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/SQLCover/SQLCover/CodeCoverage.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public class CodeCoverage
2121
private readonly bool _logging;
2222
private readonly SourceGateway _source;
2323
private CoverageResult _result;
24-
public const short TIMEOUT_EXPIRED = -2; //From TdsEnums
2524

25+
public const short TIMEOUT_EXPIRED = -2; //From TdsEnums
26+
public SqlCoverException Exception { get; private set; } = null;
27+
public bool IsStarted { get; private set; } = false;
2628

2729
private TraceController _trace;
2830

@@ -59,17 +61,22 @@ public CodeCoverage(string connectionString, string databaseName, string[] exclu
5961
_database = new DatabaseGateway(connectionString, databaseName);
6062
_source = new DatabaseSourceGateway(_database);
6163
}
62-
public bool Start()
64+
65+
public bool Start()
6366
{
67+
Exception = null;
6468
try
6569
{
6670
_trace = new TraceControllerBuilder().GetTraceController(_database, _databaseName, _traceType);
6771
_trace.Start();
72+
IsStarted = true;
6873
return true;
6974
}
7075
catch (Exception ex)
7176
{
7277
Debug("Error starting trace: {0}", ex);
78+
Exception = new SqlCoverException("SQL Cover failed to start.", ex);
79+
IsStarted = false;
7380
return false;
7481
}
7582
}
@@ -85,6 +92,11 @@ private List<string> StopInternal()
8592

8693
public CoverageResult Stop()
8794
{
95+
if(!IsStarted)
96+
throw new SqlCoverException("SQL Cover was not started, or did not start correctly.");
97+
98+
IsStarted = false;
99+
88100
WaitForTraceMaxLatency();
89101

90102
var results = StopInternal();
@@ -201,7 +213,7 @@ private void RunProcess(string exe, string args, string workingDir)
201213
private void GenerateResults(List<string> filter, List<string> xml)
202214
{
203215
var batches = _source.GetBatches(filter);
204-
_result = new CoverageResult(batches, xml, _databaseName);
216+
_result = new CoverageResult(batches, xml, _databaseName, _database.DataSource);
205217
}
206218

207219
public CoverageResult Results()

src/SQLCover/SQLCover/CoverageResult.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ namespace SQLCover
1414
public class CoverageResult : CoverageSummary
1515
{
1616
private readonly IEnumerable<Batch> _batches;
17-
private readonly string _database;
17+
18+
public string DatabaseName { get; }
19+
public string DataSource { get; }
1820

1921
private readonly StatementChecker _statementChecker = new StatementChecker();
2022

21-
public CoverageResult(IEnumerable<Batch> batches, List<string> xml, string database)
23+
public CoverageResult(IEnumerable<Batch> batches, List<string> xml, string database, string dataSource)
2224
{
2325
_batches = batches;
24-
_database = database;
26+
DatabaseName = database;
27+
DataSource = dataSource;
2528
var parser = new EventsParser(xml);
2629

2730
var statement = parser.GetNextStatement();
@@ -179,8 +182,8 @@ public string OpenCoverXml()
179182
, statements, coveredStatements, coveredStatements / (float) statements * 100.0);
180183

181184
builder.Append("<Module hash=\"ED-DE-ED-DE-ED-DE-ED-DE-ED-DE-ED-DE-ED-DE-ED-DE-ED-DE-ED-DE\">");
182-
builder.AppendFormat("<FullName>{0}</FullName>", _database);
183-
builder.AppendFormat("<ModuleName>{0}</ModuleName>", _database);
185+
builder.AppendFormat("<FullName>{0}</FullName>", DatabaseName);
186+
builder.AppendFormat("<ModuleName>{0}</ModuleName>", DatabaseName);
184187

185188
var fileMap = new Dictionary<string, int>();
186189
var i = 1;

src/SQLCover/SQLCover/Gateway/DatabaseGateway.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class DatabaseGateway
99
{
1010
private readonly string _connectionString;
1111
private readonly string _databaseName;
12+
private readonly SqlConnectionStringBuilder _connectionStringBuilder;
13+
public string DataSource { get { return _connectionStringBuilder.DataSource; } }
1214

1315
public DatabaseGateway()
1416
{
@@ -18,8 +20,9 @@ public DatabaseGateway(string connectionString, string databaseName)
1820
{
1921
_connectionString = connectionString;
2022
_databaseName = databaseName;
23+
_connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
2124
}
22-
25+
2326
public virtual string GetString(string query)
2427
{
2528
using (var conn = new SqlConnection(_connectionString))

src/SQLCover/SQLCover/SqlCoverException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace SQLCover
55
{
66
[Serializable]
7-
internal class SqlCoverException : Exception
7+
public class SqlCoverException : Exception
88
{
99
public SqlCoverException()
1010
{

0 commit comments

Comments
 (0)