Skip to content

Commit

Permalink
#4 support sql render
Browse files Browse the repository at this point in the history
  • Loading branch information
holmofy committed Oct 2, 2023
1 parent 7014a5b commit a8b1ab5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<maven.flatten.version>1.4.1</maven.flatten.version>
<maven.source.version>3.2.1</maven.source.version>
<maven.javadoc.version>3.5.0</maven.javadoc.version>
<reversion>3.1.3</reversion>
<reversion>3.2.0</reversion>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.util.StringUtils;

import javax.annotation.processing.Generated;
Expand Down Expand Up @@ -74,7 +75,8 @@ public class TableModel {

public static final String SPRING_DATA_PACKAGE = "org.springframework.data.relational.core.sql";

private static final Converter<String, String> columnConverter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
private static final Converter<String, String> LOWER_UNDERSCORE = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
private static final Converter<String, String> UPPER_UNDERSCORE = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE);

private PackageElement packageElement;

Expand All @@ -97,8 +99,11 @@ public JavaFile generateSource() {
// table name
String tableName = getTableName();
builder.addField(FieldSpec.builder(
ClassName.get("java.lang", "String"), "TABLE", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
ClassName.get(String.class), "TABLE_NAME", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
).initializer("$S", tableName).build());
builder.addField(FieldSpec.builder(
ClassName.get(org.springframework.data.relational.core.sql.Table.class), "TABLE", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
).initializer("$T.table(TABLE_NAME)", SQL.class).build());

addFields(builder, this.fields, "");

Expand Down Expand Up @@ -135,10 +140,14 @@ private void addField(TypeSpec.Builder builder, VariableElement field, String co
String column_name = Optional.of(field).map(f -> f.getAnnotation(Column.class))
.map(Column::value)
.filter(s -> !s.isBlank())
.orElse(columnConverter.convert(fieldName));
.orElse(LOWER_UNDERSCORE.convert(fieldName));
builder.addField(FieldSpec.builder(
ClassName.get("java.lang", "String"), fieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
ClassName.get(String.class), fieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
).initializer("$S", columnPrefix + column_name).build());
String COLUMN_NAME = Objects.requireNonNull(UPPER_UNDERSCORE.convert(fieldName));
builder.addField(FieldSpec.builder(
ClassName.get(org.springframework.data.relational.core.sql.Column.class), COLUMN_NAME, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL
).initializer("$T.column($L, TABLE)", SQL.class, fieldName).build());
}

public String getGenerateClassName() {
Expand All @@ -155,7 +164,7 @@ private String getTableName() {
return annotation.value();
}
}
return columnConverter.convert(classElement.getSimpleName().toString());
return LOWER_UNDERSCORE.convert(classElement.getSimpleName().toString());
}
}

Expand Down
16 changes: 15 additions & 1 deletion spring-data-criteria-apt/src/test/resources/TestModel_.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

import java.lang.String;
import javax.annotation.processing.Generated;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.Table;

@Generated("io.github.holmofy.data.apt.SpringDataRelationalAnnotationProcessor")
public final class TestModel_ {
public static final String TABLE = "test_model";
public static final String TABLE_NAME = "test_model";
public static final Table TABLE = SQL.table(TABLE_NAME);

public static final String id = "id";
public static final Column ID = SQL.column(id, TABLE);

public static final String withColumn = "named_column";
public static final Column WITH_COLUMN = SQL.column(withColumn, TABLE);

public static final String lowerCamel = "lower_camel";
public static final Column LOWER_CAMEL = SQL.column(lowerCamel, TABLE);

public static final String field1 = "embedded_field1";
public static final Column FIELD1 = SQL.column(field1, TABLE);

public static final String field2 = "embedded_field_2";
public static final Column FIELD2 = SQL.column(field2, TABLE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public Page<User> search(UserDao.UserQuery query, Pageable pageable) {
return userDao.searchByQuery(query, pageable);
}

@GetMapping("/search-list")
public List<User> search(UserDao.UserQuery query) {
return userDao.searchByQuery(query);
@GetMapping("/search-list1")
public List<User> search1(UserDao.UserQuery query) {
return userDao.searchByQuery1(query);
}

@GetMapping("/search-list2")
public List<User> search2(UserDao.UserQuery query) {
return userDao.searchByQuery2(query);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.render.SqlRenderer;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

Expand All @@ -17,7 +21,19 @@

public interface UserDao extends ListCrudRepository<User, Long>, CriteriaExecutor<User>, JdbcSupport {

default List<User> searchByQuery(UserQuery query) {
default List<User> searchByQuery1(UserQuery query) {
Select sql = Select.builder().select()
.from(User_.TABLE)
.where(Conditions.isEqual(User_.PROVINCE, SQL.bindMarker(":province")))
.and(Conditions.isEqual(User_.CITY, SQL.bindMarker(":city")))
.and(Conditions.isEqual(User_.AREA, SQL.bindMarker(":area")))
.and(Conditions.isEqual(User_.NAME, SQL.bindMarker(":nick")))
.build();
return namedJdbcTemplate().queryForList(SqlRenderer.toString(sql)
, new BeanPropertySqlParameterSource(query), User.class);
}

default List<User> searchByQuery2(UserQuery query) {
return namedJdbcTemplate().queryForList(
"select * " +
"from t_user " +
Expand Down

0 comments on commit a8b1ab5

Please sign in to comment.