Skip to content

Commit 3d2e96c

Browse files
authored
Merge pull request #55 from plantbreeding/34-improve-the-generation-of-sql-from-json-schema2
34 improve the generation of sql from json schema2
2 parents 3095e68 + 04cec4b commit 3d2e96c

22 files changed

+98
-40
lines changed

java/cli/src/main/java/org/brapi/schematools/cli/ValidateSubCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.brapi.schematools.core.brapischema.BrAPISchemaReader;
44
import org.brapi.schematools.core.brapischema.BrAPISchemaReaderOptions;
55
import org.brapi.schematools.core.model.BrAPIClass;
6-
import org.brapi.schematools.core.openapi.generator.options.OpenAPIGeneratorOptions;
76
import org.brapi.schematools.core.response.Response;
87
import picocli.CommandLine;
98

java/core/src/main/java/org/brapi/schematools/core/brapischema/BrAPISchemaReader.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import static java.util.Collections.singletonList;
3232
import static org.brapi.schematools.core.response.Response.fail;
3333
import static org.brapi.schematools.core.response.Response.success;
34-
import static org.brapi.schematools.core.utils.BrAPITypeUtils.unwrapType;
35-
import static org.brapi.schematools.core.utils.BrAPITypeUtils.validateBrAPIMetadata;
36-
import static org.brapi.schematools.core.utils.BrAPITypeUtils.mergeMetadata;
34+
import static org.brapi.schematools.core.utils.BrAPITypeUtils.*;
3735
import static org.brapi.schematools.core.utils.StringUtils.toSingular;
3836

