| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.Persist.Sql.Util
Synopsis
- parseEntityValues :: PersistEntity record => EntityDef -> [PersistValue] -> Either Text (Entity record)
- entityColumnNames :: EntityDef -> SqlBackend -> [Sql]
- keyAndEntityColumnNames :: EntityDef -> SqlBackend -> [Sql]
- entityColumnCount :: EntityDef -> Int
- isIdField :: PersistEntity record => EntityField record typ -> Bool
- hasCompositeKey :: EntityDef -> Bool
- hasCompositePrimaryKey :: EntityDef -> Bool
- hasNaturalKey :: EntityDef -> Bool
- dbIdColumns :: SqlBackend -> EntityDef -> [Text]
- dbIdColumnsEsc :: (DBName -> Text) -> EntityDef -> [Text]
- dbColumns :: SqlBackend -> EntityDef -> [Text]
- updateFieldDef :: PersistEntity v => Update v -> FieldDef
- updatePersistValue :: Update v -> PersistValue
- mkUpdateText :: PersistEntity record => SqlBackend -> Update record -> Text
- mkUpdateText' :: PersistEntity record => (DBName -> Text) -> (Text -> Text) -> Update record -> Text
- commaSeparated :: [Text] -> Text
- parenWrapped :: Text -> Text
- mkInsertValues :: PersistEntity rec => rec -> [PersistValue]
- mkInsertPlaceholders :: EntityDef -> (DBName -> Text) -> [(Text, Text)]
Documentation
parseEntityValues :: PersistEntity record => EntityDef -> [PersistValue] -> Either Text (Entity record) Source #
entityColumnNames :: EntityDef -> SqlBackend -> [Sql] Source #
keyAndEntityColumnNames :: EntityDef -> SqlBackend -> [Sql] Source #
entityColumnCount :: EntityDef -> Int Source #
isIdField :: PersistEntity record => EntityField record typ -> Bool Source #
hasCompositeKey :: EntityDef -> Bool Source #
Deprecated: hasCompositeKey is misleading - it returns True if the entity is defined with the Primary keyword. See issue #685 for discussion.
If you want the same behavior, use hasNaturalKey. If you want to know if the key has multiple fields, use hasCompositePrimaryKey. This function will be removed in the next major version.
Deprecated as of 2.11. See hasNaturalKey or hasCompositePrimaryKey
for replacements.
hasCompositePrimaryKey :: EntityDef -> Bool Source #
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
hasNaturalKey :: EntityDef -> Bool Source #
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
dbIdColumns :: SqlBackend -> EntityDef -> [Text] Source #
updateFieldDef :: PersistEntity v => Update v -> FieldDef Source #
updatePersistValue :: Update v -> PersistValue Source #
mkUpdateText :: PersistEntity record => SqlBackend -> Update record -> Text Source #
mkUpdateText' :: PersistEntity record => (DBName -> Text) -> (Text -> Text) -> Update record -> Text Source #
commaSeparated :: [Text] -> Text Source #
parenWrapped :: Text -> Text Source #
mkInsertValues :: PersistEntity rec => rec -> [PersistValue] Source #
Make a list PersistValue suitable for detabase inserts. Pairs nicely
with the function mkInsertPlaceholders.
Does not include generated columns.
Since: 2.11.0.0