graphql-engine-1.0.0: GraphQL API over Postgres
Safe HaskellNone
LanguageHaskell2010

Hasura.Backends.MSSQL.ToQuery

Description

MSSQL ToQuery

Convert the simple T-SQL AST to an SQL query, ready to be passed to the odbc package's query/exec functions.

We define a custom prettyprinter with the type Printer.

If you'd like to trace and see what a Printer looks like as SQL, you can use something like: > ltraceM "sql" (ODBC.renderQuery (toQueryPretty myPrinter))

Synopsis

Types

data Printer Source #

Instances

Instances details
Eq Printer Source # 
Instance details

Defined in Hasura.Backends.MSSQL.ToQuery

Methods

(==) :: Printer -> Printer -> Bool #

(/=) :: Printer -> Printer -> Bool #

Show Printer Source # 
Instance details

Defined in Hasura.Backends.MSSQL.ToQuery

IsString Printer Source # 
Instance details

Defined in Hasura.Backends.MSSQL.ToQuery

Methods

fromString :: String -> Printer #

Instances

Printer generators

fromInsertValuesIntoTempTable :: InsertValuesIntoTempTable -> Printer Source #

Generate a statement to insert values into temporary table.

mergeSourceAlias :: Text Source #

Alias for the source table in a MERGE statement. Used when pretty printing MERGE statments.

mergeTargetAlias :: Text Source #

Alias for the target table in a MERGE statement. Used when pretty printing MERGE statments.

fromMergeUsing :: MergeUsing -> Printer Source #

USING section of a MERGE statement. Used in fromMerge.

fromMergeOn :: MergeOn -> Printer Source #

ON section of a MERGE statement. Used in fromMerge.

fromMergeWhenMatched :: MergeWhenMatched -> Printer Source #

WHEN MATCHED section of a MERGE statement. Used in fromMerge.

fromMergeWhenNotMatched :: MergeWhenNotMatched -> Printer Source #

WHEN NOT MATCHED section of a MERGE statement. Used in fromMerge.

fromMerge :: Merge -> Printer Source #

Generate a MERGE SQL statement

fromDelete :: Delete -> Printer Source #

Generate a delete statement

Delete
  (Aliased (TableName "table" "schema") "alias")
  [ColumnName "id", ColumnName "name"]
  (Where [OpExpression EQ' (ValueExpression (IntValue 1)) (ValueExpression (IntValue 1))])

Becomes:

DELETE [alias] OUTPUT DELETED.[id], DELETED.[name] INTO #deleted([id], [name]) FROM [schema].[table] AS [alias] WHERE ((1) = (1))

fromUpdate :: Update -> Printer Source #

Generate an update statement

Update
   (Aliased (TableName "table" "schema") "alias")
   (fromList [(ColumnName "name", ValueExpression (TextValue "updated_name"))])
   (Output Inserted)
   (TempTable (TempTableName "updated") [ColumnName "id", ColumnName "name"])
   (Where [OpExpression EQ' (ColumnName "id") (ValueExpression (IntValue 1))])

Becomes:

UPDATE [alias] SET [alias].[name] = 'updated_name' OUTPUT INSERTED.[id], INSERTED.[name] INTO
#updated([id], [name]) FROM [schema].[table] AS [alias] WHERE (id = 1)

fromSelectIntoTempTable :: SelectIntoTempTable -> Printer Source #

Converts SelectIntoTempTable.

SelectIntoTempTable (TempTableName "deleted")  [UnifiedColumn "id" IntegerType, UnifiedColumn "name" TextType] (TableName "table" "schema")

Becomes:

SELECT [id], [name] INTO #deleted([id], [name]) FROM [schema].[table] WHERE (1<>1) UNION ALL SELECT [id], [name] FROM [schema].[table];

We add the `UNION ALL` part to avoid copying identity constraints, and we cast columns with types such as timestamp which are non-insertable to a different type.

fromTempTableName :: TempTableName -> Printer Source #

TempTableName "deleted" becomes #deleted

dropTempTableQuery :: TempTableName -> Printer Source #

@TempTableName "temp_table" is converted to "DROP TABLE #temp_table"

collapseWhere :: Expression -> Maybe Expression Source #

Drop useless examples like this from the output:

WHERE (((1<>1)) AND ((1=1))) AND ((1=1))

And

WHERE ((1<>1))

They're redundant, but make the output less readable.

Basic printing API

toQueryFlat :: Printer -> Query Source #

Pretty-prints a Printer as one line, converting NewlinePrinter to space.

toQueryPretty :: Printer -> Query Source #

Pretty-prints a Printer as multiple lines as defined by the printer.

Orphan instances

ToJSON Expression Source # 
Instance details

Methods

toJSON :: Expression -> Value

toEncoding :: Expression -> Encoding

toJSONList :: [Expression] -> Value

toEncodingList :: [Expression] -> Encoding