From 2e91fb9e8114d22bf4afabd25bf4622cc49b17a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6nke=20Hahn?= <soenkehahn@gmail.com>
Date: Wed, 22 Jan 2014 15:54:14 +0800
Subject: [PATCH] Allow qualified names ('schema.table') in from clauses

---
 Language/SQL/SimpleSQL/Parser.lhs          | 1 +
 Language/SQL/SimpleSQL/Pretty.lhs          | 1 +
 Language/SQL/SimpleSQL/Syntax.lhs          | 2 ++
 tools/Language/SQL/SimpleSQL/TableRefs.lhs | 3 +++
 4 files changed, 7 insertions(+)

diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs
index eabb28a..06f6bef 100644
--- a/Language/SQL/SimpleSQL/Parser.lhs
+++ b/Language/SQL/SimpleSQL/Parser.lhs
@@ -601,6 +601,7 @@ tref
 >                                          *> nonJoinTref)
 >                          ,try (TRFunction <$> name
 >                                           <*> parens (commaSep valueExpr))
+>                          ,try (TRQualified <$> name <*> (char '.' >> name))
 >                          ,TRSimple <$> name]
 >                   >>= optionSuffix aliasSuffix
 >     aliasSuffix j = option j (TRAlias j <$> alias)
diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs
index 85d5f5f..ceb9b1c 100644
--- a/Language/SQL/SimpleSQL/Pretty.lhs
+++ b/Language/SQL/SimpleSQL/Pretty.lhs
@@ -229,6 +229,7 @@ which have been changed to try to improve the layout of the output.
 >         ,nest 5 $ vcat $ punctuate comma $ map tr ts]
 >   where
 >     tr (TRSimple t) = name t
+>     tr (TRQualified s t) = name s <> text "." <> name t
 >     tr (TRLateral t) = text "lateral" <+> tr t
 >     tr (TRFunction f as) =
 >         name f <> parens (commaSep $ map valueExpr as)
diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs
index 0015c71..70f88d1 100644
--- a/Language/SQL/SimpleSQL/Syntax.lhs
+++ b/Language/SQL/SimpleSQL/Syntax.lhs
@@ -282,6 +282,8 @@ I'm not sure if this is valid syntax or not.
 > -- | Represents a entry in the csv of tables in the from clause.
 > data TableRef = -- | from t
 >                 TRSimple Name
+>                 -- | from s.t
+>               | TRQualified Name Name
 >                 -- | from a join b
 >               | TRJoin TableRef JoinType TableRef (Maybe JoinCondition)
 >                 -- | from (a)
diff --git a/tools/Language/SQL/SimpleSQL/TableRefs.lhs b/tools/Language/SQL/SimpleSQL/TableRefs.lhs
index d2357d6..c75212e 100644
--- a/tools/Language/SQL/SimpleSQL/TableRefs.lhs
+++ b/tools/Language/SQL/SimpleSQL/TableRefs.lhs
@@ -20,6 +20,9 @@ expression
 >     ,("select a from t,u"
 >      ,ms [TRSimple "t", TRSimple "u"])
 
+>     ,("select a from s.t"
+>      ,ms [TRQualified "s" "t"])
+
 these lateral queries make no sense but the syntax is valid
 
 >     ,("select a from lateral a"