forked from database-rider/database-rider
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes script replacer in favor of scriptable dataset so the script evaluation happens for each row instead of each replacement.
- Loading branch information
Showing
8 changed files
with
216 additions
and
147 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
rider-core/src/main/java/com/github/database/rider/core/api/dataset/ScriptableDataSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.database.rider.core.api.dataset; | ||
|
||
import org.dbunit.dataset.AbstractDataSet; | ||
import org.dbunit.dataset.DataSetException; | ||
import org.dbunit.dataset.IDataSet; | ||
import org.dbunit.dataset.ITableIterator; | ||
|
||
public class ScriptableDataSet extends AbstractDataSet { | ||
|
||
private IDataSet delegate; | ||
|
||
public ScriptableDataSet(IDataSet delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
protected ITableIterator createIterator(boolean reversed) throws DataSetException { | ||
return new ScriptableDataSetIterator(reversed ? delegate.reverseIterator() : delegate.iterator()); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...e/src/main/java/com/github/database/rider/core/api/dataset/ScriptableDataSetIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.database.rider.core.api.dataset; | ||
|
||
import org.dbunit.dataset.DataSetException; | ||
import org.dbunit.dataset.ITable; | ||
import org.dbunit.dataset.ITableIterator; | ||
import org.dbunit.dataset.ITableMetaData; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Rafael Pestano</a> | ||
* | ||
*/ | ||
public class ScriptableDataSetIterator implements ITableIterator{ | ||
|
||
private ITableIterator delegate; | ||
|
||
public ScriptableDataSetIterator(ITableIterator delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public boolean next() throws DataSetException { | ||
return delegate.next(); | ||
} | ||
|
||
@Override | ||
public ITableMetaData getTableMetaData() throws DataSetException { | ||
return delegate.getTableMetaData(); | ||
} | ||
|
||
@Override | ||
public ITable getTable() throws DataSetException { | ||
return new ScriptableTable(delegate.getTable()); | ||
} | ||
|
||
|
||
|
||
} |
100 changes: 100 additions & 0 deletions
100
rider-core/src/main/java/com/github/database/rider/core/api/dataset/ScriptableTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.github.database.rider.core.api.dataset; | ||
|
||
import org.dbunit.dataset.DataSetException; | ||
import org.dbunit.dataset.ITable; | ||
import org.dbunit.dataset.ITableMetaData; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import javax.script.ScriptException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Adds support for script language (JSR 223) in table values. | ||
* | ||
* @author <a href="mailto:[email protected]">Rafael Pestano</a> | ||
*/ | ||
public class ScriptableTable implements ITable { | ||
|
||
//any non digit char (except 'regex') followed by ':' followed by 1 or more chars e.g: js: new Date().toString() | ||
private static final Pattern scriptEnginePattern = Pattern.compile("^(?!regex)[a-zA-Z]+:.+"); | ||
|
||
private static Logger log = Logger.getLogger(ScriptableTable.class.getName()); | ||
|
||
private Map<String, ScriptEngine> engines; | ||
|
||
private ScriptEngineManager manager; | ||
|
||
private ITable delegate; | ||
|
||
|
||
public ScriptableTable(ITable delegate) { | ||
this.delegate = delegate; | ||
engines = new HashMap<>(); | ||
manager = new ScriptEngineManager(); | ||
} | ||
|
||
@Override | ||
public ITableMetaData getTableMetaData() { | ||
return delegate.getTableMetaData(); | ||
} | ||
|
||
@Override | ||
public int getRowCount() { | ||
return delegate.getRowCount(); | ||
} | ||
|
||
@Override | ||
public Object getValue(int row, String column) throws DataSetException { | ||
Object value = delegate.getValue(row, column); | ||
if (value != null && scriptEnginePattern.matcher(value.toString()).matches()) { | ||
ScriptEngine engine = getScriptEngine(value.toString().trim()); | ||
if (engine != null) { | ||
try { | ||
return getScriptResult(value.toString(), engine); | ||
} catch (Exception e) { | ||
log.log(Level.WARNING,String.format("Could not evaluate script expression for table '%s', column '%s'. The original value will be used.", getTableMetaData().getTableName(), column),e); | ||
} | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
|
||
/** | ||
* Parses table cell to get script engine | ||
* | ||
* @param value the table cell | ||
* @return scriptEngine | ||
*/ | ||
private ScriptEngine getScriptEngine(String value) { | ||
String engineName = value.substring(0, value.indexOf(":")); | ||
if (engines.containsKey(engineName)) { | ||
return engines.get(engineName); | ||
} else { | ||
ScriptEngine engine = manager.getEngineByName(engineName); | ||
if (engine != null) { | ||
engines.put(engineName, engine); | ||
} else { | ||
throw new RuntimeException(String.format("Could not find script engine with name %s in classpath", engineName)); | ||
} | ||
return engine; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Evaluates the script expression | ||
* | ||
* @return script expression result | ||
*/ | ||
private Object getScriptResult(String script, ScriptEngine engine) throws ScriptException { | ||
String scriptToExecute = script.substring(script.indexOf(":") + 1); | ||
return engine.eval(scriptToExecute); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
rider-core/src/main/java/com/github/database/rider/core/replacer/DateTimeReplacer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 0 additions & 97 deletions
97
rider-core/src/main/java/com/github/database/rider/core/replacer/ScriptReplacer.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.