Database.Persist.Sql.Util

persistent-2.14.6.3: Type-safe, multi-backend data serialization.

Safe Haskell | None
Language | Haskell2010

Database.Persist.Sql.Util

Synopsis

- parseEntityValues :: PersistEntity record => EntityDef -> [PersistValue] -> Either Text (Entity record)
- keyAndEntityColumnNames :: EntityDef -> SqlBackend -> NonEmpty Text
- entityColumnCount :: EntityDef -> Int
- isIdField :: PersistEntity record => EntityField record typ -> Bool
- hasNaturalKey :: EntityDef -> Bool
- hasCompositePrimaryKey :: EntityDef -> Bool
- dbIdColumns :: SqlBackend -> EntityDef -> NonEmpty Text
- dbIdColumnsEsc :: (FieldNameDB -> Text) -> EntityDef -> NonEmpty Text
- dbColumns :: SqlBackend -> EntityDef -> NonEmpty Text
- updateFieldDef :: PersistEntity v => Update v -> FieldDef
- updatePersistValue :: Update v -> PersistValue
- mkUpdateText :: PersistEntity record => SqlBackend -> Update record -> Text
- mkUpdateText' :: PersistEntity record => (FieldNameDB -> Text) -> (Text -> Text) -> Update record -> Text
- commaSeparated :: [Text] -> Text
- parenWrapped :: Text -> Text
- mkInsertValues :: PersistEntity rec => rec -> [PersistValue]
- mkInsertPlaceholders :: EntityDef -> (FieldNameDB -> Text) -> [(Text, Text)]
- parseExistsResult :: Maybe [PersistValue] -> Text -> String -> Bool

# Documentation

parseEntityValues :: PersistEntity record => EntityDef -> [PersistValue] -> Either Text (Entity record) #

keyAndEntityColumnNames :: EntityDef -> SqlBackend -> NonEmpty Text #

entityColumnCount :: EntityDef -> Int #

isIdField :: PersistEntity record => EntityField record typ -> Bool #

hasNaturalKey :: EntityDef -> Bool #

Returns `True` if the entity has a natural key defined with the Primary keyword.

A natural key is a key that is inherent to the record, and is part of the actual Haskell record. The opposite of a natural key is a "surrogate key", which is not part of the normal domain object. Automatically generated ID columns are the most common surrogate ID, while an email address is a common natural key.

    User
        email String
        name String
        Primary email

    Person
        Id   UUID
        name String

    Follower
        name String

Given these entity definitions, `User` would return `True`, because the `Primary` keyword sets the `email` column to be the primary key. The generated Haskell type would look like this:

    data User = User
        { userEmail :: String
        , userName :: String
        }

`Person` would be false. While the `Id` syntax allows you to define a custom ID type for an entity, the `Id` column is a surrogate key.

The same is true for `Follower`. The automatically generated autoincremented integer primary key is a surrogate key.

There's nothing preventing you from defining a `Primary` definition that refers to a surrogate key. This is totally fine.

Since: 2.11.0

hasCompositePrimaryKey :: EntityDef -> Bool #

Returns `True` if the provided entity has a custom composite primary key. Composite keys have multiple fields in them.

    User
        email String
        name String
        Primary userId

    Profile
        personId PersonId
        email    String
        Primary personId email

    Person
        Id   UUID
        name String

    Follower
        name String

Given these entity definitions, only `Profile` would return `True`, because it is the only entity with multiple columns in the primary key. `User` has a single column natural key. `Person` has a custom single column surrogate key defined with `Id`. And `Follower` has a default single column surrogate key.

Since: 2.11.0

dbIdColumns :: SqlBackend -> EntityDef -> NonEmpty Text #

dbIdColumnsEsc :: (FieldNameDB -> Text) -> EntityDef -> NonEmpty Text #

dbColumns :: SqlBackend -> EntityDef -> NonEmpty Text #

updateFieldDef :: PersistEntity v => Update v -> FieldDef #

Gets the `FieldDef` for an `Update`.

updatePersistValue :: Update v -> PersistValue #

mkUpdateText :: PersistEntity record => SqlBackend -> Update record -> Text #

mkUpdateText' :: PersistEntity record => (FieldNameDB -> Text) -> (Text -> Text) -> Update record -> Text #

commaSeparated :: [Text] -> Text #

parenWrapped :: Text -> Text #

mkInsertValues :: PersistEntity rec => rec -> [PersistValue] #

Make a list `PersistValue` suitable for database inserts. Pairs nicely with the function `mkInsertPlaceholders`.

Does not include generated columns.

Since: 2.11.0.0

mkInsertPlaceholders #

Arguments

:: EntityDef |
-> (FieldNameDB -> Text) | An `escape` function
-> [(Text, Text)] |

Returns a list of escaped field names and `"?"` placeholder values for performing inserts. This does not include generated columns.

Does not include generated columns.

Since: 2.11.0.0

parseExistsResult :: Maybe [PersistValue] -> Text -> String -> Bool #