DataSourceInterface* DataSourceManager::getImpl(string name) { string appName = CommonUtils::getAppName(); StringUtil::trim(name); if(name=="") { name = defDsnName; } name = appName + name; if(dsns.find(name)==dsns.end()) { throw "Data Source Not found..."; } DataSourceManager* dsnMgr = dsns[name]; DataSourceInterface* t = NULL; if(StringUtil::toLowerCopy(dsnMgr->props.getType())=="sql") { #ifdef INC_SDORM_SQL t = new SQLDataSourceImpl(dsnMgr->pool, &dsnMgr->mapping); #endif } else if(StringUtil::toLowerCopy(dsnMgr->props.getType())=="mongo") { #ifdef INC_SDORM_MONGO t = new MongoDBDataSourceImpl(dsnMgr->pool, &dsnMgr->mapping); #endif } if(t == NULL) { return NULL; } t->appName = dsnMgr->mapping.getAppName(); t->dlib = dlopen(INTER_LIB_FILE, RTLD_NOW); if(t->dlib == NULL) { cerr << dlerror() << endl; throw "Cannot load application shared library"; } t->reflector = new Reflector(t->dlib); map<string, DataSourceEntityMapping>::iterator it; for(it=dsnMgr->mapping.getDseMap().begin();it!=dsnMgr->mapping.getDseMap().end();++it) { DataSourceEntityMapping dsemp = it->second; if(dsemp.isIdGenerate()) { t->init(dsemp, true); } } return t; }
bool DataSourceInterface::executeInsertInternal(Query& query, void* entity) { DataSourceEntityMapping dsemp = mapping->getDataSourceEntityMapping(query.getClassName()); ClassInfo clas = reflector->getClassInfo(query.getClassName(), appName); if(dsemp.isIdGenerate() && dsemp.getIdgendbEntityType()!="identity") { assignId(dsemp, clas, entity); } bool flag = executeInsert(query, entity); if(flag && dsemp.isIdGenerate() && dsemp.getIdgendbEntityType()=="identity") { assignId(dsemp, clas, entity); } return flag; }
void DataSourceInterface::assignId(DataSourceEntityMapping& dsemp, ClassInfo& clas, void* entity) { GenericObject idv; next(dsemp, idv); if(!idv.isNull()) { Field fld = clas.getField(dsemp.getIdPropertyName()); vector<void *> valus; if(GenericObject::isNumber32(idv.getTypeName()) && GenericObject::isNumber(fld.getType())) { long* id; idv.get(id); valus.push_back(id); } else if(GenericObject::isNumber64(idv.getTypeName()) && GenericObject::isNumber(fld.getType())) { long long* id; idv.get(id); valus.push_back(id); } else if(GenericObject::isFPN(idv.getTypeName()) && GenericObject::isFPN(fld.getType())) { long double* id; idv.get(id); valus.push_back(id); } else if(GenericObject::isString(idv.getTypeName()) && GenericObject::isString(fld.getType())) { string* id; idv.set(id); valus.push_back(id); } else { throw "Data-Source-Object/Entity types do not match for id property" + dsemp.getClassName() + ":" + dsemp.getIdPropertyName(); } args argus; argus.push_back(fld.getType()); string methname = "set"+StringUtil::capitalizedCopy(fld.getFieldName()); Method meth = clas.getMethod(methname,argus); reflector->invokeMethodGVP(entity,meth,valus); } }
void Mapping::addDataSourceEntityMapping(const DataSourceEntityMapping& dsemp) { if(dsemp.getClassName()!="" && dseMap.find(dsemp.getClassName())==dseMap.end()) { dseMap[dsemp.getClassName()] = dsemp; if(!dsemp.isEmbedded()) { tableClassMapping[dsemp.getTableName()] = dsemp.getClassName(); } } }