@Test void getSqlCreateJunctionTable() { Attribute refIdAttr = when(mock(Attribute.class).getName()).thenReturn("refIdAttr").getMock(); when(refIdAttr.getIdentifier()).thenReturn("refIdAttrId"); when(refIdAttr.getDataType()).thenReturn(STRING); EntityType refEntityType = when(mock(EntityType.class).getId()).thenReturn("refEntity").getMock(); when(refEntityType.getId()).thenReturn("refEntityId"); when(refEntityType.getIdAttribute()).thenReturn(refIdAttr); EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("entity").getMock(); when(entityType.getId()).thenReturn("entityTypeId"); Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("idAttr").getMock(); when(idAttr.getIdentifier()).thenReturn("idAttrId"); when(idAttr.getDataType()).thenReturn(STRING); when(entityType.getIdAttribute()).thenReturn(idAttr); Attribute attr = when(mock(Attribute.class).getName()).thenReturn("attr").getMock(); when(attr.getIdentifier()).thenReturn("attrId"); when(attr.getDataType()).thenReturn(MREF); when(attr.getRefEntity()).thenReturn(refEntityType); String expectedSql = "CREATE TABLE \"entityTypeId#c34894ba_attr\" (\"order\" INT,\"idAttr\" character varying(255) NOT NULL, \"attr\" character varying(255) NOT NULL, FOREIGN KEY (\"idAttr\") REFERENCES \"entityTypeId#c34894ba\"(\"idAttr\") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, FOREIGN KEY (\"attr\") REFERENCES \"refEntityId#07f902bf\"(\"refIdAttr\") DEFERRABLE INITIALLY DEFERRED, UNIQUE (\"idAttr\",\"attr\"), UNIQUE (\"order\",\"idAttr\"))"; assertEquals(expectedSql, PostgreSqlQueryGenerator.getSqlCreateJunctionTable(entityType, attr)); }
static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) { Attribute idAttr = entityType.getIdAttribute(); StringBuilder sql = new StringBuilder("CREATE TABLE ") .append(getJunctionTableName(entityType, attr)) .append(" (") .append(getJunctionTableOrderColumnName()) .append(" INT,") .append(getColumnName(idAttr)) .append(' ') .append(getPostgreSqlType(idAttr)) .append(" NOT NULL, ") .append(getColumnName(attr)) .append(' ') .append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())) .append(" NOT NULL") .append(", FOREIGN KEY (") .append(getColumnName(idAttr)) .append(") REFERENCES ") .append(getTableName(entityType)) .append('(') .append(getColumnName(idAttr)) .append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"); if (isPersistedInPostgreSql(attr.getRefEntity())) { sql.append(", FOREIGN KEY (") .append(getColumnName(attr)) .append(") REFERENCES ") .append(getTableName(attr.getRefEntity())) .append('(') .append(getColumnName(attr.getRefEntity().getIdAttribute())) .append(") DEFERRABLE INITIALLY DEFERRED"); } AttributeType attrType = attr.getDataType(); switch (attrType) { case CATEGORICAL_MREF: case MREF: sql.append(", UNIQUE (") .append(getColumnName(idAttr)) .append(',') .append(getColumnName(attr)) .append(')'); break; default: throw new IllegalAttributeTypeException(attrType); } sql.append(", UNIQUE (") .append(getJunctionTableOrderColumnName()) .append(',') .append(getColumnName(idAttr)) .append(')'); sql.append(')'); return sql.toString(); }
PostgreSqlQueryGenerator { static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) { Attribute idAttr = entityType.getIdAttribute(); StringBuilder sql = new StringBuilder("CREATE TABLE ") .append(getJunctionTableName(entityType, attr)) .append(" (") .append(getJunctionTableOrderColumnName()) .append(" INT,") .append(getColumnName(idAttr)) .append(' ') .append(getPostgreSqlType(idAttr)) .append(" NOT NULL, ") .append(getColumnName(attr)) .append(' ') .append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())) .append(" NOT NULL") .append(", FOREIGN KEY (") .append(getColumnName(idAttr)) .append(") REFERENCES ") .append(getTableName(entityType)) .append('(') .append(getColumnName(idAttr)) .append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"); if (isPersistedInPostgreSql(attr.getRefEntity())) { sql.append(", FOREIGN KEY (") .append(getColumnName(attr)) .append(") REFERENCES ") .append(getTableName(attr.getRefEntity())) .append('(') .append(getColumnName(attr.getRefEntity().getIdAttribute())) .append(") DEFERRABLE INITIALLY DEFERRED"); } AttributeType attrType = attr.getDataType(); switch (attrType) { case CATEGORICAL_MREF: case MREF: sql.append(", UNIQUE (") .append(getColumnName(idAttr)) .append(',') .append(getColumnName(attr)) .append(')'); break; default: throw new IllegalAttributeTypeException(attrType); } sql.append(", UNIQUE (") .append(getJunctionTableOrderColumnName()) .append(',') .append(getColumnName(idAttr)) .append(')'); sql.append(')'); return sql.toString(); } }
PostgreSqlQueryGenerator { static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) { Attribute idAttr = entityType.getIdAttribute(); StringBuilder sql = new StringBuilder("CREATE TABLE ") .append(getJunctionTableName(entityType, attr)) .append(" (") .append(getJunctionTableOrderColumnName()) .append(" INT,") .append(getColumnName(idAttr)) .append(' ') .append(getPostgreSqlType(idAttr)) .append(" NOT NULL, ") .append(getColumnName(attr)) .append(' ') .append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())) .append(" NOT NULL") .append(", FOREIGN KEY (") .append(getColumnName(idAttr)) .append(") REFERENCES ") .append(getTableName(entityType)) .append('(') .append(getColumnName(idAttr)) .append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"); if (isPersistedInPostgreSql(attr.getRefEntity())) { sql.append(", FOREIGN KEY (") .append(getColumnName(attr)) .append(") REFERENCES ") .append(getTableName(attr.getRefEntity())) .append('(') .append(getColumnName(attr.getRefEntity().getIdAttribute())) .append(") DEFERRABLE INITIALLY DEFERRED"); } AttributeType attrType = attr.getDataType(); switch (attrType) { case CATEGORICAL_MREF: case MREF: sql.append(", UNIQUE (") .append(getColumnName(idAttr)) .append(',') .append(getColumnName(attr)) .append(')'); break; default: throw new IllegalAttributeTypeException(attrType); } sql.append(", UNIQUE (") .append(getJunctionTableOrderColumnName()) .append(',') .append(getColumnName(idAttr)) .append(')'); sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) { Attribute idAttr = entityType.getIdAttribute(); StringBuilder sql = new StringBuilder("CREATE TABLE ") .append(getJunctionTableName(entityType, attr)) .append(" (") .append(getJunctionTableOrderColumnName()) .append(" INT,") .append(getColumnName(idAttr)) .append(' ') .append(getPostgreSqlType(idAttr)) .append(" NOT NULL, ") .append(getColumnName(attr)) .append(' ') .append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())) .append(" NOT NULL") .append(", FOREIGN KEY (") .append(getColumnName(idAttr)) .append(") REFERENCES ") .append(getTableName(entityType)) .append('(') .append(getColumnName(idAttr)) .append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"); if (isPersistedInPostgreSql(attr.getRefEntity())) { sql.append(", FOREIGN KEY (") .append(getColumnName(attr)) .append(") REFERENCES ") .append(getTableName(attr.getRefEntity())) .append('(') .append(getColumnName(attr.getRefEntity().getIdAttribute())) .append(") DEFERRABLE INITIALLY DEFERRED"); } AttributeType attrType = attr.getDataType(); switch (attrType) { case CATEGORICAL_MREF: case MREF: sql.append(", UNIQUE (") .append(getColumnName(idAttr)) .append(',') .append(getColumnName(attr)) .append(')'); break; default: throw new IllegalAttributeTypeException(attrType); } sql.append(", UNIQUE (") .append(getJunctionTableOrderColumnName()) .append(',') .append(getColumnName(idAttr)) .append(')'); sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) { Attribute idAttr = entityType.getIdAttribute(); StringBuilder sql = new StringBuilder("CREATE TABLE ") .append(getJunctionTableName(entityType, attr)) .append(" (") .append(getJunctionTableOrderColumnName()) .append(" INT,") .append(getColumnName(idAttr)) .append(' ') .append(getPostgreSqlType(idAttr)) .append(" NOT NULL, ") .append(getColumnName(attr)) .append(' ') .append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())) .append(" NOT NULL") .append(", FOREIGN KEY (") .append(getColumnName(idAttr)) .append(") REFERENCES ") .append(getTableName(entityType)) .append('(') .append(getColumnName(idAttr)) .append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"); if (isPersistedInPostgreSql(attr.getRefEntity())) { sql.append(", FOREIGN KEY (") .append(getColumnName(attr)) .append(") REFERENCES ") .append(getTableName(attr.getRefEntity())) .append('(') .append(getColumnName(attr.getRefEntity().getIdAttribute())) .append(") DEFERRABLE INITIALLY DEFERRED"); } AttributeType attrType = attr.getDataType(); switch (attrType) { case CATEGORICAL_MREF: case MREF: sql.append(", UNIQUE (") .append(getColumnName(idAttr)) .append(',') .append(getColumnName(attr)) .append(')'); break; default: throw new IllegalAttributeTypeException(attrType); } sql.append(", UNIQUE (") .append(getJunctionTableOrderColumnName()) .append(',') .append(getColumnName(idAttr)) .append(')'); sql.append(')'); return sql.toString(); } private  PostgreSqlQueryGenerator();   }