getDistinctFieldQuery
    :: forall a m rec
     . (MonadResource m, MonadSql m, PersistFieldSql a, SqlRecord rec)
     => EntityField rec a
     -> m (Query a)
getDistinctFieldQuery entityField = Sql.runTransaction $ do
    table <- Transaction $ Sqlite.getTableName (undefined :: rec)
    field <- Transaction $ Sqlite.getFieldName entityField
    let queryText = [i|SELECT DISTINCT #{table}.#{field} FROM #{table}|]
    return Query{convert = Simple converter, ..}
  where
    queryName :: Text
    queryName = "distinctFieldQuery"

    commonTableExpressions :: [CTE]
    commonTableExpressions = []

    params :: [PersistValue]
    params = []

    converter
        :: (MonadIO n, MonadLogger n, MonadThrow n)
        => [PersistValue] -> n a
    converter [v] | Right val <- Sqlite.fromPersistValue v = return val
    converter actualValues = logThrowM $ QueryResultUnparseable actualValues
        [Sqlite.sqlType (Proxy :: Proxy a)]
getDistinctFieldQuery
    :: forall a m rec
     . (MonadResource m, MonadSql m, PersistFieldSql a, SqlRecord rec)
     => EntityField rec a
     -> m (Query a)
getDistinctFieldQuery entityField = Sql.runTransaction $ do
    table <- Transaction $ Sqlite.getTableName (undefined :: rec)
    field <- Transaction $ Sqlite.getFieldName entityField
    let queryText = [i|SELECT DISTINCT #{table}.#{field} FROM #{table}|]
    return Query{convert = Simple converter, ..}
  where
    queryName :: Text
    queryName = "distinctFieldQuery"

    commonTableExpressions :: [CTE]
    commonTableExpressions = []

    params :: [PersistValue]
    params = []

    converter
        :: (MonadIO n, MonadLogger n, MonadThrow n)
        => [PersistValue] -> n a
    converter [v] | Right val <- Sqlite.fromPersistValue v = return val
    converter actualValues = logThrowM $ QueryResultUnparseable actualValues
        [Sqlite.sqlType (Proxy :: Proxy a)]
getDistinctFieldQuery entityField = Sql.runTransaction $ do
    table <- Transaction $ Sqlite.getTableName (undefined :: rec)
    field <- Transaction $ Sqlite.getFieldName entityField
    let queryText = [i|SELECT DISTINCT #{table}.#{field} FROM #{table}|]
    return Query{convert = Simple converter, ..}
  where
    queryName :: Text
    queryName = "distinctFieldQuery"

    commonTableExpressions :: [CTE]
    commonTableExpressions = []

    params :: [PersistValue]
    params = []

    converter
        :: (MonadIO n, MonadLogger n, MonadThrow n)
        => [PersistValue] -> n a
    converter [v] | Right val <- Sqlite.fromPersistValue v = return val
    converter actualValues = logThrowM $ QueryResultUnparseable actualValues
        [Sqlite.sqlType (Proxy :: Proxy a)]