/
DbImportCommand.java
195 lines (156 loc) · 6.84 KB
/
DbImportCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.io.data.DbImport;
import org.jumpmind.symmetric.io.data.DbImport.Format;
import org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter;
/**
* Import data from file to database tables.
*/
public class DbImportCommand extends AbstractCommandLauncher {
private static final String OPTION_FORMAT = "format";
private static final String OPTION_CATALOG = "catalog";
private static final String OPTION_SCHEMA = "schema";
private static final String OPTION_TABLE = "table";
private static final String OPTION_USE_VARIABLE_DATES = "use-variable-dates";
private static final String OPTION_COMMIT = "commit";
private static final String OPTION_INTERVAL = "interval";
private static final String OPTION_IGNORE = "ignore";
private static final String OPTION_REPLACE = "replace";
private static final String OPTION_FORCE = "force";
private static final String OPTION_ALTER = "alter";
private static final String OPTION_FILTER_CLASSES = "filter-classes";
private static final String OPTION_DROP_IF_EXISTS = "drop-if-exists";
private static final String OPTION_ALTER_CASE = "alter-case";
public DbImportCommand() {
super("dbimport", "[file...]", "DbImport.Option.");
}
public static void main(String[] args) {
new DbImportCommand().execute(args);
}
@Override
protected boolean printHelpIfNoOptionsAreProvided() {
return false;
}
@Override
protected boolean requiresPropertiesFile(CommandLine line) {
return true;
}
@Override
protected void printHelp(CommandLine cmd, Options options) {
System.out.println(app + " version " + Version.version());
System.out.println("Import data from file to database tables.\n");
super.printHelp(cmd, options);
}
@Override
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, null, OPTION_FORMAT, true);
addOption(options, null, OPTION_CATALOG, true);
addOption(options, null, OPTION_SCHEMA, true);
addOption(options, null, OPTION_TABLE, true);
addOption(options, null, OPTION_USE_VARIABLE_DATES, false);
addOption(options, null, OPTION_COMMIT, true);
addOption(options, null, OPTION_INTERVAL, true);
addOption(options, null, OPTION_IGNORE, false);
addOption(options, null, OPTION_REPLACE, false);
addOption(options, null, OPTION_FORCE, false);
addOption(options, null, OPTION_ALTER, false);
addOption(options, null, OPTION_FILTER_CLASSES, true);
addOption(options, null, OPTION_DROP_IF_EXISTS, false);
addOption(options, null, OPTION_ALTER_CASE, false);
}
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
DbImport dbImport = new DbImport(getDatabasePlatform(true));
if (line.hasOption(OPTION_FORMAT)) {
dbImport.setFormat(Format.valueOf(line.getOptionValue(OPTION_FORMAT).toUpperCase()));
}
if (line.hasOption(OPTION_CATALOG)) {
dbImport.setCatalog(line.getOptionValue(OPTION_CATALOG));
}
if (line.hasOption(OPTION_SCHEMA)) {
dbImport.setSchema(line.getOptionValue(OPTION_SCHEMA));
}
if (line.hasOption(OPTION_USE_VARIABLE_DATES)) {
dbImport.setUseVariableForDates(true);
}
if (line.hasOption(OPTION_COMMIT)) {
dbImport.setCommitRate(Long.parseLong(line.getOptionValue(OPTION_COMMIT)));
}
if (line.hasOption(OPTION_INTERVAL)) {
dbImport.setInterval((Integer.parseInt(line.getOptionValue(OPTION_INTERVAL))));
}
if (line.hasOption(OPTION_ALTER_CASE)) {
dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
}
if (line.hasOption(OPTION_DROP_IF_EXISTS)) {
dbImport.setDropIfExists(true);
}
if (line.hasOption(OPTION_ALTER)) {
dbImport.setAlterTables(true);
}
if (line.hasOption(OPTION_FILTER_CLASSES)) {
String filters = line.getOptionValue(OPTION_FILTER_CLASSES);
if (StringUtils.isNotBlank(filters)) {
String[] clazzes = filters.split(",");
for (String clazz : clazzes) {
if (StringUtils.isNotBlank(clazz)) {
IDatabaseWriterFilter databaseWriterFilter = (IDatabaseWriterFilter) Class
.forName(clazz.trim()).getDeclaredConstructor().newInstance();
dbImport.addDatabaseWriterFilter(databaseWriterFilter);
}
}
}
}
if (line.hasOption(OPTION_FORCE)) {
dbImport.setForceImport(true);
}
if (line.hasOption(OPTION_REPLACE)) {
dbImport.setReplaceRows(true);
}
if (line.hasOption(OPTION_IGNORE)) {
dbImport.setIgnoreCollisions(true);
}
String[] args = line.getArgs();
if (args.length == 0) {
dbImport.importTables(System.in, line.getOptionValue(OPTION_TABLE));
} else {
for (String fileName : args) {
if (! new File(fileName).exists()) {
throw new RuntimeException("Cannot find file " + fileName);
}
}
for (String fileName : args) {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
dbImport.importTables(in, line.getOptionValue(OPTION_TABLE));
in.close();
}
}
return true;
}
}