/*! \brief Expands the template in \a filename for \a table * * This initializes a dictionary with the values for each column in table mapped to the * marker tags used in the template \a filename (Note: see top of file for supported tags) * It then expands the template replacing the tag markers and returns a string suitable for * writing to a file. * * \todo Break this up into smaller functions, its getting long .. and formating the variable strings * (eg. variable_gettor/variable_settor) should at least be broken out into a funcion and ideally * be moved to the template .. but, we need a smarter templating system for that. * * \param std::string filename - the file containing the template * \param WSqlTable table - the table being generated * \retval std::string an expanded template */ std::string WormClassGenerator::expand ( const std::string &filename, const WSqlTable &table, WormCodeTemplate::Type template_type ) { std::string strToReturn; bool has_string = false; bool has_vector = false; std::string type_declaration; std::string column_name; std::string variable_name; std::string variable_settor; std::string variable_gettor; std::vector<std::string> forward_declarations; TemplateDictionary *topdict = new TemplateDictionary ( filename ); TemplateDictionary *forwarddecls_dict = 0; TemplateDictionary *belongsto_dict = 0; TemplateDictionary *hasmany_dict = 0; TemplateDictionary *includes_dict = 0; TemplateDictionary *columns_dict = 0; TemplateDictionary *primary_key_dict = 0; topdict->SetValue ( kcd_CLASS_NAME, table.className() ); topdict->SetValue ( kcd_TABLE_NAME, table.name() ); std::string header_guard = table.className() + "_H"; WSqlDataType::toUpper(header_guard); topdict->SetValue("HEADER_GUARD", header_guard); std::string includes_string; //TODO: move this stuff to a method .. if( WormCodeTemplate::ClassDefinition == template_type ) { includes_string = "#include \"" + table.className() + ".h\""; WSqlDataType::toLower(includes_string); includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, includes_string); } if( WormCodeTemplate::ClassDefinitionBase == template_type ) { includes_string = "#include \"" + table.className() + "base.h\""; WSqlDataType::toLower(includes_string); includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, includes_string); } if( usesBaseClass && WormCodeTemplate::ClassDeclaration == template_type ) { includes_string = "#include \"" + table.className() + "base.h\""; WSqlDataType::toLower(includes_string); includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, includes_string); } const std::vector<WSqlColumn>& columns = table.columns(); std::vector<WSqlColumn>::const_iterator col_it = columns.begin(); if ( table.hasForeignKeys() ) { std::vector< WSqlForeignKey >fks = table.foreignKeys(); std::vector< WSqlForeignKey >::const_iterator fks_it = fks.begin(); for ( ; fks_it != fks.end(); ++fks_it ) { //fks_it->dump(); std::vector<std::string>::const_iterator it = std::find ( forward_declarations.begin(), forward_declarations.end(), fks_it->referencedClassName() ); if ( it == forward_declarations.end() ) //avoid repetition .. { forwarddecls_dict = topdict->AddSectionDictionary ( kcd_FORWARD_DECLARATIONS ); forwarddecls_dict->SetValue ( kcd_REFERENCED_CLASSNAME, fks_it->referencedClassName() ); forward_declarations.push_back ( fks_it->referencedClassName() ); if ( ( ! usesBaseClass && WormCodeTemplate::ClassDefinition == template_type) || WormCodeTemplate::ClassDefinitionBase == template_type ) { includes_string = "#include \"" + fks_it->referencedClassName() + ".h\""; WSqlDataType::toLower(includes_string); includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, includes_string); } } belongsto_dict = topdict->AddSectionDictionary ( kcd_BELONGS_TO ); belongsto_dict->SetValue ( kcd_REFERENCED_CLASSNAME, fks_it->referencedClassName() ); belongsto_dict->SetValue ( kcd_REFERENCED_TABLENAME, fks_it->referencedTableName() ); belongsto_dict->SetValue(kcd_REFERENCED_COLUMN_NAME, fks_it->referencedColumnName()); belongsto_dict->SetValue(kcd_REFERING_COLUMN_NAME, fks_it->columnName()); belongsto_dict->SetValue(kcd_REFERENCED_VARIABLE_NAME, fks_it->referencedVariableName()); belongsto_dict->SetValue(kcd_REFERING_VARIABLE_NAME, fks_it->referingVariableName()); } } if ( table.hasReferencedKeys() ) { has_vector = true; std::vector< WSqlReferencedKey >rks = table.referencedKeys(); std::vector< WSqlReferencedKey >::const_iterator rks_it = rks.begin(); for ( ; rks_it != rks.end(); ++rks_it ) { //rks_it->dump(); std::vector<std::string>::const_iterator it = std::find ( forward_declarations.begin(), forward_declarations.end(), rks_it->referingClassName() ); if ( it == forward_declarations.end() ) { forwarddecls_dict = topdict->AddSectionDictionary ( kcd_FORWARD_DECLARATIONS ); forwarddecls_dict->SetValue ( kcd_REFERENCED_CLASSNAME, rks_it->referingClassName() ); forward_declarations.push_back ( rks_it->referingClassName() ); if ( ( ! usesBaseClass && WormCodeTemplate::ClassDefinition == template_type) || WormCodeTemplate::ClassDefinitionBase == template_type ) { includes_string = "#include \"" + rks_it->referingClassName() + ".h\""; WSqlDataType::toLower(includes_string); includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, includes_string); } } hasmany_dict = topdict->AddSectionDictionary ( kcd_HAS_MANY ); hasmany_dict->SetValue ( kcd_FOREIGNKEY_CLASSNAME, rks_it->referingClassName() ); hasmany_dict->SetValue ( kcd_FOREIGNKEY_CLASS_PLURAL, rks_it->referingClassNamePlural() ); } } int i; char buff[8]; for (i=0; col_it != columns.end(); ++col_it, i++ ) { columns_dict = topdict->AddSectionDictionary ( kcd_COLUMNS ); type_declaration = col_it->typeDeclaration(); variable_name = col_it->variableName(); column_name = col_it->columnName(); std::string tmp = variable_name; tmp[0] = toupper ( tmp[0] ); variable_settor = "set" + tmp; variable_gettor = "get" + tmp; if ( type_declaration.compare ( "std::string" ) == 0 ) has_string = true; if ( ! col_it->typeIsSupported() ) columns_dict->ShowSection ( kcd_UNSUPPORTED ); if( col_it->isPrimaryKey() ) { primary_key_dict = topdict->AddSectionDictionary ( "PK_SECTION" ); primary_key_dict->SetValue("PK_VARIABLE_NAME", variable_name); primary_key_dict->SetValue("PK_COLUMN_NAME", column_name); } columns_dict->SetValue ( kcd_COLUMN_NAME, column_name ); columns_dict->SetValue ( kcd_DATATYPE, type_declaration ); if(! type_declaration.empty()) { type_declaration[0] = ::toupper(type_declaration[0]); //NOTE: transform std::string to StdString .. for Qt - should be some how moved or made more flexible. Or, depend on a config option. //ie. if(qt_support) .. which we might determine from the template file ? in any case, commandline and/or settings (which we don't have ..). size_t pos = type_declaration.find_last_of(':'); if(std::string::npos != pos) { type_declaration[pos+1] = ::toupper(type_declaration[pos+1]); type_declaration.erase (std::remove(type_declaration.begin(), type_declaration.end(), ':'), type_declaration.end()); } //Also for Qt: longlong to LongLong pos = type_declaration.find("Long long"); if(std::string::npos != pos) { type_declaration[pos] = ::toupper(type_declaration[pos]); pos = type_declaration.find(' '); type_declaration.erase(pos,1); pos = type_declaration.find_last_of('l'); type_declaration[pos] = ::toupper(type_declaration[pos]); } } columns_dict->SetValue ( "TO_DATATYPE", type_declaration ); columns_dict->SetValue ( kcd_VARIABLE_NAME, variable_name ); columns_dict->SetValue ( kcd_VARIABLE_SETTOR, variable_settor ); columns_dict->SetValue ( kcd_VARIABLE_GETTOR, variable_gettor ); snprintf(buff, 8, "%d", i); columns_dict->SetValue("CURRENT_COLUMN_NUMBER", buff); snprintf(buff, 8, "%d", i+1); columns_dict->SetValue("COLUMN_COUNT", buff); //FIXME always unsigned .. deep strangeness .. /* if(col_it->isUnsigned()) { coldict->ShowSection(kcd_UNSIGNED); std::cerr << "in table " << tbl.className() << " " << col_it->columnName() << "is unsigned .." << std::endl << " type: " << WSqlDataType::toString(col_it->type()) << std::endl; } */ } //used for a final "QString.arg()" in cases of WHERE queries .. snprintf(buff, 8, "%d", i+1); topdict->SetValue("COLUMN_COUNT", buff); if(has_string) { if ( ( ! usesBaseClass && WormCodeTemplate::ClassDeclaration == template_type) || WormCodeTemplate::ClassDeclarationBase == template_type ) { includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, "#include <string>"); } } if(has_vector) { if ( ( ! usesBaseClass && WormCodeTemplate::ClassDeclaration == template_type) || WormCodeTemplate::ClassDeclarationBase == template_type ) { includes_dict = topdict->AddSectionDictionary(kcd_INCLUDES); includes_dict->SetValue(kcd_INCLUDE, "#include <vector>"); } } ExpandTemplate ( filename, DO_NOT_STRIP, topdict, &strToReturn ); delete topdict; return strToReturn; }
bool UserInfoCollector::fillData(const std::string& path, const Str2StrMap& cookies, const Str2StrMap& parameter, TemplateDictionary& dict) { int uid = GetUserId(cookies, parameter); if (uid <= 0) { MCE_WARN("UserInfoCollector::fillData --> uid:" << uid << " path:" << path); return false; } string ticket = GetTicket(cookies, parameter); if (checkTicket(uid, ticket)) { dict.SetValue("userid", boost::lexical_cast<string>(uid)); TalkUserPtr uPtr; try { uPtr = TalkCacheClient::instance().GetUserByIdWithLoad(uid); } catch (Ice::Exception& e) { MCE_WARN("UserInfoCollector::fillData -->TalkCacheClient::GetUserByIdWithLoad-->err : " <<e); } if(!uPtr){ return false; } UserUrlInfoPtr urlinfo; try{ urlinfo = UserUrlAdapter::instance().getUserUrl(uid); }catch(Ice::Exception& e){ MCE_WARN("UserInfoCollector::fillData-->UserUrlAdapter::getUserUrl-->uid:" << uid << " error:" << e); } if(!urlinfo){ return false; } if(uPtr && urlinfo){ string username = uPtr->name; string url = urlinfo->headUrl(); string tinyurl = PhotoUrlHelper::instance().GetFullUrl(url); dict.SetValue("tinyurl", tinyurl); dict.SetValue("username", username); } try{ ClientScoreDataN sd = ScoreCacheNAdapter::instance().getClientScoreDataN(uid); dict.SetValue("historyLoginDays", boost::lexical_cast<string>(sd.historyLoginDays)); dict.SetValue("continueLoginDays", boost::lexical_cast<string>(sd.continueLoginDays)); Date datetime((sd.lastLoginTime+0.0) / 1000.0); MCE_INFO("UserInfoCollector::fillData --> userid:" << uid << " lastLoginTime:" << (sd.lastLoginTime +0.0) / 1000.0 << " datetime:" << datetime.toDateTime()); ostringstream os; if(datetime.month() <= 9){ os << "0" << datetime.month() << "-"; }else{ os << datetime.month() << "-"; } if(datetime.day() <= 9){ os << "0" << datetime.day(); }else{ os << datetime.day(); }os << " "; if(datetime.hour() <= 9){ os << "0" << datetime.hour() << ":"; }else{ os << datetime.hour() << ":"; } if(datetime.minute() <= 9){ os << "0" << datetime.minute(); }else{ os << datetime.minute(); } dict.SetValue("lastLoginTime", boost::lexical_cast<string>(os.str())); dict.SetValue("loginType", boost::lexical_cast<string>(sd.loginType)); dict.SetValue("score", boost::lexical_cast<string>(sd.score)); dict.SetValue("level", boost::lexical_cast<string>(sd.level)); dict.SetValue("nextLevelScore", boost::lexical_cast<string>(sd.nextLevelScore)); dict.SetValue("awardCount", boost::lexical_cast<string>(sd.awardCount)); int vip = sd.vip; if(vip > 0){ MCE_INFO("UserInfoCollector::fillData --> userid:" << uid << " vip:" << vip); dict.ShowSection("VIP_SEC"); dict.SetValue("vip", boost::lexical_cast<string>(vip)); } ostringstream oss; for(vector<string>::iterator it = sd.icons.begin(); it != sd.icons.end(); ++it){ string level = (*it); size_t pos = level.find("-"); if(string::npos ==pos){ continue; } level.erase(pos,1); dict.AddSectionDictionary(level); oss << level << "(" << (*it) << ") ,"; } MCE_INFO("UserInfoCollector::fillData --> icons:" << oss.str()); }catch(Ice::Exception& e) { MCE_WARN("UserInfoCollector::fillData --> call ScoreCache err : " << e); } } else { return false; } }
void GenerateParser(string _srcPath, string _folder, string _name, const Grammar& _grammar) { TemplateDictionary dict(_name); dict.SetValue("namespace", _name); Defs::const_iterator i, iEnd = _grammar.defs.end(); for (i = _grammar.defs.begin(); i != iEnd; ++i) { TemplateDictionary* pDef = dict.AddSectionDictionary("def"); pDef->SetValue("name", i->first); if (i->second.isNode) pDef->ShowSection("isNode"); else if (i->second.isMemoized) pDef->ShowSection("isSkip"); ostringstream parseCodeStream; ParserGenerator parserGenerator(parseCodeStream, _grammar); parserGenerator.Emit(i->second, -1); string parseCodeFilename = "parseCode_" + i->first; string parseCode = parseCodeStream.str(); StringToTemplateCache(parseCodeFilename, parseCode, STRIP_BLANK_LINES); pDef->AddIncludeDictionary("parseCode")->SetFilename(parseCodeFilename); ostringstream traverseCodeStream; ParserGenerator traverserGenerator(traverseCodeStream, _grammar, true); traverserGenerator.Emit(i->second, -1); string traverseCodeFilename = "traverseCode_" + i->first; string traverseCode = traverseCodeStream.str(); StringToTemplateCache(traverseCodeFilename, traverseCode, STRIP_BLANK_LINES); pDef->AddIncludeDictionary("traverseCode")->SetFilename(traverseCodeFilename); } string headerText; if (!ExpandTemplate("Parser.h.tpl", STRIP_BLANK_LINES, &dict, &headerText)) throw runtime_error("CTemplate Parser.h.tpl expansion failed!"); ofstream headerFile; headerFile.exceptions(ofstream::failbit | ofstream::badbit); headerFile.open((_folder + _name + ".h").c_str()); WriteAutoGenNotice(headerFile, _srcPath); headerFile << format("#ifndef %1%_H\n#define %1%_H\n\n") % to_upper_copy(_name); headerFile << headerText; headerFile << "\n#endif\n"; dict.SetValue("header", headerText); string sourceText; if (!ExpandTemplate("Parser.cpp.tpl", STRIP_BLANK_LINES, &dict, &sourceText)) throw runtime_error("CTemplate Parser.cpp.tpl expansion failed!"); ofstream sourceFile; sourceFile.exceptions(ofstream::failbit | ofstream::badbit); string sourcePath = _folder + _name + ".cpp"; sourceFile.open(sourcePath.c_str()); WriteAutoGenNotice(sourceFile, _srcPath); sourceFile << sourceText; }