Skip to content

Commit 20d1869

Browse files
authored
Merge pull request #2 from bornsql/bump
Bug fixes and refactoring
2 parents 2b1734f + b03eca0 commit 20d1869

File tree

8 files changed

+28
-98
lines changed

8 files changed

+28
-98
lines changed

SqlExcelExporter/DatabaseHelper.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ private List<ResultEntity> RunStandaloneScript(string connectionString, string d
6363
{
6464
try
6565
{
66+
var sqlBatch = string.Empty;
67+
var cmd = new SqlCommand(string.Empty, cn);
68+
cn.Open();
69+
6670
if (!database.Equals("master", StringComparison.InvariantCultureIgnoreCase))
6771
{
6872
cn.ChangeDatabase(database);
6973
}
7074

71-
var sqlBatch = string.Empty;
72-
var cmd = new SqlCommand(string.Empty, cn);
73-
cn.Open();
74-
7575
script += $"{Environment.NewLine}GO"; // make sure last batch is executed.
7676

7777
foreach (var line in script.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
@@ -183,17 +183,11 @@ private void InstallStoredProcedure(string connectionString, string database, st
183183
/// <summary>
184184
/// Runs a single ad hoc script against a target database
185185
/// </summary>
186+
/// <param name="connectionString">The connection string</param>
186187
/// <param name="database">The target database</param>
187188
/// <param name="scriptName">The name of the script for the Excel worksheet</param>
188189
/// <param name="script">The script file</param>
189-
/// <returns>A result that will be parsed by the file writer</returns>
190-
public ResultEntity RunScript(string database, string scriptName, FileInfo script)
191-
{
192-
return File.Exists(script.FullName)
193-
? RunScript(m_connEntity.ToString(), database, scriptName, File.ReadAllText(script.FullName))
194-
: null;
195-
}
196-
190+
/// <returns></returns>
197191
private ResultEntity RunScript(string connectionString, string database, string scriptName, string script)
198192
{
199193
var results = new ResultEntity

SqlExcelExporter/Entities/ConnectionEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ConnectionEntity
88
/// <summary>
99
/// Generates a data source string based on instance and port settings.
1010
/// </summary>
11-
public string DataSource => !string.IsNullOrEmpty(InstanceName)
11+
private string DataSource => !string.IsNullOrEmpty(InstanceName)
1212
? $"{ServerName}\\{InstanceName}"
1313
: (Port != 1433 ? $"{ServerName},{Port}" : $"{ServerName}");
1414

SqlExcelExporter/FileWriter.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

SqlExcelExporter/SqlExcelExporter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<ReleaseVersion>0.2</ReleaseVersion>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0" />
9-
<PackageReference Include="ClosedXML" Version="0.95.3" />
8+
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.1" />
9+
<PackageReference Include="ClosedXML" Version="0.95.4" />
1010
</ItemGroup>
1111
<ItemGroup>
1212
<Folder Include="Entities\" />

SqlExcelExporter/Tools.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ public static class Tools
1414
/// <returns>The object as type T</returns>
1515
public static T ReadJsonItem<T>(FileInfo file)
1616
{
17-
if (!File.Exists(file.FullName))
18-
{
19-
return default;
20-
}
21-
22-
return JsonSerializer.Deserialize<T>(File.ReadAllText(file.FullName));
17+
return !File.Exists(file.FullName)
18+
? default
19+
: JsonSerializer.Deserialize<T>(File.ReadAllText(file.FullName));
2320
}
2421

2522
/// <summary>

SqlExcelExporter/WorkbookManager.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ public WorkbookManager(List<ResultEntity> results)
3939
}
4040
}
4141

42-
private static string RemoveSpecialCharacters(String str)
42+
private static string RemoveSpecialCharacters(string str)
4343
{
4444
var sb = new StringBuilder();
45-
foreach (var c in str)
45+
foreach (var c in str.Where(c => char.IsLetterOrDigit(c) || c == '.' || c == '_'))
4646
{
47-
if (char.IsLetterOrDigit(c) || c == '.' || c == '_')
48-
{
49-
sb.Append(c);
50-
}
47+
sb.Append(c);
5148
}
5249
return sb.ToString();
5350
}
@@ -56,7 +53,7 @@ private static string GetMaxLengthSheetName(string workbook)
5653
{
5754
var w = RemoveSpecialCharacters(workbook).Trim();
5855

59-
return w.Length > 31 ? w.Substring(0, 31) : w;
56+
return w.Length > 31 ? w[..31] : w;
6057
}
6158

6259
public FileInfo PrepareInstanceWorkbook()
@@ -77,10 +74,10 @@ public FileInfo PrepareInstanceWorkbook()
7774
{
7875
var s = ctr.ToString().Trim();
7976

80-
worksheetName = worksheetName + s;
77+
worksheetName += s;
8178
if (worksheetName.Length > 31)
8279
{
83-
worksheetName = worksheetName.Substring(0, 31 - s.Length) + s;
80+
worksheetName = worksheetName[..(31 - s.Length)] + s;
8481
}
8582
}
8683

@@ -97,10 +94,10 @@ public FileInfo PrepareInstanceWorkbook()
9794
{
9895
for (var c = 0; c < dr.Results.Columns.Count; c++)
9996
{
100-
var cellValue = dr.Results.Rows[r][c].ToString().Trim();
101-
if (cellValue.Length > 32767)
97+
var cellValue = dr.Results.Rows[r][c].ToString()?.Trim();
98+
if (cellValue != null && cellValue.Length > 32767)
10299
{
103-
cellValue = cellValue.Substring(0, 32767);
100+
cellValue = cellValue[..32767];
104101
}
105102

106103
worksheet.Cell(r + 2, c + 1).Value = cellValue;
@@ -138,24 +135,6 @@ public FileInfo PrepareInstanceWorkbook()
138135
return filename;
139136
}
140137

141-
public List<FileInfo> PrepareDatabaseWorkbooks(ConfigurationEntity config)
142-
{
143-
var xls = new List<FileInfo>();
144-
145-
foreach (var database in m_databaseResults)
146-
{
147-
// Create a new file per database
148-
var workbook = PrepareExcel(database.Value, config);
149-
150-
if (workbook != null)
151-
{
152-
xls.Add(workbook);
153-
}
154-
}
155-
156-
return xls;
157-
}
158-
159138
public FileInfo PrepareExcel(List<ResultEntity> results, ConfigurationEntity config)
160139
{
161140
var database = results.First().Database;
@@ -185,10 +164,10 @@ public FileInfo PrepareExcel(List<ResultEntity> results, ConfigurationEntity con
185164
{
186165
var s = ctr.ToString().Trim();
187166

188-
worksheetName = worksheetName + s;
167+
worksheetName += s;
189168
if (worksheetName.Length > 31)
190169
{
191-
worksheetName = worksheetName.Substring(0, 31 - s.Length) + s;
170+
worksheetName = worksheetName[..(31 - s.Length)] + s;
192171
}
193172
}
194173

@@ -205,10 +184,10 @@ public FileInfo PrepareExcel(List<ResultEntity> results, ConfigurationEntity con
205184
{
206185
for (var c = 0; c < dr.Results.Columns.Count; c++)
207186
{
208-
var cellValue = dr.Results.Rows[r][c].ToString().Trim();
187+
var cellValue = dr.Results.Rows[r][c].ToString()?.Trim();
209188
if (cellValue.Length > 32767)
210189
{
211-
cellValue = cellValue.Substring(0, 32767);
190+
cellValue = cellValue[..32767];
212191
}
213192

214193
worksheet.Cell(r + 2, c + 1).Value = cellValue;

sql2xls/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Program
1313

1414
private static void Main()
1515
{
16-
Console.WriteLine("--- SQL to Excel Exporter v0.2.1");
16+
Console.WriteLine("--- SQL to Excel Exporter v0.2.2");
1717
Console.WriteLine("--- Copyright (c) Born SQL");
1818
Console.WriteLine("--- Written by Randolph West and other contributors. https://bornsql.ca/.");
1919
Console.WriteLine();
@@ -99,8 +99,7 @@ private static void Main()
9999
var mgr = new WorkbookManager(results);
100100
var f = mgr.PrepareExcel(results, m_config);
101101

102-
Console.WriteLine(
103-
$"Excel file [{f.Name}] generated in folder [{(Tools.IsWindows() ? m_config.ExportFilePathWin : m_config.ExportFilePathMac)}].");
102+
Console.WriteLine($"Excel file [{f.Name}] generated in folder [{(Tools.IsWindows() ? m_config.ExportFilePathWin : m_config.ExportFilePathMac)}].");
104103

105104
Console.WriteLine("Press any key to exit.");
106105
Console.ReadKey();

sql2xls/sql2xls.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<PublishTrimmed>true</PublishTrimmed>
7-
<ReleaseVersion>0.2</ReleaseVersion>
7+
<ReleaseVersion>0.2.2</ReleaseVersion>
88
</PropertyGroup>
99

1010
<ItemGroup>

0 commit comments

Comments
 (0)