module Data.SqlCommenter
( sqlCommenterGoogle,
sqlCommenterStandard,
Attribute,
)
where
import Data.List.NonEmpty qualified as NE
import Data.Text.Extended (commaSeparated)
import Hasura.Prelude
import Hasura.QueryTags
import Network.URI.Encode qualified as URI
sqlCommenterGoogle :: QueryTagsAttributes -> QueryTagsComment
QueryTagsAttributes
qtAttributes
| [Attribute] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Attribute]
attributes = QueryTagsComment
emptyQueryTagsComment
| Bool
otherwise = Text -> QueryTagsComment
QueryTagsComment (Text -> QueryTagsComment) -> Text -> QueryTagsComment
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall a. (Semigroup a, IsString a) => a -> a
createSQLComment (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ NonEmpty Attribute -> Text
generateCommentTags ([Attribute] -> NonEmpty Attribute
forall a. [a] -> NonEmpty a
NE.fromList [Attribute]
attributes)
where
createSQLComment :: a -> a
createSQLComment a
comment = a
" /* " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
comment a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
" */"
attributes :: [Attribute]
attributes = QueryTagsAttributes -> [Attribute]
_unQueryTagsAttributes QueryTagsAttributes
qtAttributes
sqlCommenterStandard :: QueryTagsAttributes -> QueryTagsComment
sqlCommenterStandard :: QueryTagsAttributes -> QueryTagsComment
sqlCommenterStandard QueryTagsAttributes
qtAttributes
| [Attribute] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Attribute]
attributes = QueryTagsComment
emptyQueryTagsComment
| Bool
otherwise = Text -> QueryTagsComment
QueryTagsComment (Text -> QueryTagsComment) -> Text -> QueryTagsComment
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall a. (Semigroup a, IsString a) => a -> a
createSQLComment (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ NonEmpty Attribute -> Text
forall t.
(ToTxt t, Semigroup t, IsString t) =>
NonEmpty (t, t) -> Text
generateComment ([Attribute] -> NonEmpty Attribute
forall a. [a] -> NonEmpty a
NE.fromList [Attribute]
attributes)
where
generateComment :: NonEmpty (t, t) -> Text
generateComment NonEmpty (t, t)
attr = [t] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
commaSeparated [t
k t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"=" t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
v | (t
k, t
v) <- NonEmpty (t, t) -> [(t, t)]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (t, t)
attr]
createSQLComment :: a -> a
createSQLComment a
comment = a
" /* " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
comment a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
" */"
attributes :: [Attribute]
attributes = QueryTagsAttributes -> [Attribute]
_unQueryTagsAttributes QueryTagsAttributes
qtAttributes
generateCommentTags :: NE.NonEmpty Attribute -> Text
NonEmpty Attribute
attributes =
let
encoded :: NonEmpty Attribute
encoded = (Attribute -> Attribute)
-> NonEmpty Attribute -> NonEmpty Attribute
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map Attribute -> Attribute
urlEncodePair NonEmpty Attribute
attributes
sorted :: NonEmpty Attribute
sorted = NonEmpty Attribute -> NonEmpty Attribute
forall a. Ord a => NonEmpty a -> NonEmpty a
NE.sort NonEmpty Attribute
encoded
serialized :: Text
serialized = NonEmpty Attribute -> Text
forall t.
(ToTxt t, Semigroup t, IsString t) =>
NonEmpty (t, t) -> Text
serializePairs NonEmpty Attribute
sorted
in Text
serialized
where
urlEncodePair :: Attribute -> Attribute
urlEncodePair (Text
k, Text
v) = (Text -> Text
URI.encodeText Text
k, Text -> Text
URI.encodeText Text
v)
serializePairs :: NonEmpty (t, t) -> Text
serializePairs NonEmpty (t, t)
pairs = [t] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
commaSeparated [t
k t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"='" t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
v t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"'" | (t
k, t
v) <- NonEmpty (t, t) -> [(t, t)]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (t, t)
pairs]