Skip to content

Commit

Permalink
add HList to doc
Browse files Browse the repository at this point in the history
  • Loading branch information
tpolecat committed Mar 14, 2015
1 parent 1bf8835 commit 89a1080
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions doc/src/main/tut/04-Selecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ We can select multiple columns, of course, and map them to a tuple. The `gnp` co
.query[(String, String, Int, Option[Double])]
.process.take(5).quick.run)
```
**doobie** automatically supports row mappings for atomic column types, as well as options, tuples, and case classes thereof. So let's try the same query, mapping rows to a case class.
**doobie** automatically supports row mappings for atomic column types, as well as options, tuples, `HList`s and case classes thereof. So let's try the same query with an `HList`:

```tut
import shapeless._
(sql"select code, name, population, gnp from country"
.query[String :: String :: Int :: Option[Double] :: HNil]
.process.take(5).quick.run)
```

And again, mapping rows to a case class.

```tut:silent
case class Country(code: String, name: String, pop: Int, gnp: Option[Double])
Expand All @@ -111,7 +120,7 @@ case class Country(code: String, name: String, pop: Int, gnp: Option[Double])
.process.take(5).quick.run)
```

You can also nest case classes and/or tuples arbitrarily as long as the eventual members are of supported columns types. For instance, here we map the same set of columns to a tuple of two case classes:
You can also nest case classes, `HList`s, and/or tuples arbitrarily as long as the eventual members are of supported columns types. For instance, here we map the same set of columns to a tuple of two case classes:

```tut:silent
case class Code(code: String)
Expand Down
4 changes: 2 additions & 2 deletions doc/src/main/tut/10-Custom-Mappings.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ HPS.set(("foo", 42))
Composite[(String,Int)].set(1, ("foo", 42))
```

**doobie** can derive `Composite` instances for primitive column types, plus tuples and case classes whose elements have `Composite` instances. These primitive column types are identified by `Atom` instances, which describe `null`-safe column mappings. These `Atom` instances are almost always derived from lower-level `null`-unsafe mappings specified by the `Meta` typeclass.
**doobie** can derive `Composite` instances for primitive column types, plus tuples, `HList`s. and case classes whose elements have `Composite` instances. These primitive column types are identified by `Atom` instances, which describe `null`-safe column mappings. These `Atom` instances are almost always derived from lower-level `null`-unsafe mappings specified by the `Meta` typeclass.

So our strategy for mapping custom types is to construct a new `Meta` instance (given `Meta[A]` you get `Atom[A]` and `Atom[Option[A]]` for free); and our strategy for multi-column mappings is to construct a new `Composite` instance. We consider both case below.

Expand Down Expand Up @@ -179,7 +179,7 @@ sql"select name, owner from pet".query[(String,String)].quick.run

### Composite by Invariant Map

We get `Composite[A]` for free given `Atom[A]`, or for tuples and case classes whose fields have `Composite` instances. This covers a lot of cases, but we still need a way to map other types. For example, what if we wanted to map a `java.awt.Point` across two columns? Because it's not a tuple or case class we can't do it for free, but we can get there via `xmap`. Here we map `Point` to a pair of `Int` columns.
We get `Composite[A]` for free given `Atom[A]`, or for tuples, `HList`s, and case classes whose fields have `Composite` instances. This covers a lot of cases, but we still need a way to map other types. For example, what if we wanted to map a `java.awt.Point` across two columns? Because it's not a tuple or case class we can't do it for free, but we can get there via `xmap`. Here we map `Point` to a pair of `Int` columns.

```tut:silent
implicit val Point2DComposite: Composite[Point] =
Expand Down

0 comments on commit 89a1080

Please sign in to comment.