31
31
import org .b3log .latke .model .Plugin ;
32
32
import org .b3log .latke .model .User ;
33
33
import org .b3log .latke .plugin .ViewLoadEventData ;
34
+ import org .b3log .latke .repository .jdbc .util .Connections ;
34
35
import org .b3log .latke .service .LangPropsService ;
35
36
import org .b3log .latke .service .ServiceException ;
36
37
import org .b3log .latke .servlet .HTTPRequestContext ;
60
61
import java .io .FileInputStream ;
61
62
import java .io .FileOutputStream ;
62
63
import java .io .OutputStream ;
64
+ import java .sql .Connection ;
65
+ import java .sql .ResultSet ;
66
+ import java .sql .Statement ;
63
67
import java .util .*;
64
68
65
69
/**
66
70
* Admin console render processing.
67
71
*
68
72
* @author <a href="http://88250.b3log.org">Liang Ding</a>
69
- * @version 1.3 .2.11, Mar 31 , 2017
73
+ * @version 1.4 .2.11, May 8 , 2017
70
74
* @since 0.4.1
71
75
*/
72
76
@ RequestProcessor
@@ -75,7 +79,7 @@ public class AdminConsole {
75
79
/**
76
80
* Logger.
77
81
*/
78
- private static final Logger LOGGER = Logger .getLogger (AdminConsole .class . getName () );
82
+ private static final Logger LOGGER = Logger .getLogger (AdminConsole .class );
79
83
80
84
/**
81
85
* Language service.
@@ -231,7 +235,8 @@ public void showAdminFunctions(final HttpServletRequest request, final HTTPReque
231
235
final Map <String , String > langs = langPropsService .getAll (locale );
232
236
final Map <String , Object > dataModel = renderer .getDataModel ();
233
237
234
- dataModel .put ("isMySQL" , RuntimeDatabase .MYSQL == Latkes .getRuntimeDatabase ());
238
+ dataModel .put ("supportExport" , RuntimeDatabase .MYSQL == Latkes .getRuntimeDatabase ()
239
+ || RuntimeDatabase .H2 == Latkes .getRuntimeDatabase ());
235
240
dataModel .putAll (langs );
236
241
Keys .fillRuntime (dataModel );
237
242
dataModel .put (Option .ID_C_LOCALE_STRING , locale .toString ());
@@ -308,41 +313,81 @@ public void exportSQL(final HttpServletRequest request, final HttpServletRespons
308
313
return ;
309
314
}
310
315
311
- if (!Latkes .runsWithJDBCDatabase () || RuntimeDatabase . MYSQL != Latkes . getRuntimeDatabase () ) {
312
- context .renderJSON ().renderMsg ("Just support MySQL export now" );
316
+ if (!Latkes .runsWithJDBCDatabase ()) {
317
+ context .renderJSON ().renderMsg ("Just support MySQL/H2 export now" );
313
318
314
319
return ;
315
320
}
316
321
322
+ final RuntimeDatabase runtimeDatabase = Latkes .getRuntimeDatabase ();
323
+
317
324
final String dbUser = Latkes .getLocalProperty ("jdbc.username" );
318
325
final String dbPwd = Latkes .getLocalProperty ("jdbc.password" );
319
326
final String dbURL = Latkes .getLocalProperty ("jdbc.URL" );
320
- String db = StringUtils .substringAfterLast (dbURL , "/" );
321
- db = StringUtils .substringBefore (db , "?" );
327
+ String sql ; // exported SQL script
322
328
323
- String sql ;
324
- try {
325
- if (StringUtils .isNotBlank (dbPwd )) {
326
- sql = Execs .exec ("mysqldump -u" + dbUser + " -p" + dbPwd + " --databases " + db );
327
- } else {
328
- sql = Execs .exec ("mysqldump -u" + dbUser + " --databases " + db );
329
+ if (RuntimeDatabase .MYSQL == runtimeDatabase ) {
330
+ String db = StringUtils .substringAfterLast (dbURL , "/" );
331
+ db = StringUtils .substringBefore (db , "?" );
332
+
333
+ try {
334
+ if (StringUtils .isNotBlank (dbPwd )) {
335
+ sql = Execs .exec ("mysqldump -u" + dbUser + " -p" + dbPwd + " --databases " + db );
336
+ } else {
337
+ sql = Execs .exec ("mysqldump -u" + dbUser + " --databases " + db );
338
+ }
339
+ } catch (final Exception e ) {
340
+ LOGGER .log (Level .ERROR , "Export failed" , e );
341
+ context .renderJSON ().renderMsg ("Export failed, please check log" );
342
+
343
+ return ;
329
344
}
330
- } catch (final Exception e ) {
331
- LOGGER .log (Level .ERROR , "Export failed" , e );
345
+ } else if (RuntimeDatabase .H2 == runtimeDatabase ) {
346
+ final Connection connection = Connections .getConnection ();
347
+ final Statement statement = connection .createStatement ();
348
+
349
+ try {
350
+ final StringBuilder sqlBuilder = new StringBuilder ();
351
+ final ResultSet resultSet = statement .executeQuery ("SCRIPT" );
352
+ while (resultSet .next ()) {
353
+ final String stmt = resultSet .getString (1 );
354
+ sqlBuilder .append (stmt ).append (Strings .LINE_SEPARATOR );
355
+ }
356
+ resultSet .close ();
357
+ statement .close ();
358
+
359
+ sql = sqlBuilder .toString ();
360
+ } catch (final Exception e ) {
361
+ LOGGER .log (Level .ERROR , "Export failed" , e );
362
+ context .renderJSON ().renderMsg ("Export failed, please check log" );
363
+
364
+ return ;
365
+ } finally {
366
+ if (null != connection ) {
367
+ connection .close ();
368
+ }
369
+ }
370
+ } else {
371
+ context .renderJSON ().renderMsg ("Just support MySQL/H2 export now" );
372
+
373
+ return ;
374
+ }
375
+
376
+ if (StringUtils .isBlank (sql )) {
332
377
context .renderJSON ().renderMsg ("Export failed, please check log" );
333
378
334
379
return ;
335
380
}
336
381
337
382
final String tmpDir = System .getProperty ("java.io.tmpdir" );
338
- String localFilePath = tmpDir + "/ b3_solo_" + UUID .randomUUID ().toString () + ".sql" ;
339
- LOGGER .info (localFilePath );
383
+ String localFilePath = tmpDir + File . separator + " b3_solo_" + UUID .randomUUID ().toString () + ".sql" ;
384
+ LOGGER .trace (localFilePath );
340
385
final File localFile = new File (localFilePath );
341
386
342
387
try {
343
388
final byte [] data = sql .getBytes ("UTF-8" );
344
389
345
- OutputStream output = new FileOutputStream (localFile );
390
+ final OutputStream output = new FileOutputStream (localFile );
346
391
IOUtils .write (data , output );
347
392
IOUtils .closeQuietly (output );
348
393
@@ -364,6 +409,7 @@ public void exportSQL(final HttpServletRequest request, final HttpServletRespons
364
409
context .renderJSON ().renderMsg ("Export failed, please check log" );
365
410
366
411
return ;
412
+
367
413
}
368
414
}
369
415
0 commit comments