106 TObjArray* arrTableJoin =
nullptr;
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();
121 cout <<
"ERROR: Required settings were not found in the JSON file: settings->connectionName and "
122 "settings->classPrefix"
129 isOnlyUpdate = data[
"settings"][
"onlyUpdateClasses"];
135 strClassDirectory = ((string)data[
"settings"][
"classDirectory"]).c_str();
136 if ((strClassDirectory !=
"") && (strClassDirectory[strClassDirectory.Length() - 1] !=
'/'))
137 strClassDirectory +=
'/';
138 strClassDirectory = gSystem->ExpandPathName(strClassDirectory.Data());
140 strClassDirectory =
"";
143 cout <<
"ERROR: JSON file with configuration was not found: " << file_path << endl;
148 TClass* connection_class = TClass::GetClass(strConnectionName.Data());
149 TMethodCall open_method(connection_class,
"Open",
"");
150 Long_t method_result = 0;
151 open_method.Execute(method_result);
152 TObject* pConnectionObject = (TObject*)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);
161 TSQLServer* db_server = (TSQLServer*)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;
181 TList* lst = db_server->GetTablesList();
186 while ((obj = next()) !=
nullptr) {
188 TString strTableName = obj->GetName();
191 if ((strTableName.BeginsWith(
"_"))
192 || ((currentDBMS == 1) && (strTableName.BeginsWith(
"pg_") || strTableName.BeginsWith(
"sql_"))))
195 cout <<
"Parsing table: " << strTableName << endl;
196 TSQLTableInfo* pTableInfo = db_server->GetTableInfo(strTableName.Data());
199 vector<structColumnInfo*> vecColumns;
200 if (currentDBMS == 0) {
201 sql = TString::Format(
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());
225 TSQLResult* res = db_server->Query(sql);
226 int nrows = res->GetRowCount();
228 cout <<
"CRITICAL ERROR: table with no attributes (columns) was found: " << strTableName << endl;
234 while ((row = res->Next()) !=
nullptr) {
236 TString strColumnName = row->GetField(1);
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,
' ');
255 TSQLColumnInfo* pColumnInfo = pTableInfo->FindColumn(strColumnName);
256 switch (pColumnInfo->GetSQLType()) {
257 case TSQLServer::kSQL_CHAR: {
265 case TSQLServer::kSQL_VARCHAR: {
273 case TSQLServer::kSQL_INTEGER: {
274 TString strDataType = row->GetField(2);
275 if (strDataType ==
"uint") {
281 if (strDataType.BeginsWith(
"bool")) {
287 if (strDataType.BeginsWith(
"bigint")) {
303 case TSQLServer::kSQL_FLOAT: {
311 case TSQLServer::kSQL_DOUBLE: {
319 case TSQLServer::kSQL_BINARY: {
328 case TSQLServer::kSQL_TIMESTAMP: {
331 if ((row->GetField(7))[0] ==
'0') {
351 TString strDataType = row->GetField(2);
352 if (strDataType ==
"bit") {
358 if (strDataType ==
"datetime") {
365 if (strDataType.BeginsWith(
"character")) {
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) {
391 sColumnInfo->
isNullable = ((row->GetField(3))[0] ==
'1');
392 sColumnInfo->
isIdentity = ((row->GetField(4))[0] ==
'1');
393 sColumnInfo->
isPrimary = ((row->GetField(5))[0] ==
'1');
394 sColumnInfo->
isUnique = ((row->GetField(6))[0] ==
'1');
395 }
else if (currentDBMS == 1) {
396 string def_value = row->GetField(4);
397 sColumnInfo->
isNullable = ((row->GetField(3))[0] ==
'f');
398 sColumnInfo->
isIdentity = (def_value.find(
"nextval") == 0);
399 sColumnInfo->
isPrimary = ((row->GetField(5))[0] ==
't');
403 sColumnInfo->
isUnique = ((row->GetField(6))[0] ==
't');
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";
466 TString strTempFileName;
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";
498 hFile << (TString::Format(
"// %s header file\n", strClassName.Data())).Data();
499 hFile << (TString::Format(
"// Generated %s\n",
get_current_date().c_str())).Data();
500 hFile <<
"// ----------------------------------------------------------------------\n\n";
502 hFile << TString::Format(
"/** %s\n", strFileName.Data());
503 hFile << (TString::Format(
" ** Class for the table: %s\n", strTableName.Data())).Data();
506 TString strClassNameUpper = strClassName;
507 strClassNameUpper.ToUpper();
508 hFile << (TString::Format(
"#ifndef %s_H\n", strClassNameUpper.Data())).Data();
509 hFile << (TString::Format(
"#define %s_H 1\n\n", strClassNameUpper.Data())).Data();
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";
516 hFile << (TString::Format(
"class %s\n", strClassName.Data())).Data();
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) {
538 hFile << (TString::Format(
" /// Table: %s - column %s (read-only)\n",
541 hFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(),
548 hFile <<
"\n //Constructor\n";
549 hFile << (TString::Format(
" %s(%s* db_connect", strClassName.Data(), strConnectionName.Data())).Data();
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();
590 hFile << (TString::Format(
" static %s* Create%s(", strClassName.Data(), strShortTableName.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();
610 hFile << (TString::Format(
" static %s* Get%s(", strClassName.Data(), strShortTableName.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();
638 hFile << (TString::Format(
" static %s* Get%s(%s %s);\n", strClassName.Data(),
646 hFile << (TString::Format(
647 " /// check %s exists in the database: 1 - true, 0 - false, <0 - database operation error\n",
648 strTableNameSpace.Data()))
650 hFile << (TString::Format(
" static int Check%sExists(", strShortTableName.Data())).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()))
681 hFile << (TString::Format(
" static int Check%sExists(%s %s);\n", strShortTableName.Data(),
688 hFile << (TString::Format(
" /// delete %s from the database\n", strTableNameSpace.Data())).Data();
689 hFile << (TString::Format(
" static int Delete%s(", strShortTableName.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();
717 hFile << (TString::Format(
" static int Delete%s(%s %s);\n", strShortTableName.Data(),
724 hFile << (TString::Format(
" /// print all %ss\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) {
731 hFile << (TString::Format(
" /// get %s of the current %s\n", cur_col->
strColumnNameSpace.Data(),
732 strTableNameSpace.Data()))
734 hFile << (TString::Format(
" %s Get%s() {", cur_col->
strVariableType.Data(),
739 hFile << (TString::Format(
"if (%s == nullptr) return nullptr; else ", cur_col->
strVariableName.Data()))
743 hFile << (TString::Format(
"{%s %s = new %s[%s]; ", cur_col->
strVariableType.Data(),
758 hFile << (TString::Format(
"%s %s = new %s[%s]; ", cur_col->
strVariableType.Data(),
767 hFile << (TString::Format(
"return %s;", cur_col->
strVariableName.Data())).Data();
781 hFile << (TString::Format(
" %s Get%s() {", cur_col->
strVariableType.Data(),
786 hFile << (TString::Format(
"if (%s == nullptr) return nullptr; else ",
791 hFile << (TString::Format(
"{%s %s = new %s[%s]; ", cur_col->
strVariableType.Data(),
807 hFile << (TString::Format(
"%s %s = new %s[%s]; ", cur_col->
strVariableType.Data(),
817 hFile << (TString::Format(
"return %s;", cur_col->
strVariableName.Data())).Data();
825 hFile <<
"\n // Setters\n";
826 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
832 hFile << (TString::Format(
" /// set %s of the current %s\n", cur_col->
strColumnNameSpace.Data(),
833 strTableNameSpace.Data()))
839 hFile << (TString::Format(
" /// set %s of the current %s\n", cur_col->
strColumnNameSpace.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;
868 hFile << (TString::Format(
"\n ClassDef(%s,1);\n", strClassName.Data())).Data();
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";
917 cxxFile << (TString::Format(
"// %s cxx file\n", strClassName.Data())).Data();
918 cxxFile << (TString::Format(
"// Generated %s\n",
get_current_date().c_str())).Data();
919 cxxFile <<
"// ----------------------------------------------------------------------\n\n";
921 cxxFile <<
"#include \"TSQLServer.h\"\n";
922 cxxFile <<
"#include \"TSQLStatement.h\"\n";
923 cxxFile << (TString::Format(
"\n#include \"%s.h\"\n\n", strClassName.Data())).Data();
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";
933 cxxFile << (TString::Format(
"%s::%s(%s* db_connect", strClassName.Data(), strClassName.Data(),
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) {
954 cxxFile << (TString::Format(
" %s = %s;\n", cur_col->
strVariableName.Data(),
963 cxxFile << (TString::Format(
" %s = %s;\n", cur_col->
strVariableName.Data(),
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) {
979 cxxFile << (TString::Format(
" if (%s)\n delete %s;\n", cur_col->
strVariableName.Data(),
983 cxxFile << (TString::Format(
" if (%s)\n delete [] %s;\n", cur_col->
strVariableName.Data(),
993 cxxFile << (TString::Format(
" if (%s)\n delete %s;\n", cur_col->
strVariableName.Data(),
997 cxxFile << (TString::Format(
" if (%s)\n delete [] %s;\n", cur_col->
strVariableName.Data(),
1005 cxxFile << (TString::Format(
"// ----- Creating new %s in the database ---------------------------\n",
1006 strTableNameSpace.Data()))
1008 cxxFile << (TString::Format(
"%s* %s::Create%s(", strClassName.Data(), strClassName.Data(),
1009 strShortTableName.Data()))
1012 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1039 cxxFile << (TString::Format(
", %s %s", cur_col->
strVariableType.Data(),
1046 cxxFile <<
")\n{\n";
1047 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.Data(),
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) {
1063 cxxFile << (TString::Format(
"%s", cur_col->
strColumnName.Data())).Data();
1065 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
1071 cxxFile <<
") \"\n \"values (";
1072 for (
int i = 1;
i <= count;
i++) {
1073 if (currentDBMS == 0) {
1078 }
else if (currentDBMS == 1) {
1082 cxxFile << (TString::Format(
", $%d",
i)).Data();
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 ",
1105 cxxFile << (TString::Format(
" stmt->Set%s(%d, %s);\n", cur_col->
strStatementType.Data(), count,
1109 cxxFile << (TString::Format(
" stmt->Set%s(%d, %s, ", cur_col->
strStatementType.Data(), count,
1114 cxxFile << (TString::Format(
"%s, 0x4000000);\n", cur_col->
strColumnName.Data())).Data();
1120 cxxFile << (TString::Format(
"\n // inserting new %s to the Database\n", strTableNameSpace.Data())).Data();
1121 cxxFile <<
" if (!stmt->Process())\n {\n";
1122 cxxFile << (TString::Format(
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";
1132 cxxFile << (TString::Format(
" int %s;\n", strIdentityColumnName.Data())).Data();
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";
1169 cxxFile << (TString::Format(
" %s", cur_col->
strColumnName.Data())).Data();
1171 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
1177 cxxFile << (TString::Format(
" \"\n \"from %s \"\n \"where",
1181 cxxFile << (TString::Format(
" lower(%s) = lower('%%%s')",
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";
1224 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data())).Data();
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();
1233 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.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();
1248 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.Data(),
1251 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.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) {
1272 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data())).Data();
1275 cxxFile << (TString::Format(
" if (%s == nullptr) %s = nullptr;\n else\n ",
1281 cxxFile << (TString::Format(
" {\n %s %s = %s;\n", cur_col->
strVariableType.Data(),
1284 cxxFile << (TString::Format(
" %s = new %s[%s];\n", TempVar.Data(),
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(),
1298 cxxFile << (TString::Format(
" %s %s = %s;\n", cur_col->
strVariableType.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(),
1309 << (TString::Format(
" %s = %s;\n", TempVar.Data(), cur_col->
strColumnName.Data())).Data();
1313 cxxFile << (TString::Format(
"\n return new %s(connDb", strClassName.Data())).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()))
1331 cxxFile << (TString::Format(
"%s* %s::Get%s(", strClassName.Data(), strClassName.Data(),
1332 strShortTableName.Data()))
1336 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1352 cxxFile <<
")\n{\n";
1354 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.Data(),
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) {
1365 cxxFile << (TString::Format(
" %s", cur_col->
strColumnName.Data())).Data();
1367 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
1379 cxxFile << (TString::Format(
" %s", cur_col->
strColumnName.Data())).Data();
1381 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
1388 cxxFile << (TString::Format(
" \"\n \"from %s", strTableName.Data())).Data();
1391 cxxFile << (TString::Format(
" join %s on %s.%s=%s.%s", curTableJoin->
strJoinTableName.Data(),
1397 cxxFile <<
" \"\n \"where";
1399 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1408 cxxFile << (TString::Format(
" lower(%s) = lower('%%%s')", cur_col->
strColumnName.Data(),
1419 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1425 cxxFile << (TString::Format(
", %s.Data()", cur_col->
strColumnName.Data())).Data();
1427 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
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) {
1460 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data())).Data();
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();
1470 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.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();
1486 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.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(),
1508 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data())).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();
1517 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.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();
1532 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.Data(),
1535 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.Data(),
1539 cxxFile << (TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(), StatementType.Data(),
1549 cxxFile <<
"\n delete stmt;\n\n";
1551 cxxFile << (TString::Format(
" return new %s(connDb", strClassName.Data())).Data();
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()))
1586 cxxFile << (TString::Format(
"%s* %s::Get%s(%s %s)\n{\n", strClassName.Data(), strClassName.Data(),
1591 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.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();
1604 cxxFile << (TString::Format(
" %s", current_col->
strColumnName.Data())).Data();
1606 cxxFile << (TString::Format(
", %s", current_col->
strColumnName.Data())).Data();
1613 cxxFile << (TString::Format(
" \"\n \"from %s", strTableName.Data())).Data();
1616 cxxFile << (TString::Format(
" join %s on %s.%s=%s.%s", curTableJoin->
strJoinTableName.Data(),
1623 cxxFile << (TString::Format(
" \"\n \"where lower(%s) = lower('%%%s')\", %s.Data());\n",
1628 cxxFile << (TString::Format(
" \"\n \"where %s = %%%s\", %s);\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";
1637 cxxFile << (TString::Format(
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();
1665 cxxFile << (TString::Format(
" %s %s;\n", current_col->
strVariableType.Data(), TempVar.Data()))
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;
1676 cxxFile << (TString::Format(
" %s %s = 0;\n", current_col->
strVariableType.Data(),
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;
1692 cxxFile << (TString::Format(
" %s %s = 0;\n", current_col->
strVariableType.Data(),
1695 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.Data(),
1699 cxxFile << (TString::Format(
" %s = stmt->Get%s(%d);\n", TempVar.Data(),
1700 StatementType.Data(), count))
1713 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data()))
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();
1723 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.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();
1738 cxxFile << (TString::Format(
" %s %s = 0;\n", cur_col->
strVariableType.Data(),
1741 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n",
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";
1758 cxxFile << (TString::Format(
" return new %s(connDb", strClassName.Data())).Data();
1759 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
1767 current_col = *it_inner;
1768 cxxFile << (TString::Format(
", %s", current_col->
strColumnName.Data())).Data();
1779 cxxFile << (TString::Format(
", %s", current_col->
strColumnName.Data())).Data();
1783 cxxFile <<
");\n}\n\n";
1788 cxxFile << (TString::Format(
"// ----- Check %s exists in the database ---------------------------\n",
1789 strTableNameSpace.Data()))
1791 cxxFile << (TString::Format(
"int %s::Check%sExists(", strClassName.Data(), strShortTableName.Data())).Data();
1794 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1810 cxxFile <<
")\n{\n";
1812 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.Data(),
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()))
1823 cxxFile << (TString::Format(
" join %s on %s.%s=%s.%s", curTableJoin->
strJoinTableName.Data(),
1829 cxxFile <<
" \"\n \"where";
1831 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1840 cxxFile << (TString::Format(
" lower(%s) = lower('%%%s')", cur_col->
strColumnName.Data(),
1851 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1857 cxxFile << (TString::Format(
", %s.Data()", cur_col->
strColumnName.Data())).Data();
1859 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
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()))
1897 cxxFile << (TString::Format(
"int %s::Check%sExists(%s %s)\n{\n", strClassName.Data(),
1902 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.Data(),
1903 strConnectionName.Data()))
1905 cxxFile <<
" if (connDb == nullptr) return -1;\n\n";
1907 cxxFile <<
" TSQLServer* db_server = connDb->GetSQLServer();\n\n";
1908 cxxFile << (TString::Format(
1909 " TString sql = TString::Format(\n \"select 1 \"\n \"from %s",
1910 strTableName.Data()))
1914 cxxFile << (TString::Format(
" join %s on %s.%s=%s.%s", curTableJoin->
strJoinTableName.Data(),
1921 cxxFile << (TString::Format(
" \"\n \"where lower(%s) = lower('%%%s')\", %s.Data());\n",
1926 cxxFile << (TString::Format(
" \"\n \"where %s = %%%s\", %s);\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";
1935 cxxFile << (TString::Format(
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()))
1962 cxxFile << (TString::Format(
"int %s::Delete%s(", strClassName.Data(), strShortTableName.Data())).Data();
1965 for (vector<structColumnInfo*>::iterator it = vecColumns.begin(); it != vecColumns.end(); ++it) {
1981 cxxFile <<
")\n{\n";
1983 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.Data(),
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) {
2003 cxxFile << (TString::Format(
" lower(%s) = lower(?)", cur_col->
strColumnName.Data())).Data();
2005 cxxFile << (TString::Format(
" %s = ?", cur_col->
strColumnName.Data())).Data();
2006 }
else if (currentDBMS == 1) {
2008 cxxFile << (TString::Format(
" lower(%s) = lower($%d)", cur_col->
strColumnName.Data(), count + 1))
2011 cxxFile << (TString::Format(
" %s = $%d", cur_col->
strColumnName.Data(), count + 1)).Data();
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) {
2027 cxxFile << (TString::Format(
" stmt->Set%s(%d, %s);\n", cur_col->
strStatementType.Data(), count,
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()))
2056 cxxFile << (TString::Format(
"int %s::Delete%s(%s %s)\n{\n", strClassName.Data(),
2061 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.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";
2095 cxxFile << (TString::Format(
" stmt->Set%s(0, %s);\n", cur_col->
strStatementType.Data(),
2099 cxxFile << (TString::Format(
"\n // delete %s from the dataBase\n", strTableNameSpace.Data())).Data();
2100 cxxFile <<
" if (!stmt->Process())\n {\n";
2101 cxxFile << (TString::Format(
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()))
2119 cxxFile << (TString::Format(
"int %s::PrintAll()\n{\n", strClassName.Data())).Data();
2121 cxxFile << (TString::Format(
" %s* connDb = %s::Open();\n", strConnectionName.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) {
2132 cxxFile << (TString::Format(
" %s", cur_col->
strColumnName.Data())).Data();
2134 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
2146 cxxFile << (TString::Format(
" %s", cur_col->
strColumnName.Data())).Data();
2148 cxxFile << (TString::Format(
", %s", cur_col->
strColumnName.Data())).Data();
2155 cxxFile << (TString::Format(
" \"\n \"from %s\");\n", strTableName.Data())).Data();
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";
2161 cxxFile << (TString::Format(
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<<\"";
2185 cxxFile << (TString::Format(
"%s: \";\n", cur_col->
strColumnName.Data())).Data();
2188 cxxFile << (TString::Format(
" if (stmt->IsNull(%d)) cout<<\"nullptr\";\n else\n ",
2192 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(), TempVar.Data()))
2194 cxxFile << (TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
2197 cxxFile << (TString::Format(
" %s %s=0;\n", cur_col->
strVariableType.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\")";
2217 cxxFile << (TString::Format(
" %s %s = nullptr;\n", cur_col->
strVariableType.Data(),
2222 cxxFile << (TString::Format(
" %s %s=0;\n", cur_col->
strVariableType.Data(),
2225 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.Data(),
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<<\"";
2253 cxxFile << (TString::Format(
"%s: \";\n", cur_col->
strColumnName.Data())).Data();
2256 cxxFile << (TString::Format(
" if (stmt->IsNull(%d)) cout<<\"nullptr\";\n else\n ",
2260 cxxFile << (TString::Format(
" %s %s;\n", cur_col->
strVariableType.Data(),
2263 cxxFile << (TString::Format(
" {\n %s = nullptr;\n", TempVar.Data())).Data();
2265 cxxFile << (TString::Format(
" %s %s=0;\n", cur_col->
strVariableType.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",
2277 << (TString::Format(
" cout<<stmt->Get%s(%d)", StatementType.Data(), count)).Data();
2279 cxxFile <<
".AsSQLString()";
2281 cxxFile <<
".AsString(\"s\")";
2286 cxxFile << (TString::Format(
" %s %s = nullptr;\n", cur_col->
strVariableType.Data(),
2290 cxxFile << (TString::Format(
" %s %s=0;\n", cur_col->
strVariableType.Data(),
2293 cxxFile << (TString::Format(
" stmt->Get%s(%d, (void*&)%s, %s);\n", StatementType.Data(),
2296 cxxFile << (TString::Format(
" cout<<(void*)%s<<\", binary size: \"<<%s;\n",
2301 << (TString::Format(
" cout<<(stmt->Get%s(%d))", StatementType.Data(), count)).Data();
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) {
2329 cxxFile << (TString::Format(
"int %s::Set%s(%s %s)\n{\n", strClassName.Data(),
2334 cxxFile << (TString::Format(
"int %s::Set%s(%s %s, ", strClassName.Data(),
2340 cxxFile << (TString::Format(
"%s %s)\n{\n", temp_col->
strVariableType.Data(),
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) {
2371 cxxFile << (TString::Format(
" %s = ?", current_col->
strColumnName.Data())).Data();
2373 cxxFile << (TString::Format(
" and %s = ?", current_col->
strColumnName.Data())).Data();
2374 }
else if (currentDBMS == 1) {
2376 cxxFile << (TString::Format(
" %s = $2", current_col->
strColumnName.Data())).Data();
2379 << (TString::Format(
" and %s = $%d", current_col->
strColumnName.Data(), count + 2)).Data();
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 ",
2396 cxxFile << (TString::Format(
" stmt->Set%s(0, %s);\n", cur_col->
strStatementType.Data(),
2400 cxxFile << (TString::Format(
" stmt->Set%s(0, %s, ", cur_col->
strStatementType.Data(),
2403 cxxFile << (TString::Format(
"%s, 0x4000000);\n", temp_col->
strColumnName.Data())).Data();
2407 for (vector<structColumnInfo*>::iterator it_inner = vecColumns.begin(); it_inner != vecColumns.end();
2414 cxxFile << (TString::Format(
" stmt->Set%s(%d, %s);\n", current_col->
strStatementType.Data(), count,
2420 cxxFile <<
"\n // write new value to the database\n";
2421 cxxFile <<
" if (!stmt->Process())\n {\n";
2422 cxxFile << (TString::Format(
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";
2430 cxxFile << (TString::Format(
" if (%s)\n delete %s;\n", cur_col->
strVariableName.Data(),
2434 cxxFile << (TString::Format(
" if (%s)\n delete [] %s;\n", cur_col->
strVariableName.Data(),
2439 cxxFile << (TString::Format(
" if (%s == nullptr) %s = nullptr;\n else\n ",
2443 cxxFile << (TString::Format(
" {\n %s %s = %s;\n", temp_col->
strVariableType.Data(),
2446 cxxFile << (TString::Format(
" %s = new %s[%s];\n", cur_col->
strVariableName.Data(),
2450 cxxFile << (TString::Format(
" memcpy(%s, %s, %s);\n }\n", cur_col->
strVariableName.Data(),
2454 cxxFile << (TString::Format(
" %s = new %s(*%s);\n", cur_col->
strVariableName.Data(),
2459 cxxFile << (TString::Format(
" %s = %s;\n", temp_col->
strVariableName.Data(),
2462 cxxFile << (TString::Format(
" %s = new %s[%s];\n", cur_col->
strVariableName.Data(),
2466 cxxFile << (TString::Format(
" memcpy(%s, %s, %s);\n", cur_col->
strVariableName.Data(),
2470 cxxFile << (TString::Format(
" %s = %s;\n", cur_col->
strVariableName.Data(),
2475 cxxFile <<
"\n delete stmt;\n";
2476 cxxFile <<
" return 0;\n}\n\n";
2480 cxxFile << (TString::Format(
"// ----- Print current %s ---------------------------------------\n",
2481 strTableNameSpace.Data()))
2483 cxxFile << (TString::Format(
"void %s::Print()\n{\n", strClassName.Data())).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) {
2490 cxxFile << (TString::Format(
"<<\". %s: \"<<(void*)%s", cur_col->
strColumnName.Data(),
2495 cxxFile << (TString::Format(
"<<\", binary size: \"<<%s", cur_col->
strVariableName.Data())).Data();
2499 cxxFile << (TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsSQLString())",
2505 cxxFile << (TString::Format(
2506 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsString(\"s\"))",
2512 cxxFile << (TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": *%s)",
2518 cxxFile << (TString::Format(
2519 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": TString::Format(\"%%%s\", "
2527 cxxFile << (TString::Format(
"<<\". %s: \"<<%s", cur_col->
strColumnName.Data(),
2531 cxxFile <<
".AsSQLString()";
2533 cxxFile <<
".AsString(\"s\")";
2543 cxxFile << (TString::Format(
"<<\". %s: \"<<(void*)%s", cur_col->
strColumnName.Data(),
2547 cxxFile << (TString::Format(
"<<\", binary size: \"<<%s", cur_col->
strVariableName.Data())).Data();
2551 cxxFile << (TString::Format(
2552 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsSQLString())",
2558 cxxFile << (TString::Format(
2559 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": (*%s).AsString(\"s\"))",
2565 cxxFile << (TString::Format(
"<<\". %s: \"<<(%s == nullptr? \"nullptr\": *%s)",
2571 cxxFile << (TString::Format(
2572 "<<\". %s: \"<<(%s == nullptr? \"nullptr\": "
2573 "TString::Format(\"%%%s\", *%s))",
2580 cxxFile << (TString::Format(
"<<\". %s: \"<<%s", cur_col->
strColumnName.Data(),
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;