Skip to content

Commit a1fb2fc

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/2.1.0-dev'
2 parents a6148a4 + 43156e5 commit a1fb2fc

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/main/java/org/b3log/solo/processor/console/AdminConsole.java

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.b3log.latke.model.Plugin;
3232
import org.b3log.latke.model.User;
3333
import org.b3log.latke.plugin.ViewLoadEventData;
34+
import org.b3log.latke.repository.jdbc.util.Connections;
3435
import org.b3log.latke.service.LangPropsService;
3536
import org.b3log.latke.service.ServiceException;
3637
import org.b3log.latke.servlet.HTTPRequestContext;
@@ -60,13 +61,16 @@
6061
import java.io.FileInputStream;
6162
import java.io.FileOutputStream;
6263
import java.io.OutputStream;
64+
import java.sql.Connection;
65+
import java.sql.ResultSet;
66+
import java.sql.Statement;
6367
import java.util.*;
6468

6569
/**
6670
* Admin console render processing.
6771
*
6872
* @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
7074
* @since 0.4.1
7175
*/
7276
@RequestProcessor
@@ -75,7 +79,7 @@ public class AdminConsole {
7579
/**
7680
* Logger.
7781
*/
78-
private static final Logger LOGGER = Logger.getLogger(AdminConsole.class.getName());
82+
private static final Logger LOGGER = Logger.getLogger(AdminConsole.class);
7983

8084
/**
8185
* Language service.
@@ -231,7 +235,8 @@ public void showAdminFunctions(final HttpServletRequest request, final HTTPReque
231235
final Map<String, String> langs = langPropsService.getAll(locale);
232236
final Map<String, Object> dataModel = renderer.getDataModel();
233237

234-
dataModel.put("isMySQL", RuntimeDatabase.MYSQL == Latkes.getRuntimeDatabase());
238+
dataModel.put("supportExport", RuntimeDatabase.MYSQL == Latkes.getRuntimeDatabase()
239+
|| RuntimeDatabase.H2 == Latkes.getRuntimeDatabase());
235240
dataModel.putAll(langs);
236241
Keys.fillRuntime(dataModel);
237242
dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString());
@@ -308,41 +313,81 @@ public void exportSQL(final HttpServletRequest request, final HttpServletRespons
308313
return;
309314
}
310315

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");
313318

314319
return;
315320
}
316321

322+
final RuntimeDatabase runtimeDatabase = Latkes.getRuntimeDatabase();
323+
317324
final String dbUser = Latkes.getLocalProperty("jdbc.username");
318325
final String dbPwd = Latkes.getLocalProperty("jdbc.password");
319326
final String dbURL = Latkes.getLocalProperty("jdbc.URL");
320-
String db = StringUtils.substringAfterLast(dbURL, "/");
321-
db = StringUtils.substringBefore(db, "?");
327+
String sql; // exported SQL script
322328

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;
329344
}
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)) {
332377
context.renderJSON().renderMsg("Export failed, please check log");
333378

334379
return;
335380
}
336381

337382
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);
340385
final File localFile = new File(localFilePath);
341386

342387
try {
343388
final byte[] data = sql.getBytes("UTF-8");
344389

345-
OutputStream output = new FileOutputStream(localFile);
390+
final OutputStream output = new FileOutputStream(localFile);
346391
IOUtils.write(data, output);
347392
IOUtils.closeQuietly(output);
348393

@@ -364,6 +409,7 @@ public void exportSQL(final HttpServletRequest request, final HttpServletRespons
364409
context.renderJSON().renderMsg("Export failed, please check log");
365410

366411
return;
412+
367413
}
368414
}
369415

src/main/webapp/admin-others.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</div>
4848
<div id="tabOthersPanel_other" class="none">
4949
<button class="margin12" onclick="admin.others.removeUnusedTags();">${removeUnusedTagsLabel}</button>
50-
<#if isMySQL>
50+
<#if supportExport>
5151
<button class="margin12" onclick="admin.others.exportSQL();">${exportSQLLabel}</button>
5252
</#if>
5353
</div>

0 commit comments

Comments
 (0)