-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (126 loc) · 4.64 KB
/
Copy pathProgram.cs
File metadata and controls
155 lines (126 loc) · 4.64 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Configuration;
using System.Reflection;
using QueryExecution.Dapper.CommandFactory;
var queries = typeof(Program).Assembly.GetTypes().Where(t => t.IsAssignableTo(typeof(ISQLQueryCommand)));
var dbList = new Dictionary<string, string>();
var allQueryList = new Dictionary<string, Dictionary<string, ExecutionQuery>>();
foreach (var query in queries.OrderBy(q => q.GetCustomAttribute<SQLQueryCommandAttribute>()?.Group ?? 0).ThenBy(q => q.Name))
{
var queryIdAttr = query.GetCustomAttribute<SQLQueryCommandAttribute>();
var dbName = query.Namespace.Split(".").Last();
var executionQuery = new ExecutionQuery
{
Database = dbName,
Name = query.Name,
Description = queryIdAttr?.Description,
QueryType = query,
ConnectionString = ConfigurationManager.ConnectionStrings[dbName].ConnectionString
};
if (!allQueryList.TryGetValue(dbName, out Dictionary<string, ExecutionQuery> queryList))
{
queryList = new Dictionary<string, ExecutionQuery>();
allQueryList.Add(dbName, queryList);
dbList.Add((dbList.Count + 1).ToString(), dbName);
}
queryList.Add((queryList.Count + 1).ToString(), executionQuery);
}
do
{
Console.Clear();
Console.WriteLine("Database list:");
var idx = 0;
foreach (var db in dbList)
{
Console.WriteLine($" {db.Key} => {db.Value}");
}
Console.WriteLine();
Console.WriteLine("Enter the database identifier and press ENTER, to finish type END.");
Console.WriteLine("Database identifier:");
var idDb = Console.ReadLine();
if (string.Equals(idDb, "end", StringComparison.OrdinalIgnoreCase))
{
break;
}
if (dbList.TryGetValue(idDb, out string dbName))
{
var queryList = allQueryList[dbName];
do
{
Console.Clear();
Console.WriteLine("Query list:");
const int rowMax = 25;
idx = 0;
int step = 0;
foreach (var item in queryList)
{
idx++;
Console.WriteLine($" {item.Key} => {item.Value.Name}");
if (!string.IsNullOrWhiteSpace(item.Value.Description))
{
idx++;
Console.WriteLine(item.Value.Description);
}
if (idx == rowMax && (queryList.Count - (rowMax * step)) > rowMax)
{
Console.WriteLine("Press Enter to list other queries...");
Console.ReadLine();
idx = 0;
step++;
}
}
Console.WriteLine();
Console.WriteLine("Enter the identifier of the query you want to execute and press ENTER, finally write END.");
Console.WriteLine("Query identifier:");
var idQuery = Console.ReadLine();
if (string.Equals(idQuery, "end", StringComparison.OrdinalIgnoreCase))
{
break;
}
if (queryList.TryGetValue(idQuery, out ExecutionQuery query))
{
Console.WriteLine();
Console.WriteLine($"Query execution: {query.Name} ({query.Description})");
Console.WriteLine();
try
{
var queryInstance = (IEnumerable<string>)Activator.CreateInstance(query.QueryType, new SQLQueryCommandFactory(query.ConnectionString));
foreach (var item in queryInstance)
{
if (item == "BREAK")
{
Console.ReadLine();
}
else
{
Console.WriteLine(item);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"ERRORE: {ex.Message}");
}
Console.WriteLine();
Console.WriteLine("Query completed. Press ENTER to continue.");
}
else
{
Console.WriteLine("Query identifier not valid. Press ENTER to continue.");
}
Console.ReadLine();
} while (true);
}
else
{
Console.WriteLine("Database identifier not valid. Press ENTER to continue.");
Console.ReadLine();
}
} while (true);
class ExecutionQuery
{
public string Database { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Type QueryType { get; set; }
public string ConnectionString { get; set; }
}