-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | sqlite-simple snaplet for the Snap Framework -- -- This snaplet provides support for using the SQLite database with a -- Snap Framework application via the sqlite-simple package. It also -- includes an authentication backend. -- -- See -- https://github.com/nurpax/snaplet-sqlite-simple/tree/master/example -- for an example project that demonstrates the use of this snaplet. It -- implements a stand-alone application with a login screen that persists -- user information in a SQLite database and allows a logged in user to -- save comments. Querying and listing comments demonstrates how a user -- of snaplet-sqlite-simple might relate Snaplet.Auth based AuthUsers to -- his own tables. @package snaplet-sqlite-simple @version 0.3.0 -- | This snaplet makes it simple to use a SQLite database from your Snap -- application and is based on the sqlite-simple library -- (http://hackage.haskell.org/package/sqlite-simple). Now, adding -- a database to your web app takes just two simple steps. -- -- First, include this snaplet in your application's state. -- --
-- data App = App
-- { ... -- Other state needed in your app
-- , _db :: Snaplet Sqlite
-- }
--
--
-- Next, call the sqliteInit from your application's initializer.
--
-- -- appInit = makeSnaplet ... $ do -- ... -- d <- nestSnaplet "db" db sqliteInit -- return $ App ... d ---- -- Now you can use any of the sqlite-simple wrapper functions defined in -- this module anywhere in your application handlers. For instance: -- --
-- postHandler :: Handler App App () -- postHandler = do -- posts <- with db $ query_ "select * from blog_post" -- ... ---- -- Optionally, if you find yourself doing many database queries, you can -- eliminate some of the boilerplate by defining a HasSqlite instance for -- your application. -- --
-- instance HasSqlite (Handler b App) where -- getSqliteState = with db get ---- -- With this code, our postHandler example no longer requires the -- with function: -- --
-- postHandler :: Handler App App () -- postHandler = do -- posts <- query_ "select * from blog_post" -- ... ---- -- The first time you run an application with the sqlite-simple snaplet, -- a configuration file devel.cfg is created in the -- snaplets/sqlite-simple directory underneath your project -- root. It specifies how to connect to your Sqlite database. Edit this -- file and modify the values appropriately and you'll be off and -- running. -- -- If you want to have out-of-the-box authentication, look at the -- documentation for the Snap.Snaplet.Auth.Backends.Sqlite module. module Snap.Snaplet.SqliteSimple -- | The state for the sqlite-simple snaplet. To use it in your app include -- this in your application state and use sqliteInit to initialize -- it. data Sqlite Sqlite :: Pool Connection -> Sqlite -- | Function for retrieving the connection pool sqlitePool :: Sqlite -> Pool Connection -- | Instantiate this typeclass on 'Handler b YourAppState' so this snaplet -- can find the connection source. If you need to have multiple instances -- of the sqlite snaplet in your application, then don't provide this -- instance and leverage the default instance by using "with -- dbLens" in front of calls to snaplet-sqlite-simple functions. class MonadCatchIO m => HasSqlite m getSqliteState :: HasSqlite m => m Sqlite -- | Initialize the snaplet sqliteInit :: SnapletInit b Sqlite -- | See query query :: (HasSqlite m, ToRow q, FromRow r) => Query -> q -> m [r] -- | See query_ query_ :: (HasSqlite m, FromRow r) => Query -> m [r] execute :: (HasSqlite m, ToRow q, MonadCatchIO m) => Query -> q -> m () execute_ :: (HasSqlite m, MonadCatchIO m) => Query -> m () -- | Connection to an open database. data Connection :: * -- | A query string. This type is intended to make it difficult to -- construct a SQL query by concatenating string fragments, as that is an -- extremely common way to accidentally introduce SQL injection -- vulnerabilities into an application. -- -- This type is an instance of IsString, so the easiest way to -- construct a query is to enable the OverloadedStrings language -- extension and then simply write the query in double quotes. -- --
-- {-# LANGUAGE OverloadedStrings #-}
--
-- import Database.PostgreSQL.Simple
--
-- q :: Query
-- q = "select ?"
--
--
-- The underlying type is a Text, and literal Haskell strings
-- that contain Unicode characters will be correctly transformed to
-- UTF-8.
data Query :: *
-- | A single-value "collection".
--
-- This is useful if you need to supply a single parameter to a SQL
-- query, or extract a single column from a SQL result.
--
-- Parameter example:
--
-- -- query c "select x from scores where x > ?" (Only (42::Int)) ---- -- Result example: -- --
-- xs <- query_ c "select id from users"
-- forM_ xs $ \(Only id) -> {- ... -}
--
newtype Only a :: * -> *
Only :: a -> Only a
fromOnly :: Only a -> a
-- | Exception thrown if a Query was malformed. This may occur if
-- the number of '?' characters in the query string does not
-- match the number of parameters provided.
data FormatError :: *
-- | Exception thrown if conversion from a SQL value to a Haskell value
-- fails.
data ResultError :: *
-- | A composite type to parse your custom data structures without having
-- to define dummy newtype wrappers every time.
--
-- -- instance FromRow MyData where ... ---- --
-- instance FromRow MyData2 where ... ---- -- then I can do the following for free: -- --
-- res <- query' c ...
-- forM res $ \(MyData{..} :. MyData2{..}) -> do
-- ....
--
data (:.) h t :: * -> * -> *
(:.) :: h -> t -> :. h t
-- | A collection type that can be turned into a list of SQLData elements.
class ToRow a
toRow :: ToRow a => a -> [SQLData]
-- | A collection type that can be converted from a sequence of fields.
-- Instances are provided for tuples up to 10 elements and lists of any
-- length.
--
-- Note that instances can defined outside of sqlite-simple, which is
-- often useful. For example, here's an instance for a user-defined pair:
--
--
-- data User = User { name :: String, fileQuota :: Int }
--
-- instance FromRow User where
-- fromRow = User <$> field <*> field
--
--
-- The number of calls to field must match the number of fields
-- returned in a single row of the query result. Otherwise, a
-- ConversionFailed exception will be thrown.
--
-- Note the caveats associated with user-defined implementations of
-- fromRow.
class FromRow a
fromRow :: FromRow a => RowParser a
field :: FromField a => RowParser a
instance MonadCatchIO m => HasSqlite (ReaderT Sqlite m)
instance MonadCatchIO m => HasSqlite (ReaderT (Snaplet Sqlite) m)
instance HasSqlite (Handler b Sqlite)
-- | This module allows you to use the auth snaplet with your user database
-- stored in a SQLite database. When you run your application with this
-- snaplet, a config file will be copied into the the
-- snaplets/sqlite-auth directory. This file contains all of the
-- configurable options for the snaplet and allows you to change them
-- without recompiling your application.
--
-- To use this snaplet in your application enable the session, sqlite,
-- and auth snaplets as follows:
--
--
-- data App = App
-- { ... -- your own application state here
-- , _sess :: Snaplet SessionManager
-- , _db :: Snaplet Sqlite
-- , _auth :: Snaplet (AuthManager App)
-- }
--
--
-- Then in your initializer you'll have something like this:
--
-- -- d <- nestSnaplet "db" db sqliteInit -- a <- nestSnaplet "auth" auth $ initSqliteAuth sess d ---- -- If you have not already created the database table for users, it will -- automatically be created for you the first time you run your -- application. module Snap.Snaplet.Auth.Backends.SqliteSimple -- | Initializer for the sqlite backend to the auth snaplet. initSqliteAuth :: SnapletLens b SessionManager -> Snaplet Sqlite -> SnapletInit b (AuthManager b) instance IAuthBackend SqliteAuthManager instance ToField Password instance FromRow AuthUser instance FromField Password instance FromField UserId