This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (110 loc) · 4.08 KB
/
Copy pathmain.cpp
File metadata and controls
137 lines (110 loc) · 4.08 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
// MAIN SOURCE
#include <iostream>
#include <fstream>
#include "checker.h"
#include "include/configuration.h"
#include "gflags/gflags.h"
namespace sqlcheck {
Configuration state;
} // namespace sqlcheck
DEFINE_bool(c, false, "Display warnings in color mode");
DEFINE_bool(color_mode, false, "Display warnings in color mode");
DEFINE_bool(v, false, "Display verbose warnings");
DEFINE_bool(verbose, false, "Display verbose warnings");
DEFINE_string(d, "", "Query delimiter string (default -- ;)");
DEFINE_string(delimiter, "", "Query delimiter string (default -- ;)");
DEFINE_bool(h, false, "Print help message");
DEFINE_uint64(r, sqlcheck::RISK_LEVEL_ALL,
"Set of anti-patterns to check \n"
"1 (all anti-patterns, default) \n"
"2 (only medium and high risk anti-patterns) \n"
"3 (only high risk anti-patterns) \n");
DEFINE_uint64(risk_level, sqlcheck::RISK_LEVEL_ALL,
"Set of anti-patterns to check \n"
"1 (all anti-patterns, default) \n"
"2 (only medium and high risk anti-patterns) \n"
"3 (only high risk anti-patterns) \n");
DEFINE_string(f, "", "SQL file name"); // standard input
DEFINE_string(file_name, "", "SQL file name"); // standard input
void ConfigureChecker(sqlcheck::Configuration &state) {
// Default Values
state.risk_level = sqlcheck::RISK_LEVEL_ALL;
state.file_name = "";
state.delimiter = ";";
state.testing_mode = false;
state.verbose = false;
state.color_mode = false;
// Configure checker
state.color_mode = FLAGS_c || FLAGS_color_mode;
state.verbose = FLAGS_v || FLAGS_verbose;
if(FLAGS_f.empty() == false){
state.file_name = FLAGS_f;
}
if(FLAGS_file_name.empty() == false){
state.file_name = FLAGS_file_name;
}
if(FLAGS_d.empty() == false){
state.delimiter = FLAGS_f;
}
if(FLAGS_delimiter.empty() == false){
state.delimiter = FLAGS_delimiter;
}
if(FLAGS_r != 0){
state.risk_level = (sqlcheck::RiskLevel) FLAGS_r;
}
if(FLAGS_risk_level != 0){
state.risk_level = (sqlcheck::RiskLevel) FLAGS_risk_level;
}
// Run validators
std::cout << "+-------------------------------------------------+\n"
<< "| SQLCHECK |\n"
<< "+-------------------------------------------------+\n";
ValidateRiskLevel(state);
ValidateFileName(state);
ValidateColorMode(state);
ValidateVerbose(state);
ValidateDelimiter(state);
std::cout << "-------------------------------------------------\n";
}
void Usage() {
std::cout <<
"Command line options : sqlcheck <options>\n"
" -f -file_name : SQL file name\n"
" -r -risk_level : Set of anti-patterns to check\n"
" : 1 (all anti-patterns, default) \n"
" : 2 (only medium and high risk anti-patterns) \n"
" : 3 (only high risk anti-patterns) \n"
" -c -color_mode : Display warnings in color mode \n"
" -v -verbose : Display verbose warnings \n"
" -d -delimiter : Query delimiter string (; by default) \n"
" -h -help : Print help message \n";
}
int main(int argc, char **argv) {
bool has_issues = false;
try {
// Parse the input arguments from the user
gflags::SetUsageMessage("");
gflags::SetVersionString("1.2.1");
gflags::ParseCommandLineFlags(&argc, &argv, true);
// Print help message
if(FLAGS_h == true){
FLAGS_h = false;
Usage();
gflags::ShutDownCommandLineFlags();
return (EXIT_SUCCESS);
}
// Customize the checker configuration
ConfigureChecker(sqlcheck::state);
// Invoke the checker
has_issues = sqlcheck::Check(sqlcheck::state);
}
// Catching at the top level ensures that
// destructors are always called
catch (std::exception& exc) {
std::cerr << exc.what() << std::endl;
gflags::ShutDownCommandLineFlags();
exit(EXIT_FAILURE);
}
gflags::ShutDownCommandLineFlags();
(has_issues) ? exit(EXIT_FAILURE) : exit(EXIT_SUCCESS);
}