/
DbImport.java
317 lines (254 loc) · 10.1 KB
/
DbImport.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
O * 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 Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License 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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.exception.IoException;
import org.jumpmind.symmetric.io.data.DataProcessor;
import org.jumpmind.symmetric.io.data.reader.CsvTableDataReader;
import org.jumpmind.symmetric.io.data.reader.SqlDataReader;
import org.jumpmind.symmetric.io.data.reader.SymXmlDataReader;
import org.jumpmind.symmetric.io.data.reader.XmlDataReader;
import org.jumpmind.symmetric.io.data.writer.Conflict;
import org.jumpmind.symmetric.io.data.writer.Conflict.DetectConflict;
import org.jumpmind.symmetric.io.data.writer.Conflict.ResolveConflict;
import org.jumpmind.symmetric.io.data.writer.DatabaseWriter;
import org.jumpmind.symmetric.io.data.writer.DatabaseWriterErrorIgnorer;
import org.jumpmind.symmetric.io.data.writer.DatabaseWriterSettings;
import org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter;
/**
* Import data from file to database tables.
*/
public class DbImport {
public enum Format {
SQL, CSV, XML, SYM_XML
};
private Format format = Format.SQL;
private String catalog;
private String schema;
private long commitRate = 10000;
private boolean useVariableDates = false;
/**
* Force the import to continue, regardless of errors that might occur.
*/
private boolean forceImport = false;
/**
* If a row already exists, then replace it using an update statement.
*/
private boolean replaceRows = false;
/**
* Ignore rows that already exist.
*/
private boolean ignoreCollisions = false;
private boolean alterCaseToMatchDatabaseDefaultCase = false;
private boolean alterTables = false;
private boolean dropIfExists = false;
protected IDatabasePlatform platform;
protected List<IDatabaseWriterFilter> databaseWriterFilters;
public DbImport() {
this.databaseWriterFilters = new ArrayList<IDatabaseWriterFilter>();
}
public DbImport(IDatabasePlatform platform) {
this();
this.platform = platform;
}
public DbImport(DataSource dataSource) {
this();
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
public void importTables(String importData, String tableName) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(importData.getBytes());
importTables(in, tableName);
in.close();
} catch (IOException e) {
throw new IoException(e);
}
}
public void importTables(String importData) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(importData.getBytes());
importTables(in);
in.close();
} catch (IOException e) {
throw new IoException(e);
}
}
public void importTables(InputStream in) {
importTables(in, null);
}
public void importTables(InputStream in, String tableName) {
if (format == Format.SQL) {
importTablesFromSql(in);
} else if (format == Format.CSV) {
importTablesFromCsv(in, tableName);
} else if (format == Format.XML) {
importTablesFromXml(in);
} else if (format == Format.SYM_XML) {
importTablesFromSymXml(in);
}
}
protected Conflict buildConflictSettings() {
Conflict conflict = new Conflict();
conflict.setDetectType(DetectConflict.USE_PK_DATA);
if (replaceRows) {
conflict.setResolveType(ResolveConflict.FALLBACK);
} else if (forceImport || ignoreCollisions) {
conflict.setResolveType(ResolveConflict.IGNORE);
} else {
conflict.setResolveType(ResolveConflict.MANUAL);
}
return conflict;
}
protected DatabaseWriterSettings buildDatabaseWriterSettings() {
DatabaseWriterSettings settings = new DatabaseWriterSettings();
settings.setMaxRowsBeforeCommit(commitRate);
settings.setDefaultConflictSetting(buildConflictSettings());
settings.setUsePrimaryKeysFromSource(false);
settings.setAlterTable(alterTables);
settings.setCreateTableDropFirst(dropIfExists);
settings.setCreateTableFailOnError(!forceImport);
settings.setDatabaseWriterFilters(databaseWriterFilters);
settings.setCreateTableAlterCaseToMatchDatabaseDefault(alterCaseToMatchDatabaseDefaultCase);
if (forceImport) {
settings.addErrorHandler(new DatabaseWriterErrorIgnorer());
}
return settings;
}
protected void importTablesFromCsv(InputStream in, String tableName) {
Table table = platform.readTableFromDatabase(catalog, schema, tableName);
if (table == null) {
throw new RuntimeException("Unable to find table");
}
CsvTableDataReader reader = new CsvTableDataReader(BinaryEncoding.HEX, table.getCatalog(),
table.getSchema(), table.getName(), in);
DatabaseWriter writer = new DatabaseWriter(platform, buildDatabaseWriterSettings());
DataProcessor dataProcessor = new DataProcessor(reader, writer);
dataProcessor.process();
}
protected void importTablesFromXml(InputStream in) {
XmlDataReader reader = new XmlDataReader(in);
DatabaseWriter writer = new DatabaseWriter(platform, buildDatabaseWriterSettings());
DataProcessor dataProcessor = new DataProcessor(reader, writer);
dataProcessor.process();
}
protected void importTablesFromSymXml(InputStream in) {
SymXmlDataReader reader = new SymXmlDataReader(in);
DatabaseWriter writer = new DatabaseWriter(platform, buildDatabaseWriterSettings());
DataProcessor dataProcessor = new DataProcessor(reader, writer);
dataProcessor.process();
}
protected void importTablesFromSql(InputStream in) {
SqlDataReader reader = new SqlDataReader(in);
DatabaseWriter writer = new DatabaseWriter(platform, buildDatabaseWriterSettings());
DataProcessor dataProcessor = new DataProcessor(reader, writer);
dataProcessor.process();
}
public Format getFormat() {
return format;
}
public void setFormat(Format format) {
this.format = format;
}
public String getCatalog() {
return catalog;
}
public void setCatalog(String catalog) {
this.catalog = catalog;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public IDatabasePlatform getPlatform() {
return platform;
}
public void setPlatform(IDatabasePlatform platform) {
this.platform = platform;
}
public boolean isUseVariableDates() {
return useVariableDates;
}
public void setUseVariableForDates(boolean useVariableDates) {
this.useVariableDates = useVariableDates;
}
public void setAlterCaseToMatchDatabaseDefaultCase(boolean alterCaseToMatchDatabaseDefaultCase) {
this.alterCaseToMatchDatabaseDefaultCase = alterCaseToMatchDatabaseDefaultCase;
}
public boolean isAlterCaseToMatchDatabaseDefaultCase() {
return alterCaseToMatchDatabaseDefaultCase;
}
public void setCommitRate(long commitRate) {
this.commitRate = commitRate;
}
public long getCommitRate() {
return commitRate;
}
public void setDataSource(DataSource dataSource) {
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
public void setForceImport(boolean forceImport) {
this.forceImport = forceImport;
}
public boolean isForceImport() {
return forceImport;
}
public void setIgnoreCollisions(boolean ignoreConflicts) {
this.ignoreCollisions = ignoreConflicts;
}
public boolean isIgnoreCollisions() {
return ignoreCollisions;
}
public void setReplaceRows(boolean replaceRows) {
this.replaceRows = replaceRows;
}
public boolean isReplaceRows() {
return replaceRows;
}
public void setAlterTables(boolean alterTables) {
this.alterTables = alterTables;
}
public boolean isAlterTables() {
return alterTables;
}
public void setDropIfExists(boolean dropIfExists) {
this.dropIfExists = dropIfExists;
}
public boolean isDropIfExists() {
return dropIfExists;
}
public void addDatabaseWriterFilter(IDatabaseWriterFilter filter) {
databaseWriterFilters.add(filter);
}
public void removeDatabaseWriterFilter(IDatabaseWriterFilter filter) {
databaseWriterFilters.remove(filter);
}
}