use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class PostgreSqlQueryGenerator method getSqlCreateJunctionTable.
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");
// for self-referencing data defer checking constraints until the end of the transaction
if (attr.getRefEntity().getId().equals(entityType.getId())) {
sql.append(" 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(")");
// for self-referencing data defer checking constraints until the end of the transaction
if (attr.getRefEntity().getId().equals(entityType.getId())) {
sql.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 RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
}
sql.append(", UNIQUE (").append(getJunctionTableOrderColumnName()).append(',').append(getColumnName(idAttr)).append(')');
sql.append(')');
return sql.toString();
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class PostgreSqlQueryGenerator method getSqlDefaulValue.
private static String getSqlDefaulValue(Attribute attribute, String defaultValueAsString) {
String sqlDefaultValue;
Object defaultTypedValue = AttributeUtils.getDefaultTypedValue(attribute, defaultValueAsString);
AttributeType attributeType = attribute.getDataType();
switch(attributeType) {
case BOOL:
Boolean booleanDefaultValue = (Boolean) defaultTypedValue;
sqlDefaultValue = booleanDefaultValue ? "TRUE" : "FALSE";
break;
case CATEGORICAL:
case FILE:
case XREF:
Entity refDefaultValue = (Entity) defaultTypedValue;
sqlDefaultValue = getSqlDefaulValue(attribute.getRefEntity().getIdAttribute(), refDefaultValue.getIdValue().toString());
break;
case DATE:
LocalDate dateDefaultValue = (LocalDate) defaultTypedValue;
sqlDefaultValue = '\'' + dateDefaultValue.toString() + '\'';
break;
case DATE_TIME:
Instant instantDefaultValue = (Instant) defaultTypedValue;
// As a workaround for #5674, we don't store milliseconds
sqlDefaultValue = '\'' + instantDefaultValue.truncatedTo(ChronoUnit.SECONDS).atOffset(UTC).toString() + '\'';
break;
case DECIMAL:
Double doubleDefaultValue = (Double) defaultTypedValue;
sqlDefaultValue = doubleDefaultValue.toString();
break;
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
sqlDefaultValue = '\'' + (String) defaultTypedValue + '\'';
break;
case INT:
Integer intDefaultValue = (Integer) defaultTypedValue;
sqlDefaultValue = intDefaultValue.toString();
break;
case LONG:
Long longDefaultValue = (Long) defaultTypedValue;
sqlDefaultValue = longDefaultValue.toString();
break;
case CATEGORICAL_MREF:
case COMPOUND:
case MREF:
case ONE_TO_MANY:
throw new RuntimeException(format("Illegal attribute type [%s]", attributeType.toString()));
default:
throw new UnexpectedEnumException(attributeType);
}
return sqlDefaultValue;
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class PostgreSqlUtils method getPostgreSqlValue.
/**
* Returns the PostgreSQL value for the given entity attribute
*
* @param entity entity
* @param attr attribute
* @return PostgreSQL value
*/
static Object getPostgreSqlValue(Entity entity, Attribute attr) {
String attrName = attr.getName();
AttributeType attrType = attr.getDataType();
switch(attrType) {
case BOOL:
return entity.getBoolean(attrName);
case CATEGORICAL:
case XREF:
Entity xrefEntity = entity.getEntity(attrName);
return xrefEntity != null ? getPostgreSqlValue(xrefEntity, xrefEntity.getEntityType().getIdAttribute()) : null;
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
Iterable<Entity> entities = entity.getEntities(attrName);
return stream(entities.spliterator(), false).map(mrefEntity -> getPostgreSqlValue(mrefEntity, mrefEntity.getEntityType().getIdAttribute())).collect(toList());
case DATE:
return entity.getLocalDate(attrName);
case DATE_TIME:
// As a workaround for #5674, we don't store milliseconds
Instant instant = entity.getInstant(attrName);
return instant != null ? instant.truncatedTo(ChronoUnit.SECONDS).atOffset(UTC) : null;
case DECIMAL:
return entity.getDouble(attrName);
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
return entity.getString(attrName);
case FILE:
FileMeta fileEntity = entity.getEntity(attrName, FileMeta.class);
return fileEntity != null ? getPostgreSqlValue(fileEntity, fileEntity.getEntityType().getIdAttribute()) : null;
case INT:
return entity.getInt(attrName);
case LONG:
return entity.getLong(attrName);
case COMPOUND:
throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
default:
throw new UnexpectedEnumException(attrType);
}
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class PostgreSqlQueryGeneratorTest method getSqlCreateTable.
@Test
public void getSqlCreateTable() {
// ref entity with string id attribute
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);
// ref entity with int id attribute
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);
// entity with attributes of all types and flavors
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(PostgreSqlQueryGenerator.getSqlCreateTable(entityType), "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\"),CONSTRAINT \"entityTypeId#c34894ba_enum_chk\" CHECK (\"enum\" IN ('enum0','enum1')),CONSTRAINT \"entityTypeId#c34894ba_file_fkey\" FOREIGN KEY (\"file\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\"),CONSTRAINT \"entityTypeId#c34894ba_xref_fkey\" FOREIGN KEY (\"xref\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\"),CONSTRAINT \"entityTypeId#c34894ba_categorical_nillable_fkey\" FOREIGN KEY (\"categorical_nillable\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\"),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\"),CONSTRAINT \"entityTypeId#c34894ba_xref_nillable_fkey\" FOREIGN KEY (\"xref_nillable\") REFERENCES \"refEntityInt#78255ee1\"(\"refIdAttrInt\"),CONSTRAINT \"entityTypeId#c34894ba_bool_unique_key\" UNIQUE (\"bool_unique\"),CONSTRAINT \"entityTypeId#c34894ba_categorical_unique_fkey\" FOREIGN KEY (\"categorical_unique\") REFERENCES \"refEntityStr#305ca1a9\"(\"refIdAttrStr\"),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\"),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\"),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\"),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\"),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\"),CONSTRAINT \"entityTypeId#c34894ba_xref_unique_nillable_key\" UNIQUE (\"xref_unique_nillable\"))");
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class PostgreSqlQueryUtilsTest method getPersistedAttributesProvider.
@DataProvider(name = "getPersistedAttributesProvider")
public static Iterator<Object[]> getPersistedAttributesProvider() {
List<Object[]> dataList = newArrayList();
for (AttributeType attrType : AttributeType.values()) {
Attribute attr = mock(Attribute.class);
when(attr.getDataType()).thenReturn(attrType);
when(attr.toString()).thenReturn("attr_" + attrType.toString());
dataList.add(new Object[] { attr, singletonList(attr) });
Attribute attrWithExpression = mock(Attribute.class);
when(attrWithExpression.getDataType()).thenReturn(attrType);
when(attrWithExpression.getExpression()).thenReturn("expression");
when(attrWithExpression.toString()).thenReturn("attrWithExpression_" + attrType.toString());
dataList.add(new Object[] { attrWithExpression, emptyList() });
}
return dataList.iterator();
}
Aggregations