1 |
#include <iostream> |
2 |
#include <fstream> |
3 |
#include <sstream> |
4 |
#include <string> |
5 |
#include <vector> |
6 |
|
7 |
|
8 |
#include <boost/algorithm/string.hpp> |
9 |
#include <boost/filesystem.hpp> |
10 |
#include <boost/program_options.hpp> |
11 |
|
12 |
namespace po = boost::program_options; |
13 |
|
14 |
using namespace std; |
15 |
|
16 |
const int BUFSIZE = 1024*1024; |
17 |
|
18 |
string remove_comments(string in) { |
19 |
bool isComment = false; |
20 |
ostringstream out; |
21 |
for (int i=0; i<in.length() -1; i++) { |
22 |
if (in.at(i) == '/' && in.at(i+1) == '*') |
23 |
isComment = true; |
24 |
|
25 |
if (in.at(i) == '*' && in.at(i+1) == '/') { |
26 |
i++; |
27 |
isComment = false; |
28 |
continue; |
29 |
} |
30 |
|
31 |
if (isComment == false) |
32 |
out << in.at(i) ; |
33 |
} |
34 |
|
35 |
return out.str(); |
36 |
} |
37 |
|
38 |
string getLastWord(string input) { |
39 |
input = remove_comments(input); |
40 |
|
41 |
boost::erase_all(input,"`"); |
42 |
boost::trim(input); |
43 |
vector<string> words; |
44 |
words = boost::split(words, input, boost::is_any_of(" ") ); |
45 |
|
46 |
string last = words.back(); |
47 |
|
48 |
boost::erase_all(last, ";"); |
49 |
boost::trim(last); |
50 |
|
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 |
ifstream in( inputfile.c_str() ); |
96 |
ofstream out; |
97 |
|
98 |
ostringstream header; |
99 |
|
100 |
if (!in.is_open() ) { |
101 |
cout << "Could not open " << argv[1] << endl; |
102 |
return 2; |
103 |
} |
104 |
|
105 |
while ( in.good() ) { |
106 |
in.getline(linebuf, BUFSIZE); |
107 |
|
108 |
string line(linebuf); |
109 |
|
110 |
|
111 |
//if (line.substr(0, 15) == "CREATE DATABASE" ) { |
112 |
if ( boost::starts_with(line, "CREATE DATABASE") ) { |
113 |
|
114 |
if (out.is_open() ) { |
115 |
out.close(); |
116 |
} |
117 |
|
118 |
boost::trim(line); |
119 |
string dbname = getLastWord(line); |
120 |
cout << ">" << dbname << endl; |
121 |
|
122 |
|
123 |
ostringstream oss; |
124 |
oss << dbname << "_dump.sql";
|
125 |
|
126 |
string filename = oss.str(); |
127 |
|
128 |
bool did_exist = boost::filesystem::exists(filename); |
129 |
|
130 |
out.open( filename.c_str(), ios::app ); |
131 |
|
132 |
|
133 |
|
134 |
if (!out.is_open() ) { |
135 |
cout << "could not create outfile " << filename << endl; |
136 |
return 3; |
137 |
} |
138 |
|
139 |
if (!did_exist) { |
140 |
out << header.str() << endl; //write preamble in new file |
141 |
} |
142 |
|
143 |
} |
144 |
|
145 |
|
146 |
if (out.is_open() ) { |
147 |
out << line << endl; |
148 |
} else { |
149 |
header << line << endl; //collect preamble for later use |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
|
155 |
if(out.is_open()) |
156 |
out.close(); |
157 |
|
158 |
|
159 |
in.close(); |
160 |
} |