Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate sql script from entity #1202

Closed
wants to merge 9 commits into from

Conversation

lilliCao
Copy link
Contributor

@lilliCao lilliCao commented Jul 1, 2020

⚠️ THIS PR IS ONLY FOR DISCUSSION⚠️
Relate #955 .

Implements

  • Generating 1 sql script V0000__Create_{EntityName}.sql for each Entity
  • Mapping between java type and db type is based on this
  • Annotations to be considered: Table, Id, JoinColumn, Column, Size, NotNull, ManyToOne, OneToOne, ManyToMany

@lilliCao lilliCao requested a review from maybeec July 1, 2020 14:23
<#assign type = "BOOLEAN">
<#elseif input_type?contains("Instant") || input_type?contains("Timestamp")>
<#assign type = "TIMESTAMP">
<#elseif input_type?contains("Date") || input_type?contains("Calender")>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is "Calendar" (typo)

If you have types here, please check for the full type name as otherwise, it might have unexpected side effects if there are more than one class named like that.

</#if>
<#assign columns = columns + [{"name": name, "type":type}]>
<#elseif !field.type?starts_with("List<") && !field.type?starts_with("Set<")>
<#if field.type?contains("Entity")>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more compliant would be to check if the type name ends with.
As a class named EntityCrawler would even match this check

<#assign type = type + " AUTO_INCREMENT">
</#if>
<#assign columns = columns + [{"name": name, "type":type}]>
<#elseif !field.type?starts_with("List<") && !field.type?starts_with("Set<")>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I would better ask for instanceof in a java implementation. I think there are already functions to do so.
So you could easily ask JavaUtil.isCollection( for the full qualified type name.
This would even include the cases where you have Collection< or LinkedList< or anything else.

Copy link
Contributor Author

@lilliCao lilliCao Jul 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation of isCollection only checks the field which is declared in the class itself and not from parent class. I added a new implementation to check all field recursively!

<#else>
<#assign type = "VARCHAR(255)">
</#if>
</#macro>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I would think macro is a little problematic.
In FreeMarker, you have macros and functions.
Macros are program snippets which will be copied to the place and executed there. No isolation of variables etc. such that it might be hard to reuse macros in any place as variables have to have the same name etc.
So what you want to do here is I think better call a freemarker function which returns to you the string of the type, which you can then assign in your template to a local variable.

Copy link
Collaborator

@MikeSchumacherCapgemini MikeSchumacherCapgemini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but the new Java methods should have test cases to make sure they always work correctly

* full qualified class name
* @return the annotated table name if existed or null
*/
public String getEntityTableName(String className) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for this method to src/test/java/com/devonfw/cobigen/templates/devon4j/test/utils/JavaUtilTest.java

* full qualified class name
* @return the primary key and its type if found or null
*/
public String getPrimaryKey(String className) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for this method to src/test/java/com/devonfw/cobigen/templates/devon4j/test/utils/JavaUtilTest.java

* {@link String} the name of the field
* @return type of the field in String
*/
public String getCanonicalNameOfField(String className, String fieldName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for this method to src/test/java/com/devonfw/cobigen/templates/devon4j/test/utils/JavaUtilTest.java

* {@link String} the name of the field
* @return true if the field is an instance of java.utils.Collections
*/
public boolean isCollection2(Class<?> pojoClass, String fieldName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for this method to src/test/java/com/devonfw/cobigen/templates/devon4j/test/utils/JavaUtilTest.java

@maybeec maybeec added this to the devonfw-templates-20.04.003 milestone Aug 17, 2020
@MikeSchumacherCapgemini MikeSchumacherCapgemini modified the milestones: devonfw-templates-v2020.12.001, devonfw-templates-v2020.12.002 Dec 17, 2020
@maybeec maybeec changed the base branch from master to develop January 7, 2021 10:11
@maybeec maybeec changed the base branch from develop to master April 28, 2021 11:17
@maybeec maybeec dismissed MikeSchumacherCapgemini’s stale review April 28, 2021 11:17

The base branch was changed.

@maybeec maybeec removed this from the devonfw-templates-v2020.12.002 milestone Jul 8, 2021
@maybeec maybeec marked this pull request as draft December 7, 2021 15:37
@CLAassistant
Copy link

CLAassistant commented Aug 3, 2022

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 3 committers have signed the CLA.

❌ lilliCao
❌ maybeec
❌ MikeSchumacherCapgemini
You have signed the CLA already but the status is still pending? Let us recheck it.

@maybeec
Copy link
Member

maybeec commented Sep 7, 2022

continued in #1570

@maybeec maybeec closed this Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants