Advertisement
S0lll0s

Relational Algebra to SQL Subqueries

Mar 11th, 2015
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- NAME         PYTHON-NOTATION                             HASKELL-NOTATION
  2. -- selektion:   S[a.name != b.name]( ... )                  S String Rel
  3. -- projektion:  P[a.name, b.budget]( ... )                  P String Rel
  4. -- join:        J[a.budget = b.budget, proj]( RELA, RELB )  J String String Rel Rel
  5. -- rename:      R[budget->geld, name->zeug]( ... )          R [(String,String)] Rel
  6. -- from:        F[a]( ... )                                 F String Rel
  7. --              T[""]()                                     T String
  8. --
  9. -- Beispiel: (P "a.name,a.einnahmen,b.alter,CONCAT(b.vname, b.nname)" (J "a.angestellter = b.id" "a.name, a.einnahmen, b.alter, b.vname, b.nname" (T "a") (T "b")))
  10.  
  11. data Rel = T String | F String Rel | R [(String,String)] Rel | J String String Rel Rel | P String Rel | S String Rel deriving Show
  12.  
  13. render rel                  = "SELECT * FROM " ++ (render' rel)
  14.  
  15. render' (T a)               = a
  16. render' (F new rel)         = (render' rel) ++ " AS " ++ new
  17. render' (R a rel)           = "(SELECT " ++ (rename a) ++ " FROM " ++ (render' rel) ++ ")"
  18. render' (J cond sel a b)    = "(SELECT " ++ sel ++ " FROM " ++ (render' a) ++ ", " ++ (render' b) ++ " WHERE " ++ cond ++ ")"
  19. render' (P sel rel)         = "(SELECT " ++ sel ++ " FROM " ++ (render' rel) ++ ")"
  20. render' (S cond rel)        = "(SELECT * FROM " ++ (render' rel) ++ " WHERE " ++ cond ++ ")"
  21.  
  22. rename []           = ""
  23. rename ((o,n):[])   = o ++ " AS " ++ n
  24. rename ((o,n):rs)   = o ++ " AS " ++ n ++ ", " ++ (rename rs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement