-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (91 loc) · 3.74 KB
/
Program.cs
File metadata and controls
108 lines (91 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.IO;
using SqlExcelExporter;
using SqlExcelExporter.Entities;
namespace SqlToExcelExporter
{
class Program
{
private static ConnectionEntity m_connectionSettings;
private static ConfigurationEntity m_config;
private static void Main()
{
Console.WriteLine("--- SQL to Excel Exporter v0.2.2");
Console.WriteLine("--- Copyright (c) Born SQL");
Console.WriteLine("--- Written by Randolph West and other contributors. https://bornsql.ca/.");
Console.WriteLine();
var path = Directory.GetCurrentDirectory();
var errorConnection = false;
var errorDiagnostics = false;
string error;
var results = new List<ResultEntity>();
try
{
m_connectionSettings = Tools.ReadJsonItem<ConnectionEntity>(new FileInfo("connection.json"));
}
catch (Exception ex)
{
error = ex.Message;
errorConnection = true;
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] *** ERROR *** An error occurred validating the connection.json file [{error}].");
}
try
{
m_config = Tools.ReadJsonItem<ConfigurationEntity>(new FileInfo(Path.Combine(path, "config.json")));
}
catch (Exception ex)
{
error = ex.Message;
errorDiagnostics = true;
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] *** ERROR *** An error occurred validating the config.json file [{error}].");
}
if (errorConnection || errorDiagnostics)
{
Console.WriteLine("Exiting.");
return;
}
var database = m_config.DefaultDatabase;
// Default value
if (string.IsNullOrEmpty(database))
{
database = "master";
}
var dbh = new DatabaseHelper(m_connectionSettings);
// Test connection
if (!dbh.TestConnection())
{
return;
}
// Find stored procedure files
var sp = new DirectoryInfo(m_config.StoredProcedureFolder);
if (!string.IsNullOrEmpty(sp.Name) && Directory.Exists(sp.FullName))
{
foreach (var file in sp.GetFiles("*.sql", SearchOption.TopDirectoryOnly))
{
Console.WriteLine($"Installing stored procedure from file [{file.Name}] in database [{database}]...");
dbh.InstallStoredProcedure(database, file);
}
}
// Find ad hoc scripts
var di = new DirectoryInfo(m_config.AdHocScriptFolder);
if (string.IsNullOrEmpty(di.Name) || !Directory.Exists(di.FullName))
{
Console.WriteLine("No script files, or path does not exist.");
}
else
{
foreach (var fi in di.GetFiles("*.sql", SearchOption.TopDirectoryOnly))
{
Console.WriteLine($"Running script [{fi.Name}] in database [{database}]");
results.AddRange(dbh.RunStandaloneScript(database, fi.Name.Replace(fi.Extension, ""), fi));
}
}
var mgr = new WorkbookManager(results);
var f = mgr.PrepareExcel(results, m_config);
Console.WriteLine($"Excel file [{f.Name}] generated in folder [{(Tools.IsWindows() ? m_config.ExportFilePathWin : m_config.ExportFilePathMac)}].");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}