Skip to content

Instantly share code, notes, and snippets.

@agasiev
Last active October 13, 2015 08:08
Show Gist options
  • Save agasiev/4165674 to your computer and use it in GitHub Desktop.
Save agasiev/4165674 to your computer and use it in GitHub Desktop.
Romip data fast loading for convert
// Compile:
// sudo g++ ./RomipLoad.cpp ./pugixml.cpp
#include "pugixml.hpp"
using namespace std;
struct imhonet {
string score;
string content_id;
string element_id;
string user_id;
string text;
};
struct market {
string id;
string model_id;
string author_id;
string cr_time;
string rating;
string text;
string pro;
string contra;
string rank;
};
inline std::string replace(std::string text, std::string s, std::string d)
{
for(size_t index=0; index=text.find(s, index), index!=std::string::npos;)
{
text.replace(index, s.length(), d);
index+=d.length();
}
return text;
}
void xml_loader_imhonet(string filename) {
string ofilename = filename + ".sql";
ofstream out(ofilename.c_str());
pugi::xml_document doc;
doc.load_file(filename.c_str());
pugi::xml_node tools = doc.child("table").child("rows");
out << "set autocommit=0;" << endl;
//by row
int cntr = 0;
for (pugi::xml_node row = tools.first_child(); row; row = row.next_sibling())
{
cntr++;
imhonet item;
pugi::xml_node value = row.first_child();
item.score = value.child_value();
value = value.next_sibling();
item.content_id = value.child_value();
value = value.next_sibling();
item.element_id = value.child_value();
value = value.next_sibling();
item.user_id = value.child_value();
value = value.next_sibling();
item.text = value.child_value();
item.text = replace(item.text, "\"", "\\\"");
out << "INSERT INTO imhonet (element_id,content_id,user_id,score,text,item_id) VALUES (" << item.element_id << ","
<< item.content_id << "," << item.user_id << "," << item.score << ", \" " << item.text << " \"," << row.attribute("rowNumber").value() << ");" << std::endl;
}
out << "commit;" << endl;
out.close();
}
void xml_loader_market(string filename) {
string ofilename = filename + ".sql";
ofstream out(ofilename.c_str());
pugi::xml_document doc;
doc.load_file(filename.c_str());
pugi::xml_node tools = doc.child("table").child("rows");
out << "set autocommit=0;" << endl;
//by row
int cntr = 0;
for (pugi::xml_node row = tools.first_child(); row; row = row.next_sibling())
{
cntr++;
market item;
pugi::xml_node value = row.first_child();
item.id = value.child_value();
value = value.next_sibling();
item.model_id = value.child_value();
value = value.next_sibling();
item.author_id = value.child_value();
value = value.next_sibling();
item.cr_time = value.child_value();
value = value.next_sibling();
item.rating = value.child_value();
value = value.next_sibling();
item.text = value.child_value();
value = value.next_sibling();
item.pro = value.child_value();
value = value.next_sibling();
item.contra = value.child_value();
value = value.next_sibling();
item.rank = value.child_value();
value = value.next_sibling();
item.text = replace(item.text, "\"", "\\\"");
item.pro = replace(item.pro, "\"", "\\\"");
item.contra = replace(item.contra, "\"", "\\\"");
if (item.model_id.length() == 0)
item.model_id = "0";
if (item.author_id.length() == 0)
item.author_id = "0";
if (item.rating.length() == 0)
item.rating = "0";
if (item.rank.length() == 0)
item.rank = "0";
out << "INSERT INTO market (post_id, model_id,author_id,cr_time,rating,text,pro,contra,rank,item_id) VALUES ("
<< item.id << "," << item.model_id << "," << item.author_id << ",\"" << item.cr_time << "\"," << item.rating << ",\" " << item.text
<< " \",\" " << item.pro << " \",\" " << item.contra << " \"," << item.rank << "," << row.attribute("rowNumber").value() << ");" << std::endl;
}
out << "commit;" << endl;
out.close();
}
int main()
{
xml_loader_imhonet("./imhonet-films.xml");
xml_loader_imhonet("./imhonet-books.xml");
xml_loader_market("./ya-market-cameras.xml");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment