-- | MSSQL SQL Value
--
-- Provide a function to translate from a column value to its literal (textual)
-- value specific to MSSQL. Used in the 'BackendExecute' instance.
module Hasura.Backends.MSSQL.SQL.Value (txtEncodedColVal) where

import Data.Text.Encoding (decodeUtf8)
import Data.Text.Extended
import Database.ODBC.SQLServer qualified as ODBC
import Hasura.GraphQL.Execute.Subscription.Plan ()
import Hasura.Prelude
import Hasura.RQL.Types.Column qualified as RQL
import Hasura.RQL.Types.Metadata.Instances ()
import Hasura.SQL.Backend
import Hasura.SQL.Value (TxtEncodedVal (..))

txtEncodedVal :: ODBC.Value -> TxtEncodedVal
txtEncodedVal :: Value -> TxtEncodedVal
txtEncodedVal Value
ODBC.NullValue = TxtEncodedVal
TENull
txtEncodedVal (ODBC.ByteStringValue ByteString
b) = Text -> TxtEncodedVal
TELit (Text -> TxtEncodedVal) -> Text -> TxtEncodedVal
forall a b. (a -> b) -> a -> b
$ ByteString -> Text
decodeUtf8 ByteString
b
txtEncodedVal (ODBC.TextValue Text
t) = Text -> TxtEncodedVal
TELit Text
t
txtEncodedVal Value
val = Text -> TxtEncodedVal
TELit (Text -> TxtEncodedVal) -> Text -> TxtEncodedVal
forall a b. (a -> b) -> a -> b
$ Query -> Text
forall a. ToTxt a => a -> Text
toTxt (Query -> Text) -> Query -> Text
forall a b. (a -> b) -> a -> b
$ Value -> Query
forall a. ToSql a => a -> Query
ODBC.toSql Value
val

txtEncodedColVal :: RQL.ColumnValue 'MSSQL -> TxtEncodedVal
txtEncodedColVal :: ColumnValue 'MSSQL -> TxtEncodedVal
txtEncodedColVal = Value -> TxtEncodedVal
txtEncodedVal (Value -> TxtEncodedVal)
-> (ColumnValue 'MSSQL -> Value)
-> ColumnValue 'MSSQL
-> TxtEncodedVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ColumnValue 'MSSQL -> Value
forall (b :: BackendType). ColumnValue b -> ScalarValue b
RQL.cvValue