ConfigNameHash GetConfigTitles(const wxString &szarp_dir, wxArrayString* hidden) { ConfigNameHash result; wxDir dir(szarp_dir); wxString prefix; std::map<wxString, std::pair<wxString, time_t> > ci; wxConfig* cfg = new wxConfig(_T("SZARPCONFIGURATIONS")); if (cfg->Exists(_T("CONFIGURATIONS"))) { wxString cn = cfg->Read(_T("CONFIGURATIONS")); wxStringTokenizer tknz(cn, _T("/"), wxTOKEN_STRTOK); while (tknz.HasMoreTokens()) { wxString prefix = tknz.GetNextToken(); if (!tknz.HasMoreTokens()) break; wxString title = tknz.GetNextToken(); if (!tknz.HasMoreTokens()) break; wxString _time = tknz.GetNextToken(); long t; _time.ToLong(&t); if(hidden == 0 || (hidden != 0 && hidden->Index(prefix) != wxNOT_FOUND))//asm ci[prefix] = std::make_pair(title, time_t(t)); } } bool changed = false; if (dir.GetFirst(&prefix, wxEmptyString, wxDIR_DIRS)) do { if(hidden != 0 && hidden->Index(prefix) != wxNOT_FOUND) continue;//asm wxString file_path = szarp_dir + prefix + _T("/config/params.xml"); if (!wxFile::Exists(file_path) || wxRegEx(wxString(std::string(".*[-._]bak.*").c_str(), wxConvUTF8), 0).Matches(prefix.Lower(), 0)) continue; wxFileName file_name(file_path); std::map<wxString, std::pair<wxString, time_t> >::iterator i; if ((i = ci.find(prefix)) != ci.end()) { time_t t = i->second.second; if (t == (long)file_name.GetModificationTime().GetTicks()) { result[prefix] = i->second.first; continue; } } xmlTextReaderPtr reader = xmlNewTextReaderFilename(SC::S2A(file_path).c_str()); if (reader == NULL) continue; while (xmlTextReaderRead(reader) == 1) { const xmlChar *name = xmlTextReaderConstName(reader); if (name == NULL) continue; if (xmlStrcmp(name, BAD_CAST "params")) continue; xmlChar* title = xmlTextReaderGetAttribute(reader, BAD_CAST "title"); if (title != NULL) { wxString _title = SC::U2S(title).c_str(); result[prefix] = _title; changed = true; ci[prefix] = std::make_pair(_title, file_name.GetModificationTime().GetTicks()); xmlFree(title); } break; } xmlFreeTextReader(reader); } while (dir.GetNext(&prefix)); if (!changed) { delete cfg; return result; } wxString out; for (std::map<wxString, std::pair<wxString, time_t> >::iterator i = ci.begin(); i != ci.end(); i++) { out += i->first + _T("/"); out += i->second.first + _T("/"); wxString ts; ts << i->second.second; out += ts + _T("/"); } cfg->Write(_T("CONFIGURATIONS"), out); cfg->Flush(); delete cfg; return result; }
bool ClassGenerateDialog::GenerateFile(Table* pTab, wxTextFile& htmpFile, wxString& hFile, const wxString& classItemName, const wxString& classItemDef, const wxString& classColName, const wxString& classTableName, const wxString& classUtilName) { Constraint* pPK = NULL; int colCount = 0; int lastEditParam = 0; SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Constraint* pConstr = wxDynamicCast(node->GetData(),Constraint); if (pConstr) { if (pConstr->GetType() == Constraint::primaryKey) pPK = pConstr; } else colCount++; node = node->GetNext(); } Column* pPKCol = NULL; if (pPK) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { if (pCol->GetName() == pPK->GetLocalColumn()) pPKCol = pCol; } node = node->GetNext(); } } if( (pPKCol == NULL) && (pTab->IsView() == false) ) { m_textLog->AppendText( wxString::Format( _("Table %s has no primary key defined!\n"), pTab->GetName().c_str() ) ); return false; } for ( wxString str = htmpFile.GetFirstLine(); !htmpFile.Eof(); str = htmpFile.GetNextLine() ) { if (str.Contains(wxT("%%classItemGetters%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxString::Format(wxT("\tconst %s Get%s() const {"), GetResTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; hFile << wxString::Format(wxT("\t\treturn m_%s;"), pCol->GetName().c_str()) << "\n"; hFile << wxString::Format(wxT("\t\t}")) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classItemVariables%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxString::Format(wxT("\t%s m_%s;"), GetTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str())<< "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classItemLoading%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxString::Format(wxT("\t\tm_%s = pResult->%s(wxT(\"%s\"));"),pCol->GetName().c_str(), GetResultFunction(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classItemBindings%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << GetDebeaBinding(pCol) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classItemAddParameters%%"))) { bool first = true; SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { if( first ) { hFile << wxString::Format(wxT("\t\t\t%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; first = false; } else { hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; } } node = node->GetNext(); } } else if (str.Contains(wxT("%%classItemSetParams%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol ) { hFile << wxT("\tm_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classColLabelFillGrid%%"))) { int i = 0; SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxT("\t\tpGrid->AppendCols(1);")<< "\n"; hFile << wxString::Format(wxT("\t\tpGrid->SetColLabelValue(%i,wxT(\"%s\"));"),i++,pCol->GetName().c_str())<< "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classColDataFillGrid%%"))) { int i = 0; SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << GetFillData(pCol, i++) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%primaryKeyHeader%%"))) { if (pPKCol) { hFile << wxString::Format(wxT("\t/*! \\brief Return %s from db on the %s base */"),pPKCol->GetParentName().c_str(),pPKCol->GetName().c_str()) << "\n"; hFile << wxString::Format(wxT("\tstatic %s* GetBy%s(%s %s, DatabaseLayer* pDbLayer);"),classItemName.c_str(),pPKCol->GetName().c_str(), GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(),pPKCol->GetName().c_str()) << "\n"; } } else if (str.Contains(wxT("%%primaryKeyBody%%"))) { if (pPKCol) { hFile << wxString::Format(wxT("%s* %s::GetBy%s(%s %s, DatabaseLayer* pDbLayer)"),classItemName.c_str(),classItemName.c_str(),pPKCol->GetName().c_str(), GetTypeName(pPKCol->GetType()->GetUniversalType()).c_str(),pPKCol->GetName().c_str()) << "\n"; hFile << wxT("{") << "\n"; hFile << wxT("\tDatabaseResultSet* resSet = NULL;") << "\n"; hFile << wxT("\tPreparedStatement* pStatement = NULL;") << "\n"; hFile << wxT("\tif (pDbLayer){") << "\n"; hFile << wxT("\t\tif (pDbLayer->IsOpen()){") << "\n"; hFile << wxString::Format(wxT("\t\t\tpStatement = pDbLayer->PrepareStatement(wxT(\"SELECT * FROM %s WHERE %s = ?\"));"),classTableName.c_str(), pPKCol->GetName().c_str()) << "\n"; hFile << wxString::Format(wxT("\t\t\tpStatement->%s(1, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) << "\n"; hFile << wxT("\t\t\tresSet = pStatement->RunQueryWithResults();") << "\n"; hFile << wxT("\t\t\t}") << "\n"; hFile << wxT("\t\t}") << "\n"; hFile << wxT("\tif (resSet){") << "\n"; hFile << wxString::Format(wxT("\t\tif (resSet->Next()) return new %s(resSet);"),classItemName.c_str()) << "\n"; hFile << wxT("\t\tpStatement->Close();") << "\n"; hFile << wxT("\t\t}") << "\n"; hFile << wxT("\treturn NULL;") << "\n"; hFile << wxT("}") << "\n"; } } else if (str.Contains(wxT("%%classUtilsAddParameters%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classUtilsAddParametersWithoutPK%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) { hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pCol->GetType()->GetUniversalType()).c_str(), pCol->GetName().c_str()) << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classUtilsDeleteParameters%%"))) { if (pPKCol) hFile << wxString::Format(wxT("\t\t\t,%s %s"), GetParamTypeName(pPKCol->GetType()->GetUniversalType()).c_str(), pPKCol->GetName().c_str()) << "\n"; } else if (str.Contains(wxT("%%classUtilsAddSetParams%%"))) { int i = 1; SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pCol->GetType()->GetUniversalType()).c_str(),i++,pCol->GetName().c_str()) << "\n"; } node = node->GetNext(); } lastEditParam = i; } else if (str.Contains(wxT("%%classUtilsAddSetDebeaParams%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) { hFile << wxT("\t\tc.m_") + pCol->GetName() + wxT(" = ") + pCol->GetName() + wxT(";") << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classUtilsAddDelParams%%"))) { if (pPKCol) hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(),1,pPKCol->GetName().c_str()) << "\n"; } else if (str.Contains(wxT("%%classUtilsAddStatement%%"))) { wxString cols = wxT(""); wxString params = wxT(""); SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { if (!cols.IsEmpty()) cols = cols + wxT(","); cols += pCol->GetName(); if (!params.IsEmpty()) params += wxT(","); params += wxT("?"); } node = node->GetNext(); } hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"INSERT INTO %s (%s) VALUES (%s)\"));"),pTab->GetName().c_str(), cols.c_str(), params.c_str()) << "\n"; } else if (str.Contains(wxT("%%classUtilsEditStatement%%"))) { wxString cols = wxT(""); wxString params = wxT(""); SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol) { if (!cols.IsEmpty()) cols = cols + wxT(","); cols += pCol->GetName() + wxT(" = ?"); } node = node->GetNext(); } if (pPKCol) hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"UPDATE %s SET %s WHERE %s = ?\"));"),pTab->GetName().c_str(), cols.c_str(), pPKCol->GetName().c_str()) << "\n"; else hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n"; } else if (str.Contains(wxT("%%classUtilsEditDebeaStatement%%"))) { SerializableList::compatibility_iterator node = pTab->GetFirstChildNode(); while( node ) { Column* pCol = wxDynamicCast(node->GetData(),Column); if (pCol && ( !pPKCol || (pCol->GetName() != pPKCol->GetName()) ) ) { hFile << wxT("\t\t\tc->setMember(c->m_") + pCol->GetName() + wxT(", ") + pCol->GetName() + wxT(");") << "\n"; } node = node->GetNext(); } } else if (str.Contains(wxT("%%classUtilsDeleteStatement%%"))) { if (pPKCol) hFile << wxString::Format(wxT("\t\tPreparedStatement* pStatement = pDbLayer->PrepareStatement(wxT(\"DELETE FROM %s WHERE %s = ?\"));"),pTab->GetName().c_str(), pPKCol->GetName().c_str()) << "\n"; else hFile << wxT("\t\tPreparedStatement* pStatement = NULL;") << "\n"; } else if (str.Contains(wxT("%%classUtilsPKSetParams%%"))) { if (pPKCol) hFile << wxString::Format(wxT("\t\t\tpStatement->%s(%i, %s);"), GetAddParamFunction(pPKCol->GetType()->GetUniversalType()).c_str(),lastEditParam,pPKCol->GetName().c_str()) << "\n"; } else if (str.Contains(wxT("%%classUtilsCreateStatement%%"))) { wxStringTokenizer tknz( m_pDbAdapter->GetCreateTableSql( pTab, true ), wxT("\n"), wxTOKEN_STRTOK ); while( true ) { wxString line = tknz.GetNextToken(); if( !tknz.HasMoreTokens() ) break; // omit last line hFile << wxT("\t\t\t\"") + line + wxT("\" \\") << "\n"; } } else if (str.Contains(wxT("%%classUtilsDropStatement%%"))) { wxStringTokenizer tknz( m_pDbAdapter->GetDropTableSql( pTab ), wxT("\n"), wxTOKEN_STRTOK ); while( tknz.HasMoreTokens() ) { hFile << wxT("\t\t\t\"") + tknz.GetNextToken() + wxT("\" \\") << "\n"; } } else { str.Replace(wxT("%%classItemName%%"),classItemName); str.Replace(wxT("%%classItemDef%%"),classItemDef); str.Replace(wxT("%%classColName%%"),classColName); str.Replace(wxT("%%classTableName%%"),classTableName); str.Replace(wxT("%%classUtilName%%"), classUtilName); if( pPKCol ) { str.Replace(wxT("%%pkType%%"), GetParamTypeName(pPKCol->GetType()->GetUniversalType())); str.Replace(wxT("%%pkName%%"), pPKCol->GetName()); } hFile << str << "\n"; } } return true; }