Orville is a PostgreSQL client library, so it needs a working PostgreSQL server
to work. This tutorial assumes that PostgreSQL in running on localhost
,
listening on port 5432
and that the user postgres
exists with password
postgres
.
Orville itself depends on the native LibPQ library being installed to
communicate with the server, so we need to install it before building
everything. If you’re on Debian-like distribution that uses apt
, this command
will do that.
apt update
apt install -y libpq-dev
With that setup out of the way, let’s make a new project for this demo:
mkdir orville-getting-started
cd orville-getting-started
stack new orville-getting-started --bare simple --resolver lts-21.19
Now we need to edit stack.yaml
and add orville-postgresql
as an extra-dep.
42c42,43
< # extra-deps: []
---
> extra-deps:
> - orville-postgresql-1.0.0.0
Next, edit the orville-getting-started.cabal
file to make it depend on
Orville.
22c22
< build-depends: base >= 4.7 && < 5
---
> build-depends: base >= 4.7 && < 5, orville-postgresql ^>= 1.0.0.0
Here is a minimal program that simply computes 1+1 on the database. Replace
the src/Main.hs
with it.
module Main
( mainwhere
)
import qualified Orville.PostgreSQL as O
import qualified Orville.PostgreSQL.Execution as Execution
import qualified Orville.PostgreSQL.Raw.Connection as Connection
import qualified Orville.PostgreSQL.Raw.RawSql as RawSql
import qualified Orville.PostgreSQL.Raw.SqlValue as SqlValue
main :: IO ()
= do
main <-
pool
O.createConnectionPoolO.ConnectionOptions
= "host=localhost user=postgres password=postgres"
{ O.connectionString = O.DisableNoticeReporting
, O.connectionNoticeReporting = O.OneStripePerCapability
, O.connectionPoolStripes = 10
, O.connectionPoolLingerTime = O.MaxConnectionsPerStripe 1
, O.connectionPoolMaxConnections
}
$ \connection -> do
Connection.withPoolConnection pool <- RawSql.execute connection (RawSql.fromString "SELECT 1+1")
result <- Execution.readRows result
[[(_, sqlValue)]] print (SqlValue.toInt sqlValue)
All that’s left is to build the executable and run it. When it runs it will
connect to the PostgreSQL server, run the SQL to calculate 1 + 1
and then
print the result out.
stack build
stack exec orville-getting-started
This is the output you will see in the console if everything went as planned:
Right 2