Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added examples of use.

  • Loading branch information...
commit 66e8769314bd4f12d4a1183ff4083e5cf236f6ee 1 parent 8667b94
Nicolas Wu zenzike authored

Showing 1 changed file with 60 additions and 0 deletions. Show diff stats Hide diff stats

  1. +60 0 README.md
60 README.md
Source Rendered
@@ -5,3 +5,63 @@ This package provides an HDBC connection that can be used as part
5 5 of the state in a Snap application.
6 6
7 7 This API is currently a work in progress, and should not be considered stable.
  8 +
  9 +In order to use an HDBC connection, you may want to use one of the following
  10 +packages which simply export the appropriate initializers:
  11 +
  12 +* [snap-extension-hdbc-odbc](https://github.com/zenzike/snap-extension-hdbc-odbc)
  13 +* [snap-extension-hdbc-postgresql](https://github.com/zenzike/snap-extension-hdbc-postgresql)
  14 +* [snap-extension-hdbc-sqlite3](https://github.com/zenzike/snap-extension-hdbc-sqlite3)
  15 +
  16 +Example
  17 +-------
  18 +
  19 +If you want to use this as part of your Snap application, you should
  20 +change your 'ApplicationState' to contain the 'HDBCState', the following
  21 +will be typically placed in the 'Application.hs' file:
  22 +
  23 +~~~~
  24 +type Application = SnapExtend ApplicationState
  25 +
  26 +
  27 +data ApplicationState = ApplicationState
  28 + { hdbcState :: HDBCState
  29 + }
  30 +~~~~
  31 +
  32 +In addition, you should provide an instance of HasHDBCState for your
  33 +application:
  34 +
  35 +~~~~
  36 +instance HasHDBCState ApplicationState where
  37 + getHDBCState = hdbcState
  38 + setHDBCState s a = a { hdbcState = s }
  39 +~~~~
  40 +
  41 +Finally, you should tell Snap how to initialize your state. This part is
  42 +specific to the backend implementation you are using, where
  43 +the type of 'hdbcInitializer' depends on the package you have imported.
  44 +For example, iff you have imported 'Snap.Extension.HDBC.Sqlite3' then
  45 +you could set things up with the following code:
  46 +
  47 +~~~~
  48 +applicationInitializer :: Initializer ApplicationState
  49 +applicationInitializer = do
  50 + hdbc <- hdbcInitializer "resources/test1.db"
  51 + return $ ApplicationState hdbc
  52 +~~~~
  53 +
  54 +The following code shows how you can access a connection to your
  55 +database using the 'connWrapper' function that is exported from
  56 +this package:
  57 +
  58 +~~~~
  59 +type Application = SnapExtend ApplicationState
  60 +
  61 +indexR :: Application ()
  62 +indexR = ifTop $ do
  63 + conn <- connWrapper
  64 + as <- liftIO $ liftM (map fst) $ describeTable conn "users"
  65 + ts <- liftIO $ liftM (map (map fromSql)) $ quickQuery' conn "SELECT * FROM users" []
  66 + ...
  67 +~~~~

0 comments on commit 66e8769

Please sign in to comment.
Something went wrong with that request. Please try again.