| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : ClassGenerateDialog.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "ClassGenerateDialog.h" |
| 27 |
#include "event_notifier.h" |
| 28 |
#include <wx/xrc/xmlres.h> |
| 29 |
#include <wx/tokenzr.h> |
| 30 |
#include "globals.h" |
| 31 |
#include "cl_command_event.h" |
| 32 |
#include "codelite_events.h" |
| 33 |
|
| 34 |
ClassGenerateDialog::ClassGenerateDialog( |
| 35 |
wxWindow* parent, IDbAdapter* dbAdapter, xsSerializable* pItems, IManager* pMgr) |
| 36 |
: _ClassGenerateDialog(parent) |
| 37 |
{ |
| 38 |
m_pDbAdapter = dbAdapter; |
| 39 |
m_pItems = pItems; |
| 40 |
m_mgr = pMgr; |
| 41 |
|
| 42 |
m_mapTemplateFiles[wxT("DatabaseLayer (wxWidgets)")] = |
| 43 |
wxT("dataClass_dbl.htmp;dataClass_dbl.ctmp;viewClass_dbl.htmp;viewClass_dbl.ctmp"); |
| 44 |
m_mapTemplateFiles[wxT("Debea (STL)")] = |
| 45 |
wxT("dataClass_dba.htmp;dataClass_dba.ctmp;viewClass_dba.htmp;viewClass_dba.ctmp"); |
| 46 |
m_mapTemplateFiles[wxT("wxDebea (wxWidgets)")] = |
| 47 |
wxT("dataClass_wxdba.htmp;dataClass_wxdba.ctmp;viewClass_wxdba.htmp;viewClass_wxdba.ctmp"); |
| 48 |
m_mapTemplateFiles[wxT("Generic classes (STL)")] = |
| 49 |
wxT("dataClass.htmp;dataClass.ctmp;viewClass.htmp;viewClass.ctmp"); |
| 50 |
|
| 51 |
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView); |
| 52 |
if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeVirtualDirectory) { |
| 53 |
m_txVirtualDir->SetValue( |
| 54 |
VirtualDirectorySelectorDlg::DoGetPath(m_mgr->GetWorkspaceTree(), item.m_item, false)); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
ClassGenerateDialog::~ClassGenerateDialog() { Destroy(); } |
| 59 |
|
| 60 |
bool ClassGenerateDialog::GenerateClass(Table* pTab, const wxString& path) |
| 61 |
{ |
| 62 |
wxString hFileName = wxT(""); |
| 63 |
wxString cFileName = wxT(""); |
| 64 |
|
| 65 |
wxArrayString arrFileNames = |
| 66 |
wxStringTokenize(m_mapTemplateFiles[m_choiceTemplates->GetStringSelection()], wxT(";"), wxTOKEN_RET_EMPTY); |
| 67 |
|
| 68 |
if(pTab->IsView()) { |
| 69 |
hFileName = arrFileNames[2]; |
| 70 |
cFileName = arrFileNames[3]; |
| 71 |
|
| 72 |
} else { |
| 73 |
hFileName = arrFileNames[0]; |
| 74 |
cFileName = arrFileNames[1]; |
| 75 |
} |
| 76 |
|
| 77 |
wxTextFile htmpFile(m_mgr->GetInstallDirectory() + wxT("/templates/databaselayer/") + hFileName); |
| 78 |
wxTextFile ctmpFile(m_mgr->GetInstallDirectory() + wxT("/templates/databaselayer/") + cFileName); |
| 79 |
|
| 80 |
if(!htmpFile.Open() || !ctmpFile.Open()) return false; |
| 81 |
|
| 82 |
classTableName = pTab->GetName(); |
| 83 |
classItemName = m_txPrefix->GetValue() + pTab->GetName() + m_txPostfix->GetValue(); |
| 84 |
classItemDef = wxT("__") + classItemName.Upper() + wxT("_H__"); |
| 85 |
classColName = m_txPrefix->GetValue() + pTab->GetName() + wxT("Collection") + m_txPostfix->GetValue(); |
| 86 |
classUtilName = m_txPrefix->GetValue() + pTab->GetName() + wxT("Utils") + m_txPostfix->GetValue(); |
| 87 |
|
| 88 |
wxString hFile; |
| 89 |
wxFileName fnHeaderFileName(path + wxT("/") + classItemName + wxT(".h")); |
| 90 |
|
| 91 |
wxString cFile; |
| 92 |
wxFileName fnCppFileName(path + wxT("/") + classItemName + wxT(".cpp")); |
| 93 |
|
| 94 |
bool suc = |
| 95 |
GenerateFile(pTab, htmpFile, hFile, classItemName, classItemDef, classColName, classTableName, classUtilName); |
| 96 |
suc &= |
| 97 |
GenerateFile(pTab, ctmpFile, cFile, classItemName, classItemDef, classColName, classTableName, classUtilName); |
| 98 |
|
| 99 |
htmpFile.Close(); |
| 100 |
ctmpFile.Close(); |
| 101 |
|
| 102 |
// format output files |
| 103 |
FormatFile(hFile, fnHeaderFileName); |
| 104 |
FormatFile(cFile, fnCppFileName); |
| 105 |
|
| 106 |
::WriteFileWithBackup(fnCppFileName.GetFullPath(), cFile, false); |
| 107 |
::WriteFileWithBackup(fnHeaderFileName.GetFullPath(), hFile, false); |
| 108 |
|
| 109 |
// add files to the workspace |
| 110 |
wxArrayString arrString; |
| 111 |
arrString.Add(path + wxT("/") + classItemName + wxT(".h")); |
| 112 |
arrString.Add(path + wxT("/") + classItemName + wxT(".cpp")); |
| 113 |
|
| 114 |
m_mgr->AddFilesToVirtualFolder(m_txVirtualDir->GetValue(), arrString); |
| 115 |
|
| 116 |
// retag workspace |
| 117 |
wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace")); |
| 118 |
EventNotifier::Get()->TopFrame()->GetEventHandler()->AddPendingEvent(evt); |
| 119 |
|
| 120 |
return suc; |
| 121 |
} |
| 122 |
|
| 123 |
void ClassGenerateDialog::OnCancelClick(wxCommandEvent& event) { Destroy(); } |
| 124 |
|
| 125 |
void ClassGenerateDialog::OnGenerateClick(wxCommandEvent& event) |
| 126 |
{ |
| 127 |
if(m_txVirtualDir->GetValue().IsEmpty()) { |
| 128 |
wxMessageBox(_("Virtual name cannot be empty"), _("CodeLite"), wxICON_WARNING | wxOK); |
| 129 |
m_txVirtualDir->SetFocus(); |
| 130 |
return; |
| 131 |
} |
| 132 |
if(m_dirPicker->GetPath().IsEmpty()) { |
| 133 |
wxMessageBox(_("Folder name cannot be empty"), _("CodeLite"), wxICON_WARNING | wxOK); |
| 134 |
m_dirPicker->SetFocus(); |
| 135 |
} |
| 136 |
|
| 137 |
m_textLog->Clear(); |
| 138 |
|
| 139 |
wxString err_msg; |
| 140 |
wxString project = m_txVirtualDir->GetValue().BeforeFirst(wxT(':')); |
| 141 |
ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg); |
| 142 |
if(proj) { |
| 143 |
wxString filePath = m_dirPicker->GetPath(); // proj->GetFileName().GetPath(); |
| 144 |
|
| 145 |
Table* pTable = wxDynamicCast(m_pItems, Table); |
| 146 |
|
| 147 |
if(pTable) { |
| 148 |
if(GenerateClass(pTable, filePath)) |
| 149 |
m_textLog->AppendText(pTable->GetName() + _("......... Generated successfully!\n")); |
| 150 |
else |
| 151 |
m_textLog->AppendText(pTable->GetName() + _("......... Error!!!\n")); |
| 152 |
} else { |
| 153 |
SerializableList::compatibility_iterator node = m_pItems->GetFirstChildNode(); |
| 154 |
while(node) { |
| 155 |
Table* pTab = wxDynamicCast(node->GetData(), Table); |
| 156 |
if(pTab) { |
| 157 |
if(GenerateClass(pTab, filePath)) |
| 158 |
m_textLog->AppendText(pTab->GetName() + _("......... Generated successfully!\n")); |
| 159 |
else |
| 160 |
m_textLog->AppendText(pTab->GetName() + _("......... Error!!!\n")); |
| 161 |
} |
| 162 |
|
| 163 |
node = node->GetNext(); |
| 164 |
} |
| 165 |
} |
| 166 |
wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace")); |
| 167 |
m_mgr->GetTheApp()->GetTopWindow()->GetEventHandler()->AddPendingEvent(e); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
bool ClassGenerateDialog::GenerateFile(Table* pTab, wxTextFile& htmpFile, wxString& hFile, |
| 172 |
const wxString& classItemName, const wxString& classItemDef, const wxString& classColName, |
| 173 |
const wxString& classTableName, const wxString& classUtilName) |
| 174 |
{ |
| 175 |
Constraint* pPK = NULL; |
| 176 |
int colCount = 0; |
| 177 |
int lastEditParam = 0; |
| 178 |
|
| 179 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 180 |
while(node) { |
| 181 |
Constraint* pConstr = wxDynamicCast(node->GetData(), Constraint); |
| 182 |
if(pConstr) { |
| 183 |
if(pConstr->GetType() == Constraint::primaryKey) pPK = pConstr; |
| 184 |
} else |
| 185 |
colCount++; |
| 186 |
node = node->GetNext(); |
| 187 |
} |
| 188 |
Column* pPKCol = NULL; |
| 189 |
|
| 190 |
if(pPK) { |
| 191 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 192 |
while(node) { |
| 193 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 194 |
if(pCol) { |
| 195 |
if(pCol->GetName() == pPK->GetLocalColumn()) pPKCol = pCol; |
| 196 |
} |
| 197 |
node = node->GetNext(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if((pPKCol == NULL) && (pTab->IsView() == false)) { |
| 202 |
m_textLog->AppendText(wxString::Format(_("Table %s has no primary key defined!\n"), pTab->GetName().c_str())); |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
for(wxString str = htmpFile.GetFirstLine(); !htmpFile.Eof(); str = htmpFile.GetNextLine()) { |
| 207 |
if(str.Contains(wxT("%%classItemGetters%%"))) { |
| 208 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 209 |
while(node) { |
| 210 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 211 |
if(pCol) { |
| 212 |
hFile << wxString::Format(wxT("\tconst %s Get%s() const {"), |
| 213 |
GetResTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) |
| 214 |
<< "\n"; |
| 215 |
hFile << wxString::Format(wxT("\t\treturn m_%s;"), pCol->GetName().c_str()) << "\n"; |
| 216 |
hFile << wxString::Format(wxT("\t\t}")) << "\n"; |
| 217 |
} |
| 218 |
node = node->GetNext(); |
| 219 |
} |
| 220 |
|
| 221 |
} else if(str.Contains(wxT("%%classItemVariables%%"))) { |
| 222 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 223 |
while(node) { |
| 224 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 225 |
if(pCol) { |
| 226 |
hFile << wxString::Format(wxT("\t%s m_%s;"), |
| 227 |
GetTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) |
| 228 |
<< "\n"; |
| 229 |
} |
| 230 |
node = node->GetNext(); |
| 231 |
} |
| 232 |
|
| 233 |
} else if(str.Contains(wxT("%%classItemLoading%%"))) { |
| 234 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 235 |
while(node) { |
| 236 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 237 |
if(pCol) { |
| 238 |
hFile << wxString::Format(wxT("\t\tm_%s = pResult->%s(wxT(\"%s\"));"), pCol->GetName().c_str(), |
| 239 |
GetResultFunction(pCol->GetType()->GetUniversalType()).c_str(), |
| 240 |
pCol->GetName().c_str()) |
| 241 |
<< "\n"; |
| 242 |
} |
| 243 |
node = node->GetNext(); |
| 244 |
} |
| 245 |
|
| 246 |
} else if(str.Contains(wxT("%%classItemBindings%%"))) { |
| 247 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 248 |
while(node) { |
| 249 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 250 |
if(pCol) { |
| 251 |
hFile << GetDebeaBinding(pCol) << "\n"; |
| 252 |
} |
| 253 |
node = node->GetNext(); |
| 254 |
} |
| 255 |
|
| 256 |
} else if(str.Contains(wxT("%%classItemAddParameters%%"))) { |
| 257 |
bool first = true; |
| 258 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 259 |
while(node) { |
| 260 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 261 |
if(pCol) { |
| 262 |
if(first) { |
| 263 |
hFile << wxString::Format(wxT("\t\t\t%s %s"), |
| 264 |
GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), |
| 265 |
pCol->GetName().c_str()) |
| 266 |
<< "\n"; |
| 267 |
first = false; |
| 268 |
} else { |
| 269 |
hFile << wxString::Format(wxT("\t\t\t,%s %s"), |
| 270 |
GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), |
| 271 |
pCol->GetName().c_str()) |
| 272 |
<< "\n"; |
| 273 |
} |
| 274 |
} |
| 275 |
node = node->GetNext(); |
| 276 |
} |
| 277 |
|
| 278 |
} else if(str.Contains(wxT("%%classItemSetParams%%"))) { |
| 279 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 280 |
while(node) { |
| 281 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 282 |
if(pCol) { |
| 283 |
hFile << wxT("\tm_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n"; |
| 284 |
} |
| 285 |
node = node->GetNext(); |
| 286 |
} |
| 287 |
|
| 288 |
} else if(str.Contains(wxT("%%classColLabelFillGrid%%"))) { |
| 289 |
int i = 0; |
| 290 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 291 |
while(node) { |
| 292 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 293 |
if(pCol) { |
| 294 |
hFile << wxT("\t\tpGrid->AppendCols(1);") << "\n"; |
| 295 |
hFile << wxString::Format( |
| 296 |
wxT("\t\tpGrid->SetColLabelValue(%i,wxT(\"%s\"));"), i++, pCol->GetName().c_str()) |
| 297 |
<< "\n"; |
| 298 |
} |
| 299 |
node = node->GetNext(); |
| 300 |
} |
| 301 |
|
| 302 |
} else if(str.Contains(wxT("%%classColDataFillGrid%%"))) { |
| 303 |
int i = 0; |
| 304 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 305 |
while(node) { |
| 306 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 307 |
if(pCol) { |
| 308 |
hFile << GetFillData(pCol, i++) << "\n"; |
| 309 |
} |
| 310 |
node = node->GetNext(); |
| 311 |
} |
| 312 |
|
| 313 |
} else if(str.Contains(wxT("%%primaryKeyHeader%%"))) { |
| 314 |
if(pPKCol) { |
| 315 |
hFile << wxString::Format(wxT("\t/*! \\brief Return %s from db on the %s base */"), |
| 316 |
pPKCol->GetParentName().c_str(), pPKCol->GetName().c_str()) |
| 317 |
<< "\n"; |
| 318 |
hFile << wxString::Format(wxT("\tstatic %s* GetBy%s(%s %s, DatabaseLayer* pDbLayer);"), |
| 319 |
classItemName.c_str(), pPKCol->GetName().c_str(), |
| 320 |
GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) |
| 321 |
<< "\n"; |
| 322 |
} |
| 323 |
|
| 324 |
} else if(str.Contains(wxT("%%primaryKeyBody%%"))) { |
| 325 |
if(pPKCol) { |
| 326 |
hFile << wxString::Format(wxT("%s* %s::GetBy%s(%s %s, DatabaseLayer* pDbLayer)"), classItemName.c_str(), |
| 327 |
classItemName.c_str(), pPKCol->GetName().c_str(), |
| 328 |
GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) |
| 329 |
<< "\n"; |
| 330 |
hFile << wxT("{") << "\n"; |
| 331 |
hFile << wxT("\tDatabaseResultSet* resSet = NULL;") << "\n"; |
| 332 |
hFile << wxT("\tPreparedStatement* pStatement = NULL;") << "\n"; |
| 333 |
hFile << wxT("\tif (pDbLayer){") << "\n"; |
| 334 |
hFile << wxT("\t\tif (pDbLayer->IsOpen()){") << "\n"; |
| 335 |
|
| 336 |
hFile << wxString::Format(wxT("\t\t\tpStatement = pDbLayer->PrepareStatement(wxT(\"SELECT * FROM %s " |
| 337 |
"WHERE %s = ?\"));"), |
| 338 |
classTableName.c_str(), pPKCol->GetName().c_str()) |
| 339 |
<< "\n"; |
| 340 |
hFile << wxString::Format(wxT("\t\t\tpStatement->%s(1, %s);"), |
| 341 |
GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(), |
| 342 |
pPKCol->GetName().c_str()) |
| 343 |
<< "\n"; |
| 344 |
hFile << wxT("\t\t\tresSet = pStatement->RunQueryWithResults();") << "\n"; |
| 345 |
hFile << wxT("\t\t\t}") << "\n"; |
| 346 |
hFile << wxT("\t\t}") << "\n"; |
| 347 |
|
| 348 |
hFile << wxT("\tif (resSet){") << "\n"; |
| 349 |
hFile << wxString::Format(wxT("\t\tif (resSet->Next()) return new %s(resSet);"), classItemName.c_str()) |
| 350 |
<< "\n"; |
| 351 |
hFile << wxT("\t\tpStatement->Close();") << "\n"; |
| 352 |
hFile << wxT("\t\t}") << "\n"; |
| 353 |
hFile << wxT("\treturn NULL;") << "\n"; |
| 354 |
|
| 355 |
hFile << wxT("}") << "\n"; |
| 356 |
} |
| 357 |
|
| 358 |
} else if(str.Contains(wxT("%%classUtilsAddParameters%%"))) { |
| 359 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 360 |
while(node) { |
| 361 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 362 |
if(pCol) { |
| 363 |
hFile << wxString::Format(wxT("\t\t\t,%s %s"), |
| 364 |
GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) |
| 365 |
<< "\n"; |
| 366 |
} |
| 367 |
node = node->GetNext(); |
| 368 |
} |
| 369 |
|
| 370 |
} else if(str.Contains(wxT("%%classUtilsAddParametersWithoutPK%%"))) { |
| 371 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 372 |
while(node) { |
| 373 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 374 |
if(pCol && (!pPKCol || (pCol->GetName() != pPKCol->GetName()))) { |
| 375 |
hFile << wxString::Format(wxT("\t\t\t,%s %s"), |
| 376 |
GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) |
| 377 |
<< "\n"; |
| 378 |
} |
| 379 |
node = node->GetNext(); |
| 380 |
} |
| 381 |
|
| 382 |
} else if(str.Contains(wxT("%%classUtilsDeleteParameters%%"))) { |
| 383 |
if(pPKCol) |
| 384 |
hFile << wxString::Format(wxT("\t\t\t,%s %s"), |
| 385 |
GetParamTypeName(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) |
| 386 |
<< "\n"; |
| 387 |
|
| 388 |
} else if(str.Contains(wxT("%%classUtilsAddSetParams%%"))) { |
| 389 |
int i = 1; |
| 390 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 391 |
while(node) { |
| 392 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 393 |
if(pCol) { |
| 394 |
hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), |
| 395 |
GetAddParamFunction(pCol->GetType()->GetUniversalType()).c_str(), i++, |
| 396 |
pCol->GetName().c_str()) |
| 397 |
<< "\n"; |
| 398 |
} |
| 399 |
node = node->GetNext(); |
| 400 |
} |
| 401 |
lastEditParam = i; |
| 402 |
|
| 403 |
} else if(str.Contains(wxT("%%classUtilsEditSetParams%%"))) { |
| 404 |
int i = 1; |
| 405 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 406 |
while(node) { |
| 407 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 408 |
if(pCol && (!pPKCol || (pCol->GetName() != pPKCol->GetName()))) { |
| 409 |
hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), |
| 410 |
GetAddParamFunction(pCol->GetType()->GetUniversalType()).c_str(), i++, |
| 411 |
pCol->GetName().c_str()) |
| 412 |
<< "\n"; |
| 413 |
} |
| 414 |
node = node->GetNext(); |
| 415 |
} |
| 416 |
lastEditParam = i; |
| 417 |
|
| 418 |
} else if(str.Contains(wxT("%%classUtilsAddSetDebeaParams%%"))) { |
| 419 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 420 |
while(node) { |
| 421 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 422 |
if(pCol && (!pPKCol || (pCol->GetName() != pPKCol->GetName()))) { |
| 423 |
hFile << wxT("\t\tc.m_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n"; |
| 424 |
} |
| 425 |
node = node->GetNext(); |
| 426 |
} |
| 427 |
|
| 428 |
} else if(str.Contains(wxT("%%classUtilsAddDelParams%%"))) { |
| 429 |
if(pPKCol) |
| 430 |
hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), |
| 431 |
GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(), 1, |
| 432 |
pPKCol->GetName().c_str()) |
| 433 |
<< "\n"; |
| 434 |
|
| 435 |
} else if(str.Contains(wxT("%%classUtilsAddStatement%%"))) { |
| 436 |
wxString cols = wxT(""); |
| 437 |
wxString params = wxT(""); |
| 438 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 439 |
while(node) { |
| 440 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 441 |
if(pCol) { |
| 442 |
if(!cols.IsEmpty()) cols = cols + wxT(","); |
| 443 |
cols += pCol->GetName(); |
| 444 |
|
| 445 |
if(!params.IsEmpty()) params += wxT(","); |
| 446 |
params += wxT("?"); |
| 447 |
} |
| 448 |
node = node->GetNext(); |
| 449 |
} |
| 450 |
hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"INSERT " |
| 451 |
"INTO %s (%s) VALUES (%s)\"));"), |
| 452 |
pTab->GetName().c_str(), cols.c_str(), params.c_str()) |
| 453 |
<< "\n"; |
| 454 |
|
| 455 |
} else if(str.Contains(wxT("%%classUtilsEditStatement%%"))) { |
| 456 |
wxString cols = wxT(""); |
| 457 |
wxString params = wxT(""); |
| 458 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 459 |
while(node) { |
| 460 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 461 |
if(pCol && (!pPKCol || (pCol->GetName() != pPKCol->GetName()))) { |
| 462 |
if(!cols.IsEmpty()) cols = cols + wxT(","); |
| 463 |
cols += pCol->GetName() + wxT(" = ?"); |
| 464 |
} |
| 465 |
node = node->GetNext(); |
| 466 |
} |
| 467 |
if(pPKCol) |
| 468 |
hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = " |
| 469 |
"pDbLayer->PrepareStatement(wxT(\"UPDATE %s SET %s WHERE %s = ?\"));"), |
| 470 |
pTab->GetName().c_str(), cols.c_str(), pPKCol->GetName().c_str()) |
| 471 |
<< "\n"; |
| 472 |
else |
| 473 |
hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n"; |
| 474 |
|
| 475 |
} else if(str.Contains(wxT("%%classUtilsEditDebeaStatement%%"))) { |
| 476 |
SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); |
| 477 |
while(node) { |
| 478 |
Column* pCol = wxDynamicCast(node->GetData(), Column); |
| 479 |
if(pCol && (!pPKCol || (pCol->GetName() != pPKCol->GetName()))) { |
| 480 |
hFile << wxT("\t\t\tc->setMember(c->m_") + pCol->GetName() + wxT(", ") + pCol->GetName() + wxT(");") |
| 481 |
<< "\n"; |
| 482 |
} |
| 483 |
node = node->GetNext(); |
| 484 |
} |
| 485 |
|
| 486 |
} else if(str.Contains(wxT("%%classUtilsDeleteStatement%%"))) { |
| 487 |
if(pPKCol) |
| 488 |
hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = " |
| 489 |
"pDbLayer->PrepareStatement(wxT(\"DELETE FROM %s WHERE %s = ?\"));"), |
| 490 |
pTab->GetName().c_str(), pPKCol->GetName().c_str()) |
| 491 |
<< "\n"; |
| 492 |
else |
| 493 |
hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n"; |
| 494 |
|
| 495 |
} else if(str.Contains(wxT("%%classUtilsPKSetParams%%"))) { |
| 496 |
if(pPKCol) |
| 497 |
hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), |
| 498 |
GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(), lastEditParam, |
| 499 |
pPKCol->GetName().c_str()) |
| 500 |
<< "\n"; |
| 501 |
|
| 502 |
} else if(str.Contains(wxT("%%classUtilsCreateStatement%%"))) { |
| 503 |
wxStringTokenizer tknz(m_pDbAdapter->GetCreateTableSql(pTab, false), wxT("\n"), wxTOKEN_STRTOK); |
| 504 |
while(true) { |
| 505 |
wxString line = tknz.GetNextToken(); |
| 506 |
if(!tknz.HasMoreTokens()) break; // omit last line |
| 507 |
hFile << wxT("\t\t\t\"") + line + wxT("\" \\") << "\n"; |
| 508 |
} |
| 509 |
|
| 510 |
} else if(str.Contains(wxT("%%classUtilsDropStatement%%"))) { |
| 511 |
wxStringTokenizer tknz(m_pDbAdapter->GetDropTableSql(pTab), wxT("\n"), wxTOKEN_STRTOK); |
| 512 |
while(tknz.HasMoreTokens()) { |
| 513 |
hFile << wxT("\t\t\t\"") + tknz.GetNextToken() + wxT("\" \\") << "\n"; |
| 514 |
} |
| 515 |
|
| 516 |
} else { |
| 517 |
str.Replace(wxT("%%classItemName%%"), classItemName); |
| 518 |
str.Replace(wxT("%%classItemDef%%"), classItemDef); |
| 519 |
str.Replace(wxT("%%classColName%%"), classColName); |
| 520 |
str.Replace(wxT("%%classTableName%%"), classTableName); |
| 521 |
str.Replace(wxT("%%classUtilName%%"), classUtilName); |
| 522 |
if(pPKCol) { |
| 523 |
str.Replace(wxT("%%pkType%%"), GetParamTypeName(pPKCol->GetType()->GetUniversalType())); |
| 524 |
str.Replace(wxT("%%pkName%%"), pPKCol->GetName()); |
| 525 |
} |
| 526 |
hFile << str << "\n"; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
return true; |
| 531 |
} |
| 532 |
|
| 533 |
wxString ClassGenerateDialog::GetFillData(Column* pCol, int colIndex) |
| 534 |
{ |
| 535 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxDebea"))) { |
| 536 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_TEXT) |
| 537 |
return wxT("\t\t\tpGrid->SetCellValue(row.m_") + pCol->GetName() + |
| 538 |
wxString::Format(wxT(",i,%i);"), colIndex); |
| 539 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DATE_TIME) |
| 540 |
return wxT("\t\t\tpGrid->SetCellValue(row.m_") + pCol->GetName() + |
| 541 |
wxString::Format(wxT(".Format(),i,%i);"), colIndex); |
| 542 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_INT) |
| 543 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%i\"),row.m_") + pCol->GetName() + |
| 544 |
wxString::Format(wxT("),i,%i);"), colIndex); |
| 545 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_FLOAT) |
| 546 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%lf\"),row.m_") + pCol->GetName() + |
| 547 |
wxString::Format(wxT("),i,%i);"), colIndex); |
| 548 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DECIMAL) |
| 549 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%lf\"),row.m_") + pCol->GetName() + |
| 550 |
wxString::Format(wxT("),i,%i);"), colIndex); |
| 551 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_BOOLEAN) |
| 552 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%i\"),row.m_") + pCol->GetName() + |
| 553 |
wxString::Format(wxT("),i,%i);"), colIndex); |
| 554 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_OTHER) |
| 555 |
return wxT("\t\t\tpGrid->SetCellValue(wxT(\"some data\")") + wxString::Format(wxT(",i,%i);"), colIndex); |
| 556 |
} else { |
| 557 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_TEXT) |
| 558 |
return wxT("\t\t\tpGrid->SetCellValue(row->Get") + pCol->GetName() + |
| 559 |
wxString::Format(wxT("(),i,%i);"), colIndex); |
| 560 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DATE_TIME) |
| 561 |
return wxT("\t\t\tpGrid->SetCellValue(row->Get") + pCol->GetName() + |
| 562 |
wxString::Format(wxT("().Format(),i,%i);"), colIndex); |
| 563 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_INT) |
| 564 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%i\"),row->Get") + pCol->GetName() + |
| 565 |
wxString::Format(wxT("()),i,%i);"), colIndex); |
| 566 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_FLOAT) |
| 567 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%lf\"),row->Get") + pCol->GetName() + |
| 568 |
wxString::Format(wxT("()),i,%i);"), colIndex); |
| 569 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_DECIMAL) |
| 570 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%lf\"),row->Get") + pCol->GetName() + |
| 571 |
wxString::Format(wxT("()),i,%i);"), colIndex); |
| 572 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_BOOLEAN) |
| 573 |
return wxT("\t\t\tpGrid->SetCellValue(wxString::Format(wxT(\"%i\"),row->Get") + pCol->GetName() + |
| 574 |
wxString::Format(wxT("()),i,%i);"), colIndex); |
| 575 |
if(pCol->GetType()->GetUniversalType() == IDbType::dbtTYPE_OTHER) |
| 576 |
return wxT("\t\t\tpGrid->SetCellValue(wxT(\"some data\")") + wxString::Format(wxT(",i,%i);"), colIndex); |
| 577 |
} |
| 578 |
|
| 579 |
return wxT(""); |
| 580 |
} |
| 581 |
|
| 582 |
wxString ClassGenerateDialog::GetAddParamFunction(IDbType::UNIVERSAL_TYPE type) |
| 583 |
{ |
| 584 |
if(type == IDbType::dbtTYPE_TEXT) return wxT("SetParamString"); |
| 585 |
if(type == IDbType::dbtTYPE_DATE_TIME) return wxT("SetParamDate"); |
| 586 |
if(type == IDbType::dbtTYPE_INT) return wxT("SetParamInt"); |
| 587 |
if(type == IDbType::dbtTYPE_FLOAT) return wxT("SetParamDouble"); |
| 588 |
if(type == IDbType::dbtTYPE_DECIMAL) return wxT("SetParamDouble"); |
| 589 |
if(type == IDbType::dbtTYPE_BOOLEAN) return wxT("SetParamBool"); |
| 590 |
if(type == IDbType::dbtTYPE_OTHER) return wxT("SetParamBlob"); |
| 591 |
return wxT(""); |
| 592 |
} |
| 593 |
|
| 594 |
wxString ClassGenerateDialog::GetTypeName(IDbType::UNIVERSAL_TYPE type) |
| 595 |
{ |
| 596 |
if(type == IDbType::dbtTYPE_TEXT) { |
| 597 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 598 |
return wxT("wxString"); |
| 599 |
else |
| 600 |
return wxT("std::string"); |
| 601 |
} |
| 602 |
if(type == IDbType::dbtTYPE_DATE_TIME) { |
| 603 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 604 |
return wxT("wxDateTime"); |
| 605 |
else |
| 606 |
return wxT("tm"); |
| 607 |
} |
| 608 |
if(type == IDbType::dbtTYPE_INT) return wxT("int"); |
| 609 |
if(type == IDbType::dbtTYPE_FLOAT) return wxT("double"); |
| 610 |
if(type == IDbType::dbtTYPE_DECIMAL) return wxT("double"); |
| 611 |
if(type == IDbType::dbtTYPE_BOOLEAN) return wxT("bool"); |
| 612 |
if(type == IDbType::dbtTYPE_OTHER) return wxT("void*"); |
| 613 |
return wxT(""); |
| 614 |
} |
| 615 |
|
| 616 |
wxString ClassGenerateDialog::GetResTypeName(IDbType::UNIVERSAL_TYPE type) |
| 617 |
{ |
| 618 |
if(type == IDbType::dbtTYPE_TEXT) { |
| 619 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 620 |
return wxT("wxString&"); |
| 621 |
else |
| 622 |
return wxT("std::string&"); |
| 623 |
} |
| 624 |
if(type == IDbType::dbtTYPE_DATE_TIME) { |
| 625 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 626 |
return wxT("wxDateTime&"); |
| 627 |
else |
| 628 |
return wxT("tm&"); |
| 629 |
} |
| 630 |
if(type == IDbType::dbtTYPE_INT) return wxT("int"); |
| 631 |
if(type == IDbType::dbtTYPE_FLOAT) return wxT("double"); |
| 632 |
if(type == IDbType::dbtTYPE_DECIMAL) return wxT("double"); |
| 633 |
if(type == IDbType::dbtTYPE_BOOLEAN) return wxT("bool"); |
| 634 |
if(type == IDbType::dbtTYPE_OTHER) return wxT("void*"); |
| 635 |
return wxT(""); |
| 636 |
} |
| 637 |
|
| 638 |
wxString ClassGenerateDialog::GetDebeaBinding(Column* pCol) |
| 639 |
{ |
| 640 |
wxString ret; |
| 641 |
|
| 642 |
if(pCol) { |
| 643 |
wxString bind, type; |
| 644 |
|
| 645 |
switch(pCol->GetType()->GetUniversalType()) { |
| 646 |
case IDbType::dbtTYPE_TEXT: |
| 647 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) { |
| 648 |
bind = wxT("BIND_STR"); |
| 649 |
type = wxT("wxdba::String"); |
| 650 |
} else { |
| 651 |
bind = wxT("BIND_STR"); |
| 652 |
type = wxT("dba::String"); |
| 653 |
} |
| 654 |
break; |
| 655 |
case IDbType::dbtTYPE_DATE_TIME: |
| 656 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) { |
| 657 |
bind = wxT("BIND_DAT"); |
| 658 |
type = wxT("wxdba::DateTime"); |
| 659 |
} else { |
| 660 |
bind = wxT("BIND_DAT"); |
| 661 |
type = wxT("dba::DateTime"); |
| 662 |
} |
| 663 |
break; |
| 664 |
case IDbType::dbtTYPE_INT: |
| 665 |
bind = wxT("BIND_INT"); |
| 666 |
type = wxT("dba::Int"); |
| 667 |
break; |
| 668 |
case IDbType::dbtTYPE_FLOAT: |
| 669 |
bind = wxT("BIND_FLT"); |
| 670 |
type = wxT("dba::Double"); |
| 671 |
break; |
| 672 |
case IDbType::dbtTYPE_DECIMAL: |
| 673 |
bind = wxT("BIND_FLT"); |
| 674 |
type = wxT("dba::Double"); |
| 675 |
break; |
| 676 |
case IDbType::dbtTYPE_BOOLEAN: |
| 677 |
bind = wxT("BIND_INT"); |
| 678 |
type = wxT("dba::Bool"); |
| 679 |
break; |
| 680 |
default: |
| 681 |
break; |
| 682 |
} |
| 683 |
|
| 684 |
ret = bind + wxT("(") + classItemName + wxT("::m_") + pCol->GetName() + wxT(", ") + type + wxT(", \"") + |
| 685 |
pCol->GetName() + wxT("\")"); |
| 686 |
} |
| 687 |
|
| 688 |
return ret; |
| 689 |
} |
| 690 |
|
| 691 |
wxString ClassGenerateDialog::GetParamTypeName(IDbType::UNIVERSAL_TYPE type) |
| 692 |
{ |
| 693 |
if(type == IDbType::dbtTYPE_TEXT) { |
| 694 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 695 |
return wxT("const wxString&"); |
| 696 |
else |
| 697 |
return wxT("const std::string&"); |
| 698 |
} |
| 699 |
if(type == IDbType::dbtTYPE_DATE_TIME) { |
| 700 |
if(m_choiceTemplates->GetStringSelection().Contains(wxT("wxWidgets"))) |
| 701 |
return wxT("const wxDateTime&"); |
| 702 |
else |
| 703 |
return wxT("const tm&"); |
| 704 |
} |
| 705 |
if(type == IDbType::dbtTYPE_INT) return wxT("int"); |
| 706 |
if(type == IDbType::dbtTYPE_FLOAT) return wxT("double"); |
| 707 |
if(type == IDbType::dbtTYPE_DECIMAL) return wxT("double"); |
| 708 |
if(type == IDbType::dbtTYPE_BOOLEAN) return wxT("bool"); |
| 709 |
if(type == IDbType::dbtTYPE_OTHER) return wxT("const void*"); |
| 710 |
return wxT(""); |
| 711 |
} |
| 712 |
|
| 713 |
wxString ClassGenerateDialog::GetResultFunction(IDbType::UNIVERSAL_TYPE type) |
| 714 |
{ |
| 715 |
if(type == IDbType::dbtTYPE_TEXT) return wxT("GetResultString"); |
| 716 |
if(type == IDbType::dbtTYPE_DATE_TIME) return wxT("GetResultDate"); |
| 717 |
if(type == IDbType::dbtTYPE_INT) return wxT("GetResultInt"); |
| 718 |
if(type == IDbType::dbtTYPE_FLOAT) return wxT("GetResultDouble"); |
| 719 |
if(type == IDbType::dbtTYPE_DECIMAL) return wxT("GetResultDouble"); |
| 720 |
if(type == IDbType::dbtTYPE_BOOLEAN) return wxT("GetResultBool"); |
| 721 |
if(type == IDbType::dbtTYPE_OTHER) return wxT("GetResultBlob"); |
| 722 |
return wxT(""); |
| 723 |
} |
| 724 |
|
| 725 |
void ClassGenerateDialog::OnBtnBrowseClick(wxCommandEvent& event) |
| 726 |
{ |
| 727 |
VirtualDirectorySelectorDlg dlg(this, m_mgr->GetWorkspace(), m_txVirtualDir->GetValue()); |
| 728 |
if(dlg.ShowModal() == wxID_OK) { |
| 729 |
m_txVirtualDir->SetValue(dlg.GetVirtualDirectoryPath()); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
void ClassGenerateDialog::FormatFile(wxString& content, const wxFileName& filename) |
| 734 |
{ |
| 735 |
clSourceFormatEvent evt(wxEVT_FORMAT_STRING); |
| 736 |
evt.SetInputString(content); |
| 737 |
evt.SetFileName(filename.GetFullPath()); |
| 738 |
EventNotifier::Get()->ProcessEvent(evt); |
| 739 |
content = evt.GetFormattedString(); |
| 740 |
} |