2022-02-25 19:08:18 +03:00
|
|
|
module Hasura.Backends.DataWrapper.IR.Query
|
2021-12-22 03:10:28 +03:00
|
|
|
( Query (..),
|
|
|
|
Field (..),
|
|
|
|
ColumnContents (..),
|
|
|
|
RelationshipContents (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-02-25 19:08:18 +03:00
|
|
|
import Hasura.Backends.DataWrapper.IR.Column qualified as Column (Name)
|
|
|
|
import Hasura.Backends.DataWrapper.IR.Expression (Expression)
|
|
|
|
import Hasura.Backends.DataWrapper.IR.OrderBy (OrderBy)
|
|
|
|
import Hasura.Backends.DataWrapper.IR.Table qualified as Table (Name)
|
2021-12-22 03:10:28 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | An abstract request to retrieve structured data from some source.
|
2021-12-22 03:10:28 +03:00
|
|
|
data Query = Query
|
|
|
|
{ -- NOTE: We should clarify what the 'Text' key is supposed to indicate.
|
|
|
|
fields :: HashMap Text Field,
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | Reference to the table these fields are in.
|
2021-12-22 03:10:28 +03:00
|
|
|
from :: Table.Name,
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | Optionally limit to N results.
|
2021-12-22 03:10:28 +03:00
|
|
|
limit :: Maybe Int,
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | Optionally offset from the Nth result.
|
2021-12-22 03:10:28 +03:00
|
|
|
offset :: Maybe Int,
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | Optionally constrain the results to satisfy some predicate.
|
2021-12-22 03:10:28 +03:00
|
|
|
where_ :: Maybe Expression,
|
2022-02-25 19:08:18 +03:00
|
|
|
-- | Optionally order the results by the value of one or more fields.
|
2021-12-22 03:10:28 +03:00
|
|
|
orderBy :: [OrderBy]
|
|
|
|
}
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | The specific fields that are targeted by a 'Query'.
|
|
|
|
--
|
|
|
|
-- A field conceptually falls under one of the two following categories:
|
|
|
|
-- 1. a "column" within the data store that the query is being issued against
|
|
|
|
-- 2. a "relationship", which indicates that the field is the result of
|
|
|
|
-- another query that must be executed on its own
|
|
|
|
data Field
|
|
|
|
= Column ColumnContents
|
|
|
|
| Relationship RelationshipContents
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
|
|
|
|
newtype ColumnContents = ColumnContents
|
|
|
|
{ column :: Column.Name
|
|
|
|
}
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
|
|
|
|
-- | A relationship consists of the following components:
|
|
|
|
-- - a sub-query, from the perspective that a relationship field will occur
|
|
|
|
-- within a broader 'Query'
|
|
|
|
-- - a join condition relating the data returned by the sub-query with that
|
|
|
|
-- of the broader 'Query'
|
|
|
|
--
|
|
|
|
-- cf. https://en.wikipedia.org/wiki/Join_(SQL)
|
|
|
|
-- https://www.postgresql.org/docs/13/tutorial-join.html
|
|
|
|
-- https://www.postgresql.org/docs/13/queries-table-expressions.html#QUERIES-FROM
|
|
|
|
data RelationshipContents = RelationshipContents
|
|
|
|
{ joinCondition :: HashMap PrimaryKey ForeignKey,
|
|
|
|
query :: Query
|
|
|
|
}
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
|
|
|
|
newtype PrimaryKey = PrimaryKey Column.Name
|
|
|
|
deriving stock (Data, Generic)
|
|
|
|
deriving newtype (Eq, Hashable, Ord, Show)
|
|
|
|
|
|
|
|
newtype ForeignKey = ForeignKey Column.Name
|
|
|
|
deriving stock (Data, Generic)
|
|
|
|
deriving newtype (Eq, Hashable, Ord, Show)
|