-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (91 loc) · 4.97 KB
/
Program.cs
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
109
110
111
112
using Mono.Options;
namespace SqlChart;
class Program
{
static async Task Main(string[] args)
{
IDatabaseConnector? databaseConnector = null;
try
{
var options = new SqlChartOptions();
var optionSet = new OptionSet
{
{ "q|query=", "The SQL query to be executed", v => options.Query = v },
{ "d|db-type=", "Type of the database. Possible Values: MySQL, PostgreSQL, SQLServer, SQLite",
v => options.DbType = Enum.TryParse(v, true, out SqlChartOptions.DatabaseType dbType) ? dbType : throw new ArgumentException("Invalid database type") },
{ "c|connection-string=", "The connection string to the database", v => options.ConnectionString = v },
{ "t|chart-type=", "Type of the chart. Possible Values: Bar, Line, Histogram",
v => options.Chart = Enum.TryParse(v, true, out SqlChartOptions.ChartType chartType) ? chartType : throw new ArgumentException("Invalid chart type") },
{ "o|output=", "Output file path for the chart", v => options.Output = v },
{ "title=", "Title of the chart. Default Value is empty", v => options.Title = v },
{ "x-axis=", "Label for the X-axis. Default Value is empty", v => options.XAxis = v },
{ "y-axis=", "Label for the Y-axis. Default Value is empty", v => options.YAxis = v },
{ "width=", "Width of the chart in pixels. Default Value: 960", (int v) => options.Width = v },
{ "height=", "Height of the chart in pixels; Default Value: 540", (int v) => options.Height = v },
{ "color-scheme=", "Color scheme for the chart. Possible Values: Light, Dark. Default Value: Light",
v => options.Color = Enum.TryParse(v, true, out SqlChartOptions.ColorScheme colorScheme) ? colorScheme : throw new ArgumentException("Invalid color scheme") },
{ "ds|db-server=", "Database server address", v => options.DbServer = v },
{ "dn|db-name=", "Database name", v => options.DbName = v },
{ "du|db-user=", "Database user name", v => options.DbUser = v },
{ "dpw|db-password=", "Database password", v => options.DbPassword = v },
{ "h|help", "show this message and exit", v => options.Help = v != null },
};
// parse the options
optionSet.Parse(args);
// Validate the options after parsing
options.Validate();
// show help
if (options.Help)
optionSet.WriteOptionDescriptions (Console.Out);
// assign the database
switch (options.DbType)
{
case SqlChartOptions.DatabaseType.MySQL:
databaseConnector = new MySqlDatabaseConnector();
break;
case SqlChartOptions.DatabaseType.PostgreSQL:
databaseConnector = new PostgreSqlDatabaseConnector();
break;
case SqlChartOptions.DatabaseType.SQLServer:
databaseConnector = new SqlServerDatabaseConnector();
break;
case SqlChartOptions.DatabaseType.SQLLite:
databaseConnector = new SqlLiteDatabaseConnector();
break;
default:
throw new ArgumentException("Invalid/ Unsupported SQL Database");
}
// connect to database
databaseConnector.Connect(options.DbConnectionString);
// create instace of chart
Charter chart = new Charter(options);
// get data from database
OrderedDictionary<string, List<object>>? data = await databaseConnector.RunQuery(options.Query);
if (data == null || data.Count == 0)
throw new ArgumentException("The provided query didn't generate any output");
chart.AddData(data);
// discountect from database
databaseConnector.Disconnect();
// save chart picture
chart.Save(options.Output, options.Width, options.Height);
}
catch (Exception e)
{
if (e.GetType() == typeof(AggregateException))
{
foreach (Exception innerException in ((AggregateException) e).InnerExceptions)
{
Console.WriteLine(innerException.Message);
}
}
else
{
Console.WriteLine($"Error: {e.Message}");
}
Console.WriteLine("Try 'sqlchart --help' for more information.");
if (databaseConnector != null)
databaseConnector.Disconnect();
return;
}
}
}