roc is a modern Finagle Postgresql Client. What's modern? A Client relying on a 6.x + version of Finagle.
roc is currently under heavy development and is not being published with a current version of 0.0.1-ALPHA
.
If you need a real driver right now you should absolutely use finagle-postgres.
roc is not being published yet. You'll have to clone/fork this repository, then build locally.
Once built, open sbt console
and insert the following
scala > :paste
import com.twitter.util.Await
import roc.Postgresql
import roc.postgresql.{Request, Row}
val client = Postgresql.client
.withUserAndPasswd("username", "password")
.withDatabase("database")
.newRichClient("inet!localhost:5432")
val req = new Request("SELECT * FROM STATES;")
val result = Await.result(client.query(req))
A result contains two types, a list of columns detailing information about the result, and a list of rows returned from the database.
scala> result.columns
res1: List[com.github.finagle.roc.postgresql.Column] =
List(Column(name='id, columnType=Int4, formatCode=Text),
Column(name='name, columnType=VarChar, formatCode=Text), Column(name='abbrv, columnType=VarChar, formatCode=Text), Column(name='last_modified_at, columnType=TimestampWithTimezone, formatCode=Text),
Column(name='inserted_at, columnType=TimestampWithTimezone, formatCode=Text)
)
scala> :paste
val display = (r: Row) => {
val id = r.get[Int]('id)
val name = r.get[String]('name)
val abbrv = r.get[String]('abbrv)
val insertedAt = r.get[String]('inserted_at)
val lastModifiedAt = r.get[String]('last_modified_at)
println(s"Row $id, $name, $abbrv, $insertedAt, $lastModifiedAt")
}
// Exiting paste mode, now interpreting.
display: com.github.finagle.roc.postgresql.Row => Unit = <function1>
scala> result.rows.map(display(_))
Row 1, North Dakota, ND, 2016-02-18 09:50:55.445246-06, 2016-02-18 09:50:55.445246-06
res4: List[Unit] = List(())
At the moment, we can only return Ints
and Strings
(I told you it was ALPHA
).
The good news is that most Postgresql
columns can be read as strings first, then transformed into the appropriate type
( see the handling of Timestamps With TimeZone above).
Why create your own when there is an existing implementation? Two reasons
- The current finagle-postgres was developed pre Finagle 6.x and does not use modern finagle abstractions. These are core enough to require what amounts to a complete rewrite of the driver.
- The current project lacks a true owner - Twitter decided to give the Open Source team a long vacation last fall, and since then only a handful of projects are being actively managed.
It is absolutely the goal of this project to become the reference implementation, and hopefully be moved into the Finagle ecosystem.
roc (Rokh or Rukh) is named after the Persian mythological bird of prey Roc, which was based in part on the now extinct elephant bird. Yes, that means we've hit the rare 4 point play with the name
Goal | Status |
---|---|
short name | ✔️ |
obligatory mythological reference | ✔️ |
bird reference for Twitter | ✔️ |
elephant reference for Postgresql | ✔️ |
roc is currently maintained by Jeffrey Davis.
The roc project supports the Typelevel code of conduct and wants all of its channels (GitHub, etc.) to be welcoming environments for everyone.
Licensed under the BSD-3-Clause (Revised BSD Liscense); you may not use this software except in compliance with the License.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.