Skip to content

Commit

Permalink
devonfw#860 Added first tests for sql generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylesto committed Nov 9, 2022
1 parent 8ab95ab commit b480ef0
Show file tree
Hide file tree
Showing 4 changed files with 512 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.cobigen.templates.devon4j.utils;

import java.util.*;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -85,15 +86,14 @@ public String foreignKeyStatement(Map<String, ?> field) {

public String basicStatement(Map<String, ?> field) {

Map<String, ?> columnAnnotation = chainAccess(field, new String[] {"annotations", "javax_persistence_Column"});
String fieldName = getFieldName(field),
typeString = Objects.requireNonNull(getValue(field, "type")),
Map<String, ?> columnAnnotation = chainAccess(field, new String[] { "annotations", "javax_persistence_Column" });
String fieldName = getFieldName(field), typeString = Objects.requireNonNull(getValue(field, "type")),
fieldType = mapType(typeString);
Integer fieldLength = 255;
boolean nullable = true,
unique = false;
boolean nullable = true, unique = false;
// Try to infer fieldType from possible annotations
Map<String, ?> enumerateAnnotation = chainAccess(field, new String[]{"annotations", "javax_persistence_Enumerated"});
Map<String, ?> enumerateAnnotation = chainAccess(field,
new String[] { "annotations", "javax_persistence_Enumerated" });
if (enumerateAnnotation != null) {
String enumType = Objects.requireNonNull(getValue(enumerateAnnotation, "value"));
if (enumType.equals("STRING")) {
Expand All @@ -105,46 +105,55 @@ public String basicStatement(Map<String, ?> field) {
// Parse @Column if present
if (columnAnnotation != null) {
Integer columnLength = Integer.parseInt(Objects.requireNonNull(getValue(columnAnnotation, "length")));
if (columnLength != null) fieldLength = columnLength;
if (columnLength != null)
fieldLength = columnLength;
nullable = isNullable(columnAnnotation);
unique = isUnique(columnAnnotation);
}

// If fieldType is still empty throw exception
if (fieldType == null) throw new IllegalArgumentException("Couldn't map Java type to SQL type for typeString: " + typeString);
if (fieldType == null)
throw new IllegalArgumentException("Couldn't map Java type to SQL type for typeString: " + typeString);

// Add size to VARCHAR fields
if (fieldType.equals("VARCHAR")) {
fieldType = String.format("VARCHAR(%d)", fieldLength);
}
String statement = String.format("%s %s", fieldName, fieldType);
if (!nullable) statement += " NOT NULL";
if (unique) statement += " UNIQUE";
if (!nullable)
statement += " NOT NULL";
if (unique)
statement += " UNIQUE";
return statement;
}

/**
* Extracts value of nullable from @Column and @JoinColumn annotations
*
* @param columnAnnotation Map for the Column and JoinColumn annotations
*/
private static boolean isNullable(Map<String, ?> columnAnnotation) {

return Boolean.parseBoolean(getValue(columnAnnotation, "nullable"));
}

/**
* Extracts value of unique from @Column and @JoinColumn annotations
*
* @param columnAnnotation Map for the Column and JoinColumn annotations
*/
private static boolean isUnique(Map<String, ?> columnAnnotation) {

return Boolean.parseBoolean(getValue(columnAnnotation, "unique"));
}

/**
* Helper function to map simple SQL types, returns null on unmappable type
*/
public static String mapType(String typeString) {

// Shortcut for case insensitive regex matching with start and ending ignore
Function<String, Boolean> match = (regex) -> typeString.matches(".*" + "(?i)" + regex + ".*");
Function<String, Boolean> match = (regex) -> typeString.matches("(?i).*" + regex + ".*");
if (match.apply("(integer)|(int)")) {
return "INTEGER";
} else if (match.apply("long")) {
Expand Down Expand Up @@ -175,7 +184,7 @@ public static String mapType(String typeString) {
return "CLOB";
} else if (match.apply("(Class)|(Locale)|(TimeZone)|(Currency)")) {
return "VARCHAR";
}else {
} else {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
package com.devonfw.cobigen.templates.devon4j.test.templates;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import com.devonfw.cobigen.templates.devon4j.test.templates.testclasses.SQLTestEntity;
import com.devonfw.cobigen.templates.devon4j.test.templates.testclasses.SQLTestEntityDataTypes;
import com.devonfw.cobigen.templates.devon4j.utils.SQLUtil;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;

public class SQLTemplateGenerationTest extends AbstractJavaTemplateTest {
@Test
public void generateSQLTest() {
String output = this.process(SQLTestEntity.class);

String output = process(SQLTestEntity.class);
}

@Override
public Class<?>[] getUtils() {

return new Class<?>[] { SQLUtil.class };
}

@Override
public String getTemplatePath() {
return "src/main/templates/sql_java_app/templates/V0000__Create_${variables.entityName}Entity.sql.ftl";

return "src/main/templates/sql_java_app/templates/V0000__Create_${variables.entityName}Entity.sql.ftl";
}

/**
* Test the correct generation of data types
*/
@Test
public void testDatatypeMapping() {

String ouptut = process(SQLTestEntityDataTypes.class);
assertThat(ouptut).contains("_timestamp2 TIMESTAMP").contains("_blob2 BLOB").contains("_bit BIT,")
.contains("_date DATE").contains("_tinyint TINYINT").contains("_integer2 INTEGER").contains("_bigint BIGINT")
.contains("_varchar3 VARCHAR").contains("_integer1 INTEGER").contains("_varchar4 VARCHAR")
.contains("_clob CLOB").contains("_blob BLOB").contains("_varchar VARCHAR").contains("_char2 CHAR(1)")
.contains(" _smallint SMALLINT").contains(" _char CHAR(1)").contains("_timestamp TIMESTAMP")
.contains("_time TIME").contains("_numeric NUMERIC").contains("_varchar2 VARCHAR");

}
}
Loading

0 comments on commit b480ef0

Please sign in to comment.