Fix placeholder examples.

This commit is contained in:
Kei Hibino 2014-12-25 09:42:21 +09:00
parent 6ed1cb5160
commit 24cb13a547
3 changed files with 51 additions and 42 deletions

View File

@ -137,6 +137,7 @@ personAndBirthdayL <span class="fu">=</span> relation <span class="fu">$</span>
agesOfFamilies <span class="fu">=</span> aggregateRelation <span class="fu">$</span> <span class="kw">do</span>
my <span class="ot">&lt;-</span> query myTable
gFam <span class="ot">&lt;-</span> groupBy <span class="fu">$</span> my <span class="fu">!</span> family' <span class="co">-- Specify grouping key</span>
return <span class="fu">$</span> gFam <span class="fu">&gt;&lt;</span> sum' (my <span class="fu">!</span> age') <span class="co">-- Aggregated results</span></code></pre>
<p>sums ages per family.</p>
<p>Generated SQL:</p>
@ -306,27 +307,31 @@ uncurryPB <span class="fu">=</span> <span class="dt">PersonAndBirthday</span> <
</div><div id="record-mapping---placeholders" class="slide section level2">
<h1>Record Mapping - Placeholders</h1>
<p>Placeholders can be mapped to a record:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">placeholder3 f <span class="fu">=</span>
placeholder (\p0 <span class="ot">-&gt;</span> placeholder
(\p1 <span class="ot">-&gt;</span> placeholder (\p2 <span class="ot">-&gt;</span> f p0 p1 p2)))
<span class="ot">personAndBirthdayP2 ::</span> <span class="dt">Relation</span> ((<span class="dt">String</span>, <span class="dt">Int32</span>), <span class="dt">String</span>)
<span class="dt">PersonAndBirthday</span>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">personAndBirthdayP2 ::</span> <span class="dt">Relation</span> <span class="dt">Person</span> <span class="dt">PersonAndBirthday</span>
personAndBirthdayP2 <span class="fu">=</span> relation' <span class="fu">$</span> <span class="kw">do</span>
p <span class="ot">&lt;-</span> query person
b <span class="ot">&lt;-</span> query birthday
(ph0, (ph1, (ph2, ()))) <span class="ot">&lt;-</span>
placeholder3 (\ph0' ph1' ph2' <span class="ot">-&gt;</span>
wheres <span class="fu">$</span>
(<span class="dt">Person</span> <span class="fu">|$|</span> p <span class="fu">!</span> Person.name'
<span class="fu">|*|</span> p <span class="fu">!</span> Person.age' <span class="fu">|*|</span> p <span class="fu">!</span> Person.address')
<span class="fu">.=.</span>
(<span class="dt">Person</span> <span class="fu">|$|</span> ph0' <span class="fu">|*|</span> ph1' <span class="fu">|*|</span> ph2') )
return <span class="fu">$</span> (ph0 <span class="fu">&gt;&lt;</span> ph1 <span class="fu">&gt;&lt;</span> ph2, <span class="dt">PersonAndBirthday</span> <span class="fu">|$|</span> p <span class="fu">|*|</span> b)</code></pre>
(ph0, ()) <span class="ot">&lt;-</span> placeholder (\ph0' <span class="ot">-&gt;</span> on <span class="fu">$</span> p <span class="fu">!</span> Person.name' <span class="fu">.=.</span> ph0')
(ph1, ()) <span class="ot">&lt;-</span> placeholder (\ph1' <span class="ot">-&gt;</span> on <span class="fu">$</span> p <span class="fu">!</span> Person.age' <span class="fu">.=.</span> ph1')
(ph2, ()) <span class="ot">&lt;-</span> placeholder (\ph2' <span class="ot">-&gt;</span> on <span class="fu">$</span> p <span class="fu">!</span> Person.address' <span class="fu">.=.</span> ph2')
return (<span class="dt">Person</span> <span class="fu">|$|</span> ph0 <span class="fu">|*|</span> ph1 <span class="fu">|*|</span> ph2,
<span class="dt">PersonAndBirthday</span> <span class="fu">|$|</span> p <span class="fu">|*|</span> b)
<span class="ot">(|$|) ::</span> <span class="dt">ProductConstructor</span> (a <span class="ot">-&gt;</span> b)
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Placeholders</span> a <span class="ot">-&gt;</span> <span class="dt">Placeholders</span> b
<span class="ot">(|*|) ::</span> <span class="dt">Placeholders</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Placeholders</span> a <span class="ot">-&gt;</span> <span class="dt">Placeholders</span> b</code></pre>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">ALL</span> T0.name <span class="kw">AS</span> f0, T0.age <span class="kw">AS</span> f1, T0.address <span class="kw">AS</span> f2,
T1.name <span class="kw">AS</span> f3, T1.day <span class="kw">AS</span> f4
<span class="kw">FROM</span> PUBLIC.person T0 <span class="kw">INNER</span> <span class="kw">JOIN</span> PUBLIC.birthday T1
<span class="kw">ON</span> (((T0.name = ?) <span class="kw">AND</span> (T0.age = ?)) <span class="kw">AND</span> (T0.address = ?))</code></pre>
</div><div id="record-mapping---record-placeholders" class="slide section level2">
<h1>Record Mapping - Record Placeholders</h1>
<p>Record-typed placeholder:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">personAndBirthdayP ::</span> <span class="dt">Relation</span> <span class="dt">Person</span> <span class="dt">PersonAndBirthday</span>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">placeholder ::</span> (<span class="dt">PersistableWidth</span> t, <span class="dt">Monad</span> m)
<span class="ot">=&gt;</span> (<span class="dt">Projection</span> c t <span class="ot">-&gt;</span> m a) <span class="ot">-&gt;</span> m (<span class="dt">PlaceHolders</span> t, a)
<span class="ot">personAndBirthdayP ::</span> <span class="dt">Relation</span> <span class="dt">Person</span> <span class="dt">PersonAndBirthday</span>
personAndBirthdayP <span class="fu">=</span> relation' <span class="fu">$</span> <span class="kw">do</span>
p <span class="ot">&lt;-</span> query person
b <span class="ot">&lt;-</span> query birthday
@ -335,8 +340,7 @@ personAndBirthdayP <span class="fu">=</span> relation' <span class="fu">$</span
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">ALL</span> T0.name <span class="kw">AS</span> f0, T0.age <span class="kw">AS</span> f1, T0.address <span class="kw">AS</span> f2,
T1.name <span class="kw">AS</span> f3, T1.day <span class="kw">AS</span> f4
<span class="kw">FROM</span> PUBLIC.person T0 <span class="kw">INNER</span> <span class="kw">JOIN</span> PUBLIC.birthday T1
<span class="kw">ON</span> (<span class="dv">0</span>=<span class="dv">0</span>)
<span class="kw">WHERE</span> ((T0.name, T0.age, T0.address) = (?, ?, ?))</code></pre>
<span class="kw">ON</span> ((T0.name, T0.age, T0.address) = (?, ?, ?))</code></pre>
</div><div id="questions" class="slide section level2">
<h1>Questions?</h1>
</div><div id="window-function" class="slide section level2">

View File

@ -127,6 +127,7 @@ agesOfFamilies :: Relation () (String, Maybe Int32)
agesOfFamilies = aggregateRelation $ do
my <- query myTable
gFam <- groupBy $ my ! family' -- Specify grouping key
return $ gFam >< sum' (my ! age') -- Aggregated results
~~~~~
@ -389,23 +390,27 @@ Record Mapping - Placeholders
Placeholders can be mapped to a record:
~~~~~ {.haskell}
placeholder3 f =
placeholder (\p0 -> placeholder
(\p1 -> placeholder (\p2 -> f p0 p1 p2)))
personAndBirthdayP2 :: Relation ((String, Int32), String)
PersonAndBirthday
personAndBirthdayP2 :: Relation Person PersonAndBirthday
personAndBirthdayP2 = relation' $ do
p <- query person
b <- query birthday
(ph0, (ph1, (ph2, ()))) <-
placeholder3 (\ph0' ph1' ph2' ->
wheres $
(Person |$| p ! Person.name'
|*| p ! Person.age' |*| p ! Person.address')
.=.
(Person |$| ph0' |*| ph1' |*| ph2') )
return $ (ph0 >< ph1 >< ph2, PersonAndBirthday |$| p |*| b)
(ph0, ()) <- placeholder (\ph0' -> on $ p ! Person.name' .=. ph0')
(ph1, ()) <- placeholder (\ph1' -> on $ p ! Person.age' .=. ph1')
(ph2, ()) <- placeholder (\ph2' -> on $ p ! Person.address' .=. ph2')
return (Person |$| ph0 |*| ph1 |*| ph2,
PersonAndBirthday |$| p |*| b)
(|$|) :: ProductConstructor (a -> b)
=> (a -> b) -> Placeholders a -> Placeholders b
(|*|) :: Placeholders (a -> b) -> Placeholders a -> Placeholders b
~~~~~
~~~~~ {.sql}
SELECT ALL T0.name AS f0, T0.age AS f1, T0.address AS f2,
T1.name AS f3, T1.day AS f4
FROM PUBLIC.person T0 INNER JOIN PUBLIC.birthday T1
ON (((T0.name = ?) AND (T0.age = ?)) AND (T0.address = ?))
~~~~~
Record Mapping - Record Placeholders
@ -414,6 +419,9 @@ Record Mapping - Record Placeholders
Record-typed placeholder:
~~~~~ {.haskell}
placeholder :: (PersistableWidth t, Monad m)
=> (Projection c t -> m a) -> m (PlaceHolders t, a)
personAndBirthdayP :: Relation Person PersonAndBirthday
personAndBirthdayP = relation' $ do
p <- query person
@ -426,8 +434,7 @@ personAndBirthdayP = relation' $ do
SELECT ALL T0.name AS f0, T0.age AS f1, T0.address AS f2,
T1.name AS f3, T1.day AS f4
FROM PUBLIC.person T0 INNER JOIN PUBLIC.birthday T1
ON (0=0)
WHERE ((T0.name, T0.age, T0.address) = (?, ?, ?))
ON ((T0.name, T0.age, T0.address) = (?, ?, ?))
~~~~~
Questions?

View File

@ -102,21 +102,19 @@ personAndBirthdayP :: Relation Person PersonAndBirthday
personAndBirthdayP = relation' $ do
p <- query person
b <- query birthday
(ph, ()) <- placeholder (\ph' -> wheres $ p .=. ph')
(ph, ()) <- placeholder (\ph' -> on $ p .=. ph')
return $ (ph, PersonAndBirthday |$| p |*| b)
placeholder3 :: (Monad m, PersistableWidth t, PersistableWidth t1, PersistableWidth t2, SqlProjectable p, SqlProjectable p1, SqlProjectable p2) => (p t -> p1 t1 -> p2 t2 -> m a) -> m (PlaceHolders t, (PlaceHolders t1, (PlaceHolders t2, a)))
placeholder3 f =
placeholder (\p0 -> placeholder (\p1 -> placeholder (\p2 -> f p0 p1 p2)))
personAndBirthdayP2 :: Relation ((String, Int32), String) PersonAndBirthday
personAndBirthdayP2 :: Relation Person PersonAndBirthday
personAndBirthdayP2 = relation' $ do
p <- query person
b <- query birthday
(ph0, (ph1, (ph2, ()))) <-
placeholder3 (\ph0' ph1' ph2' ->
wheres $
(Person |$| p ! Person.name' |*| p ! Person.age' |*| p ! Person.address')
.=.
(Person |$| ph0' |*| ph1' |*| ph2') )
return $ (ph0 >< ph1 >< ph2, PersonAndBirthday |$| p |*| b)
(ph0, ()) <- placeholder (\ph0' -> on $ p ! Person.name' .=. ph0')
(ph1, ()) <- placeholder (\ph1' -> on $ p ! Person.age' .=. ph1')
(ph2, ()) <- placeholder (\ph2' -> on $ p ! Person.address' .=. ph2')
return (Person |$| ph0 |*| ph1 |*| ph2, PersonAndBirthday |$| p |*| b)