/
DbImport.java
294 lines (239 loc) · 11.2 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
/*
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.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.DdlException;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.sql.DmlStatement;
import org.jumpmind.db.sql.DmlStatement.DmlType;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.db.sql.SqlScript;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.symmetric.csv.CsvReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
/**
* 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 boolean useVariableDates = false;
private boolean alterCaseToMatchDatabaseDefaultCase = false;
private boolean failOnError = false;
private IDatabasePlatform platform;
public DbImport() {
}
public DbImport(IDatabasePlatform platform) {
this.platform = platform;
}
public DbImport(DataSource dataSource) {
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
public void importTables(String importData) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(importData.getBytes());
importTables(in);
in.close();
}
public void importTables(InputStream in) throws IOException {
importTables(in, null);
}
public void importTables(InputStream in, String tableName) throws IOException {
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 void importTablesFromCsv(InputStream in, String tableName) throws IOException {
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
Table table = platform.readTableFromDatabase(catalog, schema, tableName);
if (table == null) {
throw new RuntimeException("Unable to find table");
}
DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, table);
CsvReader csvReader = new CsvReader(new InputStreamReader(in));
csvReader.setEscapeMode(CsvReader.ESCAPE_MODE_BACKSLASH);
csvReader.setSafetySwitch(false);
csvReader.setUseComments(true);
csvReader.setCaptureRawRecord(false);
csvReader.readHeaders();
while (csvReader.readRecord()) {
String[] values = csvReader.getValues();
Object[] data = platform.getObjectValues(BinaryEncoding.HEX, table, csvReader.getHeaders(), values, useVariableDates);
for (String value : values) {
System.out.print("|" + value);
}
System.out.print("\n");
int rows = sqlTemplate.update(statement.getSql(), data);
System.out.println(rows + " rows updated.");
}
csvReader.close();
}
protected void importTablesFromXml(InputStream in) {
Database database = platform.readDatabaseFromXml(in, alterCaseToMatchDatabaseDefaultCase);
platform.createDatabase(database, false, !failOnError);
// TODO: read in data from XML also
}
protected void importTablesFromSymXml(InputStream in) {
InputStreamReader reader = new InputStreamReader(in);
try {
String tableName = null;
String dataValue = null;
String dmlType = null;
String columnName = null;
boolean nullValue = false;
Map<String,String> rowData = new LinkedHashMap<String,String>();
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(reader);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.TEXT:
if (!nullValue)
dataValue = parser.getText();
case XmlPullParser.START_TAG:
String name = parser.getName();
if ("row".equalsIgnoreCase(name)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("entity")) {
tableName = attributeValue;
} else if (attributeName.equalsIgnoreCase("dml")) {
dmlType = attributeValue;
}
}
} else if ("data".equalsIgnoreCase(name)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if ("key".equalsIgnoreCase(attributeName)) {
columnName = attributeValue;
} else if ("dml".equalsIgnoreCase(attributeName)) {
dmlType = attributeValue;
} else if ("xsi:nil".equalsIgnoreCase(attributeName)){
nullValue = true;
}
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if ("row".equalsIgnoreCase(name)) {
Table table = platform.readTableFromDatabase(catalog, schema, tableName);
if (table == null) {
throw new RuntimeException("Unable to find table");
}
DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, table);
Object[] sqlData = platform.getObjectValues(BinaryEncoding.HEX,
table,
rowData.keySet().toArray(new String[0]),
rowData.values().toArray(new String[0]),
useVariableDates);
rowData.clear();
int rows = sqlTemplate.update(statement.getSql(), sqlData);
System.out.println(rows + " rows updated.");
} else if ("data".equalsIgnoreCase(name)) {
rowData.put(columnName, nullValue ? null:dataValue);
columnName = null;
dataValue = null;
nullValue = false;
}
break;
}
eventType = parser.next();
}
} catch (Exception e) {
throw new DdlException(e);
}
}
protected void importTablesFromSql(InputStream in) throws IOException {
// TODO: SqlScript should be able to stream from standard input to run large SQL script
List<String> lines = IOUtils.readLines(in);
SqlScript script = new SqlScript(lines, platform.getSqlTemplate(), true, SqlScript.QUERY_ENDS,
platform.getSqlScriptReplacementTokens());
script.execute();
}
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 setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}
public boolean isFailOnError() {
return failOnError;
}
public void setDataSource(DataSource dataSource) {
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
}