/[projects]/misc/mysql_splitter/table_splitter.cpp
ViewVC logotype

Annotation of /misc/mysql_splitter/table_splitter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2848 - (hide annotations) (download)
Tue Jan 26 12:59:51 2016 UTC (8 years, 4 months ago) by torben
File size: 3486 byte(s)
add table_splitter
1 torben 2848 #include <iostream>
2     #include <iomanip>
3     #include <sstream>
4     #include <string>
5     #include <vector>
6    
7     #include <cstdio>
8    
9     #include <boost/algorithm/string.hpp>
10     #include <boost/filesystem.hpp>
11     #include <boost/program_options.hpp>
12    
13     namespace po = boost::program_options;
14    
15     using namespace std;
16    
17     const int BUFSIZE = 1024*1024;
18    
19     string remove_comments(string in) {
20     bool isComment = false;
21     ostringstream out;
22     for (unsigned int i=0; i<in.length() -1; i++) {
23     if (in.at(i) == '/' && in.at(i+1) == '*')
24     isComment = true;
25    
26     if (in.at(i) == '*' && in.at(i+1) == '/') {
27     i++;
28     isComment = false;
29     continue;
30     }
31    
32     if (isComment == false)
33     out << in.at(i) ;
34     }
35    
36     return out.str();
37     }
38    
39     string get_db_name(string input) {
40     input = remove_comments(input);
41    
42     boost::erase_all(input,"`");
43     boost::erase_all(input, ";");
44     boost::trim(input);
45    
46     vector<string> words;
47     words = boost::split(words, input, boost::is_any_of(" ") );
48    
49     string last = words.back();
50     boost::trim(last);
51    
52     return last;
53     }
54    
55    
56     int main(int argc, char** argv) {
57    
58     po::options_description desc("Mysql dump file splitter:\nAllowed options");
59     desc.add_options()
60     ("help", "produce help message")
61     ("data", "ignore ceate tablestatements")
62     ("input-file", po::value< string >(), "input file")
63     ;
64    
65     po::positional_options_description p;
66     p.add("input-file", -1);
67    
68    
69     po::variables_map vm;
70     po::store( po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
71     po::notify(vm);
72    
73     if (vm.count("help")) {
74     cout << desc << "\n";
75     return 1;
76     }
77    
78    
79    
80     if ( vm.count("input-file") == 0) {
81     cout << desc << endl;
82     return 1;
83     }
84    
85    
86     char linebuf[BUFSIZE];
87    
88     string inputfile = vm["input-file"].as< string >();
89     if (! boost::filesystem::exists(inputfile) ) {
90     cout << "No file named " << inputfile << endl;
91     return 1;
92     }
93    
94    
95     FILE* in = fopen64( inputfile.c_str(), "r" );
96     FILE* out = NULL;
97     //ifstream in( inputfile.c_str() );
98     //ofstream out;
99    
100     ostringstream header;
101    
102     if ( in == NULL ) {
103     cout << "Could not open " << argv[1] << endl;
104     perror("");
105     return 2;
106     }
107    
108     time_t start = time(NULL);
109    
110     const char* SEARCH = "CREATE TABLE";
111     const int SEARCHLEN = strlen(SEARCH);
112    
113     while ( feof(in) == 0 && ferror(in) == 0 ) {
114     fgets(linebuf, BUFSIZE, in);
115    
116    
117     //if (line.substr(0, 15) == "CREATE DATABASE" ) {
118     //if ( boost::starts_with(line, "CREATE DATABASE") ) {
119     if (strncmp(linebuf,SEARCH, SEARCHLEN) == 0) {
120    
121     string line(linebuf);
122    
123     if (out != NULL ) {
124     fclose(out);
125     }
126    
127     boost::trim(line);
128     string dbname = get_db_name(line);
129     cout << ">" << dbname << endl;
130    
131    
132     ostringstream oss;
133     oss << dbname << "_dump.sql";
134    
135     string filename = oss.str();
136    
137     bool did_exist = boost::filesystem::exists(filename);
138    
139     //out.open( filename.c_str(), ios::app );
140     out = fopen64(filename.c_str(), "a");
141    
142    
143    
144     if ( out == NULL ) {
145     cout << "could not create outfile " << filename << endl;
146     return 3;
147     }
148    
149     if (!did_exist) {
150     fputs(header.str().c_str(), out);
151     //out << header.str() << endl; //write preamble in new file
152     }
153    
154     }
155    
156    
157     if (out != NULL ) {
158     fputs(linebuf, out);
159     //fputs("\n", out);
160     //out << linebuf << endl;
161     } else {
162     header << linebuf; //collect preamble for later use
163     }
164    
165     }
166    
167    
168     if (out != NULL)
169     fclose(out);
170    
171     // if (out.is_open())
172     // out.close();
173    
174     time_t end = time(NULL);
175    
176    
177     time_t elapsed = end-start;
178     cout << setfill('0') << "Elapsed time " << elapsed/60 << ":" << setw(2) << elapsed%60 << endl;
179    
180     fclose(in);
181     }

  ViewVC Help
Powered by ViewVC 1.1.20