2022-04-10 07:47:15 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
|
|
module Test.Data
|
2022-07-29 11:05:57 +03:00
|
|
|
( -- = Schema
|
|
|
|
schemaTables,
|
|
|
|
-- = Artists table
|
|
|
|
artistsTableName,
|
|
|
|
artistsRows,
|
|
|
|
artistsRowsById,
|
|
|
|
artistsTableRelationships,
|
|
|
|
-- = Albums table
|
|
|
|
albumsTableName,
|
|
|
|
albumsRelationshipName,
|
|
|
|
albumsRows,
|
2022-08-19 10:00:46 +03:00
|
|
|
albumsRowsById,
|
2022-07-29 11:05:57 +03:00
|
|
|
albumsTableRelationships,
|
|
|
|
artistRelationshipName,
|
2022-08-01 03:40:45 +03:00
|
|
|
tracksRelationshipName,
|
2022-07-29 11:05:57 +03:00
|
|
|
-- = Customers table
|
|
|
|
customersTableName,
|
|
|
|
customersRows,
|
|
|
|
customersTableRelationships,
|
|
|
|
supportRepRelationshipName,
|
|
|
|
-- = Employees table
|
|
|
|
employeesTableName,
|
|
|
|
employeesRows,
|
|
|
|
employeesRowsById,
|
|
|
|
employeesTableRelationships,
|
|
|
|
supportRepForCustomersRelationshipName,
|
|
|
|
-- = Invoices table
|
|
|
|
invoicesTableName,
|
|
|
|
invoicesRows,
|
2022-08-01 03:40:45 +03:00
|
|
|
-- = InvoiceLines table
|
|
|
|
invoiceLinesTableName,
|
|
|
|
invoiceLinesRows,
|
|
|
|
-- = MediaTypes table
|
|
|
|
mediaTypesTableName,
|
|
|
|
mediaTypesRows,
|
|
|
|
-- = Tracks table
|
|
|
|
tracksTableName,
|
|
|
|
tracksRows,
|
|
|
|
tracksTableRelationships,
|
|
|
|
invoiceLinesRelationshipName,
|
|
|
|
mediaTypeRelationshipName,
|
2022-08-19 10:00:46 +03:00
|
|
|
albumRelationshipName,
|
2022-09-20 06:59:47 +03:00
|
|
|
genreRelationshipName,
|
|
|
|
-- = Genres table
|
|
|
|
genresTableName,
|
|
|
|
genresRows,
|
|
|
|
genresTableRelationships,
|
2022-07-29 11:05:57 +03:00
|
|
|
-- = Utilities
|
|
|
|
emptyQuery,
|
2022-04-10 07:47:15 +03:00
|
|
|
sortBy,
|
2022-06-24 09:58:25 +03:00
|
|
|
filterColumnsByQueryFields,
|
2022-08-01 03:40:45 +03:00
|
|
|
filterColumns,
|
|
|
|
renameColumns,
|
2022-07-29 11:05:57 +03:00
|
|
|
onlyKeepRelationships,
|
2022-07-20 08:20:49 +03:00
|
|
|
queryFields,
|
|
|
|
responseRows,
|
|
|
|
sortResponseRowsBy,
|
2022-07-28 08:39:48 +03:00
|
|
|
responseAggregates,
|
2022-09-21 08:11:53 +03:00
|
|
|
mkFieldsMap,
|
|
|
|
insertField,
|
|
|
|
deleteField,
|
|
|
|
field,
|
2022-07-20 08:20:49 +03:00
|
|
|
_ColumnFieldNumber,
|
|
|
|
_ColumnFieldString,
|
|
|
|
_ColumnFieldBoolean,
|
2022-07-29 11:05:57 +03:00
|
|
|
columnField,
|
2022-09-20 06:59:47 +03:00
|
|
|
queryComparisonColumn,
|
|
|
|
currentComparisonColumn,
|
2022-08-19 10:00:46 +03:00
|
|
|
orderByColumn,
|
2022-04-10 07:47:15 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
import Codec.Compression.GZip qualified as GZip
|
2022-09-21 08:11:53 +03:00
|
|
|
import Control.Arrow (first, (>>>))
|
2022-07-26 05:28:57 +03:00
|
|
|
import Control.Lens (Index, IxValue, Ixed, Traversal', ix, (%~), (&), (^.), (^..), (^?))
|
2022-09-21 08:11:53 +03:00
|
|
|
import Data.Aeson (eitherDecodeStrict)
|
2022-07-26 05:28:57 +03:00
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Aeson.Lens (_Bool, _Number, _String)
|
2022-08-01 03:40:45 +03:00
|
|
|
import Data.Bifunctor (bimap)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.ByteString (ByteString)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BSL
|
2022-08-04 11:34:45 +03:00
|
|
|
import Data.CaseInsensitive (CI)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.CaseInsensitive qualified as CI
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.FileEmbed (embedFile, makeRelativeToProject)
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-08-01 03:40:45 +03:00
|
|
|
import Data.List (find, sortOn)
|
2022-08-04 11:34:45 +03:00
|
|
|
import Data.List.NonEmpty (NonEmpty (..))
|
|
|
|
import Data.List.NonEmpty qualified as NonEmpty
|
2022-07-20 08:20:49 +03:00
|
|
|
import Data.Maybe (fromMaybe, mapMaybe)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Data.Scientific (Scientific)
|
|
|
|
import Data.Text (Text)
|
2022-06-13 23:58:44 +03:00
|
|
|
import Data.Text qualified as Text
|
|
|
|
import Data.Text.Encoding qualified as Text
|
2022-07-20 08:20:49 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-06-13 23:58:44 +03:00
|
|
|
import Text.XML qualified as XML
|
|
|
|
import Text.XML.Lens qualified as XML
|
2022-04-10 07:47:15 +03:00
|
|
|
import Prelude
|
|
|
|
|
|
|
|
schemaBS :: ByteString
|
2022-09-27 10:20:46 +03:00
|
|
|
schemaBS = $(makeRelativeToProject "test/Test/Data/schema-tables.json" >>= embedFile)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
schemaTables :: [API.TableInfo]
|
2022-09-21 08:11:53 +03:00
|
|
|
schemaTables = sortOn API._tiName . either error id . eitherDecodeStrict $ schemaBS
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-07-28 08:39:48 +03:00
|
|
|
numericColumns :: [API.ColumnName]
|
2022-09-21 08:11:53 +03:00
|
|
|
numericColumns = schemaTables >>= (API._tiColumns >>> mapMaybe (\API.ColumnInfo {..} -> if _ciType == API.NumberTy then Just _ciName else Nothing))
|
2022-07-28 08:39:48 +03:00
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
chinookXmlBS :: ByteString
|
2022-09-27 10:20:46 +03:00
|
|
|
chinookXmlBS = $(makeRelativeToProject "test/Test/Data/ChinookData.xml.gz" >>= embedFile)
|
2022-06-13 23:58:44 +03:00
|
|
|
|
|
|
|
chinookXml :: XML.Document
|
|
|
|
chinookXml = XML.parseLBS_ XML.def . GZip.decompress $ BSL.fromStrict chinookXmlBS
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
readTableFromXmlIntoRows :: API.TableName -> [HashMap API.FieldName API.FieldValue]
|
2022-08-04 11:34:45 +03:00
|
|
|
readTableFromXmlIntoRows tableName =
|
2022-06-13 23:58:44 +03:00
|
|
|
rowToJsonObject <$> tableRows
|
|
|
|
where
|
2022-08-04 11:34:45 +03:00
|
|
|
tableNameToXmlTag :: API.TableName -> CI Text
|
|
|
|
tableNameToXmlTag (API.TableName names) = CI.mk . Text.intercalate "_" $ NonEmpty.toList names
|
|
|
|
|
2022-06-13 23:58:44 +03:00
|
|
|
tableRows :: [XML.Element]
|
2022-08-04 11:34:45 +03:00
|
|
|
tableRows = chinookXml ^.. XML.root . XML.nodes . traverse . XML._Element . XML.named (tableNameToXmlTag tableName)
|
2022-06-13 23:58:44 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
rowToJsonObject :: XML.Element -> HashMap API.FieldName API.FieldValue
|
2022-06-13 23:58:44 +03:00
|
|
|
rowToJsonObject element =
|
|
|
|
let columnElements = element ^.. XML.nodes . traverse . XML._Element
|
|
|
|
keyValuePairs = columnElementToProperty <$> columnElements
|
2022-09-21 08:11:53 +03:00
|
|
|
in HashMap.fromList keyValuePairs
|
2022-06-13 23:58:44 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
columnElementToProperty :: XML.Element -> (API.FieldName, API.FieldValue)
|
2022-06-13 23:58:44 +03:00
|
|
|
columnElementToProperty columnElement =
|
2022-09-21 08:11:53 +03:00
|
|
|
let name = columnElement ^. XML.localName
|
2022-07-28 08:39:48 +03:00
|
|
|
value = case columnElement ^. XML.nodes of
|
|
|
|
[] -> API.mkColumnFieldValue $ J.Null
|
|
|
|
_ ->
|
|
|
|
let textValue = Text.concat $ columnElement ^.. XML.text
|
2022-09-21 08:11:53 +03:00
|
|
|
in if API.ColumnName name `elem` numericColumns
|
2022-07-28 08:39:48 +03:00
|
|
|
then case eitherDecodeStrict $ Text.encodeUtf8 textValue of
|
|
|
|
Left _ -> API.mkColumnFieldValue $ J.String textValue
|
|
|
|
Right scientific -> API.mkColumnFieldValue $ J.Number scientific
|
|
|
|
else API.mkColumnFieldValue $ J.String textValue
|
2022-09-21 08:11:53 +03:00
|
|
|
in (API.FieldName name, value)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-08-04 11:34:45 +03:00
|
|
|
mkTableName :: Text -> API.TableName
|
|
|
|
mkTableName = API.TableName . (:| [])
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
artistsTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
artistsTableName = mkTableName "Artist"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
artistsRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
artistsRows = sortBy (API.FieldName "ArtistId") $ readTableFromXmlIntoRows artistsTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
artistsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-07-29 11:05:57 +03:00
|
|
|
artistsRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\artist -> (,artist) <$> artist ^? field "ArtistId" . _ColumnFieldNumber) artistsRows
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
albumsRelationshipName :: API.RelationshipName
|
|
|
|
albumsRelationshipName = API.RelationshipName "Albums"
|
|
|
|
|
|
|
|
artistsTableRelationships :: API.TableRelationships
|
|
|
|
artistsTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "ArtistId", API.ColumnName "ArtistId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
artistsTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (albumsRelationshipName, API.Relationship albumsTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
albumsTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
albumsTableName = mkTableName "Album"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
albumsRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
albumsRows = sortBy (API.FieldName "AlbumId") $ readTableFromXmlIntoRows albumsTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
albumsRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-08-19 10:00:46 +03:00
|
|
|
albumsRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\album -> (,album) <$> album ^? field "AlbumId" . _ColumnFieldNumber) albumsRows
|
2022-08-19 10:00:46 +03:00
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
albumsTableRelationships :: API.TableRelationships
|
|
|
|
albumsTableRelationships =
|
2022-08-01 03:40:45 +03:00
|
|
|
let artistsJoinFieldMapping = HashMap.fromList [(API.ColumnName "ArtistId", API.ColumnName "ArtistId")]
|
|
|
|
tracksJoinFieldMapping = HashMap.fromList [(API.ColumnName "AlbumId", API.ColumnName "AlbumId")]
|
2022-07-29 11:05:57 +03:00
|
|
|
in API.TableRelationships
|
|
|
|
albumsTableName
|
|
|
|
( HashMap.fromList
|
2022-08-01 03:40:45 +03:00
|
|
|
[ (artistRelationshipName, API.Relationship artistsTableName API.ObjectRelationship artistsJoinFieldMapping),
|
|
|
|
(tracksRelationshipName, API.Relationship tracksTableName API.ArrayRelationship tracksJoinFieldMapping)
|
2022-07-29 11:05:57 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
artistRelationshipName :: API.RelationshipName
|
|
|
|
artistRelationshipName = API.RelationshipName "Artist"
|
|
|
|
|
2022-08-01 03:40:45 +03:00
|
|
|
tracksRelationshipName :: API.RelationshipName
|
|
|
|
tracksRelationshipName = API.RelationshipName "Tracks"
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
customersTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
customersTableName = mkTableName "Customer"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
customersRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
customersRows = sortBy (API.FieldName "CustomerId") $ readTableFromXmlIntoRows customersTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
customersTableRelationships :: API.TableRelationships
|
|
|
|
customersTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "SupportRepId", API.ColumnName "EmployeeId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
customersTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (supportRepRelationshipName, API.Relationship employeesTableName API.ObjectRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
supportRepRelationshipName :: API.RelationshipName
|
|
|
|
supportRepRelationshipName = API.RelationshipName "SupportRep"
|
|
|
|
|
|
|
|
employeesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
employeesTableName = mkTableName "Employee"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
employeesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
employeesRows = sortBy (API.FieldName "EmployeeId") $ readTableFromXmlIntoRows employeesTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
employeesRowsById :: HashMap Scientific (HashMap API.FieldName API.FieldValue)
|
2022-07-29 11:05:57 +03:00
|
|
|
employeesRowsById =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.fromList $ mapMaybe (\employee -> (,employee) <$> employee ^? field "EmployeeId" . _ColumnFieldNumber) employeesRows
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
employeesTableRelationships :: API.TableRelationships
|
|
|
|
employeesTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "EmployeeId", API.ColumnName "SupportRepId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
employeesTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (supportRepForCustomersRelationshipName, API.Relationship customersTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
supportRepForCustomersRelationshipName :: API.RelationshipName
|
|
|
|
supportRepForCustomersRelationshipName = API.RelationshipName "SupportRepForCustomers"
|
|
|
|
|
|
|
|
invoicesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
invoicesTableName = mkTableName "Invoice"
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
invoicesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
invoicesRows = sortBy (API.FieldName "InvoiceId") $ readTableFromXmlIntoRows invoicesTableName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-08-01 03:40:45 +03:00
|
|
|
invoiceLinesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
invoiceLinesTableName = mkTableName "InvoiceLine"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
invoiceLinesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
invoiceLinesRows = sortBy (API.FieldName "InvoiceLineId") $ readTableFromXmlIntoRows invoiceLinesTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
mediaTypesTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
mediaTypesTableName = mkTableName "MediaType"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
mediaTypesRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
mediaTypesRows = sortBy (API.FieldName "MediaTypeId") $ readTableFromXmlIntoRows mediaTypesTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
tracksTableName :: API.TableName
|
2022-08-04 11:34:45 +03:00
|
|
|
tracksTableName = mkTableName "Track"
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
tracksRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
tracksRows = sortBy (API.FieldName "TrackId") $ readTableFromXmlIntoRows tracksTableName
|
2022-08-01 03:40:45 +03:00
|
|
|
|
|
|
|
tracksTableRelationships :: API.TableRelationships
|
|
|
|
tracksTableRelationships =
|
|
|
|
let invoiceLinesJoinFieldMapping = HashMap.fromList [(API.ColumnName "TrackId", API.ColumnName "TrackId")]
|
|
|
|
mediaTypeJoinFieldMapping = HashMap.fromList [(API.ColumnName "MediaTypeId", API.ColumnName "MediaTypeId")]
|
2022-08-19 10:00:46 +03:00
|
|
|
albumJoinFieldMapping = HashMap.fromList [(API.ColumnName "AlbumId", API.ColumnName "AlbumId")]
|
2022-09-20 06:59:47 +03:00
|
|
|
genreJoinFieldMapping = HashMap.fromList [(API.ColumnName "GenreId", API.ColumnName "GenreId")]
|
2022-08-01 03:40:45 +03:00
|
|
|
in API.TableRelationships
|
|
|
|
tracksTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (invoiceLinesRelationshipName, API.Relationship invoiceLinesTableName API.ArrayRelationship invoiceLinesJoinFieldMapping),
|
2022-08-19 10:00:46 +03:00
|
|
|
(mediaTypeRelationshipName, API.Relationship mediaTypesTableName API.ObjectRelationship mediaTypeJoinFieldMapping),
|
2022-09-20 06:59:47 +03:00
|
|
|
(albumRelationshipName, API.Relationship albumsTableName API.ObjectRelationship albumJoinFieldMapping),
|
|
|
|
(genreRelationshipName, API.Relationship genresTableName API.ObjectRelationship genreJoinFieldMapping)
|
2022-08-01 03:40:45 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
invoiceLinesRelationshipName :: API.RelationshipName
|
|
|
|
invoiceLinesRelationshipName = API.RelationshipName "InvoiceLines"
|
|
|
|
|
|
|
|
mediaTypeRelationshipName :: API.RelationshipName
|
|
|
|
mediaTypeRelationshipName = API.RelationshipName "MediaType"
|
|
|
|
|
2022-08-19 10:00:46 +03:00
|
|
|
albumRelationshipName :: API.RelationshipName
|
|
|
|
albumRelationshipName = API.RelationshipName "Album"
|
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
genreRelationshipName :: API.RelationshipName
|
|
|
|
genreRelationshipName = API.RelationshipName "Genre"
|
|
|
|
|
|
|
|
genresTableName :: API.TableName
|
|
|
|
genresTableName = mkTableName "Genre"
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
genresRows :: [HashMap API.FieldName API.FieldValue]
|
|
|
|
genresRows = sortBy (API.FieldName "GenreId") $ readTableFromXmlIntoRows genresTableName
|
2022-09-20 06:59:47 +03:00
|
|
|
|
|
|
|
genresTableRelationships :: API.TableRelationships
|
|
|
|
genresTableRelationships =
|
|
|
|
let joinFieldMapping = HashMap.fromList [(API.ColumnName "GenreId", API.ColumnName "GenreId")]
|
|
|
|
in API.TableRelationships
|
|
|
|
genresTableName
|
|
|
|
( HashMap.fromList
|
|
|
|
[ (tracksRelationshipName, API.Relationship tracksTableName API.ArrayRelationship joinFieldMapping)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
emptyQuery :: API.Query
|
|
|
|
emptyQuery = API.Query Nothing Nothing Nothing Nothing Nothing Nothing
|
2022-07-28 08:39:48 +03:00
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
sortBy :: (Ixed m, Ord (IxValue m)) => Index m -> [m] -> [m]
|
2022-04-10 07:47:15 +03:00
|
|
|
sortBy propName = sortOn (^? ix propName)
|
2022-06-24 09:58:25 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
filterColumnsByQueryFields :: API.Query -> HashMap API.FieldName API.FieldValue -> HashMap API.FieldName API.FieldValue
|
2022-06-24 09:58:25 +03:00
|
|
|
filterColumnsByQueryFields query =
|
2022-09-21 08:11:53 +03:00
|
|
|
HashMap.filterWithKey (\key _value -> key `elem` columns)
|
2022-06-24 09:58:25 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns = HashMap.keys $ queryFields query
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
filterColumns :: [Text] -> [HashMap API.FieldName API.FieldValue] -> [HashMap API.FieldName API.FieldValue]
|
2022-08-01 03:40:45 +03:00
|
|
|
filterColumns columns =
|
2022-09-21 08:11:53 +03:00
|
|
|
fmap (HashMap.filterWithKey (\key _value -> key `elem` columns'))
|
2022-08-01 03:40:45 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns' = API.FieldName <$> columns
|
2022-08-01 03:40:45 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
renameColumns :: [(Text, Text)] -> [HashMap API.FieldName API.FieldValue] -> [HashMap API.FieldName API.FieldValue]
|
2022-08-01 03:40:45 +03:00
|
|
|
renameColumns columns =
|
2022-09-21 08:11:53 +03:00
|
|
|
fmap (HashMap.fromList . fmap rename . HashMap.toList)
|
2022-08-01 03:40:45 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
columns' = bimap API.FieldName API.FieldName <$> columns
|
2022-08-01 03:40:45 +03:00
|
|
|
rename original@(key, value) =
|
|
|
|
case find (\(k, _) -> k == key) columns' of
|
|
|
|
Just (_, renamedKey) -> (renamedKey, value)
|
|
|
|
Nothing -> original
|
|
|
|
|
2022-07-29 11:05:57 +03:00
|
|
|
onlyKeepRelationships :: [API.RelationshipName] -> API.TableRelationships -> API.TableRelationships
|
|
|
|
onlyKeepRelationships names tableRels =
|
|
|
|
tableRels & API.trRelationships %~ HashMap.filterWithKey (\relName _ -> relName `elem` names)
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
queryFields :: API.Query -> HashMap API.FieldName API.Field
|
2022-07-20 08:20:49 +03:00
|
|
|
queryFields = fromMaybe mempty . API._qFields
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
responseRows :: API.QueryResponse -> [HashMap API.FieldName API.FieldValue]
|
2022-07-20 08:20:49 +03:00
|
|
|
responseRows = fromMaybe [] . API._qrRows
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
sortResponseRowsBy :: Text -> API.QueryResponse -> API.QueryResponse
|
|
|
|
sortResponseRowsBy columnName response = response & API.qrRows %~ (fmap (sortBy (API.FieldName columnName)))
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
responseAggregates :: API.QueryResponse -> HashMap API.FieldName J.Value
|
2022-07-28 08:39:48 +03:00
|
|
|
responseAggregates = fromMaybe mempty . API._qrAggregates
|
|
|
|
|
2022-09-21 08:11:53 +03:00
|
|
|
mkFieldsMap :: [(Text, v)] -> HashMap API.FieldName v
|
|
|
|
mkFieldsMap = HashMap.fromList . fmap (first API.FieldName)
|
|
|
|
|
|
|
|
insertField :: Text -> v -> HashMap API.FieldName v -> HashMap API.FieldName v
|
|
|
|
insertField fieldName = HashMap.insert (API.FieldName fieldName)
|
|
|
|
|
|
|
|
deleteField :: Text -> HashMap API.FieldName v -> HashMap API.FieldName v
|
|
|
|
deleteField fieldName = HashMap.delete (API.FieldName fieldName)
|
|
|
|
|
|
|
|
field :: (Ixed m, Index m ~ API.FieldName) => Text -> Traversal' m (IxValue m)
|
|
|
|
field fieldName = ix (API.FieldName fieldName)
|
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldNumber :: Traversal' API.FieldValue Scientific
|
|
|
|
_ColumnFieldNumber = API._ColumnFieldValue . _Number
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldString :: Traversal' API.FieldValue Text
|
|
|
|
_ColumnFieldString = API._ColumnFieldValue . _String
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
_ColumnFieldBoolean :: Traversal' API.FieldValue Bool
|
|
|
|
_ColumnFieldBoolean = API._ColumnFieldValue . _Bool
|
2022-07-29 11:05:57 +03:00
|
|
|
|
|
|
|
columnField :: Text -> API.Field
|
|
|
|
columnField = API.ColumnField . API.ColumnName
|
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
queryComparisonColumn :: Text -> API.ComparisonColumn
|
|
|
|
queryComparisonColumn columnName = API.ComparisonColumn API.QueryTable $ API.ColumnName columnName
|
2022-07-29 11:05:57 +03:00
|
|
|
|
2022-09-20 06:59:47 +03:00
|
|
|
currentComparisonColumn :: Text -> API.ComparisonColumn
|
|
|
|
currentComparisonColumn columnName = API.ComparisonColumn API.CurrentTable $ API.ColumnName columnName
|
2022-08-19 10:00:46 +03:00
|
|
|
|
|
|
|
orderByColumn :: [API.RelationshipName] -> Text -> API.OrderDirection -> API.OrderByElement
|
|
|
|
orderByColumn targetPath columnName orderDirection =
|
|
|
|
API.OrderByElement targetPath (API.OrderByColumn $ API.ColumnName columnName) orderDirection
|