108 bool isOnlyUpdate =
true;
109 TString strConnectionName =
"", strClassPrefix =
"", strClassDirectory =
"";
111 string file_path =
gSystem->ExpandPathName(json_configuration_file.
Data());
112 ifstream file(file_path);
113 if (file.is_open()) {
118 strConnectionName = ((string)data[
"settings"][
"connectionName"]).c_str();
119 strClassPrefix = ((string)data[
"settings"][
"classPrefix"]).c_str();
120 }
catch (
const json::type_error& e) {
121 cout <<
"ERROR: Required settings were not found in the JSON file: settings->connectionName and "
122 "settings->classPrefix"
129 isOnlyUpdate = data[
"settings"][
"onlyUpdateClasses"];
130 }
catch (
const json::type_error& e) {
135 strClassDirectory = ((string)data[
"settings"][
"classDirectory"]).c_str();
136 if ((strClassDirectory !=
"") && (strClassDirectory[strClassDirectory.Length() - 1] !=
'/'))
137 strClassDirectory +=
'/';
138 strClassDirectory =
gSystem->ExpandPathName(strClassDirectory.Data());
139 }
catch (
const json::type_error& e) {
140 strClassDirectory =
"";
143 cout <<
"ERROR: JSON file with configuration was not found: " << file_path << endl;
149 TMethodCall open_method(connection_class,
"Open",
"");
151 open_method.
Execute(method_result);
153 if (pConnectionObject ==
nullptr) {
154 cout <<
"ERROR: connection to the database was not established: " << strConnectionName << endl;
158 TMethodCall server_method(connection_class,
"GetSQLServer",
"");
160 server_method.
Execute(pConnectionObject,
"", method_result);
162 if (db_server ==
nullptr) {
163 cout <<
"ERROR: pointer to database server was not obtained" << endl;
168 int currentDBMS = -1;
169 if (strcmp(db_server->
GetDBMS(),
"MySQL") == 0)
171 else if (strcmp(db_server->
GetDBMS(),
"PgSQL") == 0)
174 cout <<
"ERROR: this type of DBMS is not supported: " << db_server->
GetDBMS() << endl;
186 while ((obj = next()) !=
nullptr) {
195 cout <<
"Parsing table: " << strTableName << endl;
199 vector<structColumnInfo*> vecColumns;
200 if (currentDBMS == 0) {
202 "SELECT ordinal_position, column_name, data_type, (is_nullable = 'YES') AS is_nullable, "
203 "(extra = 'auto_increment') AS is_identity, (column_key = 'PRI') AS is_primary, (column_key = 'UNI') "
205 "FROM INFORMATION_SCHEMA.COLUMNS "
206 "WHERE table_name = '%s' "
207 "ORDER BY ordinal_position",
208 strTableName.
Data());
209 }
else if (currentDBMS == 1) {
210 sql =
TString::Format(
"SELECT DISTINCT a.attnum as ordinal_position, a.attname as column_name, "
211 "format_type(a.atttypid, a.atttypmod) as data_type, "
212 "a.attnotnull as is_nullable, pg_get_expr(def.adbin, def.adrelid) as is_default, "
213 "coalesce(i.indisprimary, false) as is_primary, coalesce(i.indisunique, false) as "
214 "is_unique, atttypmod as type_parameter "
215 "FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid "
216 "LEFT JOIN pg_index i ON (pgc.oid = i.indrelid AND a.attnum = ANY(i.indkey)) "
217 "LEFT JOIN pg_description com on (pgc.oid = com.objoid AND a.attnum = com.objsubid) "
218 "LEFT JOIN pg_attrdef def ON (a.attrelid = def.adrelid AND a.attnum = def.adnum) "
219 "WHERE a.attnum > 0 AND pgc.oid = a.attrelid AND pg_table_is_visible(pgc.oid) "
220 "AND NOT a.attisdropped AND pgc.relname = '%s' "
221 "ORDER BY a.attnum;",
222 strTableName.
Data());
226 int nrows = res->GetRowCount();
228 cout <<
"CRITICAL ERROR: table with no attributes (columns) was found: " << strTableName << endl;
234 while ((row = res->Next()) !=
nullptr) {
243 TString strColumnNameWO = strColumnName;
244 if (strColumnNameWO[strColumnNameWO.
Length() - 1] ==
'_')
245 strColumnNameWO = strColumnNameWO.
Remove(strColumnNameWO.
Length() - 1);
247 TString strColumnNameSpace = strColumnNameWO;
249 while ((char_under = strColumnNameSpace.
First(
'_')) !=
kNPOS) {
250 strColumnNameSpace = strColumnNameSpace.
Replace(char_under, 1,
' ');
275 if (strDataType ==
"uint") {
352 if (strDataType ==
"bit") {
358 if (strDataType ==
"datetime") {
371 cout <<
"ERROR: no corresponding column type: " << row->
GetField(2)
372 <<
". SQLType: " << pColumnInfo->
GetSQLType() << endl;
381 TString strShortVar = strColumnNameWO;
382 strShortVar = strShortVar.
Replace(0, 1, toupper(strShortVar[0]));
383 while ((char_under = strShortVar.
First(
'_')) !=
kNPOS) {
384 strShortVar = strShortVar.
Remove(char_under, 1);
385 if (strShortVar.
Length() > char_under)
386 strShortVar = strShortVar.
Replace(char_under, 1, toupper(strShortVar[char_under]));
390 if (currentDBMS == 0) {
395 }
else if (currentDBMS == 1) {
396 string def_value = row->
GetField(4);
398 sColumnInfo->
isIdentity = (def_value.find(
"nextval") == 0);
414 vecColumns.push_back(sColumnInfo);
421 sColumnInfoBinary->
strColumnName =
"size_" + strColumnNameWO;
426 vecColumns.push_back(sColumnInfoBinary);
435 TIter nextJoin(arrTableJoin);
445 TString strClassName = strTableName;
446 strClassName = strClassName.
Replace(0, 1, toupper(strClassName[0]));
448 while ((char_under = strClassName.
First(
'_')) !=
kNPOS) {
449 strClassName = strClassName.
Remove(char_under, 1);
450 if (strClassName.
Length() > char_under)
451 strClassName = strClassName.
Replace(char_under, 1, toupper(strClassName[char_under]));
453 TString strShortTableName = strClassName;
454 strClassName = strClassPrefix + strClassName;
456 TString strTableNameSpace = strTableName;
457 if (strTableNameSpace[strTableNameSpace.
Length() - 1] ==
'_')
458 strTableNameSpace = strTableNameSpace.
Remove(strTableNameSpace.
Length() - 1);
459 while ((char_under = strTableNameSpace.
First(
'_')) !=
kNPOS)
460 strTableNameSpace = strTableNameSpace.
Replace(char_under, 1,
' ');
463 TString strFileName = strClassDirectory + strClassName +
".h";
469 oldFile.open(strFileName, ios::in);
470 if (!oldFile.is_open()) {
471 cout <<
"ERROR: could not open existing header file: " << strFileName << endl;
475 strTempFileName = strFileName +
"_tmp";
476 hFile.open(strTempFileName, ios::out);
477 if (!hFile.is_open()) {
478 cout <<
"ERROR: could not create temporary header file: " << strTempFileName << endl;
483 while (getline(oldFile, cur_line)) {
484 string trim_line =
trim(cur_line);
485 if (trim_line.substr(0, 20) ==
"/* GENERATED PRIVATE")
488 hFile << cur_line << endl;
491 hFile.open(strFileName, ios::out);
492 if (!hFile.is_open()) {
493 cout <<
"ERROR: could not create header file: " << strFileName << endl;
497 hFile <<
"// ----------------------------------------------------------------------\n";
500 hFile <<
"// ----------------------------------------------------------------------\n\n";
506 TString strClassNameUpper = strClassName;
511 hFile <<
"#include \"TString.h\"\n";
512 hFile <<
"#include \"TDatime.h\"\n";
513 hFile <<
"#include \"TTimeStamp.h\"\n";
514 hFile <<
"\n#include \"" << strConnectionName.
Data() <<
".h\"\n\n";
518 hFile <<
" private:\n";
521 hFile <<
" /* GENERATED PRIVATE MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */\n";
523 hFile <<
" /// connection to the database\n";
524 hFile <<
" " << strConnectionName.
Data() <<
"* connectionDB;\n\n";
527 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
548 hFile <<
"\n //Constructor\n";
550 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
564 hFile <<
" /* END OF PRIVATE GENERATED PART (SHOULD NOT BE CHANGED MANUALLY) */\n";
568 while (getline(oldFile, cur_line)) {
569 string trim_line =
trim(cur_line);
570 if (trim_line.substr(0, 27) ==
"/* END OF PRIVATE GENERATED")
574 while (getline(oldFile, cur_line)) {
575 string trim_line =
trim(cur_line);
576 if (trim_line.substr(0, 19) ==
"/* GENERATED PUBLIC")
579 hFile << cur_line << endl;
582 hFile <<
"\n public:\n";
584 hFile <<
" /* GENERATED PUBLIC MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */\n";
585 hFile << (
TString::Format(
" virtual ~%s(); // Destructor\n\n", strClassName.
Data())).Data();
587 hFile <<
" // static class functions\n";
589 hFile << (
TString::Format(
" /// add new %s to the database\n", strTableNameSpace.
Data())).Data();
592 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
609 hFile << (
TString::Format(
" /// get %s from the database\n", strTableNameSpace.
Data())).Data();
612 bool is_flag =
false;
613 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
632 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
637 hFile << (
TString::Format(
" /// get %s from the database\n", strTableNameSpace.
Data())).Data();
647 " /// check %s exists in the database: 1 - true, 0 - false, <0 - database operation error\n",
648 strTableNameSpace.
Data()))
653 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
672 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
677 hFile << (
TString::Format(
" /// check %s exists in the database: 1 - true, 0 - false, <0 - database "
679 strTableNameSpace.
Data()))
688 hFile << (
TString::Format(
" /// delete %s from the database\n", strTableNameSpace.
Data())).Data();
692 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
711 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
716 hFile << (
TString::Format(
" /// delete %s from the database\n", strTableNameSpace.
Data())).Data();
725 hFile <<
" static int PrintAll();\n";
728 hFile <<
"\n // Getters\n";
729 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
732 strTableNameSpace.
Data()))
825 hFile <<
"\n // Setters\n";
826 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
833 strTableNameSpace.
Data()))
840 strTableNameSpace.
Data()))
853 hFile << (
TString::Format(
"\n /// print information about current %s\n", strTableNameSpace.
Data())).Data();
854 hFile <<
" void Print();\n";
855 hFile <<
" /* END OF PUBLIC GENERATED PART (SHOULD NOT BE CHANGED MANUALLY) */\n";
859 while (getline(oldFile, cur_line)) {
860 string trim_line =
trim(cur_line);
861 if (trim_line.substr(0, 26) ==
"/* END OF PUBLIC GENERATED")
865 while (getline(oldFile, cur_line))
866 hFile << cur_line << endl;
870 hFile <<
"\n#endif\n";
880 rename(strTempFileName, strFileName);
884 strFileName = strClassDirectory + strClassName +
".cxx";
888 oldFile.open(strFileName, ios::in);
889 if (!oldFile.is_open()) {
890 cout <<
"ERROR: could not open existing cxx file: " << strFileName << endl;
894 strTempFileName = strFileName +
"_tmp";
895 cxxFile.open(strTempFileName, ios::out);
896 if (!cxxFile.is_open()) {
897 cout <<
"ERROR: could not create temporary cxx file: " << strTempFileName << endl;
902 while (getline(oldFile, cur_line)) {
903 string trim_line =
trim(cur_line);
904 if (trim_line.substr(0, 18) ==
"/* GENERATED CLASS")
907 cxxFile << cur_line << endl;
910 cxxFile.open(strFileName, ios::out);
911 if (!cxxFile.is_open()) {
912 cout <<
"ERROR: could not create cxx file: " << strFileName << endl;
916 cxxFile <<
"// ----------------------------------------------------------------------\n";
919 cxxFile <<
"// ----------------------------------------------------------------------\n\n";
921 cxxFile <<
"#include \"TSQLServer.h\"\n";
922 cxxFile <<
"#include \"TSQLStatement.h\"\n";
925 cxxFile <<
"#include <iostream>\n";
926 cxxFile <<
"using namespace std;\n\n";
929 cxxFile <<
"/* GENERATED CLASS MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */\n";
932 cxxFile <<
"// ----- Constructor with database connection -----------------------\n";
934 strConnectionName.
Data()))
936 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
951 cxxFile <<
" connectionDB = db_connect;\n\n";
952 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
970 cxxFile <<
"// ----- Destructor -------------------------------------------------\n";
971 cxxFile << (
TString::Format(
"%s::~%s()\n{\n if (connectionDB)\n delete connectionDB;\n",
972 strClassName.
Data(), strClassName.
Data()))
975 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1005 cxxFile << (
TString::Format(
"// ----- Creating new %s in the database ---------------------------\n",
1006 strTableNameSpace.
Data()))
1009 strShortTableName.
Data()))
1012 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1046 cxxFile <<
")\n{\n";
1048 strConnectionName.
Data()))
1050 cxxFile <<
" if (connDb == nullptr) return nullptr;\n\n";
1052 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1053 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"insert into %s(",
1054 strTableName.
Data()))
1057 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1071 cxxFile <<
") \"\n \"values (";
1072 for (
int i = 1;
i <= count;
i++) {
1073 if (currentDBMS == 0) {
1078 }
else if (currentDBMS == 1) {
1085 cxxFile <<
")\");\n";
1087 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n\n";
1089 cxxFile <<
" stmt->NextIteration();\n";
1091 TString strIdentityColumnName =
"";
1092 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1100 cxxFile << (
TString::Format(
" if (%s == nullptr)\n stmt->SetNull(%d);\n else\n ",
1120 cxxFile << (
TString::Format(
"\n // inserting new %s to the Database\n", strTableNameSpace.
Data())).Data();
1121 cxxFile <<
" if (!stmt->Process())\n {\n";
1123 " cout<<\"ERROR: inserting new %s to the Database has been failed\"<<endl;\n",
1124 strTableNameSpace.
Data()))
1126 cxxFile <<
" delete stmt;\n delete connDb;\n return nullptr;\n }\n\n";
1128 cxxFile <<
" delete stmt;\n\n";
1130 if (strIdentityColumnName !=
"") {
1131 cxxFile <<
" // getting last inserted ID\n";
1133 if (currentDBMS == 0) {
1134 cxxFile <<
" TSQLStatement* stmt_last = db_server->Statement(\"SELECT LAST_INSERT_ID()\");\n";
1135 }
else if (currentDBMS == 1) {
1136 cxxFile << (
TString::Format(
" TSQLStatement* stmt_last = db_server->Statement(\"SELECT "
1137 "currval(pg_get_serial_sequence('%s','%s'))\");\n",
1138 strTableName.
Data(), strIdentityColumnName.
Data()))
1142 cxxFile <<
"\n // process getting last id\n"
1143 " if (stmt_last->Process())\n {\n"
1144 " // store result of statement in buffer\n"
1145 " stmt_last->StoreResult();\n\n";
1146 cxxFile <<
" // if there is no last id then exit with error\n"
1147 " if (!stmt_last->NextResultRow())\n {\n"
1148 " cout<<\"ERROR: no last ID in DB!\"<<endl;\n"
1149 " delete stmt_last;\n"
1150 " return nullptr;\n }\n"
1153 << (
TString::Format(
" %s = stmt_last->GetInt(0);\n", strIdentityColumnName.
Data())).Data();
1154 cxxFile <<
" delete stmt_last;\n }\n }\n"
1156 " cout<<\"ERROR: getting last ID has been failed!\"<<endl;\n"
1157 " delete stmt_last;\n"
1158 " return nullptr;\n }\n\n";
1163 cxxFile <<
" sql = TString::Format(\n \"select";
1196 cxxFile <<
" stmt = db_server->Statement(sql);\n";
1198 cxxFile <<
"\n // get join table record from DB\n";
1199 cxxFile <<
" if (!stmt->Process())\n {\n";
1200 cxxFile << (
TString::Format(
" cout<<\"ERROR: getting join record from DB has been failed for '%s' "
1201 "table\"<<endl;\n\n",
1204 cxxFile <<
" delete stmt;\n";
1205 cxxFile <<
" delete connDb;\n";
1206 cxxFile <<
" return nullptr;\n }\n\n";
1208 cxxFile <<
" // store result of statement in buffer\n";
1209 cxxFile <<
" stmt->StoreResult();\n\n";
1211 cxxFile <<
" // extract join row\n";
1212 cxxFile <<
" if (!stmt->NextResultRow())\n {\n";
1213 cxxFile << (
TString::Format(
" cout<<\"ERROR: the record was not found in '%s' table\"<<endl;\n\n",
1216 cxxFile <<
" delete stmt;\n";
1217 cxxFile <<
" delete connDb;\n";
1227 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) %s = nullptr;\n else\n ", count,
1231 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
1236 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
1237 StatementType.
Data(), count, TempVar.Data(),
1241 cxxFile << (
TString::Format(
" %s = new %s(stmt->Get%s(%d));\n", TempVar.Data(),
1242 VariableTypePointer.Data(), StatementType.
Data(), count))
1246 cxxFile << (
TString::Format(
" %s = nullptr;\n", TempVar.Data())).Data();
1255 cxxFile << (
TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(), StatementType.
Data(),
1262 cxxFile <<
"\n delete stmt;\n\n";
1267 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1275 cxxFile << (
TString::Format(
" if (%s == nullptr) %s = nullptr;\n else\n ",
1287 cxxFile << (
TString::Format(
" memcpy(%s, %s, %s);\n }\n", TempVar.Data(),
1291 cxxFile << (
TString::Format(
" %s = new %s(*%s);\n", TempVar.Data(), VariableTypePointer.Data(),
1301 cxxFile << (
TString::Format(
" %s = new %s[%s];\n", TempVar.Data(), VariableTypePointer.Data(),
1304 cxxFile << (
TString::Format(
" memcpy(%s, %s, %s);\n", TempVar.Data(), TempColumnName.
Data(),
1314 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1325 cxxFile <<
");\n}\n\n";
1328 cxxFile << (
TString::Format(
"// ----- Get %s from the database ---------------------------\n",
1329 strTableNameSpace.
Data()))
1332 strShortTableName.
Data()))
1336 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1352 cxxFile <<
")\n{\n";
1355 strConnectionName.
Data()))
1357 cxxFile <<
" if (connDb == nullptr) return nullptr;\n\n";
1359 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1360 cxxFile <<
" TString sql = TString::Format(\n \"select";
1362 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1397 cxxFile <<
" \"\n \"where";
1399 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1419 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1431 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n";
1433 cxxFile << (
TString::Format(
"\n // get %s from the database\n", strTableNameSpace.
Data())).Data();
1434 cxxFile <<
" if (!stmt->Process())\n {\n";
1435 cxxFile << (
TString::Format(
" cout<<\"ERROR: getting %s from the database has been failed\"<<endl;\n\n",
1436 strTableNameSpace.
Data()))
1438 cxxFile <<
" delete stmt;\n";
1439 cxxFile <<
" delete connDb;\n";
1440 cxxFile <<
" return nullptr;\n }\n\n";
1442 cxxFile <<
" // store result of statement in buffer\n";
1443 cxxFile <<
" stmt->StoreResult();\n\n";
1445 cxxFile <<
" // extract row\n";
1446 cxxFile <<
" if (!stmt->NextResultRow())\n {\n";
1447 cxxFile << (
TString::Format(
" cout<<\"ERROR: %s was not found in the database\"<<endl;\n\n",
1448 strTableNameSpace.
Data()))
1450 cxxFile <<
" delete stmt;\n";
1451 cxxFile <<
" delete connDb;\n";
1452 cxxFile <<
" return nullptr;\n }\n\n";
1455 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1463 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) %s = nullptr;\n else\n ", count,
1467 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
1473 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
1474 StatementType.
Data(), count, TempVar.Data(),
1478 cxxFile << (
TString::Format(
" %s = new %s(stmt->Get%s(%d));\n", TempVar.Data(),
1479 VariableTypePointer.Data(), StatementType.
Data(), count))
1483 cxxFile << (
TString::Format(
" %s = nullptr;\n", TempVar.Data())).Data();
1489 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.
Data(), count,
1493 cxxFile << (
TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(), StatementType.
Data(),
1511 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) %s = nullptr;\n else\n ", count,
1515 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
1520 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
1521 StatementType.
Data(), count, TempVar.Data(),
1525 cxxFile << (
TString::Format(
" %s = new %s(stmt->Get%s(%d));\n", TempVar.Data(),
1526 VariableTypePointer.Data(), StatementType.
Data(), count))
1530 cxxFile << (
TString::Format(
" %s = nullptr;\n", TempVar.Data())).Data();
1539 cxxFile << (
TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(), StatementType.
Data(),
1549 cxxFile <<
"\n delete stmt;\n\n";
1552 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1574 cxxFile <<
");\n}\n\n";
1578 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1583 cxxFile << (
TString::Format(
"// ----- Get %s from the database by unique key --------------\n",
1584 strTableNameSpace.
Data()))
1592 strConnectionName.
Data()))
1594 cxxFile <<
" if (connDb == nullptr) return nullptr;\n\n";
1596 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1597 cxxFile <<
" TString sql = TString::Format(\n \"select";
1599 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
1623 cxxFile << (
TString::Format(
" \"\n \"where lower(%s) = lower('%%%s')\", %s.Data());\n",
1633 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n";
1635 cxxFile << (
TString::Format(
"\n // get %s from the database\n", strTableNameSpace.
Data())).Data();
1636 cxxFile <<
" if (!stmt->Process())\n {\n";
1638 " cout<<\"ERROR: getting %s from the database has been failed\"<<endl;\n\n",
1639 strTableNameSpace.
Data()))
1641 cxxFile <<
" delete stmt;\n";
1642 cxxFile <<
" delete connDb;\n";
1643 cxxFile <<
" return nullptr;\n }\n\n";
1645 cxxFile <<
" // store result of statement in buffer\n";
1646 cxxFile <<
" stmt->StoreResult();\n\n";
1648 cxxFile <<
" // extract row\n";
1649 cxxFile <<
" if (!stmt->NextResultRow())\n {\n";
1650 cxxFile << (
TString::Format(
" cout<<\"ERROR: %s was not found in the database\"<<endl;\n\n",
1651 strTableNameSpace.
Data()))
1653 cxxFile <<
" delete stmt;\n";
1654 cxxFile <<
" delete connDb;\n";
1655 cxxFile <<
" return nullptr;\n }\n\n";
1658 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
1669 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) %s = nullptr;\n else\n ", count,
1673 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
1675 current_col = *it_inner;
1679 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
1680 StatementType.
Data(), count, TempVar.Data(),
1684 cxxFile << (
TString::Format(
" %s = new %s(stmt->Get%s(%d));\n", TempVar.Data(),
1685 VariableTypePointer.Data(), StatementType.
Data(), count))
1689 cxxFile << (
TString::Format(
" %s = nullptr;\n", TempVar.Data())).Data();
1691 current_col = *it_inner;
1699 cxxFile << (
TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(),
1700 StatementType.
Data(), count))
1717 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) %s = nullptr;\n else\n ",
1718 count, TempVar.Data()))
1721 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
1726 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
1727 StatementType.
Data(), count, TempVar.Data(),
1731 cxxFile << (
TString::Format(
" %s = new %s(stmt->Get%s(%d));\n", TempVar.Data(),
1732 VariableTypePointer.Data(), StatementType.
Data(), count))
1736 cxxFile << (
TString::Format(
" %s = nullptr;\n", TempVar.Data())).Data();
1742 StatementType.
Data(), count, TempVar.Data(),
1746 cxxFile << (
TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(),
1747 StatementType.
Data(), count))
1756 cxxFile <<
"\n delete stmt;\n\n";
1759 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
1767 current_col = *it_inner;
1783 cxxFile <<
");\n}\n\n";
1788 cxxFile << (
TString::Format(
"// ----- Check %s exists in the database ---------------------------\n",
1789 strTableNameSpace.
Data()))
1794 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1810 cxxFile <<
")\n{\n";
1813 strConnectionName.
Data()))
1815 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
1817 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1818 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"select 1 \"\n \"from %s",
1819 strTableName.
Data()))
1829 cxxFile <<
" \"\n \"where";
1831 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1851 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1863 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n";
1865 cxxFile << (
TString::Format(
"\n // get %s from the database\n", strTableNameSpace.
Data())).Data();
1866 cxxFile <<
" if (!stmt->Process())\n {\n";
1867 cxxFile << (
TString::Format(
" cout<<\"ERROR: checking %s in the database has been failed\"<<endl;\n\n",
1868 strTableNameSpace.
Data()))
1870 cxxFile <<
" delete stmt;\n";
1871 cxxFile <<
" delete connDb;\n";
1872 cxxFile <<
" return -2;\n }\n\n";
1874 cxxFile <<
" // store result of statement in buffer\n";
1875 cxxFile <<
" stmt->StoreResult();\n\n";
1877 cxxFile <<
" // extract row\n";
1878 cxxFile <<
" if (!stmt->NextResultRow())\n {\n";
1879 cxxFile <<
" delete stmt;\n";
1880 cxxFile <<
" delete connDb;\n";
1881 cxxFile <<
" return 0;\n }\n\n";
1883 cxxFile <<
" delete stmt;\n";
1884 cxxFile <<
" delete connDb;\n\n";
1885 cxxFile <<
" return 1;\n}\n\n";
1889 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1894 cxxFile << (
TString::Format(
"// ----- Check %s exists in the database by unique key --------------\n",
1895 strTableNameSpace.
Data()))
1903 strConnectionName.
Data()))
1905 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
1907 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1909 " TString sql = TString::Format(\n \"select 1 \"\n \"from %s",
1910 strTableName.
Data()))
1921 cxxFile << (
TString::Format(
" \"\n \"where lower(%s) = lower('%%%s')\", %s.Data());\n",
1931 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n";
1933 cxxFile << (
TString::Format(
"\n // get %s from the database\n", strTableNameSpace.
Data())).Data();
1934 cxxFile <<
" if (!stmt->Process())\n {\n";
1936 " cout<<\"ERROR: getting %s from the database has been failed\"<<endl;\n\n",
1937 strTableNameSpace.
Data()))
1939 cxxFile <<
" delete stmt;\n";
1940 cxxFile <<
" delete connDb;\n";
1941 cxxFile <<
" return -2;\n }\n\n";
1943 cxxFile <<
" // store result of statement in buffer\n";
1944 cxxFile <<
" stmt->StoreResult();\n\n";
1946 cxxFile <<
" // extract row\n";
1947 cxxFile <<
" if (!stmt->NextResultRow())\n {\n";
1948 cxxFile <<
" delete stmt;\n";
1949 cxxFile <<
" delete connDb;\n";
1950 cxxFile <<
" return 0;\n }\n\n";
1952 cxxFile <<
" delete stmt;\n";
1953 cxxFile <<
" delete connDb;\n\n";
1954 cxxFile <<
" return 1;\n}\n\n";
1959 cxxFile << (
TString::Format(
"// ----- Delete %s from the database ---------------------------\n",
1960 strTableNameSpace.
Data()))
1965 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1981 cxxFile <<
")\n{\n";
1984 strConnectionName.
Data()))
1986 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
1988 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1989 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"delete from %s \"\n \"where",
1990 strTableName.
Data()))
1993 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2001 if (currentDBMS == 0) {
2006 }
else if (currentDBMS == 1) {
2016 cxxFile <<
"\");\n";
2018 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n\n";
2020 cxxFile <<
" stmt->NextIteration();\n";
2022 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2033 cxxFile << (
TString::Format(
"\n // delete %s from the database\n", strTableNameSpace.
Data())).Data();
2034 cxxFile <<
" if (!stmt->Process())\n {\n";
2035 cxxFile << (
TString::Format(
" cout<<\"ERROR: deleting %s from the database has been failed\"<<endl;\n\n",
2036 strTableNameSpace.
Data()))
2038 cxxFile <<
" delete stmt;\n";
2039 cxxFile <<
" delete connDb;\n";
2040 cxxFile <<
" return -2;\n }\n\n";
2042 cxxFile <<
" delete stmt;\n";
2043 cxxFile <<
" delete connDb;\n";
2044 cxxFile <<
" return 0;\n}\n\n";
2048 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2053 cxxFile << (
TString::Format(
"// ----- Delete %s from the database by unique key --------------\n",
2054 strTableNameSpace.
Data()))
2062 strConnectionName.
Data()))
2064 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
2066 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
2068 if (currentDBMS == 0) {
2070 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"delete from %s \"\n "
2071 " \"where lower(%s) = lower(?)\");\n",
2075 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"delete from %s \"\n "
2076 " \"where %s = ?\");\n",
2079 }
else if (currentDBMS == 1) {
2081 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"delete from %s \"\n "
2082 " \"where lower(%s) = lower($1)\");\n",
2086 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"delete from %s \"\n "
2087 " \"where %s = $1\");\n",
2092 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n\n";
2094 cxxFile <<
" stmt->NextIteration();\n";
2099 cxxFile << (
TString::Format(
"\n // delete %s from the database\n", strTableNameSpace.
Data())).Data();
2100 cxxFile <<
" if (!stmt->Process())\n {\n";
2102 " cout<<\"ERROR: deleting %s from the database has been failed\"<<endl;\n\n",
2103 strTableNameSpace.
Data()))
2105 cxxFile <<
" delete stmt;\n";
2106 cxxFile <<
" delete connDb;\n";
2107 cxxFile <<
" return -2;\n }\n\n";
2109 cxxFile <<
" delete stmt;\n";
2110 cxxFile <<
" delete connDb;\n";
2111 cxxFile <<
" return 0;\n}\n\n";
2116 cxxFile << (
TString::Format(
"// ----- Print all '%ss' ---------------------------------\n",
2117 strTableNameSpace.
Data()))
2122 strConnectionName.
Data()))
2124 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
2126 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
2127 cxxFile <<
" TString sql = TString::Format(\n \"select";
2129 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2157 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n";
2159 cxxFile << (
TString::Format(
"\n // get all '%ss' from the database\n", strTableNameSpace.
Data())).Data();
2160 cxxFile <<
" if (!stmt->Process())\n {\n";
2162 " cout<<\"ERROR: getting all '%ss' from the database has been failed\"<<endl;\n\n",
2163 strTableNameSpace.
Data()))
2165 cxxFile <<
" delete stmt;\n";
2166 cxxFile <<
" delete connDb;\n";
2167 cxxFile <<
" return -2;\n }\n\n";
2169 cxxFile <<
" // store result of statement in buffer\n";
2170 cxxFile <<
" stmt->StoreResult();\n\n";
2172 cxxFile <<
" // print rows\n";
2173 cxxFile << (
TString::Format(
" cout<<\"Table '%s':\"<<endl;\n", strTableName.
Data())).Data();
2174 cxxFile <<
" while (stmt->NextResultRow())\n {\n";
2177 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2180 cxxFile <<
" cout<<\"";
2188 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) cout<<\"nullptr\";\n else\n ",
2194 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
2200 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
2201 StatementType.
Data(), count, TempVar.Data(),
2204 cxxFile << (
TString::Format(
" cout<<(void*)%s<<\", binary size: \"<<%s;\n",
2208 cxxFile << (
TString::Format(
" cout<<stmt->Get%s(%d)", StatementType.
Data(), count)).Data();
2210 cxxFile <<
".AsSQLString()";
2212 cxxFile <<
".AsString(\"s\")";
2228 cxxFile << (
TString::Format(
" cout<<(void*)%s<<\", binary size: \"<<%s;\n", TempVar.Data(),
2232 cxxFile << (
TString::Format(
" cout<<(stmt->Get%s(%d))", StatementType.
Data(), count)).Data();
2234 cxxFile <<
".AsSQLString()";
2236 cxxFile <<
".AsString(\"s\")";
2248 cxxFile <<
" cout<<\"";
2256 cxxFile << (
TString::Format(
" if (stmt->IsNull(%d)) cout<<\"nullptr\";\n else\n ",
2263 cxxFile << (
TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
2268 cxxFile << (
TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n }\n",
2269 StatementType.
Data(), count, TempVar.Data(),
2272 cxxFile << (
TString::Format(
" cout<<(void*)%s<<\", binary size: \"<<%s;\n",
2279 cxxFile <<
".AsSQLString()";
2281 cxxFile <<
".AsString(\"s\")";
2296 cxxFile << (
TString::Format(
" cout<<(void*)%s<<\", binary size: \"<<%s;\n",
2303 cxxFile <<
".AsSQLString()";
2305 cxxFile <<
".AsString(\"s\")";
2314 cxxFile <<
" cout<<\".\"<<endl;\n }\n\n";
2316 cxxFile <<
" delete stmt;\n";
2317 cxxFile <<
" delete connDb;\n\n";
2318 cxxFile <<
" return 0;\n}\n\n";
2322 cxxFile <<
"\n// Setters functions\n";
2323 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2345 cxxFile <<
" if (!connectionDB)\n {\n cout<<\"CRITICAL ERROR: Connection object is "
2346 "null\"<<endl;\n return -1;\n }\n\n";
2348 cxxFile <<
" TSQLServer* db_server = connectionDB->GetSQLServer();\n\n";
2350 if (currentDBMS == 0) {
2351 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"update %s \"\n \"set "
2352 "%s = ? \"\n \"where",
2355 }
else if (currentDBMS == 1) {
2356 cxxFile << (
TString::Format(
" TString sql = TString::Format(\n \"update %s \"\n \"set "
2357 "%s = $1 \"\n \"where",
2362 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
2369 if (currentDBMS == 0) {
2374 }
else if (currentDBMS == 1) {
2384 cxxFile <<
"\");\n";
2386 cxxFile <<
" TSQLStatement* stmt = db_server->Statement(sql);\n\n";
2388 cxxFile <<
" stmt->NextIteration();\n";
2391 cxxFile << (
TString::Format(
" if (%s == nullptr)\n stmt->SetNull(0);\n else\n ",
2407 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
2420 cxxFile <<
"\n // write new value to the database\n";
2421 cxxFile <<
" if (!stmt->Process())\n {\n";
2423 " cout<<\"ERROR: updating information about %s has been failed\"<<endl;\n\n",
2424 strTableNameSpace.
Data()))
2426 cxxFile <<
" delete stmt;\n";
2427 cxxFile <<
" return -2;\n }\n\n";
2439 cxxFile << (
TString::Format(
" if (%s == nullptr) %s = nullptr;\n else\n ",
2475 cxxFile <<
"\n delete stmt;\n";
2476 cxxFile <<
" return 0;\n}\n\n";
2480 cxxFile << (
TString::Format(
"// ----- Print current %s ---------------------------------------\n",
2481 strTableNameSpace.
Data()))
2485 cxxFile << (
TString::Format(
" cout<<\"Table '%s'\";\n cout", strTableName.
Data())).Data();
2486 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
2499 cxxFile << (
TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsSQLString())",
2506 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsString(\"s\"))",
2512 cxxFile << (
TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": *%s)",
2519 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": TString::Format(\"%%%s\", "
2531 cxxFile <<
".AsSQLString()";
2533 cxxFile <<
".AsString(\"s\")";
2552 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsSQLString())",
2559 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsString(\"s\"))",
2565 cxxFile << (
TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": *%s)",
2572 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": "
2573 "TString::Format(\"%%%s\", *%s))",
2584 cxxFile <<
".AsSQLString()";
2586 cxxFile <<
".AsString(\"s\")";
2591 cxxFile <<
"<<endl;\n\n";
2593 cxxFile <<
" return;\n}\n";
2594 cxxFile <<
"/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */\n";
2598 while (getline(oldFile, cur_line)) {
2599 string trim_line =
trim(cur_line);
2600 if (trim_line.substr(0, 25) ==
"/* END OF GENERATED CLASS")
2604 while (getline(oldFile, cur_line))
2605 cxxFile << cur_line << endl;
2613 remove(strFileName);
2615 rename(strTempFileName, strFileName);
2621 delete arrTableJoin;