Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Query by pairs of values #30

Closed
wants to merge 4 commits into from
Closed

WIP: Query by pairs of values #30

wants to merge 4 commits into from

Conversation

amitaibu
Copy link
Owner

@amitaibu amitaibu commented Nov 17, 2023

I'm trying to query records by pair of values.

I have these projects:
image

Let's say I want to fetch only the records that have
(ProjectTypeOngoing, 2) and (ProjectTypeNotStarted, 3)

You can grab this PR, make db and go to http://localhost:8000/Projects

Comment on lines +11 to +17
-- Fetch only specific projects by a pair of values.
-- In this case we really fetch all, but show how we pair the values we want to
-- query by.
let pairs = projects
|> fmap (\project -> "(" ++ show project.projectType ++ ", " ++ project.participants ++ ")")

projectsQuery :: [Project] <- sqlQuery "SELECT * FROM projects WHERE (project_type, participants) IN (VALUES ?)" (Only (In pairs))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the query. It compiles, but isn't right:

image

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to implement ToField:

instance ToField (ProjectType, Text) where
    toField (projectType, participants) = Many [toField projectType, toField participants]


instance Controller ProjectsController where
    action ProjectsAction = do
        projects <- query @Project |> fetch

        -- Fetch only specific projects by a pair of values.
        -- In this case we really fetch all, but show how we pair the values we want to
        -- query by.
        let pairs = projects |> fmap (\project -> (project.projectType, project.participants))

        -- Use a parameterized query to handle list of tuples
        projectsQuery :: [Project] <- sqlQuery "SELECT * FROM projects WHERE (project_type, participants) IN ?" (Only $ In pairs)


        render IndexView { .. }

But this now has this error

image

@amitaibu amitaibu mentioned this pull request Nov 17, 2023
@amitaibu amitaibu closed this Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant