Safe Haskell | Safe-Inferred |
---|---|
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
- (<+>) :: Printer -> Printer -> Printer
- fromInsert :: Insert -> Printer
- fromSetIdentityInsert :: SetIdentityInsert -> Printer
- fromInsertValuesIntoTempTable :: InsertValuesIntoTempTable -> Printer
- fromMerge :: Merge -> Printer
- fromDelete :: Delete -> Printer
- fromUpdate :: Update -> Printer
- fromTempTableDDL :: TempTableDDL -> Printer
- fromSelectIntoTempTable :: SelectIntoTempTable -> Printer
- dropTempTableQuery :: TempTableName -> Printer
- fromSelect :: Select -> Printer
- fromReselect :: Reselect -> Printer
- fromTableName :: TableName -> Printer
- fromRawUnescapedText :: Text -> Printer
- toQueryFlat :: Printer -> Query
- toQueryPretty :: Printer -> Query
Types
SeqPrinter [Printer] | |
SepByPrinter Printer [Printer] | |
NewlinePrinter | |
QueryPrinter Query | |
IndentPrinter Int Printer |
Instances
Printer generators
fromInsert :: Insert -> Printer Source #
fromInsertValuesIntoTempTable :: InsertValuesIntoTempTable -> Printer Source #
Generate a statement to insert values into temporary table.
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.
dropTempTableQuery :: TempTableName -> Printer Source #
@TempTableName "temp_table" is converted to "DROP TABLE #temp_table"
fromSelect :: Select -> Printer Source #
fromReselect :: Reselect -> Printer Source #
fromTableName :: TableName -> 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 Source # toEncoding :: Expression -> Encoding Source # toJSONList :: [Expression] -> Value Source # toEncodingList :: [Expression] -> Encoding Source # |