Skip to content

Commit

Permalink
Merge pull request #28 from felixgilioli/master
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
felixgilioli authored Apr 20, 2020
2 parents c0abcf7 + 9aa9d59 commit 0a455c9
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 60 deletions.
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<version>2.23.0</version>
</dependency>

<dependency>
<groupId>org.jtwig</groupId>
<artifactId>jtwig-core</artifactId>
<version>5.87.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down Expand Up @@ -95,6 +89,13 @@
<version>1.8</version>
</dependency>

<!-- template engine -->
<dependency>
<groupId>org.jtwig</groupId>
<artifactId>jtwig-core</artifactId>
<version>5.87.0.RELEASE</version>
</dependency>

</dependencies>

<licenses>
Expand Down
106 changes: 54 additions & 52 deletions src/main/java/io/github/gasparbarancelli/NativeQueryInfo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.github.gasparbarancelli;

import io.github.gasparbarancelli.engine.jtwig.JtwigTemplateEngineSQLProcessor;
import org.aopalliance.intercept.MethodInvocation;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -13,6 +12,7 @@
import javax.persistence.Entity;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;

Expand Down Expand Up @@ -54,32 +54,33 @@ private NativeQueryInfo() {
public static NativeQueryInfo of(Class<? extends NativeQuery> classe, MethodInvocation invocation) {
NativeQueryInfo info = new NativeQueryInfo();

info.useSqlInline = invocation.getMethod().isAnnotationPresent(NativeQuerySql.class);
Method method = invocation.getMethod();
info.useSqlInline = method.isAnnotationPresent(NativeQuerySql.class);
if (info.useSqlInline) {
info.sqlInline = invocation.getMethod().getAnnotation(NativeQuerySql.class).value();
info.sqlInline = method.getAnnotation(NativeQuerySql.class).value();
} else {
setFile(classe, invocation, info);
}

info.useJdbcTemplate = invocation.getMethod().isAnnotationPresent(NativeQueryUseJdbcTemplate.class);
info.useJdbcTemplate = method.isAnnotationPresent(NativeQueryUseJdbcTemplate.class);
if (info.useJdbcTemplate) {
NativeQueryUseJdbcTemplate jdbcTemplate = invocation.getMethod().getAnnotation(NativeQueryUseJdbcTemplate.class);
NativeQueryUseJdbcTemplate jdbcTemplate = method.getAnnotation(NativeQueryUseJdbcTemplate.class);
info.useTenant = jdbcTemplate.useTenant();
}

if (invocation.getMethod().isAnnotationPresent(NativeQueryReplaceSql.class)) {
if (invocation.getMethod().getAnnotation(NativeQueryReplaceSql.class).values().length > 0) {
for (NativeQueryReplaceSqlParams value : invocation.getMethod().getAnnotation(NativeQueryReplaceSql.class).values()) {
if (method.isAnnotationPresent(NativeQueryReplaceSql.class)) {
if (method.getAnnotation(NativeQueryReplaceSql.class).values().length > 0) {
for (NativeQueryReplaceSqlParams value : method.getAnnotation(NativeQueryReplaceSql.class).values()) {
info.replaceSql.put(value.key(), value.value());
}
info.processorSqlList.addAll(Arrays.asList(invocation.getMethod().getAnnotation(NativeQueryReplaceSql.class).processorParams()));
info.processorSqlList.addAll(Arrays.asList(method.getAnnotation(NativeQueryReplaceSql.class).processorParams()));
}
}

info.returnType = invocation.getMethod().getReturnType();
info.returnType = method.getReturnType();
info.returnTypeIsIterable = Iterable.class.isAssignableFrom(info.returnType);
if (info.returnTypeIsIterable || info.returnType.getSimpleName().equals(Optional.class.getSimpleName())) {
TypeInformation<?> componentType = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod()).getComponentType();
if (info.returnTypeIsIterable || info.returnTypeIsOptional()) {
TypeInformation<?> componentType = ClassTypeInformation.fromReturnTypeOf(method).getComponentType();
info.aliasToBean = Objects.requireNonNull(componentType).getType();
} else {
info.aliasToBean = info.returnType;
Expand Down Expand Up @@ -144,58 +145,59 @@ private static void setFile(Class<? extends NativeQuery> classe, MethodInvocatio
}
}

private JtwigTemplate getJtwigTemplate() {
if (useSqlInline) {
return JtwigTemplate.inlineTemplate(sqlInline, JtwigTemplateConfig.get());
String getSql() {
if (sql != null) {
return sql;
}
return JtwigTemplate.classpathTemplate(file, JtwigTemplateConfig.get());
}

String getSql() {
if (sql == null) {
JtwigTemplate template = getJtwigTemplate();
JtwigModel model = JtwigModel.newModel();
parameterList.forEach(p -> model.with(p.getName(), p.getValue()));
sql = template.render(model);

for (Class<ProcessorSql> aClass : processorSqlList) {
try {
ProcessorSql processor = aClass.newInstance();
processor.execute(sql, replaceSql);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
sql = getSqlProcessed();

for (Map.Entry<String, String> replaceSqlEntry : replaceSql.entrySet()) {
sql = sql.replaceAll("\\$\\{"+replaceSqlEntry.getKey()+"}", replaceSqlEntry.getValue());
for (Class<ProcessorSql> aClass : processorSqlList) {
try {
ProcessorSql processor = aClass.newInstance();
processor.execute(sql, replaceSql);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}

if (sort != null) {
StringBuilder orderBuilder = new StringBuilder();
for (Sort.Order order : sort) {
if (orderBuilder.length() == 0) {
orderBuilder.append(" ORDER BY ");
} else {
orderBuilder.append(", ");
}
orderBuilder.append(order.getProperty())
.append(" ")
.append(order.getDirection().name());
}
for (Map.Entry<String, String> replaceSqlEntry : replaceSql.entrySet()) {
sql = sql.replaceAll("\\$\\{"+replaceSqlEntry.getKey()+"}", replaceSqlEntry.getValue());
}

sql += orderBuilder.toString();
if (sort != null) {
StringBuilder orderBuilder = new StringBuilder();
for (Sort.Order order : sort) {
if (orderBuilder.length() == 0) {
orderBuilder.append(" ORDER BY ");
} else {
orderBuilder.append(", ");
}
orderBuilder.append(order.getProperty())
.append(" ")
.append(order.getDirection().name());
}

if (useTenant) {
NativeQueryTenantNamedParameterJdbcTemplateInterceptor tenantJdbcTemplate = ApplicationContextProvider.getApplicationContext().getBean(NativeQueryTenantNamedParameterJdbcTemplateInterceptor.class);
sql = sql.replace(":SCHEMA", tenantJdbcTemplate.getTenant());
}
sql += orderBuilder.toString();
}

if (useTenant) {
NativeQueryTenantNamedParameterJdbcTemplateInterceptor tenantJdbcTemplate = ApplicationContextProvider.getApplicationContext().getBean(NativeQueryTenantNamedParameterJdbcTemplateInterceptor.class);
sql = sql.replace(":SCHEMA", tenantJdbcTemplate.getTenant());
}

return sql;
}

private String getSqlProcessed() {
return new JtwigTemplateEngineSQLProcessor()
.setParameter(parameterList)
.inline(useSqlInline)
.setClasspathTemplate(file)
.setInlineTemplate(sqlInline)
.getSql();
}

String getSqlTotalRecord() {
return "select count(*) as totalRecords from (" + getSql() + ") x";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Map;

class NativeQueryParameter implements Serializable, Cloneable {
public class NativeQueryParameter implements Serializable, Cloneable {

private final String name;

Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.gasparbarancelli;
package io.github.gasparbarancelli.engine.jtwig;

import org.jtwig.environment.EnvironmentConfiguration;
import org.jtwig.environment.EnvironmentConfigurationBuilder;
Expand Down
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);
}

}

0 comments on commit 0a455c9

Please sign in to comment.