3937
/**

java/core/src/main/java/org/brapi/schematools/core/ontmodel/options/OntModelGeneratorOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import lombok.NoArgsConstructor;
66
import lombok.Setter;
77
import lombok.experimental.Accessors;
8+
import org.brapi.schematools.core.ontmodel.OntModelGenerator;
89
import org.brapi.schematools.core.options.AbstractMainGeneratorOptions;
910
import org.brapi.schematools.core.utils.ConfigurationUtils;
10-
import org.brapi.schematools.core.ontmodel.OntModelGenerator;
1111

1212
import java.io.IOException;
1313
import java.io.InputStream;

java/core/src/main/java/org/brapi/schematools/core/r/options/RGeneratorOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import org.brapi.schematools.core.options.AbstractMainGeneratorOptions;
1111
import org.brapi.schematools.core.options.LinkType;
1212
import org.brapi.schematools.core.options.PropertiesOptions;
13+
import org.brapi.schematools.core.r.RGenerator;
1314
import org.brapi.schematools.core.utils.ConfigurationUtils;
1415
import org.brapi.schematools.core.validiation.Validation;
15-
import org.brapi.schematools.core.r.RGenerator;
1616

1717
import java.io.IOException;
1818
import java.io.InputStream;

java/core/src/main/java/org/brapi/schematools/core/sql/ANSICreateTableDDLGenerator.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.brapi.schematools.core.utils.StringUtils.removeCarriageReturns;
2727
import static org.brapi.schematools.core.utils.StringUtils.toPlural;
2828
import static org.brapi.schematools.core.utils.StringUtils.toSentenceCase;
29+
import static org.brapi.schematools.core.utils.StringUtils.toSnakeCase;
2930

3031
@Slf4j
3132
public class ANSICreateTableDDLGenerator implements CreateTableDDLGenerator {
@@ -73,7 +74,7 @@ public Response<String> generate() {
7374
}
7475

7576
return createTableDefinition(
76-
createTableName(brAPIObjectType),
77+
createTableNameFullName(brAPIObjectType),
7778
() -> createTableDescription(brAPIObjectType),
7879
() -> createColumnDefinitions(brAPIObjectType),
7980
getTableDescription(brAPIObjectType),
@@ -195,8 +196,23 @@ private void dedent() {
195196
indent -= options.getIndentSize() ;
196197
}
197198

199+
private String createTableNameFullName(BrAPIObjectType brAPIObjectType) {
200+
return metadata.getTablePrefix() != null ?
201+
metadata.getTablePrefix() + createTableName(brAPIObjectType) : createTableName(brAPIObjectType);
202+
}
203+
198204
private String createTableName(BrAPIObjectType brAPIObjectType) {
199-
return metadata.getTablePrefix() != null ? metadata.getTablePrefix() + brAPIObjectType.getName() : brAPIObjectType.getName();
205+
String name = brAPIObjectType.getName() ;
206+
207+
if (options.isUsingPluralTableNames()) {
208+
name = toPlural(name) ;
209+
}
210+
211+
if (options.isUsingSnakeCaseTableNames()) {
212+
name = toSnakeCase(name) ;
213+
}
214+
215+
return name ;
200216
}
201217

202218
private String getTableDescription(BrAPIObjectType brAPIObjectType) {
@@ -319,7 +335,13 @@ private String createLinkTableFullName(LinkTable linkedTable) {
319335
}
320336

321337
private String createLinkTableName(LinkTable linkedTable) {
322-
return linkedTable.getDereferencedItemType().getName() + "By" + linkedTable.getParentType().getName();
338+
String name = linkedTable.getDereferencedItemType().getName() + "By" + linkedTable.getParentType().getName();
339+
340+
if (options.isUsingSnakeCaseTableNames()) {
341+
name = toSnakeCase(name) ;
342+
}
343+
344+
return name ;
323345
}
324346

325347
private Response<String> createColumnDefinitions(LinkTable linkTable) {
@@ -387,20 +409,30 @@ private Response<String> appendControlledVocabularyDefinitions(String ddl) {
387409

388410
private Response<String> appendControlledVocabularyDefinition(ControlledVocabularyTable controlledVocabularyTable) {
389411
return createTableDefinition(
390-
createControlledVocabularyFullName(controlledVocabularyTable),
412+
createControlledVocabularyFullTableName(controlledVocabularyTable),
391413
() -> createTableDescription(controlledVocabularyTable),
392414
() -> createColumnDefinitions(controlledVocabularyTable),
393415
getTableComment(controlledVocabularyTable),
394416
findClusterColumns(controlledVocabularyTable));
395417
}
396418

397-
private String createControlledVocabularyFullName(ControlledVocabularyTable controlledVocabularyTable) {
419+
private String createControlledVocabularyFullTableName(ControlledVocabularyTable controlledVocabularyTable) {
398420
return metadata.getTablePrefix() != null ?
399-
metadata.getTablePrefix() + createControlledVocabularyName(controlledVocabularyTable) : createControlledVocabularyName(controlledVocabularyTable);
421+
metadata.getTablePrefix() + createControlledVocabularyTableName(controlledVocabularyTable) : createControlledVocabularyTableName(controlledVocabularyTable);
400422
}
401423

402-
private String createControlledVocabularyName(ControlledVocabularyTable controlledVocabularyTable) {
403-
return toSentenceCase(toPlural(controlledVocabularyTable.getProperty().getName())) ;
424+
private String createControlledVocabularyTableName(ControlledVocabularyTable controlledVocabularyTable) {
425+
String name = toSentenceCase(controlledVocabularyTable.getProperty().getName()) ;
426+
427+
if (options.isUsingPluralTableNames()) {
428+
name = toPlural(name) ;
429+
}
430+
431+
if (options.isUsingSnakeCaseTableNames()) {
432+
name = toSnakeCase(name) ;
433+
}
434+
435+
return name ;
404436
}
405437

406438
private Response<String> createColumnDefinitions(ControlledVocabularyTable controlledVocabularyTable) {

java/core/src/main/java/org/brapi/schematools/core/sql/options/SQLGeneratorOptions.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class SQLGeneratorOptions extends AbstractMainGeneratorOptions {
4646
@Setter(AccessLevel.PRIVATE)
4747
private ControlledVocabularyOptions controlledVocabulary;
4848
private Boolean generateLinkTables;
49+
private Boolean snakeCaseTableNames;
50+
private Boolean pluralTableNames;
4951

5052
/**
5153
* Load the default options
@@ -169,6 +171,14 @@ public SQLGeneratorOptions override(SQLGeneratorOptions overrideOptions) {
169171
generateLinkTables = overrideOptions.generateLinkTables;
170172
}
171173

174+
if (overrideOptions.snakeCaseTableNames != null) {
175+
snakeCaseTableNames = overrideOptions.snakeCaseTableNames;
176+
}
177+
178+
if (overrideOptions.pluralTableNames != null) {
179+
pluralTableNames = overrideOptions.pluralTableNames;
180+
}
181+
172182
return this;
173183
}
174184

@@ -270,10 +280,31 @@ public boolean isAddingDropTable() {
270280
* Determines if the Generator create link tables for many-to-many relationships when the
271281
* link type is {@link org.brapi.schematools.core.options.LinkType#SUB_QUERY}
272282
*
273-
* @return {@code true} if the Generator should add a 'DROP TABLE;, {@code false} otherwise
283+
* @return {@code true} if the Generator create link tables for many-to-many relationships when the
284+
* link type is {@link org.brapi.schematools.core.options.LinkType#SUB_QUERY}, {@code false} otherwise
274285
*/
275286
@JsonIgnore
276287
public boolean isGeneratingLinkTables() {
277288
return generateLinkTables != null && generateLinkTables ;
278289
}
290+
291+
/**
292+
* Determines if the Generator should use snake_case table names for all tables
293+
*
294+
* @return {@code true} if the Generator should use snake_case table names for all tables, {@code false} otherwise
295+
*/
296+
@JsonIgnore
297+
public boolean isUsingSnakeCaseTableNames() {
298+
return snakeCaseTableNames != null && snakeCaseTableNames ;
299+
}
300+
301+
/**
302+
* Determines if the Generator should use plural table names for entity tables
303+
*
304+
* @return {@code true} if the Generator should use plural table names for /** tables, {@code false} otherwise
305+
*/
306+
@JsonIgnore
307+
public boolean isUsingPluralTableNames() {
308+
return pluralTableNames != null && pluralTableNames ;
309+
}
279310
}

