inline void attachChild(const std::string& name, const NodePtr& node) { typedef std::map<std::string, NodePtr>::iterator ItType; std::size_t i = 0; std::string childName(name); while(true) { std::pair<ItType, bool> result = children_.insert(std::make_pair(childName, node)); if(result.second) { return; } std::ostringstream stream; stream << name << (i++); childName = stream.str(); } }
int processProductsSubtableXML(pugi::xpath_node node, int recordPK, std::vector<int>& subtablePKs) { DbAccess* m_dbAccess = getDBAccess(); if(m_dbAccess==NULL) return 2; int ret = 0; // Get the Attribute Element Child belonging to each Record bool needPrimaryKeys = false; if (subtablePKs.empty()) { needPrimaryKeys = true; subtablePKs.push_back(0); subtablePKs.push_back(0); } int& attributePK = subtablePKs[0]; int& defaultAttributePK = subtablePKs[1]; if (needPrimaryKeys) { Rows rows; m_dbAccess->executeSqlQuery("SELECT MAX(PrimaryKey) AS PrimaryKey FROM ProductAttribute", rows); if (!rows.empty()) attributePK = convertStringToU32(rows.front()["PrimaryKey"]); rows.clear(); m_dbAccess->executeSqlQuery("SELECT MAX(PrimaryKey) AS PrimaryKey FROM ProductDefaultAttribute", rows); if (!rows.empty()) defaultAttributePK = convertStringToU32(rows.front()["PrimaryKey"]); } for (pugi::xml_node tool = node.node().first_child(); tool; tool = tool.next_sibling()) { std::string childName(tool.name()); if (childName.compare("Attribute") == 0) { std::string s_queryAttribute = ""; std::string s_queryColumn = ""; std::string s_queryValue = ""; attributePK++; int attributeCount = 0; for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute()) { if(attributeCount > 0) { s_queryColumn += ","; s_queryValue += ","; } s_queryColumn.append("'"); s_queryColumn += attr.name(); s_queryColumn.append("'"); s_queryValue += sqlify(attr.value()); attributeCount++; } s_queryAttribute = "INSERT INTO ProductAttribute('PrimaryKey',"; s_queryAttribute += s_queryColumn; s_queryAttribute += ",'FK_ProductRecord') VALUES ('"; s_queryAttribute += static_cast<std::ostringstream*>( &(std::ostringstream() << attributePK) )->str(); // PK for Device Parameter Attribute s_queryAttribute += "',"; s_queryAttribute += s_queryValue; s_queryAttribute += ",'"; s_queryAttribute += static_cast<std::ostringstream*>( &(std::ostringstream() << recordPK) )->str(); // FK to Device Parameter Record s_queryAttribute += "'"; s_queryAttribute += ")"; // std::cout << "\n" << s_queryDeviceParameterAttribute; if ( m_dbAccess->executeSqlInsert(s_queryAttribute) != 0 ) { CsErrx("Query '%s' failed", s_queryAttribute.c_str()); ret = 2; } } else if (childName.compare("DefaultAttribute") == 0) { std::string s_queryDefaultAttribute = ""; std::string s_queryColumn = ""; std::string s_queryValue = ""; defaultAttributePK++; int attributeCount = 0; for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute()) { if(attributeCount > 0) { s_queryColumn += ","; s_queryValue += ","; } s_queryColumn.append("'"); s_queryColumn += attr.name(); s_queryColumn.append("'"); s_queryValue += sqlify(attr.value()); attributeCount++; } s_queryDefaultAttribute = "INSERT INTO ProductDefaultAttribute('PrimaryKey',"; s_queryDefaultAttribute += s_queryColumn; s_queryDefaultAttribute += ",'FK_ProductRecord') VALUES ('"; s_queryDefaultAttribute += static_cast<std::ostringstream*>( &(std::ostringstream() << defaultAttributePK) )->str(); // PK for Device Parameter Attribute s_queryDefaultAttribute += "',"; s_queryDefaultAttribute += s_queryValue; s_queryDefaultAttribute += ",'"; s_queryDefaultAttribute += static_cast<std::ostringstream*>( &(std::ostringstream() << recordPK) )->str(); // FK to Device Parameter Record s_queryDefaultAttribute += "'"; s_queryDefaultAttribute += ")"; // std::cout << "\n" << s_queryDeviceParameterAttribute; if ( m_dbAccess->executeSqlInsert(s_queryDefaultAttribute) != 0 ) { CsErrx("Query '%s' failed", s_queryDefaultAttribute.c_str()); ret = 2; } } } return ret; }