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 package com.rapiddweller.benerator.main;
28
29 import com.rapiddweller.benerator.Consumer;
30 import com.rapiddweller.benerator.wrapper.ProductWrapper;
31 import com.rapiddweller.common.IOUtil;
32 import com.rapiddweller.common.NumberUtil;
33 import com.rapiddweller.common.RoundedNumberFormat;
34 import com.rapiddweller.common.StringUtil;
35 import com.rapiddweller.common.SystemInfo;
36 import com.rapiddweller.common.ui.ProgressMonitor;
37 import com.rapiddweller.format.DataContainer;
38 import com.rapiddweller.format.DataIterator;
39 import com.rapiddweller.model.data.DataModel;
40 import com.rapiddweller.model.data.Entity;
41 import com.rapiddweller.model.data.TypeDescriptor;
42 import com.rapiddweller.platform.db.DefaultDBSystem;
43 import com.rapiddweller.platform.db.SQLEntityExporter;
44 import com.rapiddweller.platform.dbunit.DbUnitEntityExporter;
45 import com.rapiddweller.platform.xls.XLSEntityExporter;
46 import org.apache.logging.log4j.LogManager;
47 import org.apache.logging.log4j.Logger;
48
49
50
51
52
53
54
55 public class DBSnapshotTool {
56
57
58
59
60 public static final String DBUNIT_FORMAT = "dbunit";
61
62
63
64 public static final String XLS_FORMAT = "xls";
65
66
67
68 public static final String SQL_FORMAT = "sql";
69
70
71
72
73 public static final String DEFAULT_FORMAT = DBUNIT_FORMAT;
74
75
76
77
78 public static final String DB_PASSWORD = "dbPassword";
79
80
81
82 public static final String DB_URL = "dbUrl";
83
84
85
86 public static final String DB_DRIVER = "dbDriver";
87
88
89
90 public static final String DB_SCHEMA = "dbSchema";
91
92
93
94 public static final String DB_USER = "dbUser";
95
96
97
98 public static final String FORMAT = "format";
99
100
101
102 public static final String DIALECT = "dialect";
103
104
105 private static final Logger logger = LogManager.getLogger(DBSnapshotTool.class);
106
107
108
109
110
111
112 public static String[] supportedFormats() {
113 return new String[] {
114 DBUNIT_FORMAT, XLS_FORMAT, SQL_FORMAT
115 };
116 }
117
118
119
120
121
122
123 public static void main(String[] args) {
124 logger.info("Starting " + DBSnapshotTool.class.getSimpleName());
125 String format = System.getProperty(FORMAT);
126 if (format == null) {
127 format = DEFAULT_FORMAT;
128 }
129 String filename = (args.length > 0 ? args[0] : defaultFilename(format));
130
131 String dbUrl = System.getProperty(DB_URL);
132 if (StringUtil.isEmpty(dbUrl)) {
133 throw new IllegalArgumentException("No database URL specified. " +
134 "Please provide the JDBC URL as an environment property like '-DdbUrl=jdbc:...'");
135 }
136 String dbDriver = System.getProperty(DB_DRIVER);
137 if (StringUtil.isEmpty(dbDriver)) {
138 throw new IllegalArgumentException("No database driver specified. " +
139 "Please provide the JDBC driver class name as an environment property like '-DdbDriver=...'");
140 }
141 String dbUser = System.getProperty(DB_USER);
142 String dbPassword = System.getProperty(DB_PASSWORD);
143 String dbSchema = System.getProperty(DB_SCHEMA);
144 String dialect = System.getProperty(DIALECT);
145
146 logger.info("Exporting data of database '" + dbUrl + "' with driver '" + dbDriver + "' as user '" + dbUser
147 + "'" + (dbSchema != null ? " using schema '" + dbSchema + "'" : "")
148 + " in " + format + " format to file " + filename);
149
150 export(dbUrl, dbDriver, dbSchema, dbUser, dbPassword, filename, format, dialect);
151 }
152
153 private static String defaultFilename(String format) {
154 if (XLS_FORMAT.equals(format)) {
155 return "snapshot.xls";
156 } else if (SQL_FORMAT.equals(format)) {
157 return "snapshot.sql";
158 } else {
159 return "snapshot.dbunit.xml";
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public static void export(String dbUrl, String dbDriver, String dbSchema,
176 String dbUser, String dbPassword, String filename, String format, String dialect) {
177 export(dbUrl, dbDriver, dbSchema, dbUser, dbPassword, filename, SystemInfo.getFileEncoding(),
178 format, dialect, null);
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static void export(String dbUrl, String dbDriver, String dbSchema,
196 String dbUser, String dbPassword, String filename, String encoding, String format, String dialect,
197 ProgressMonitor monitor) {
198 if (dbUser == null) {
199 logger.warn("No JDBC user specified");
200 }
201 String lineSeparator = SystemInfo.getLineSeparator();
202 long startTime = System.currentTimeMillis();
203
204 Consumer exporter = null;
205 int count = 0;
206 try (DefaultDBSystemultDBSystem.html#DefaultDBSystem">DefaultDBSystem db = new DefaultDBSystem("db", dbUrl, dbDriver, dbUser, dbPassword, new DataModel())) {
207
208 if (dbSchema != null) {
209 db.setSchema(dbSchema);
210 }
211 db.setDynamicQuerySupported(false);
212
213
214 if (DBUNIT_FORMAT.equalsIgnoreCase(format)) {
215 exporter = new DbUnitEntityExporter(filename, encoding);
216 } else if (XLS_FORMAT.equals(format)) {
217 exporter = new XLSEntityExporter(filename);
218 } else if (SQL_FORMAT.equals(format)) {
219 if (dialect == null) {
220 dialect = db.getDialect().getSystem();
221 }
222 exporter = new SQLEntityExporter(filename, encoding, lineSeparator, dialect);
223 } else {
224 throw new IllegalArgumentException("Unknown format: " + format);
225 }
226
227
228 TypeDescriptor[] descriptors = db.getTypeDescriptors();
229 logger.info("Starting export");
230 for (TypeDescriptor descriptor : descriptors) {
231 String note = "Exporting table " + descriptor.getName();
232 if (monitor != null) {
233 monitor.setNote(note);
234 if (monitor.isCanceled()) {
235 throw new RuntimeException("Export cancelled");
236 }
237 }
238 logger.info(note);
239 Thread.yield();
240 DataIterator<Entity> source = db.queryEntities(descriptor.getName(), null, null).iterator();
241 DataContainer<Entity> container = new DataContainer<>();
242 ProductWrapper<Entity> wrapper = new ProductWrapper<>();
243 while ((container = source.next(container)) != null) {
244 Entity entity = container.getData();
245 wrapper.wrap(entity);
246 exporter.startConsuming(wrapper);
247 wrapper.wrap(entity);
248 exporter.finishConsuming(wrapper);
249 count++;
250 }
251 if (monitor != null) {
252 monitor.advance();
253 }
254 }
255 long duration = System.currentTimeMillis() - startTime;
256 if (count == 0) {
257 logger.warn("No entities found for snapshot.");
258 } else {
259 logger.info("Exported " + NumberUtil.format(count, 0) + " entities in " +
260 RoundedNumberFormat.format(duration, 0) + " ms " +
261 "(" + RoundedNumberFormat.format(count * 3600000L / duration, 0) + " p.h.)");
262 }
263 } finally {
264 IOUtil.close(exporter);
265 }
266 }
267
268 }