1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
{-# LANGUAGE ExistentialQuantification #-}
-- | Welcome to @persistent@!
--
-- This library intends to provide an easy, flexible, and convenient interface
-- to various data storage backends. Backends include SQL databases, like
-- @mysql@, @postgresql@, and @sqlite@, as well as NoSQL databases, like
-- @mongodb@ and @redis@.
--
-- If you intend on using a SQL database, then check out "Database.Persist.Sql".
module Database.Persist
(
-- * Defining Database Models
--
-- | @persistent@ lets you define your database models using a special syntax.
-- This syntax allows you to customize the resulting Haskell datatypes and
-- database schema. See "Database.Persist.Quasi" for details on that definition
-- language.
-- ** Reference Schema & Dataset
--
-- | For a quick example of the syntax, we'll introduce this database schema, and
-- we'll use it to explain the update and filter combinators.
--
-- @
-- 'share' ['mkPersist' 'sqlSettings', 'mkMigrate' "migrateAll"] ['persistLowerCase'|
-- User
-- name String
-- age Int
-- deriving Show
-- |]
-- @
--
-- This creates a Haskell datatype that looks like this:
--
-- @
-- data User = User
-- { userName :: String
-- , userAge :: Int
-- }
-- deriving Show
-- @
--
-- In a SQL database, we'd get a migration like this:
--
-- @
-- CREATE TABLE "user" (
-- id SERIAL PRIMARY KEY,
-- name TEXT NOT NULL,
-- age INT NOT NULL
-- );
-- @
--
-- The examples below will refer to this as dataset-1.
--
-- #dataset#
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
-- * Database Operations
-- | The module "Database.Persist.Class" defines how to operate with
-- @persistent@ database models. Check that module out for basic
-- operations, like 'get', 'insert', and 'selectList'.
module Database.Persist.Class
-- * Types
-- | This module re-export contains a lot of the important types for
-- working with @persistent@ datatypes and underlying values.
, module Database.Persist.Types
-- * Query Operators
-- | A convention that @persistent@ tries to follow is that operators on
-- Database types correspond to a Haskell (or database) operator with a @.@
-- character at the end. So to do @a || b@ , you'd write @a '||.' b@. To
-- ** Query update combinators
-- | These operations are used when performing updates against the database.
-- Functions like 'upsert' use them to provide new or modified values.
, (=.), (+=.), (-=.), (*=.), (/=.)
-- ** Query filter combinators
-- | These functions are useful in the 'PersistQuery' class, like
-- 'selectList', 'updateWhere', etc.
, (==.), (!=.), (<.), (>.), (<=.), (>=.), (<-.), (/<-.), (||.)
-- * JSON Utilities
, listToJSON
, mapToJSON
, toJsonText
, getPersistMap
-- * Other utilities
, limitOffsetOrder
) where
import Data.Aeson (toJSON, ToJSON)
import Data.Aeson.Text (encodeToTextBuilder)
import qualified Data.Text as T
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Database.Persist.Types
import Database.Persist.Class
import Database.Persist.Class.PersistField (getPersistMap)
infixr 3 =., +=., -=., *=., /=.
(=.), (+=.), (-=.), (*=.), (/=.) ::
forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
-- | Assign a field a value.
--
-- === Examples
--
-- @
-- updateAge :: MonadIO m => ReaderT SqlBackend m ()
-- updateAge = updateWhere [UserName ==. \"SPJ\" ] [UserAge =. 45]
-- @
--
-- Similar to `updateWhere` which is shown in the above example you can use other functions present in the module "Database.Persist.Class". Note that the first parameter of `updateWhere` is [`Filter` val] and second parameter is [`Update` val]. By comparing this with the type of `==.` and `=.`, you can see that they match up in the above usage.
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+--------+
-- > |id |name |age |
-- > +-----+-----+--------+
-- > |1 |SPJ |40 -> 45|
-- > +-----+-----+--------+
-- > |2 |Simon|41 |
-- > +-----+-----+--------+
f =. a = Update f a Assign
-- | Assign a field by addition (@+=@).
--
-- === Examples
--
-- @
-- addAge :: MonadIO m => ReaderT SqlBackend m ()
-- addAge = updateWhere [UserName ==. \"SPJ\" ] [UserAge +=. 1]
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+---------+
-- > |id |name |age |
-- > +-----+-----+---------+
-- > |1 |SPJ |40 -> 41 |
-- > +-----+-----+---------+
-- > |2 |Simon|41 |
-- > +-----+-----+---------+
f +=. a = Update f a Add
-- | Assign a field by subtraction (@-=@).
--
-- === Examples
--
-- @
-- subtractAge :: MonadIO m => ReaderT SqlBackend m ()
-- subtractAge = updateWhere [UserName ==. \"SPJ\" ] [UserAge -=. 1]
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+---------+
-- > |id |name |age |
-- > +-----+-----+---------+
-- > |1 |SPJ |40 -> 39 |
-- > +-----+-----+---------+
-- > |2 |Simon|41 |
-- > +-----+-----+---------+
f -=. a = Update f a Subtract
-- | Assign a field by multiplication (@*=@).
--
-- === Examples
--
-- @
-- multiplyAge :: MonadIO m => ReaderT SqlBackend m ()
-- multiplyAge = updateWhere [UserName ==. \"SPJ\" ] [UserAge *=. 2]
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+--------+
-- > |id |name |age |
-- > +-----+-----+--------+
-- > |1 |SPJ |40 -> 80|
-- > +-----+-----+--------+
-- > |2 |Simon|41 |
-- > +-----+-----+--------+
f *=. a = Update f a Multiply
-- | Assign a field by division (@/=@).
--
-- === Examples
--
-- @
-- divideAge :: MonadIO m => ReaderT SqlBackend m ()
-- divideAge = updateWhere [UserName ==. \"SPJ\" ] [UserAge /=. 2]
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+---------+
-- > |id |name |age |
-- > +-----+-----+---------+
-- > |1 |SPJ |40 -> 20 |
-- > +-----+-----+---------+
-- > |2 |Simon|41 |
-- > +-----+-----+---------+
f /=. a = Update f a Divide
infix 4 ==., <., <=., >., >=., !=.
(==.), (!=.), (<.), (<=.), (>.), (>=.) ::
forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
-- | Check for equality.
--
-- === Examples
--
-- @
-- selectSPJ :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectSPJ = selectList [UserName ==. \"SPJ\" ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
f ==. a = Filter f (FilterValue a) Eq
-- | Non-equality check.
--
-- === Examples
--
-- @
-- selectSimon :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectSimon = selectList [UserName !=. \"SPJ\" ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
f !=. a = Filter f (FilterValue a) Ne
-- | Less-than check.
--
-- === Examples
--
-- @
-- selectLessAge :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectLessAge = selectList [UserAge <. 41 ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
f <. a = Filter f (FilterValue a) Lt
-- | Less-than or equal check.
--
-- === Examples
--
-- @
-- selectLessEqualAge :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectLessEqualAge = selectList [UserAge <=. 40 ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
f <=. a = Filter f (FilterValue a) Le
-- | Greater-than check.
--
-- === Examples
--
-- @
-- selectGreaterAge :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectGreaterAge = selectList [UserAge >. 40 ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
f >. a = Filter f (FilterValue a) Gt
-- | Greater-than or equal check.
--
-- === Examples
--
-- @
-- selectGreaterEqualAge :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectGreaterEqualAge = selectList [UserAge >=. 41 ] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
f >=. a = Filter f (FilterValue a) Ge
infix 4 <-., /<-.
(<-.), (/<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v
-- | Check if value is in given list.
--
-- === Examples
--
-- @
-- selectUsers :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectUsers = selectList [UserAge <-. [40, 41]] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
--
--
-- @
-- selectSPJ :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectSPJ = selectList [UserAge <-. [40]] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |1 |SPJ |40 |
-- > +-----+-----+-----+
f <-. a = Filter f (FilterValues a) In
-- | Check if value is not in given list.
--
-- === Examples
--
-- @
-- selectSimon :: MonadIO m => ReaderT SqlBackend m [Entity User]
-- selectSimon = selectList [UserAge /<-. [40]] []
-- @
--
-- The above query when applied on <#dataset dataset-1>, will produce this:
--
-- > +-----+-----+-----+
-- > |id |name |age |
-- > +-----+-----+-----+
-- > |2 |Simon|41 |
-- > +-----+-----+-----+
f /<-. a = Filter f (FilterValues a) NotIn
infixl 3 ||.
(||.) :: forall v. [Filter v] -> [Filter v] -> [Filter v]
-- | The OR of two lists of filters. For example:
--
-- > selectList
-- > ([ PersonAge >. 25
-- > , PersonAge <. 30 ] ||.
-- > [ PersonIncome >. 15000
-- > , PersonIncome <. 25000 ])
-- > []
--
-- will filter records where a person's age is between 25 and 30 /or/ a
-- person's income is between (15000 and 25000).
--
-- If you are looking for an @(&&.)@ operator to do @(A AND B AND (C OR D))@
-- you can use the @(++)@ operator instead as there is no @(&&.)@. For
-- example:
--
-- > selectList
-- > ([ PersonAge >. 25
-- > , PersonAge <. 30 ] ++
-- > ([PersonCategory ==. 1] ||.
-- > [PersonCategory ==. 5]))
-- > []
--
-- will filter records where a person's age is between 25 and 30 /and/
-- (person's category is either 1 or 5).
a ||. b = [FilterOr [FilterAnd a, FilterAnd b]]
-- | Convert list of 'PersistValue's into textual representation of JSON
-- object. This is a type-constrained synonym for 'toJsonText'.
listToJSON :: [PersistValue] -> T.Text
listToJSON = toJsonText
-- | Convert map (list of tuples) into textual representation of JSON
-- object. This is a type-constrained synonym for 'toJsonText'.
mapToJSON :: [(T.Text, PersistValue)] -> T.Text
mapToJSON = toJsonText
-- | A more general way to convert instances of `ToJSON` type class to
-- strict text 'T.Text'.
toJsonText :: ToJSON j => j -> T.Text
toJsonText = toStrict . toLazyText . encodeToTextBuilder . toJSON
-- | FIXME What's this exactly?
limitOffsetOrder :: PersistEntity val
=> [SelectOpt val]
-> (Int, Int, [SelectOpt val])
limitOffsetOrder opts =
foldr go (0, 0, []) opts
where
go (LimitTo l) (_, b, c) = (l, b ,c)
go (OffsetBy o) (a, _, c) = (a, o, c)
go x (a, b, c) = (a, b, x : c)
|