Skip to content

Commit b258bcf

Browse files
Added logging
1 parent 7ecdbfe commit b258bcf

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ desktop.ini
1919
*.user
2020
*.7z
2121
*.swp
22+
*.zip
23+
data.csv
2224
/boost
2325
/scintilla
2426
/bin
27+
/SQL

SQLiteClassBuilder.cpp

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ bool FileExists(const std::string &path)
2222
return (stat(path.c_str(), &buffer) == 0);
2323
}
2424

25+
const std::string EarlyExitErrMsg = "\
26+
\
27+
**********************************************\
28+
Performing early exit due to error!\
29+
**********************************************\
30+
\
31+
";
32+
33+
34+
#define V_COUT(VB, V) {if (sqlite3pp::sql_base::GetVerbosityLevel() >= sqlite3pp::VBLV_##VB) {std::cout << V << std::endl;} }
35+
2536
int main(int argc, char* argv[])
2637
{
2738
cxxopts::Options options("SQLiteClassBuilder.exe", "SQLiteClassBuilder: Creates type safe SQL classes.\nBy David Maisonave (www.axter.com)");
2839
options
29-
.positional_help("[optional args]")
40+
//.positional_help("[optional args]")
3041
.allow_unrecognised_options()
3142
.show_positional_help();
3243

@@ -37,11 +48,10 @@ int main(int argc, char* argv[])
3748
// Common
3849
("d,database", "File name of the SQLite database. Required argument!! Example: SQLiteClassBuilder -d\"C:\\Data\\myDatabase.db\"", cxxopts::value<std::string>()->default_value(""))
3950
("w,where", "Optional: And-Where-Clause. Can be used to specify a set of tables/views to process.\nExample1:\nSQLiteClassBuilder -d\"my.db\" -w\"AND tbl_name like 'Personnel%'\"\nExample2:\nSQLiteClassBuilder -d\"my.db\" -w\"AND tbl_name NOT like 'zzTest%'\"", cxxopts::value<std::string>()->default_value(""))
40-
41-
("p,pause", "Pause before program exit.", cxxopts::value<bool>()->default_value("false"))
51+
("v,verbosity", "Verbosity level. Default: 3. 0=No-output; 1=Error-Output-Only; 2=Err+Warn; 3=Err+Wrn+Info; 4=Err+Wrn+Info+Debug; 5=Err+Wrn+Info+Debug+Details", cxxopts::value<int>()->default_value("3"))
52+
("p,pause", "Pause before program exit. Note: If verbosity level 0, program will exit without pause unless error occurs.", cxxopts::value<bool>()->default_value("true"))
4253
("h,help", "Print usage")
4354
// ("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false")) // ToDo: Delete this argument if it doesn't get used.
44-
// ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
4555
// ("c,clipboard", "Populate bla bla from clipboard", cxxopts::value<bool>()->default_value("false"), "bool")
4656
;
4757

@@ -55,7 +65,7 @@ int main(int argc, char* argv[])
5565
("g,xgetfnc", "Exclude class get functions. Default: class get_* method is created for each data member.", cxxopts::value<bool>()->default_value("false"))
5666
("s,xsetfnc", "Exclude class set functions. Default: class set_* method is created for each data member.", cxxopts::value<bool>()->default_value("false"))
5767
("o,xostream", "Exclude operator<< function. Default: An ostream operator<< function is created for each class.", cxxopts::value<bool>()->default_value("false"))
58-
("v,delimit", "Only used with opereator<<, and can be a desired string value to mark seperation between field output. ie: \", \", \", \", \" \", \"; \", \"\".", cxxopts::value<std::string>()->default_value(","))
68+
("e,delimit", "Only used with opereator<<, and can be a desired string value to mark seperation between field output. ie: \", \", \", \", \" \", \"; \", \"\".", cxxopts::value<std::string>()->default_value(","))
5969
("c,xcmmnt", "Exclude comments. Default: Usage comments are created for each class.", cxxopts::value<bool>()->default_value("false"))
6070
("x,xinterf", "Exclude SQLite3pp::Table interface. Default: A set of methods (getTableName, getColumnNames, getSelecColumnNames, and getStreamData) are created for each class in order to interface with SQLite3pp::Table.", cxxopts::value<bool>()->default_value("false"))
6171
("b,basic_t", "Only use C++ basic types (int, double, std::string, and std::wstring). Default: Use SQLite2 and SQLite3 sub types (Text, Integer, Blob, Clob, Date, Datetime, Boolean, Bigint, Varchar, Nchr, etc...).", cxxopts::value<bool>()->default_value("false"))
@@ -73,38 +83,93 @@ int main(int argc, char* argv[])
7383
("q,dhead", "Delete all sql_*.h files before creating headers.", cxxopts::value<bool>()->default_value("true"))
7484
;
7585
auto arg_result = options.parse(argc, argv);
76-
std::string dbname = arg_result["database"].as<std::string>();
77-
if (arg_result.count("help") || !dbname.size())
86+
const int VerbosityLevel = arg_result["verbosity"].as<int>();
87+
sqlite3pp::sql_base::SetVerbosityLevel(static_cast<sqlite3pp::VerbosityLevels>(VerbosityLevel));
88+
89+
std::string db_file_name = arg_result["database"].as<std::string>();
90+
if (db_file_name.size())
91+
V_COUT(DETAIL, "Using argument -d for DB file name. " << db_file_name)
92+
else if (argc > 1)
93+
{
94+
if (argv[1][0] != '-') // Check if it's the first argument
95+
{
96+
db_file_name = argv[1];
97+
V_COUT(DEBUG, "Using first argument for DB file name. " << db_file_name);
98+
}
99+
else if (argc > 3 && argv[argc-2][0] != '-') // Check if it's the last argument
100+
{
101+
db_file_name = argv[argc-1];
102+
V_COUT(DEBUG, "Using last argument for DB file name. " << db_file_name);
103+
}
104+
}
105+
106+
if (arg_result.count("help") || !db_file_name.size())
78107
{
79108
std::cout << options.help().c_str() << std::endl << std::endl;
80109
system("pause");
81110
exit(0);
82111
}
83112

113+
if (!FileExists(db_file_name))
114+
{
115+
V_COUT(ERROR, "Error: Could not find database file '" << db_file_name << "'." << std::endl);
116+
V_COUT(ERROR, EarlyExitErrMsg);
117+
exit(-1);
118+
}
119+
84120
std::string TargetPath = arg_result["folder"].as<std::string>();
85121
TargetPath = Common::rtrim(TargetPath, "\\");
86122
if (TargetPath.size())
87123
{
124+
V_COUT(DETAIL, "Using destination path '" << TargetPath << "'.");
88125
if (arg_result["rmdir"].as<bool>())
89-
_rmdir(TargetPath.c_str());
90-
if (!FileExists(TargetPath.c_str()))
126+
{
127+
if (sqlite3pp::SQLiteClassBuilder::dir_exists(TargetPath.c_str()))
128+
{
129+
V_COUT(INFO, "Removing directory '" << TargetPath << "' before creating headers.");
130+
_rmdir(TargetPath.c_str());
131+
}
132+
}
133+
134+
if (!sqlite3pp::SQLiteClassBuilder::dir_exists(TargetPath.c_str()))
135+
{
136+
V_COUT(INFO, "Creating target path '" << TargetPath << "'.");
91137
_mkdir(TargetPath.c_str());
138+
}
92139
}
140+
141+
std::string FileExt = arg_result["fileext"].as<std::string>().size() ? arg_result["fileext"].as<std::string>() : "h";
142+
V_COUT(DETAIL, "Files created will use file extension '" << FileExt << "'.");
93143

94144
if (arg_result["dhead"].as<bool>())
95145
{
96-
std::string Command = "del /F /Q " + TargetPath + "\\" + arg_result["prefix"].as<std::string>() + "*" + arg_result["postfix"].as<std::string>() + "." + arg_result["fileext"].as<std::string>();
146+
V_COUT(INFO, "Deleting exsiting headers.");
147+
std::string Command = "del /F /Q " + TargetPath + "\\" + arg_result["prefix"].as<std::string>() + "*" + arg_result["postfix"].as<std::string>() + "." + FileExt;
148+
V_COUT(DEBUG, "Deleting exsiting headers. by using command:" << Command);
97149
system(Command.c_str()); // ToDo: Create a cleaner method for this.
98150
}
99151

100152
//StrOptions
101153
sqlite3pp::StrOptions my_StrOptions = sqlite3pp::SqlBld::strOpt_std_string;
102154
if (arg_result["strtype"].as<std::string>() == "wstring" || arg_result["strtype"].as<std::string>() == "std::wstring")
155+
{
103156
my_StrOptions = sqlite3pp::SqlBld::strOpt_std_wstring;
157+
V_COUT(DETAIL, "Using string type." << my_StrOptions.str_type);
158+
}
104159
else if (arg_result["strtype"].as<std::string>() == "tstring")
160+
{
105161
my_StrOptions = sqlite3pp::SqlBld::strOpt_sql_tstring;
162+
V_COUT(DETAIL, "Using string type." << my_StrOptions.str_type);
163+
}
106164
else if (arg_result["strtype"].as<std::string>() == "tstring_t")
165+
{
107166
my_StrOptions = sqlite3pp::SqlBld::strOpt_sql_tstring_T;
167+
V_COUT(DETAIL, "Using string type." << my_StrOptions.str_type);
168+
}
169+
else
170+
{
171+
V_COUT(DETAIL, "Using default string type std::string.");
172+
}
108173

109174
// MiscOptions
110175
sqlite3pp::MiscOptions my_MiscOptions = sqlite3pp::SqlBld::MiscOpt_max;
@@ -122,21 +187,22 @@ int main(int argc, char* argv[])
122187
//HeaderOpt
123188
sqlite3pp::HeaderOpt my_HeaderOpt = sqlite3pp::SqlBld::HeaderDefaultOpt;
124189
my_HeaderOpt.dest_folder = TargetPath + "\\";
125-
my_HeaderOpt.file_type = arg_result["fileext"].as<std::string>().size() ? arg_result["fileext"].as<std::string>() : "h";
190+
my_HeaderOpt.file_type = FileExt;
126191
my_HeaderOpt.header_postfix = arg_result["postfix"].as<std::string>();
127192
my_HeaderOpt.header_prefix = arg_result["prefix"].as<std::string>();
128193
my_HeaderOpt.header_include = arg_result["include"].as<std::string>();
129194

130195
sqlite3pp::SQLiteClassBuilder
131196
createsqlitetableclass(
132-
dbname
197+
db_file_name
133198
, my_StrOptions
134199
, arg_result["where"].as<std::string>()
135200
, my_MiscOptions
136201
, my_HeaderOpt
137202
);
138203

139-
if (arg_result["pause"].as<bool>())
204+
V_COUT(INFO, "\n\n***************************************\nProcess Complete.\nCreated " << createsqlitetableclass.GetHeadersCreated().size() << " classes and headers.\n***************************************\n");
205+
if (arg_result["pause"].as<bool>() && VerbosityLevel)
140206
system("pause");
141207

142208
return 0;

0 commit comments

Comments
 (0)