#1561 - ENH: Add ScriptRunner API ... for easier running of SQL and DDL scripts (for testing usually)

This commit is contained in:
rob bygrave
2018-11-27 14:50:48 +13:00
parent 54ba78218b
commit 2f0e5e6e46
9 changed files with 221 additions and 4 deletions
+7
View File
@@ -1442,6 +1442,13 @@ public interface EbeanServer {
*/
JsonContext json();
/**
* Return a ScriptRunner for running SQL or DDL scripts.
* <p/>
* Intended to use mostly in testing to run seed SQL scripts or truncate table scripts etc.
*/
ScriptRunner script();
/**
* Return the Document store.
*/
+65
View File
@@ -0,0 +1,65 @@
package io.ebean;
import java.net.URL;
import java.util.Map;
/**
* Runs DDL and SQL scripts.
* <p/>
* Typically these are scripts used for testing such as seed SQL scripts or truncate SQL scripts.
* <p/>
* Scripts are executed in their own transaction and committed on successful completion.
*
* <h3>Example of simple use</h3>
* <pre>{@code
*
* EbeanServer server = Ebean.getDefaultServer();
* server.script().run("/scripts/test-script.sql");
*
* }</pre>
*
*
* <h3>Example using place holders in the script</h3>
* <pre>{@code
*
* Map<String,String> placeholders = new HashMap<>();
* placeholders.put("tableName", "e_basic");
*
* EbeanServer server = Ebean.getDefaultServer();
* server.script().run("/scripts/test-script.sql");
*
* }</pre>
*/
public interface ScriptRunner {
/**
* Run a script given the resource path (that should start with "/").
*/
void run(String path);
/**
* Run a script given the resource path (that should start with "/") and place holders.
*
* <pre>{@code
*
* Map<String,String> placeholders = new HashMap<>();
* placeholders.put("tableName", "e_basic");
*
* EbeanServer server = Ebean.getDefaultServer();
* server.script().run("/scripts/test-script.sql");
*
* }</pre>
*/
void run(String path, Map<String, String> placeholderMap);
/**
* Run a DDL or SQL script given the resource.
*/
void run(URL resource);
/**
* Run a DDL or SQL script given the resource and place holders.
*/
void run(URL resource, Map<String, String> placeholderMap);
}
@@ -348,7 +348,6 @@ public class DdlGenerator {
buf.append(s).append("\n");
}
return buf.toString();
}
}
@@ -0,0 +1,116 @@
package io.ebeaninternal.server.core;
import io.ebean.ScriptRunner;
import io.ebean.migration.ddl.DdlRunner;
import io.ebean.migration.runner.ScriptTransform;
import io.ebeaninternal.api.SpiEbeanServer;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
class DScriptRunner implements ScriptRunner {
private static final String NEWLINE = "\n";
private final SpiEbeanServer server;
DScriptRunner(SpiEbeanServer server) {
this.server = server;
}
@Override
public void run(String path) {
run(path, null);
}
@Override
public void run(String path, Map<String, String> placeholderMap) {
run(this.getClass().getResource(path), path, placeholderMap);
}
@Override
public void run(URL resource) {
run(resource, null, null);
}
@Override
public void run(URL resource, Map<String, String> placeholderMap) {
run(resource, null, placeholderMap);
}
private void run(URL resource, String scriptName, Map<String, String> placeholderMap) {
if (resource == null) {
throw new IllegalArgumentException("resource is null?");
}
if (scriptName == null) {
scriptName = resource.getFile();
}
String content = content(resource);
runScript(content, scriptName, placeholderMap);
}
private String content(URL resource) {
if (resource == null) {
throw new IllegalArgumentException("resource is null?");
}
try (InputStream inputStream = resource.openStream()) {
return readContent(new InputStreamReader(inputStream));
} catch (IOException e) {
throw new PersistenceException("Failed to read script content", e);
}
}
/**
* Execute all the DDL statements in the script.
*/
private void runScript(String content, String scriptName, Map<String, String> placeholderMap) {
try {
if (placeholderMap != null) {
content = ScriptTransform.build(null, placeholderMap).transform(content);
}
try (Connection connection = obtainConnection()) {
DdlRunner runner = new DdlRunner(false, scriptName);
runner.runAll(content, connection);
connection.commit();
}
} catch (SQLException e) {
throw new PersistenceException("Failed to run script", e);
}
}
private Connection obtainConnection() {
try {
return server.getPluginApi().getDataSource().getConnection();
} catch (SQLException e) {
throw new PersistenceException("Failed to obtain connection to run script", e);
}
}
private String readContent(Reader reader) throws IOException {
StringBuilder buf = new StringBuilder();
try (LineNumberReader lineReader = new LineNumberReader(reader)) {
String line;
while ((line = lineReader.readLine()) != null) {
buf.append(line).append(NEWLINE);
}
return buf.toString();
}
}
}
@@ -22,6 +22,7 @@ import io.ebean.Query;
import io.ebean.QueryIterator;
import io.ebean.RowConsumer;
import io.ebean.RowMapper;
import io.ebean.ScriptRunner;
import io.ebean.SqlQuery;
import io.ebean.SqlRow;
import io.ebean.SqlUpdate;
@@ -185,6 +186,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
private final DdlGenerator ddlGenerator;
private final ScriptRunner scriptRunner;
private final ExpressionFactory expressionFactory;
private final SpiBackgroundExecutor backgroundExecutor;
@@ -291,6 +294,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
this.serverPlugins = config.getPlugins();
this.ddlGenerator = new DdlGenerator(this, serverConfig);
this.scriptRunner = new DScriptRunner(this);
configureServerPlugins();
@@ -370,6 +374,11 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return databasePlatform;
}
@Override
public ScriptRunner script() {
return scriptRunner;
}
@Override
public DdlHandler createDdlHandler() {
return PlatformDdlBuilder.create(databasePlatform).createDdlHandler(serverConfig);