Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flexmodel-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ try (Session session = sessionFactory.createSession("mySchema")) {
{
"objects": [
{
"type": "entity",
"type": "Entity",
"name": "User",
"comment": "用户表",
"fields": [
Expand Down
37 changes: 14 additions & 23 deletions flexmodel-engine/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,31 +339,22 @@ EnumField statusField = new EnumField("status")
.setDefaultValue("PENDING");
```

#### 关系字段
#### 模型引用字段

```java
// 一对一关系
RelationField profileField = new RelationField("profile")
.setType(RelationField.Type.ONE_TO_ONE)
.setFrom("User")
.setTo("UserProfile")
.setForeignField("user_id");

// 一对多关系
RelationField ordersField = new RelationField("orders")
.setType(RelationField.Type.ONE_TO_MANY)
.setFrom("User")
.setTo("Order")
.setForeignField("user_id");

// 多对多关系
RelationField rolesField = new RelationField("roles")
.setType(RelationField.Type.MANY_TO_MANY)
.setFrom("User")
.setTo("Role")
.setJoinTable("user_roles")
.setJoinColumn("user_id")
.setInverseJoinColumn("role_id");
// 单选引用(一对一)
ModelRefField profileField = new ModelRefField("profile")
.setFrom("UserProfile")
.setLocalField("profileId")
.setForeignField("id");

// 多选引用(一对多)
ModelRefField ordersField = new ModelRefField("orders")
.setMultiple(true)
.setFrom("Order")
.setLocalField("userId")
.setForeignField("id");

```

### 异常处理
Expand Down
2 changes: 1 addition & 1 deletion flexmodel-engine/docs/flexmodel-codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ListApiDefinitionGenerator extends ApiDefinitionGenerator {
out.println "query MyListQuery( \$where: ${modelName}BoolExp) {"
out.println " ${modelName}(where: \$where) {"
context.getModelClass().getAllFields().each {
if (!it.isRelationField()) {
if (!it.isModelRefField()) {
out.println " ${it.fieldName}"
}
}
Expand Down
4 changes: 3 additions & 1 deletion flexmodel-engine/docs/flexmodel-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ session.schema().createEntity("Student", entity -> entity
.addField(new IntField("age"))
.addField(new FloatField("score").setPrecision(5).setScale(2))
.addField(new EnumRefField("gender").setFrom("UserGender"))
.addField(new RelationField("studentClass")
.

addField(new ModelRefField("studentClass")
.setFrom("Classes")
.setLocalField("classId")
.setForeignField("id"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public enum JavaType {

RELATION(ScalarType.RELATION_TYPE, null, null, null),
MODEL_REF(ScalarType.MODEL_REF_TYPE, null, null, null),
STRING(STRING_TYPE, null, "String", "String"),
FLOAT(FLOAT_TYPE, null, "Double", "Double"),
INT(INT_TYPE, null, "Integer", "Integer"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.flexmodel.model.EntityDefinition;
import dev.flexmodel.model.ModelDefinition;
import dev.flexmodel.model.field.EnumRefField;
import dev.flexmodel.model.field.RelationField;
import dev.flexmodel.model.field.ModelRefField;
import dev.flexmodel.model.field.TypedField;

import java.util.ArrayList;
Expand All @@ -19,7 +19,7 @@ public class ModelClass extends AbstractClass<ModelClass> {
private ModelField idField;
private final List<ModelField> basicFields = new ArrayList<>();
private final List<ModelField> enumFields = new ArrayList<>();
private final List<ModelField> relationFields = new ArrayList<>();
private final List<ModelField> modelRefFields = new ArrayList<>();
private final List<ModelField> allFields = new ArrayList<>();
private ModelDefinition original;

Expand Down Expand Up @@ -56,7 +56,7 @@ public static ModelClass buildModelClass(String replaceString, String packageNam
.setComment(field.getComment());

switch (field) {
case RelationField relationField -> {
case ModelRefField relationField -> {
String ftName = StringUtils.capitalize(
StringUtils.snakeToCamel(
replaceString != null ?
Expand All @@ -68,13 +68,13 @@ public static ModelClass buildModelClass(String replaceString, String packageNam
modelField.setTypePackage("java.util")
.setFullTypeName("java.util.List")
.setShortTypeName("List<" + ftName + ">")
.setRelationField(true);
.setModelRefField(true);
} else {
modelField.setTypePackage(null)
.setFullTypeName(modelField.getTypePackage() + "." + ftName)
.setShortTypeName(ftName)
.setRelationField(true);
modelClass.getRelationFields().add(modelField);
.setModelRefField(true);
modelClass.getModelRefFields().add(modelField);
}

}
Expand Down Expand Up @@ -150,8 +150,8 @@ public List<ModelField> getEnumFields() {
return enumFields;
}

public List<ModelField> getRelationFields() {
return relationFields;
public List<ModelField> getModelRefFields() {
return modelRefFields;
}

public List<ModelField> getAllFields() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ModelField {
private String fullTypeName;
private boolean identity;
private boolean basicField;
private boolean relationField;
private boolean modelRefField;
private boolean enumField;
private Field original;

Expand Down Expand Up @@ -103,8 +103,8 @@ public ModelField setBasicField(boolean basicField) {
return this;
}

public boolean isRelationField() {
return relationField;
public boolean isModelRefField() {
return modelRefField;
}

public boolean isEnumField() {
Expand All @@ -116,8 +116,8 @@ public ModelField setEnumField(boolean enumField) {
return this;
}

public ModelField setRelationField(boolean relationField) {
this.relationField = relationField;
public ModelField setModelRefField(boolean relationField) {
this.modelRefField = relationField;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public void writeModel(PrintWriter out, GenerationContext context) {
out.println(" * " + field.getComment());
out.println(" */");
}
if (field.isRelationField()) {
out.println(" @ModelRelation");
}
if (field.isModelRefField()) {
out.println(" @ModelRef");
}
out.println(" @ModelField(\"" + field.getOriginal().getName() + "\")");
out.println(" private " + field.getShortTypeName() + " " + field.getVariableName() + ";");
}
Expand Down
20 changes: 10 additions & 10 deletions flexmodel-engine/flexmodel-codegen/src/test/resources/import.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@
}
],
"indexes": [],
"type": "entity"
"type": "Entity"
},
{
"name": "fs_datasource",
"type": "entity",
"type": "Entity",
"fields": [
{
"name": "name",
Expand Down Expand Up @@ -198,7 +198,7 @@
},
{
"name": "fs_api_log",
"type": "entity",
"type": "Entity",
"fields": [
{
"name": "id",
Expand Down Expand Up @@ -258,7 +258,7 @@
]
},
{
"type": "entity",
"type": "Entity",
"name": "Student",
"fields": [
{
Expand Down Expand Up @@ -315,7 +315,7 @@
},
{
"name": "studentDetail",
"type": "Relation",
"type": "ModelRef",
"modelName": "Student",
"unique": false,
"nullable": true,
Expand All @@ -329,7 +329,7 @@
"indexes": []
},
{
"type": "entity",
"type": "Entity",
"name": "StudentDetail",
"fields": [
{
Expand Down Expand Up @@ -363,7 +363,7 @@
},
{
"name": "LogLevel",
"type": "enum",
"type": "Enum",
"elements": [
"DEBUG",
"INFO",
Expand All @@ -374,7 +374,7 @@
},
{
"name": "UserGender",
"type": "enum",
"type": "Enum",
"elements": [
"UNKNOWN",
"MALE",
Expand All @@ -384,7 +384,7 @@
},
{
"name": "User_interest",
"type": "enum",
"type": "Enum",
"elements": [
"chang",
"tiao",
Expand Down Expand Up @@ -490,4 +490,4 @@
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface ModelRelation {
public @interface ModelRef {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.flexmodel.model;

import dev.flexmodel.model.field.RelationField;
import dev.flexmodel.model.field.ModelRefField;
import dev.flexmodel.model.field.TypedField;

import java.util.*;
Expand All @@ -21,7 +21,7 @@ public EntityDefinition(String name) {

@Override
public String getType() {
return "entity";
return "Entity";
}

public String getComment() {
Expand Down Expand Up @@ -58,9 +58,9 @@ public IndexDefinition getIndex(String name) {
.orElse(null);
}

public Optional<RelationField> findRelationByModelName(String modelName) {
public Optional<ModelRefField> findRelationByModelName(String modelName) {
for (TypedField<?, ?> field : fields) {
if (field instanceof RelationField relationField) {
if (field instanceof ModelRefField relationField) {
if (relationField.getFrom().equals(modelName)) {
return Optional.of(relationField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getName() {

@Override
public String getType() {
return "enum";
return "Enum";
}

public EnumDefinition setElements(List<String> elements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public NativeQueryDefinition setStatement(String statement) {

@Override
public String getType() {
return "native_query";
return "NativeQuery";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class EnumRefField extends TypedField<Serializable, EnumRefField> {
private String from;

public EnumRefField(String name) {
super(name, ScalarType.ENUM.getType());
super(name, ScalarType.ENUM_REF.getType());
}

public boolean isMultiple() {
Expand Down
Loading
Loading