@Test void getSqlCreateTable() { Attribute refIdAttrStr = mock(Attribute.class); when(refIdAttrStr.getIdentifier()).thenReturn("refIdAttrStrId"); when(refIdAttrStr.getName()).thenReturn("refIdAttrStr"); when(refIdAttrStr.getDataType()).thenReturn(STRING); EntityType refEntityTypeString = mock(EntityType.class); when(refEntityTypeString.getId()).thenReturn("refEntityStr"); when(refEntityTypeString.getIdAttribute()).thenReturn(refIdAttrStr); Attribute refIdAttrInt = mock(Attribute.class); when(refIdAttrInt.getIdentifier()).thenReturn("refIdAttrInt"); when(refIdAttrInt.getName()).thenReturn("refIdAttrInt"); when(refIdAttrInt.getDataType()).thenReturn(INT); EntityType refEntityTypeInt = mock(EntityType.class); when(refEntityTypeInt.getId()).thenReturn("refEntityInt"); when(refEntityTypeInt.getIdAttribute()).thenReturn(refIdAttrInt); EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("entity").getMock(); when(entityType.getId()).thenReturn("entityTypeId"); Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("id").getMock(); when(idAttr.getIdentifier()).thenReturn("idAttrId"); when(idAttr.getDataType()).thenReturn(STRING); when(entityType.getIdAttribute()).thenReturn(idAttr); List<Attribute> atomicAttrs = Lists.newArrayList(); atomicAttrs.add(idAttr); StringBuilder attrNameBuilder = new StringBuilder(16); for (boolean hasExpression : newArrayList(false, true)) { for (boolean unique : newArrayList(false, true)) { for (boolean nillable : newArrayList(false, true)) { for (AttributeType attrType : AttributeType.values()) { if (attrType != COMPOUND) { attrNameBuilder.setLength(0); attrNameBuilder.append(attrType.toString().toLowerCase()); if (hasExpression) { attrNameBuilder.append("_expression"); } if (unique) { attrNameBuilder.append("_unique"); } if (nillable) { attrNameBuilder.append("_nillable"); } Attribute attr = mock(Attribute.class); when(attr.getIdentifier()).thenReturn(attrNameBuilder.toString() + "Id"); when(attr.getName()).thenReturn(attrNameBuilder.toString()); when(attr.getDataType()).thenReturn(attrType); when(attr.getExpression()).thenReturn(hasExpression ? "expression" : null); when(attr.isUnique()).thenReturn(unique); when(attr.isNillable()).thenReturn(nillable); if (attrType == CATEGORICAL || attrType == CATEGORICAL_MREF || attrType == ONE_TO_MANY) { when(attr.getRefEntity()).thenReturn(refEntityTypeString); } else if (attrType == FILE || attrType == XREF || attrType == MREF) { when(attr.getRefEntity()).thenReturn(refEntityTypeInt); } else if (attrType == ENUM) { when(attr.getEnumOptions()).thenReturn(newArrayList("enum0", "enum1")); } atomicAttrs.add(attr); } } } } } when(entityType.getAtomicAttributes()).thenReturn(atomicAttrs); assertEquals( "CREATE TABLE \"entityTypeId#c34894ba\"(\"id\" character varying(255),\"bool\" boolean NOT NULL,\"categorical\" character varying(255) NOT NULL,\"date\" date NOT NULL,\"date_time\" timestamp with time zone NOT NULL,\"decimal\" double precision NOT NULL,\"email\" character varying(255) NOT NULL,\"enum\" character varying(255) NOT NULL,\"file\" integer NOT NULL,\"html\" text NOT NULL,\"hyperlink\" character varying(255) NOT NULL,\"int\" integer NOT NULL,\"long\" bigint NOT NULL,\"script\" text NOT NULL,\"string\" character varying(255) NOT NULL,\"text\" text NOT NULL,\"xref\" integer NOT NULL,\"bool_nillable\" boolean,\"categorical_nillable\" character varying(255),\"date_nillable\" date,\"date_time_nillable\" timestamp with time zone,\"decimal_nillable\" double precision,\"email_nillable\" character varying(255),\"enum_nillable\" character varying(255),\"file_nillable\" integer,\"html_nillable\" text,\"hyperlink_nillable\" character varying(255),\"int_nillable\" integer,\"long_nillable\" bigint,\"script_nillable\" text,\"string_nillable\" character varying(255),\"text_nillable\" text,\"xref_nillable\" integer,\"bool_unique\" boolean NOT NULL,\"categorical_unique\" character varying(255) NOT NULL,\"date_unique\" date NOT NULL,\"date_time_unique\" timestamp with time zone NOT NULL,\"decimal_unique\" double precision NOT NULL,\"email_unique\" character varying(255) NOT NULL,\"enum_unique\" character varying(255) NOT NULL,\"file_unique\" integer NOT NULL,\"html_unique\" text NOT NULL,\"hyperlink_unique\" character varying(255) NOT NULL,\"int_unique\" integer NOT NULL,\"long_unique\" bigint NOT NULL,\"script_unique\" text NOT NULL,\"string_unique\" character varying(255) NOT NULL,\"text_unique\" text NOT NULL,\"xref_unique\" integer NOT NULL,\"bool_unique_nillable\" boolean,\"categorical_unique_nillable\" character varying(255),\"date_unique_nillable\" date,\"date_time_unique_nillable\" timestamp with time zone,\"decimal_unique_nillable\" double precision,\"email_unique_nillable\" character varying(255),\"enum_unique_nillable\" character varying(255),\"file_unique_nillable\" integer,\"html_unique_nillable\" text,\"hyperlink_unique_nillable\" character varying(255),\"int_unique_nillable\" integer,\"long_unique_nillable\" bigint,\"script_unique_nillable\" text,\"string_unique_nillable\" character varying(255),\"text_unique_nillable\" text,\"xref_unique_nillable\" integer,CONSTRAINT \"entityTypeId#c34894ba_id_pkey\" PRIMARY KEY (\"id\"),CONSTRAINT \"entityTypeId#c34894ba_categorical_fkey\" FOREIGN KEY (\"categorical\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_enum_chk\" CHECK (\"enum\" IN ('enum0','enum1')),CONSTRAINT \"entityTypeId#c34894ba_file_fkey\" FOREIGN KEY (\"file\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_xref_fkey\" FOREIGN KEY (\"xref\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_categorical_nillable_fkey\" FOREIGN KEY (\"categorical_nillable\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_enum_nillable_chk\" CHECK (\"enum_nillable\" IN ('enum0','enum1')),CONSTRAINT \"entityTypeId#c34894ba_file_nillable_fkey\" FOREIGN KEY (\"file_nillable\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_xref_nillable_fkey\" FOREIGN KEY (\"xref_nillable\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_bool_unique_key\" UNIQUE (\"bool_unique\"),CONSTRAINT \"entityTypeId#c34894ba_categorical_unique_fkey\" FOREIGN KEY (\"categorical_unique\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_categorical_unique_key\" UNIQUE (\"categorical_unique\"),CONSTRAINT \"entityTypeId#c34894ba_date_unique_key\" UNIQUE (\"date_unique\"),CONSTRAINT \"entityTypeId#c34894ba_date_time_unique_key\" UNIQUE (\"date_time_unique\"),CONSTRAINT \"entityTypeId#c34894ba_decimal_unique_key\" UNIQUE (\"decimal_unique\"),CONSTRAINT \"entityTypeId#c34894ba_email_unique_key\" UNIQUE (\"email_unique\"),CONSTRAINT \"entityTypeId#c34894ba_enum_unique_key\" UNIQUE (\"enum_unique\"),CONSTRAINT \"entityTypeId#c34894ba_enum_unique_chk\" CHECK (\"enum_unique\" IN ('enum0','enum1')),CONSTRAINT \"entityTypeId#c34894ba_file_unique_fkey\" FOREIGN KEY (\"file_unique\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_file_unique_key\" UNIQUE (\"file_unique\"),CONSTRAINT \"entityTypeId#c34894ba_html_unique_key\" UNIQUE (\"html_unique\"),CONSTRAINT \"entityTypeId#c34894ba_hyperlink_unique_key\" UNIQUE (\"hyperlink_unique\"),CONSTRAINT \"entityTypeId#c34894ba_int_unique_key\" UNIQUE (\"int_unique\"),CONSTRAINT \"entityTypeId#c34894ba_long_unique_key\" UNIQUE (\"long_unique\"),CONSTRAINT \"entityTypeId#c34894ba_script_unique_key\" UNIQUE (\"script_unique\"),CONSTRAINT \"entityTypeId#c34894ba_string_unique_key\" UNIQUE (\"string_unique\"),CONSTRAINT \"entityTypeId#c34894ba_text_unique_key\" UNIQUE (\"text_unique\"),CONSTRAINT \"entityTypeId#c34894ba_xref_unique_fkey\" FOREIGN KEY (\"xref_unique\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_xref_unique_key\" UNIQUE (\"xref_unique\"),CONSTRAINT \"entityTypeId#c34894ba_bool_unique_nillable_key\" UNIQUE (\"bool_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_categorical_unique_nillable_fkey\" FOREIGN KEY (\"categorical_unique_nillable\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_categorical_unique_nillable_key\" UNIQUE (\"categorical_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_date_unique_nillable_key\" UNIQUE (\"date_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_date_time_unique_nillable_key\" UNIQUE (\"date_time_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_decimal_unique_nillable_key\" UNIQUE (\"decimal_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_email_unique_nillable_key\" UNIQUE (\"email_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_enum_unique_nillable_key\" UNIQUE (\"enum_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_enum_unique_nillable_chk\" CHECK (\"enum_unique_nillable\" IN ('enum0','enum1')),CONSTRAINT \"entityTypeId#c34894ba_file_unique_nillable_fkey\" FOREIGN KEY (\"file_unique_nillable\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_file_unique_nillable_key\" UNIQUE (\"file_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_html_unique_nillable_key\" UNIQUE (\"html_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_hyperlink_unique_nillable_key\" UNIQUE (\"hyperlink_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_int_unique_nillable_key\" UNIQUE (\"int_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_long_unique_nillable_key\" UNIQUE (\"long_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_script_unique_nillable_key\" UNIQUE (\"script_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_string_unique_nillable_key\" UNIQUE (\"string_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_text_unique_nillable_key\" UNIQUE (\"text_unique_nillable\"),CONSTRAINT \"entityTypeId#c34894ba_xref_unique_nillable_fkey\" FOREIGN KEY (\"xref_unique_nillable\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\") DEFERRABLE INITIALLY DEFERRED,CONSTRAINT \"entityTypeId#c34894ba_xref_unique_nillable_key\" UNIQUE (\"xref_unique_nillable\"))", PostgreSqlQueryGenerator.getSqlCreateTable(entityType)); }
static String getSqlCreateTable(EntityType entityType) { List<Attribute> persistedTableAttrs = getTableAttributes(entityType).collect(toList()); StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getTableName(entityType)).append('('); for (Iterator<Attribute> it = persistedTableAttrs.iterator(); it.hasNext(); ) { Attribute attr = it.next(); sql.append(getSqlColumn(entityType, attr, ColumnMode.EXCLUDE_DEFAULT_CONSTRAINT)); if (it.hasNext()) { sql.append(','); } } for (Attribute persistedTableAttr : persistedTableAttrs) { List<String> sqlTableConstraints = getSqlTableConstraints(entityType, persistedTableAttr); if (!sqlTableConstraints.isEmpty()) { sqlTableConstraints.forEach( sqlTableConstraint -> sql.append(',').append(sqlTableConstraint)); } } sql.append(')'); return sql.toString(); }
PostgreSqlQueryGenerator { static String getSqlCreateTable(EntityType entityType) { List<Attribute> persistedTableAttrs = getTableAttributes(entityType).collect(toList()); StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getTableName(entityType)).append('('); for (Iterator<Attribute> it = persistedTableAttrs.iterator(); it.hasNext(); ) { Attribute attr = it.next(); sql.append(getSqlColumn(entityType, attr, ColumnMode.EXCLUDE_DEFAULT_CONSTRAINT)); if (it.hasNext()) { sql.append(','); } } for (Attribute persistedTableAttr : persistedTableAttrs) { List<String> sqlTableConstraints = getSqlTableConstraints(entityType, persistedTableAttr); if (!sqlTableConstraints.isEmpty()) { sqlTableConstraints.forEach( sqlTableConstraint -> sql.append(',').append(sqlTableConstraint)); } } sql.append(')'); return sql.toString(); } }
PostgreSqlQueryGenerator { static String getSqlCreateTable(EntityType entityType) { List<Attribute> persistedTableAttrs = getTableAttributes(entityType).collect(toList()); StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getTableName(entityType)).append('('); for (Iterator<Attribute> it = persistedTableAttrs.iterator(); it.hasNext(); ) { Attribute attr = it.next(); sql.append(getSqlColumn(entityType, attr, ColumnMode.EXCLUDE_DEFAULT_CONSTRAINT)); if (it.hasNext()) { sql.append(','); } } for (Attribute persistedTableAttr : persistedTableAttrs) { List<String> sqlTableConstraints = getSqlTableConstraints(entityType, persistedTableAttr); if (!sqlTableConstraints.isEmpty()) { sqlTableConstraints.forEach( sqlTableConstraint -> sql.append(',').append(sqlTableConstraint)); } } sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { static String getSqlCreateTable(EntityType entityType) { List<Attribute> persistedTableAttrs = getTableAttributes(entityType).collect(toList()); StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getTableName(entityType)).append('('); for (Iterator<Attribute> it = persistedTableAttrs.iterator(); it.hasNext(); ) { Attribute attr = it.next(); sql.append(getSqlColumn(entityType, attr, ColumnMode.EXCLUDE_DEFAULT_CONSTRAINT)); if (it.hasNext()) { sql.append(','); } } for (Attribute persistedTableAttr : persistedTableAttrs) { List<String> sqlTableConstraints = getSqlTableConstraints(entityType, persistedTableAttr); if (!sqlTableConstraints.isEmpty()) { sqlTableConstraints.forEach( sqlTableConstraint -> sql.append(',').append(sqlTableConstraint)); } } sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { static String getSqlCreateTable(EntityType entityType) { List<Attribute> persistedTableAttrs = getTableAttributes(entityType).collect(toList()); StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getTableName(entityType)).append('('); for (Iterator<Attribute> it = persistedTableAttrs.iterator(); it.hasNext(); ) { Attribute attr = it.next(); sql.append(getSqlColumn(entityType, attr, ColumnMode.EXCLUDE_DEFAULT_CONSTRAINT)); if (it.hasNext()) { sql.append(','); } } for (Attribute persistedTableAttr : persistedTableAttrs) { List<String> sqlTableConstraints = getSqlTableConstraints(entityType, persistedTableAttr); if (!sqlTableConstraints.isEmpty()) { sqlTableConstraints.forEach( sqlTableConstraint -> sql.append(',').append(sqlTableConstraint)); } } sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator();   }