mirror of
https://github.com/khibino/haskell-relational-record.git
synced 2025-01-07 13:46:41 +03:00
Fix placeholder examples.
This commit is contained in:
parent
6ed1cb5160
commit
24cb13a547
@ -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>
|
agesOfFamilies <span class="fu">=</span> aggregateRelation <span class="fu">$</span> <span class="kw">do</span>
|
||||||
my <span class="ot"><-</span> query myTable
|
my <span class="ot"><-</span> query myTable
|
||||||
gFam <span class="ot"><-</span> groupBy <span class="fu">$</span> my <span class="fu">!</span> family' <span class="co">-- Specify grouping key</span>
|
gFam <span class="ot"><-</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">><</span> sum' (my <span class="fu">!</span> age') <span class="co">-- Aggregated results</span></code></pre>
|
return <span class="fu">$</span> gFam <span class="fu">><</span> sum' (my <span class="fu">!</span> age') <span class="co">-- Aggregated results</span></code></pre>
|
||||||
<p>sums ages per family.</p>
|
<p>sums ages per family.</p>
|
||||||
<p>Generated SQL:</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">
|
</div><div id="record-mapping---placeholders" class="slide section level2">
|
||||||
<h1>Record Mapping - Placeholders</h1>
|
<h1>Record Mapping - Placeholders</h1>
|
||||||
<p>Placeholders can be mapped to a record:</p>
|
<p>Placeholders can be mapped to a record:</p>
|
||||||
<pre class="sourceCode haskell"><code class="sourceCode haskell">placeholder3 f <span class="fu">=</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>
|
||||||
placeholder (\p0 <span class="ot">-></span> placeholder
|
|
||||||
(\p1 <span class="ot">-></span> placeholder (\p2 <span class="ot">-></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>
|
|
||||||
personAndBirthdayP2 <span class="fu">=</span> relation' <span class="fu">$</span> <span class="kw">do</span>
|
personAndBirthdayP2 <span class="fu">=</span> relation' <span class="fu">$</span> <span class="kw">do</span>
|
||||||
p <span class="ot"><-</span> query person
|
p <span class="ot"><-</span> query person
|
||||||
b <span class="ot"><-</span> query birthday
|
b <span class="ot"><-</span> query birthday
|
||||||
(ph0, (ph1, (ph2, ()))) <span class="ot"><-</span>
|
(ph0, ()) <span class="ot"><-</span> placeholder (\ph0' <span class="ot">-></span> on <span class="fu">$</span> p <span class="fu">!</span> Person.name' <span class="fu">.=.</span> ph0')
|
||||||
placeholder3 (\ph0' ph1' ph2' <span class="ot">-></span>
|
(ph1, ()) <span class="ot"><-</span> placeholder (\ph1' <span class="ot">-></span> on <span class="fu">$</span> p <span class="fu">!</span> Person.age' <span class="fu">.=.</span> ph1')
|
||||||
wheres <span class="fu">$</span>
|
(ph2, ()) <span class="ot"><-</span> placeholder (\ph2' <span class="ot">-></span> on <span class="fu">$</span> p <span class="fu">!</span> Person.address' <span class="fu">.=.</span> ph2')
|
||||||
(<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')
|
return (<span class="dt">Person</span> <span class="fu">|$|</span> ph0 <span class="fu">|*|</span> ph1 <span class="fu">|*|</span> ph2,
|
||||||
<span class="fu">.=.</span>
|
<span class="dt">PersonAndBirthday</span> <span class="fu">|$|</span> p <span class="fu">|*|</span> b)
|
||||||
(<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">><</span> ph1 <span class="fu">><</span> ph2, <span class="dt">PersonAndBirthday</span> <span class="fu">|$|</span> p <span class="fu">|*|</span> b)</code></pre>
|
<span class="ot">(|$|) ::</span> <span class="dt">ProductConstructor</span> (a <span class="ot">-></span> b)
|
||||||
|
<span class="ot">=></span> (a <span class="ot">-></span> b) <span class="ot">-></span> <span class="dt">Placeholders</span> a <span class="ot">-></span> <span class="dt">Placeholders</span> b
|
||||||
|
<span class="ot">(|*|) ::</span> <span class="dt">Placeholders</span> (a <span class="ot">-></span> b) <span class="ot">-></span> <span class="dt">Placeholders</span> a <span class="ot">-></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">
|
</div><div id="record-mapping---record-placeholders" class="slide section level2">
|
||||||
<h1>Record Mapping - Record Placeholders</h1>
|
<h1>Record Mapping - Record Placeholders</h1>
|
||||||
<p>Record-typed placeholder:</p>
|
<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">=></span> (<span class="dt">Projection</span> c t <span class="ot">-></span> m a) <span class="ot">-></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>
|
personAndBirthdayP <span class="fu">=</span> relation' <span class="fu">$</span> <span class="kw">do</span>
|
||||||
p <span class="ot"><-</span> query person
|
p <span class="ot"><-</span> query person
|
||||||
b <span class="ot"><-</span> query birthday
|
b <span class="ot"><-</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,
|
<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
|
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">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">ON</span> ((T0.name, T0.age, T0.address) = (?, ?, ?))</code></pre>
|
||||||
<span class="kw">WHERE</span> ((T0.name, T0.age, T0.address) = (?, ?, ?))</code></pre>
|
|
||||||
</div><div id="questions" class="slide section level2">
|
</div><div id="questions" class="slide section level2">
|
||||||
<h1>Questions?</h1>
|
<h1>Questions?</h1>
|
||||||
</div><div id="window-function" class="slide section level2">
|
</div><div id="window-function" class="slide section level2">
|
||||||
|
@ -127,6 +127,7 @@ agesOfFamilies :: Relation () (String, Maybe Int32)
|
|||||||
agesOfFamilies = aggregateRelation $ do
|
agesOfFamilies = aggregateRelation $ do
|
||||||
my <- query myTable
|
my <- query myTable
|
||||||
gFam <- groupBy $ my ! family' -- Specify grouping key
|
gFam <- groupBy $ my ! family' -- Specify grouping key
|
||||||
|
|
||||||
return $ gFam >< sum' (my ! age') -- Aggregated results
|
return $ gFam >< sum' (my ! age') -- Aggregated results
|
||||||
~~~~~
|
~~~~~
|
||||||
|
|
||||||
@ -389,23 +390,27 @@ Record Mapping - Placeholders
|
|||||||
Placeholders can be mapped to a record:
|
Placeholders can be mapped to a record:
|
||||||
|
|
||||||
~~~~~ {.haskell}
|
~~~~~ {.haskell}
|
||||||
placeholder3 f =
|
personAndBirthdayP2 :: Relation Person PersonAndBirthday
|
||||||
placeholder (\p0 -> placeholder
|
|
||||||
(\p1 -> placeholder (\p2 -> f p0 p1 p2)))
|
|
||||||
|
|
||||||
personAndBirthdayP2 :: Relation ((String, Int32), String)
|
|
||||||
PersonAndBirthday
|
|
||||||
personAndBirthdayP2 = relation' $ do
|
personAndBirthdayP2 = relation' $ do
|
||||||
p <- query person
|
p <- query person
|
||||||
b <- query birthday
|
b <- query birthday
|
||||||
(ph0, (ph1, (ph2, ()))) <-
|
(ph0, ()) <- placeholder (\ph0' -> on $ p ! Person.name' .=. ph0')
|
||||||
placeholder3 (\ph0' ph1' ph2' ->
|
(ph1, ()) <- placeholder (\ph1' -> on $ p ! Person.age' .=. ph1')
|
||||||
wheres $
|
(ph2, ()) <- placeholder (\ph2' -> on $ p ! Person.address' .=. ph2')
|
||||||
(Person |$| p ! Person.name'
|
|
||||||
|*| p ! Person.age' |*| p ! Person.address')
|
return (Person |$| ph0 |*| ph1 |*| ph2,
|
||||||
.=.
|
PersonAndBirthday |$| p |*| b)
|
||||||
(Person |$| ph0' |*| ph1' |*| ph2') )
|
|
||||||
return $ (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
|
Record Mapping - Record Placeholders
|
||||||
@ -414,6 +419,9 @@ Record Mapping - Record Placeholders
|
|||||||
Record-typed placeholder:
|
Record-typed placeholder:
|
||||||
|
|
||||||
~~~~~ {.haskell}
|
~~~~~ {.haskell}
|
||||||
|
placeholder :: (PersistableWidth t, Monad m)
|
||||||
|
=> (Projection c t -> m a) -> m (PlaceHolders t, a)
|
||||||
|
|
||||||
personAndBirthdayP :: Relation Person PersonAndBirthday
|
personAndBirthdayP :: Relation Person PersonAndBirthday
|
||||||
personAndBirthdayP = relation' $ do
|
personAndBirthdayP = relation' $ do
|
||||||
p <- query person
|
p <- query person
|
||||||
@ -426,8 +434,7 @@ personAndBirthdayP = relation' $ do
|
|||||||
SELECT ALL T0.name AS f0, T0.age AS f1, T0.address AS f2,
|
SELECT ALL T0.name AS f0, T0.age AS f1, T0.address AS f2,
|
||||||
T1.name AS f3, T1.day AS f4
|
T1.name AS f3, T1.day AS f4
|
||||||
FROM PUBLIC.person T0 INNER JOIN PUBLIC.birthday T1
|
FROM PUBLIC.person T0 INNER JOIN PUBLIC.birthday T1
|
||||||
ON (0=0)
|
ON ((T0.name, T0.age, T0.address) = (?, ?, ?))
|
||||||
WHERE ((T0.name, T0.age, T0.address) = (?, ?, ?))
|
|
||||||
~~~~~
|
~~~~~
|
||||||
|
|
||||||
Questions?
|
Questions?
|
||||||
|
@ -102,21 +102,19 @@ personAndBirthdayP :: Relation Person PersonAndBirthday
|
|||||||
personAndBirthdayP = relation' $ do
|
personAndBirthdayP = relation' $ do
|
||||||
p <- query person
|
p <- query person
|
||||||
b <- query birthday
|
b <- query birthday
|
||||||
(ph, ()) <- placeholder (\ph' -> wheres $ p .=. ph')
|
(ph, ()) <- placeholder (\ph' -> on $ p .=. ph')
|
||||||
return $ (ph, PersonAndBirthday |$| p |*| b)
|
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 :: (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 =
|
placeholder3 f =
|
||||||
placeholder (\p0 -> placeholder (\p1 -> placeholder (\p2 -> f p0 p1 p2)))
|
placeholder (\p0 -> placeholder (\p1 -> placeholder (\p2 -> f p0 p1 p2)))
|
||||||
|
|
||||||
personAndBirthdayP2 :: Relation ((String, Int32), String) PersonAndBirthday
|
personAndBirthdayP2 :: Relation Person PersonAndBirthday
|
||||||
personAndBirthdayP2 = relation' $ do
|
personAndBirthdayP2 = relation' $ do
|
||||||
p <- query person
|
p <- query person
|
||||||
b <- query birthday
|
b <- query birthday
|
||||||
(ph0, (ph1, (ph2, ()))) <-
|
(ph0, ()) <- placeholder (\ph0' -> on $ p ! Person.name' .=. ph0')
|
||||||
placeholder3 (\ph0' ph1' ph2' ->
|
(ph1, ()) <- placeholder (\ph1' -> on $ p ! Person.age' .=. ph1')
|
||||||
wheres $
|
(ph2, ()) <- placeholder (\ph2' -> on $ p ! Person.address' .=. ph2')
|
||||||
(Person |$| p ! Person.name' |*| p ! Person.age' |*| p ! Person.address')
|
|
||||||
.=.
|
return (Person |$| ph0 |*| ph1 |*| ph2, PersonAndBirthday |$| p |*| b)
|
||||||
(Person |$| ph0' |*| ph1' |*| ph2') )
|
|
||||||
return $ (ph0 >< ph1 >< ph2, PersonAndBirthday |$| p |*| b)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user