java/core/src/main/resources/sql-options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ addGeneratorComments: true
77
indent: 2
88
descriptionFormat: TODO Description for %s
99
generateLinkTables : true
10+
snakeCaseTableNames : false
11+
pluralTableNames : true
1012
generateFor:
1113
AlleleMatrix: false
1214
MarkerPosition: false

java/core/src/test/java/org/brapi/schematools/core/brapischema/BrAPISchemaReaderTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,12 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
14-
import java.util.HashSet;
15-
import java.util.List;
16-
import java.util.Map;
17-
import java.util.Objects;
18-
import java.util.Set;
14+
import java.util.*;
1915
import java.util.function.Function;
2016
import java.util.stream.Collectors;
2117

2218
import static graphql.Assert.assertNull;
23-
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertFalse;
25-
import static org.junit.jupiter.api.Assertions.assertNotNull;
26-
import static org.junit.jupiter.api.Assertions.assertTrue;
27-
import static org.junit.jupiter.api.Assertions.fail;
19+
import static org.junit.jupiter.api.Assertions.*;
2820

2921
@Slf4j
3022
class BrAPISchemaReaderTest {

java/core/src/test/java/org/brapi/schematools/core/utils/BrAPITypeUtilsTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import java.util.Collections;
1212
import java.util.List;
1313

14-
import static org.junit.jupiter.api.Assertions.*;
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertNull;
17+
import static org.junit.jupiter.api.Assertions.assertSame;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1519

1620
class BrAPITypeUtilsTest {
1721

java/core/src/test/resources/SQLGenerator/ANSI/CallSet.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
A CallSet is a collection of Calls that were generated by the same analysis of the same Sample
44
*/
5-
CREATE TABLE brapi_CallSet (
5+
CREATE TABLE brapi_CallSets (
66
additionalInfo MAP<STRING,STRING> COMMENT 'A free space containing any additional information related to a particular object. A data source may provide any JSON object, unrestricted by the BrAPI specification.',
77
callSetDbId STRING COMMENT 'The ID which uniquely identifies a CallSet within the given database server',
88
callSetName STRING COMMENT 'The human readable name which identifies a germplasm within the given database server',

0 commit comments

Comments
 (0)