Skip to content

Commit 04f5aa7

Browse files
Added example usage to class builder logic
1 parent b9aaba4 commit 04f5aa7

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

sqlite3pp_ez.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
GNU General Public License
33
44
Copyright (C) 2021 David Maisonave (www.axter.com)
5-
The RegexAssistant source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
5+
The sqlite3pp_ez source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
66
This source code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
77
88
For usage examples see https://github.com/David-Maisonave/sqlite3pp_EZ
@@ -376,8 +376,10 @@ namespace sqlite3pp
376376
{
377377
return CreateAllHeaders(m_options, WhereClause);
378378
}
379+
static const char TopHeaderCommnetsPrt1[] = "/* This file was automatically generated using [Sqlite3pp_EZ].\nSqlite3pp_EZ Copyright (C) 2021 David Maisonave (http::\\www.axter.com)";
380+
static const char TopHeaderCommnetsPrt2[] = "For more details see https://github.com/David-Maisonave/sqlite3pp_EZ\n*/";
379381

380-
bool SQLiteClassBuilder::CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, bool AppendToVect)
382+
bool SQLiteClassBuilder::CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName, std::string LastColumnName, bool AppendToVect)
381383
{
382384
std::ios_base::openmode openMode = m_AppendTableToHeader ? std::ios_base::out | std::ios_base::app : std::ios_base::out;
383385
ClassName = m_options.h.header_prefix + TableName + m_options.h.header_postfix;
@@ -391,6 +393,22 @@ namespace sqlite3pp
391393
strcpy_s(headerupper, (ClassName + "_H").c_str());
392394
_strupr_s(headerupper);
393395
HeaderUpper = headerupper;
396+
if (!m_options.m.exclude_comments && AppendToVect == true)
397+
{
398+
myfile << TopHeaderCommnetsPrt1 << std::endl;
399+
if (FirstColumnName.empty())
400+
FirstColumnName = "ColumnFoo";
401+
if (LastColumnName.empty())
402+
LastColumnName = "ColumnWiget";
403+
myfile << "Example Usage:" << std::endl;
404+
myfile << "\t// Exampel #1\n\t\tsqlite3pp::setGlobalDB(\"mydatabase.db\");" << std::endl;
405+
myfile << "\t\tsqlite3pp::Table<" << ClassName << "> my_tbl;\n\t\tfor (auto row : my_tbl)\n\t\t\tstd::wcout << row << std::endl;\n" << std::endl;
406+
407+
myfile << "\t// Exampel #2\n\t\tfor (int i = 0; i < my_tbl.size(); ++i)\n\t\t\tstd::wcout << my_tbl[i].get_" << FirstColumnName << "() << std::endl;\n" << std::endl;
408+
409+
myfile << "\t// Exampel #3\n\t\tfor (auto r = my_tbl.begin(); r != my_tbl.end(); ++r)\n\t\t\tstd::wcout << r->get_" << LastColumnName << "() << std::endl;\n" << std::endl;
410+
myfile << TopHeaderCommnetsPrt2 << std::endl;
411+
}
394412
// Add includes needed to support specified m_options.str_type
395413
myfile << "#ifndef " << HeaderUpper << std::endl;
396414
myfile << "#define " << HeaderUpper << std::endl;
@@ -415,7 +433,7 @@ namespace sqlite3pp
415433
m_options.h.header_prefix = OrgPrefix;
416434
std::ofstream myfile;
417435
std::string ClassName, HeaderUpper;
418-
if (CreateHeaderPrefix("All_Headers", myfile, ClassName, HeaderUpper, false))
436+
if (CreateHeaderPrefix("All_Headers", myfile, ClassName, HeaderUpper, "", "", false))
419437
{
420438
for (auto s : m_HeadersCreated)
421439
myfile << "#include \"" << s << "\"" << std::endl;
@@ -493,15 +511,22 @@ namespace sqlite3pp
493511
sqlite3pp::query qry(m_db, QueryStr.c_str());
494512
std::vector<std::pair<std::string, std::string> > columns;
495513
std::vector<std::pair<std::string, std::string> > columns_with_comma;
514+
std::string FirstColumnName;
515+
std::string LastColumnName = "get_MyColumnFoo()";
496516
for (int i = 0; i < qry.column_count(); ++i)
497517
{
498518
if (strstr(qry.column_name(i), ":") != NULL) continue;
519+
499520
columns.push_back(std::pair<std::string, std::string>(qry.column_name(i), GetType(qry.column_decltype(i))));
500521
columns_with_comma.push_back(std::pair<std::string, std::string>(qry.column_name(i), i ? ", " : ""));
522+
if (FirstColumnName.empty())
523+
FirstColumnName = qry.column_name(i);
524+
else
525+
LastColumnName = qry.column_name(i);
501526
}
502527
std::ofstream myfile;
503528
std::string ClassName, HeaderUpper;
504-
if (!CreateHeaderPrefix(TableName, myfile, ClassName, HeaderUpper))
529+
if (!CreateHeaderPrefix(TableName, myfile, ClassName, HeaderUpper, FirstColumnName, LastColumnName))
505530
return false;
506531
m_ClassNames.push_back(ClassName);
507532
////////////////////////////////////////////////////////////////////////////////////////////

sqlite3pp_ez.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
GNU General Public License
33
44
Copyright (C) 2021 David Maisonave (www.axter.com)
5-
The RegexAssistant source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
5+
The sqlite3pp_ez source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
66
This source code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
77
88
# Summary
@@ -324,7 +324,7 @@ namespace sqlite3pp
324324
, const std::string &WhereClause
325325
);
326326
bool ProcessClassCreation(const std::string& ClassName, std::string QueryStr = "");
327-
bool CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, bool AppendToVect = true);
327+
bool CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName = "", std::string LastColumnName = "", bool AppendToVect = true);
328328
public:
329329
// This constructor is best to use when creating a header for all tables in the constructor. (Headers can also be created by calling CreateHeader or CreateAllHeaders)
330330
SQLiteClassBuilder(const std::string& Db_filename

0 commit comments

Comments
 (0)