Safe Haskell | None |
---|---|
Language | Haskell2010 |
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
- data Printer
- = SeqPrinter [Printer]
- | SepByPrinter Printer [Printer]
- | NewlinePrinter
- | QueryPrinter Query
- | IndentPrinter Int Printer
- (<+>) :: Printer -> Printer -> Printer
- (<+>?) :: Printer -> Maybe Printer -> Printer
- (?<+>) :: Maybe Printer -> Printer -> Printer
- fromExpression :: Expression -> Printer
- fromMethodApplicationExpression :: Expression -> MethodApplicationExpression -> Printer
- fromFunctionApplicationExpression :: FunctionApplicationExpression -> Printer
- fromOp :: Op -> Printer
- fromPath :: JsonPath -> Printer
- fromFieldName :: FieldName -> Printer
- fromInserted :: Inserted -> Printer
- fromDeleted :: Deleted -> Printer
- fromOutputColumn :: Printer -> OutputColumn -> Printer
- fromOutput :: (t -> Printer) -> Output t -> Printer
- fromInsertOutput :: InsertOutput -> Printer
- fromDeleteOutput :: DeleteOutput -> Printer
- fromUpdateOutput :: UpdateOutput -> Printer
- fromValues :: Values -> Printer
- fromValuesList :: [Values] -> Printer
- fromInsert :: Insert -> Printer
- fromSetValue :: SetValue -> Printer
- fromSetIdentityInsert :: SetIdentityInsert -> Printer
- fromInsertValuesIntoTempTable :: InsertValuesIntoTempTable -> Printer
- mergeSourceAlias :: Text
- mergeTargetAlias :: Text
- fromMergeUsing :: MergeUsing -> Printer
- fromMergeOn :: MergeOn -> Printer
- fromMergeWhenMatched :: MergeWhenMatched -> Printer
- fromMergeWhenNotMatched :: MergeWhenNotMatched -> Printer
- fromMerge :: Merge -> Printer
- fromDelete :: Delete -> Printer
- fromUpdate :: Update -> Printer
- fromUpdateSet :: UpdateSet -> Printer
- fromSelectIntoTempTable :: SelectIntoTempTable -> Printer
- fromTempTableName :: TempTableName -> Printer
- fromTempTable :: TempTable -> Printer
- dropTempTableQuery :: TempTableName -> Printer
- fromSelect :: Select -> Printer
- fromWith :: With -> Printer
- fromJoinSource :: JoinSource -> Printer
- fromReselect :: Reselect -> Printer
- fromOrderBys :: Top -> Maybe Expression -> Maybe (NonEmpty OrderBy) -> Printer
- fromOrderBy :: OrderBy -> [Printer]
- fromOrder :: Order -> Printer
- fromNullsOrder :: FieldName -> NullsOrder -> Printer
- fromJoinAlias :: JoinAlias -> Printer
- fromFor :: For -> Printer
- fromProjection :: Projection -> Printer
- fromAggregate :: Aggregate -> Printer
- fromCountable :: Countable FieldName -> Printer
- fromWhere :: Where -> Printer
- collapseWhere :: Expression -> Maybe Expression
- fromFrom :: From -> Printer
- fromOpenJson :: OpenJson -> Printer
- fromJsonFieldSpec :: JsonFieldSpec -> Printer
- fromTableName :: TableName -> Printer
- fromAliased :: Aliased Printer -> Printer
- fromColumnName :: ColumnName -> Printer
- fromNameText :: Text -> Printer
- fromRawUnescapedText :: Text -> Printer
- truePrinter :: Printer
- falsePrinter :: Printer
- parens :: Printer -> Printer
- toQueryFlat :: Printer -> Query
- toQueryPretty :: Printer -> Query
Types
SeqPrinter [Printer] | |
SepByPrinter Printer [Printer] | |
NewlinePrinter | |
QueryPrinter Query | |
IndentPrinter Int Printer |
Instances
Printer generators
fromExpression :: Expression -> Printer Source #
fromFieldName :: FieldName -> Printer Source #
fromInserted :: Inserted -> Printer Source #
fromDeleted :: Deleted -> Printer Source #
fromOutputColumn :: Printer -> OutputColumn -> Printer Source #
fromValues :: Values -> Printer Source #
fromValuesList :: [Values] -> Printer Source #
fromInsert :: Insert -> Printer Source #
fromSetValue :: SetValue -> Printer Source #
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
.
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
.
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)
fromUpdateSet :: UpdateSet -> Printer Source #
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
fromTempTable :: TempTable -> Printer Source #
dropTempTableQuery :: TempTableName -> Printer Source #
@TempTableName "temp_table" is converted to "DROP TABLE #temp_table"
fromSelect :: Select -> Printer Source #
fromJoinSource :: JoinSource -> Printer Source #
fromReselect :: Reselect -> Printer Source #
fromOrderBys :: Top -> Maybe Expression -> Maybe (NonEmpty OrderBy) -> Printer Source #
fromOrderBy :: OrderBy -> [Printer] Source #
fromNullsOrder :: FieldName -> NullsOrder -> Printer Source #
fromJoinAlias :: JoinAlias -> Printer Source #
fromProjection :: Projection -> Printer Source #
fromAggregate :: Aggregate -> Printer Source #
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.
fromOpenJson :: OpenJson -> Printer Source #
fromTableName :: TableName -> Printer Source #
fromColumnName :: ColumnName -> Printer Source #
fromNameText :: Text -> Printer Source #
fromRawUnescapedText :: Text -> Printer Source #
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 # | |
toJSON :: Expression -> Value toEncoding :: Expression -> Encoding toJSONList :: [Expression] -> Value toEncodingList :: [Expression] -> Encoding |