The Artist-CD-Track sample

The original Artist-CD-Track sample can be found here:  http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Example.pod.

The Muldis-D translation can be found here: http://muldis.com/CD.html.

The Andl implementation is below. Note that they are not strictly comparable because:

  1. Andl provides no persistence. The results are only in memory.
  2. Andl provides limited output formatting.
artist  := {{ artistid:=0, name:='' }} [false]
cd      := {{ cdid:=0, artistid:=0, title:='', year:=0 }} [false]
track   := {{ trackid:=0, cdid:= 0, title:='' }} [false]

artist_data := {
  { name := 'Michael Jackson'}, 
  { name := 'Eminem' }
} [{ *artistid := ord() }]
artist := artist union artist_data

cd_data := {
  { title := 'Thriller',                name := 'Michael Jackson' },
  { title := 'Bad',                     name := 'Michael Jackson' },
  { title := 'The Marshall Mathers LP', name := 'Eminem' }
} [{ *cdid := ord() }]
cd := cd union (cd_data join artist)[{ title, cdid, artistid, year:=0}]

track_data := {
   { title := 'Beat It'         , cd := 'Thriller' },
   { title := 'Billie Jean'     , cd := 'Thriller' },
   { title := 'Dirty Diana'     , cd := 'Bad' },
   { title := 'Smooth Criminal' , cd := 'Bad' },
   { title := 'Leave Me Alone'  , cd := 'Bad' },
   { title := 'Stan'            , cd := 'The Marshall Mathers LP' },
   { title := 'The Way I Am'    , cd := 'The Marshall Mathers LP' }
 } [{ *trackid := ord() }]
track := track union (track_data join cd[{ *cd := title }]) [{ trackid, title, cdid }]

get_tracks_by_cd(t) => cd[ title = t {*title} ] join track
get_tracks_by_artist(a) => (artist[ name = a {*name}] join cd) [{cdid}] join track
get_cd_by_track(t) => track [ title = t {cdid} ] join cd
get_cds_by_artist(a) => artist [name = a {artistid} ] join cd
get_artist_by_track(t) => (track [title = t { cdid }] join cd) [{artistid}] join artist
get_artist_by_cd(t) => (cd [title = t { cdid }] join cd) [{artistid}] join artist

get_tracks_by_cd('Bad') [{ i'Track title' := title }]
get_tracks_by_artist('Michael Jackson') [{ i'Track title' := title }]
get_cd_by_track('Stan') [{ i'CD title' := title }]
get_cds_by_artist('Michael Jackson') [{ i'CD title' := title }]
get_artist_by_track('Dirty Diana') [{ i'Artist name' := name }]
get_artist_by_cd('The Marshall Mathers LP') [{ i'Artist name' := name }]

The output looks like this.

Track title
---------------
Dirty Diana
Smooth Criminal
Leave Me Alone

Track title
---------------
Beat It
Billie Jean
Dirty Diana
Smooth Criminal
Leave Me Alone

CD title
-----------------------
The Marshall Mathers LP

CD title
----------
Thriller
Bad

Artist name
---------------
Michael Jackson

Artist name
-----------
Eminem

Leave a Comment

Filed under Code sample

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.