A suggestion:

I think we should start with the format the metadata is available in. When it comes to query parsing then, you can feed the parser with mock metadata and see if it generates the appropriate SQL.

I used a ClassDescriptor class including FieldDescriptors and ReferenceDescriptors. And a MetadataHandler to retrieve the info like this:
PHP Code: