Skip to content

Commit

Permalink
set Generated's table default value to empty string #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ningpp committed Aug 13, 2024
1 parent a7d5bb5 commit 5bc8c1d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* table name
*/
String table();
String table() default "";

/**
* seperate multi columns by seperator=,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.ningpp.mmegp.annotations.GeneratedColumn;
import me.ningpp.mmegp.annotations.SoftDelete;

@Generated(table = "sys_auto_user")
@Generated
@SoftDelete(column = "deleted")
public class SysAutoUser {
@GeneratedColumn(id = true, generatedValue = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static IntrospectedTable buildIntrospectedTable(Context context,
TypeDeclaration<?> modelDeclaration,
NodeList<ImportDeclaration> importDeclarations,
MetaInfoHandler metaInfoHandler) {
GeneratedTableInfo tableInfo = JavaParserUtil.getTableValue(modelDeclaration);
GeneratedTableInfo tableInfo = JavaParserUtil.getTableValue(modelDeclaration, context);
if (tableInfo == null || StringUtils.isEmpty(tableInfo.getName())) {
return null;
}
Expand Down
36 changes: 23 additions & 13 deletions mmegp-mbg-ext/src/main/java/me/ningpp/mmegp/JavaParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,29 @@ public static JavaParser newParser() {
return new JavaParser(jpc);
}

public static GeneratedTableInfo getTableValue(TypeDeclaration<?> annotationNode) {
if (annotationNode == null || annotationNode.getFullyQualifiedName().isEmpty()) {
return null;
}
var annotationMembers = getNormalAnnotationMembers(annotationNode, Generated.class);
if (annotationMembers.isEmpty()) {
public static GeneratedTableInfo getTableValue(TypeDeclaration<?> typeDeclar, Context context) {
if (typeDeclar == null || typeDeclar.getFullyQualifiedName().isEmpty()) {
return null;
}
var annotationMembers = getNormalAnnotationMembers(typeDeclar, Generated.class);

String tableName = parseString(annotationMembers, "table", null);
String tableName = parseTableName(annotationMembers, typeDeclar.getNameAsString(), context);
List<String> countGroupByColumns = parseArrayString(annotationMembers, COUNT_GROUP_BY_COLUMNS_NAME);
return new GeneratedTableInfo(tableName, countGroupByColumns);
}

private static String parseTableName(Map<String, List<MemberValuePair>> annotationMembers,
String entityName, Context context) {
return parseNamingByStrategy(annotationMembers, "table", entityName,
"tableNamingStrategyInstance", "tableNamingStrategy", context);
}

private static String parseColumnName(Map<String, List<MemberValuePair>> annotationMembers,
String javaProperty, Context context) {
return parseNamingByStrategy(annotationMembers, "name", javaProperty,
"columnNamingStrategyInstance", "columnNamingStrategy", context);
}

public static Pair<IntrospectedColumn, Boolean> buildColumn(TypeDeclaration<?> modelDeclaration,
Map<String, ImportDeclaration> declarMappings,
Parameter param,
Expand Down Expand Up @@ -223,19 +232,20 @@ private static <N1 extends Node, N2 extends Node> Pair<IntrospectedColumn, Boole
return Pair.of(column, id);
}

private static String parseColumnName(Map<String, List<MemberValuePair>> annotationMembers,
String javaProperty, Context context) {
String name = parseString(annotationMembers, "name", "");
private static String parseNamingByStrategy(Map<String, List<MemberValuePair>> annotationMembers,
String annotationMethod, String srcName,
String instanceKey, String propertyKey,
Context context) {
String name = parseString(annotationMembers, annotationMethod, "");
if (StringUtils.isEmpty(name)) {
String instanceKey = "columnNamingStrategyInstance";
NamingStrategy strategy = (NamingStrategy) context.getProperties().get(instanceKey);
if (strategy == null) {
String strategyClassName = (String) context.getProperties()
.getOrDefault("columnNamingStrategy", SnakeCaseStrategy.class.getName());
.getOrDefault(propertyKey, SnakeCaseStrategy.class.getName());
strategy = (NamingStrategy) ObjectFactory.createExternalObject(strategyClassName);
context.getProperties().put(instanceKey, strategy);
}
name = strategy.translate(javaProperty);
name = strategy.translate(srcName);
}
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import static org.apache.ibatis.type.JdbcType.VARCHAR;

@Generated(table = "big_entity")
@Generated
public class BigEntity {
@GeneratedColumn(id = true)
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static org.apache.ibatis.type.JdbcType.VARCHAR;

@Generated(table = "big_record_entity")
@Generated
public record BigRecordEntity(
@GeneratedColumn(name = "id", jdbcType = VARCHAR, id = true)
String id,
Expand Down

0 comments on commit 5bc8c1d

Please sign in to comment.