@Test void getSqlCreateFunctionValidateUpdate() { 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"); Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn("attr0").getMock(); when(attr0.getIdentifier()).thenReturn("attr0Id"); Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn("attr1").getMock(); when(attr1.getIdentifier()).thenReturn("attr1Id"); when(entityType.getIdAttribute()).thenReturn(idAttr); String expectedSql = "CREATE FUNCTION \"validate_update_entityTypeId#c34894ba\"() RETURNS TRIGGER AS $$\n" + "BEGIN\n" + " IF OLD.\"attr0\" <> NEW.\"attr0\" THEN\n" + " RAISE EXCEPTION 'Updating read-only column \"attr0\" of table \"entityTypeId#c34894ba\" with id [%] is not allowed', OLD.\"idAttr\" USING ERRCODE = '23506';\n" + " END IF;\n" + " IF OLD.\"attr1\" <> NEW.\"attr1\" THEN\n" + " RAISE EXCEPTION 'Updating read-only column \"attr1\" of table \"entityTypeId#c34894ba\" with id [%] is not allowed', OLD.\"idAttr\" USING ERRCODE = '23506';\n" + " END IF;\n" + " RETURN NEW;\n" + "END;\n" + "$$ LANGUAGE plpgsql;"; assertEquals( expectedSql, PostgreSqlQueryGenerator.getSqlCreateFunctionValidateUpdate( entityType, asList(attr0, attr1))); }
static String getSqlCreateFunctionValidateUpdate( EntityType entityType, Collection<Attribute> readonlyTableAttrs) { StringBuilder strBuilder = new StringBuilder(512) .append("CREATE FUNCTION ") .append(getFunctionValidateUpdateName(entityType)) .append("() RETURNS TRIGGER AS $$\nBEGIN\n"); String tableName = getTableName(entityType); String idColName = getColumnName(entityType.getIdAttribute()); readonlyTableAttrs.forEach( attr -> { String colName = getColumnName(attr); strBuilder .append(" IF OLD.") .append(colName) .append(" <> NEW.") .append(colName) .append(" THEN\n"); strBuilder .append(" RAISE EXCEPTION 'Updating read-only column ") .append(colName) .append(" of table ") .append(tableName) .append(" with id [%] is not allowed', OLD.") .append(idColName) .append(" USING ERRCODE = '") .append(ERR_CODE_READONLY_VIOLATION) .append("';\n"); strBuilder.append(" END IF;\n"); }); strBuilder.append(" RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;"); return strBuilder.toString(); }
PostgreSqlQueryGenerator { static String getSqlCreateFunctionValidateUpdate( EntityType entityType, Collection<Attribute> readonlyTableAttrs) { StringBuilder strBuilder = new StringBuilder(512) .append("CREATE FUNCTION ") .append(getFunctionValidateUpdateName(entityType)) .append("() RETURNS TRIGGER AS $$\nBEGIN\n"); String tableName = getTableName(entityType); String idColName = getColumnName(entityType.getIdAttribute()); readonlyTableAttrs.forEach( attr -> { String colName = getColumnName(attr); strBuilder .append(" IF OLD.") .append(colName) .append(" <> NEW.") .append(colName) .append(" THEN\n"); strBuilder .append(" RAISE EXCEPTION 'Updating read-only column ") .append(colName) .append(" of table ") .append(tableName) .append(" with id [%] is not allowed', OLD.") .append(idColName) .append(" USING ERRCODE = '") .append(ERR_CODE_READONLY_VIOLATION) .append("';\n"); strBuilder.append(" END IF;\n"); }); strBuilder.append(" RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;"); return strBuilder.toString(); } }
PostgreSqlQueryGenerator { static String getSqlCreateFunctionValidateUpdate( EntityType entityType, Collection<Attribute> readonlyTableAttrs) { StringBuilder strBuilder = new StringBuilder(512) .append("CREATE FUNCTION ") .append(getFunctionValidateUpdateName(entityType)) .append("() RETURNS TRIGGER AS $$\nBEGIN\n"); String tableName = getTableName(entityType); String idColName = getColumnName(entityType.getIdAttribute()); readonlyTableAttrs.forEach( attr -> { String colName = getColumnName(attr); strBuilder .append(" IF OLD.") .append(colName) .append(" <> NEW.") .append(colName) .append(" THEN\n"); strBuilder .append(" RAISE EXCEPTION 'Updating read-only column ") .append(colName) .append(" of table ") .append(tableName) .append(" with id [%] is not allowed', OLD.") .append(idColName) .append(" USING ERRCODE = '") .append(ERR_CODE_READONLY_VIOLATION) .append("';\n"); strBuilder.append(" END IF;\n"); }); strBuilder.append(" RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;"); return strBuilder.toString(); } private  PostgreSqlQueryGenerator(); }
PostgreSqlQueryGenerator { static String getSqlCreateFunctionValidateUpdate( EntityType entityType, Collection<Attribute> readonlyTableAttrs) { StringBuilder strBuilder = new StringBuilder(512) .append("CREATE FUNCTION ") .append(getFunctionValidateUpdateName(entityType)) .append("() RETURNS TRIGGER AS $$\nBEGIN\n"); String tableName = getTableName(entityType); String idColName = getColumnName(entityType.getIdAttribute()); readonlyTableAttrs.forEach( attr -> { String colName = getColumnName(attr); strBuilder .append(" IF OLD.") .append(colName) .append(" <> NEW.") .append(colName) .append(" THEN\n"); strBuilder .append(" RAISE EXCEPTION 'Updating read-only column ") .append(colName) .append(" of table ") .append(tableName) .append(" with id [%] is not allowed', OLD.") .append(idColName) .append(" USING ERRCODE = '") .append(ERR_CODE_READONLY_VIOLATION) .append("';\n"); strBuilder.append(" END IF;\n"); }); strBuilder.append(" RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;"); return strBuilder.toString(); } private  PostgreSqlQueryGenerator();  }
PostgreSqlQueryGenerator { static String getSqlCreateFunctionValidateUpdate( EntityType entityType, Collection<Attribute> readonlyTableAttrs) { StringBuilder strBuilder = new StringBuilder(512) .append("CREATE FUNCTION ") .append(getFunctionValidateUpdateName(entityType)) .append("() RETURNS TRIGGER AS $$\nBEGIN\n"); String tableName = getTableName(entityType); String idColName = getColumnName(entityType.getIdAttribute()); readonlyTableAttrs.forEach( attr -> { String colName = getColumnName(attr); strBuilder .append(" IF OLD.") .append(colName) .append(" <> NEW.") .append(colName) .append(" THEN\n"); strBuilder .append(" RAISE EXCEPTION 'Updating read-only column ") .append(colName) .append(" of table ") .append(tableName) .append(" with id [%] is not allowed', OLD.") .append(idColName) .append(" USING ERRCODE = '") .append(ERR_CODE_READONLY_VIOLATION) .append("';\n"); strBuilder.append(" END IF;\n"); }); strBuilder.append(" RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;"); return strBuilder.toString(); } private  PostgreSqlQueryGenerator();   }