SPARQL Construct queries against optional data

I'd like to automatically generate a CONSTRUCT query to pull out, say, songs from a set of music. (More generally, I want to pull out hierarchical data given a known data model.)

For example, let's say I know that a song has a title, and it may, optionally, have a composer. Composers, in turn, have names and optional birthYears. If none of the fields were optional, I'd just do:

CONSTRUCT {
 ?song dcterms:title ?title.
 ?song music:hasComposer ?composer.
 ?composer foaf:name ?cname.
 ?composer music:birthYear ?y.
}
WHERE {
 ?song dcterms:title ?title.
 ?song music:hasComposer ?composer.
 ?composer foaf:name ?cname.
 ?composer music:birthYear ?y.
}

-- but since the composer is optional, I need to restrict the composer-related lines of my WHERE clause. But once I do that, I can't CONSTRUCT them.

My problem is actually bigger: there are lots of types, in a hierarchical relation, with known properties and optional properties. and I want to automatically generate CONSTRUCT queries for arbitrary types. Any thoughts on a good strategy for doing this? Any clarification I can add?

Thanks!

-B

The query:

CONSTRUCT {
   ?song dcterms:title ?title.
   ?song music:hasComposer ?composer.
   ?composer foaf:name ?cname.
   ?composer music:birthYear ?y.
}
WHERE {
   ?song dcterms:title ?title.
   OPTIONAL {
      ?song music:hasComposer ?composer.
      ?composer foaf:name ?cname.
      ?composer music:birthYear ?y.
   }
}

Should just work. The SPARQL 1.0 Recommendation says:

If any such instantiation produces a triple containing an unbound variable or an illegal RDF construct, such as a literal in subject or predicate position, then that triple is not included in the output RDF graph.

So, for songs where you have no composer, ?composer is an unbound variable, so the triples that include ?composer just get ignored.