-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from felixgilioli/master
refactoring
- Loading branch information
Showing
6 changed files
with
138 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
49 changes: 49 additions & 0 deletions
49
src/main/java/io/github/gasparbarancelli/engine/TemplateEngineSQLProcessor.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,49 @@ | ||
package io.github.gasparbarancelli.engine; | ||
|
||
import io.github.gasparbarancelli.NativeQueryParameter; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public abstract class TemplateEngineSQLProcessor { | ||
|
||
private String inlineTemplate; | ||
private String classpathTemplate; | ||
private boolean inline; | ||
private Map<String, Object> parameters; | ||
|
||
protected abstract String processInline(String sql); | ||
|
||
protected abstract String processFile(String classpathTemplate); | ||
|
||
public final TemplateEngineSQLProcessor inline(boolean inline) { | ||
this.inline = inline; | ||
return this; | ||
} | ||
|
||
public final TemplateEngineSQLProcessor setClasspathTemplate(String classpathTemplate) { | ||
this.classpathTemplate = classpathTemplate; | ||
return this; | ||
} | ||
|
||
public final TemplateEngineSQLProcessor setInlineTemplate(String inlineTemplate) { | ||
this.inlineTemplate = inlineTemplate; | ||
return this; | ||
} | ||
|
||
public final TemplateEngineSQLProcessor setParameter(List<NativeQueryParameter> parameters) { | ||
this.parameters = parameters.stream() | ||
.collect(HashMap::new, (m, v) -> m.put(v.getName(), v.getValue()), HashMap::putAll); | ||
return this; | ||
} | ||
|
||
protected Map<String, Object> getParameters() { | ||
return Collections.unmodifiableMap(parameters); | ||
} | ||
|
||
public final String getSql() { | ||
return inline ? processInline(inlineTemplate) : processFile(classpathTemplate); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...gasparbarancelli/JtwigTemplateConfig.java → ...lli/engine/jtwig/JtwigTemplateConfig.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
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/gasparbarancelli/engine/jtwig/JtwigTemplateEngineSQLProcessor.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,26 @@ | ||
package io.github.gasparbarancelli.engine.jtwig; | ||
|
||
import io.github.gasparbarancelli.engine.TemplateEngineSQLProcessor; | ||
import org.jtwig.JtwigModel; | ||
import org.jtwig.JtwigTemplate; | ||
|
||
public class JtwigTemplateEngineSQLProcessor extends TemplateEngineSQLProcessor { | ||
|
||
@Override | ||
protected String processInline(String sql) { | ||
return render(JtwigTemplate.inlineTemplate(sql, JtwigTemplateConfig.get())); | ||
} | ||
|
||
@Override | ||
protected String processFile(String classpathTemplate) { | ||
return render(JtwigTemplate.classpathTemplate(classpathTemplate, JtwigTemplateConfig.get())); | ||
} | ||
|
||
private String render(JtwigTemplate jtwigTemplate) { | ||
JtwigModel model = JtwigModel.newModel(); | ||
getParameters().forEach(model::with); | ||
|
||
return jtwigTemplate.render(model); | ||
} | ||
|
||
} |