2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2022-01-11 01:54:51 +03:00
|
|
|
-- | MSSQL ToQuery
|
|
|
|
--
|
|
|
|
-- Convert the simple T-SQL AST to an SQL query, ready to be passed to the odbc
|
|
|
|
-- package's query/exec functions.
|
2022-01-03 20:16:24 +03:00
|
|
|
--
|
|
|
|
-- 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))
|
2021-02-23 20:37:27 +03:00
|
|
|
module Hasura.Backends.MSSQL.ToQuery
|
|
|
|
( fromSelect,
|
|
|
|
fromReselect,
|
|
|
|
toQueryFlat,
|
|
|
|
toQueryPretty,
|
2021-10-01 15:52:19 +03:00
|
|
|
fromInsert,
|
2021-12-31 13:56:06 +03:00
|
|
|
fromMerge,
|
2021-10-01 15:52:19 +03:00
|
|
|
fromSetIdentityInsert,
|
2021-02-23 20:37:27 +03:00
|
|
|
fromDelete,
|
2021-12-15 16:55:41 +03:00
|
|
|
fromUpdate,
|
2021-11-19 20:05:01 +03:00
|
|
|
fromSelectIntoTempTable,
|
2021-12-31 13:56:06 +03:00
|
|
|
fromInsertValuesIntoTempTable,
|
2021-12-15 16:55:41 +03:00
|
|
|
dropTempTableQuery,
|
2022-04-28 22:33:33 +03:00
|
|
|
fromRawUnescapedText,
|
2022-06-15 20:10:32 +03:00
|
|
|
fromTableName,
|
2022-04-28 22:33:33 +03:00
|
|
|
(<+>),
|
2021-02-23 20:37:27 +03:00
|
|
|
Printer (..),
|
|
|
|
)
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-03-19 15:42:09 +03:00
|
|
|
import Data.Aeson (ToJSON (..))
|
2021-12-15 16:55:41 +03:00
|
|
|
import Data.HashMap.Strict qualified as HM
|
2021-02-23 20:37:27 +03:00
|
|
|
import Data.List (intersperse)
|
2021-03-19 15:42:09 +03:00
|
|
|
import Data.String
|
2021-02-23 20:37:27 +03:00
|
|
|
import Data.Text qualified as T
|
2021-05-10 13:17:54 +03:00
|
|
|
import Data.Text.Extended qualified as T
|
|
|
|
import Data.Text.Lazy qualified as L
|
|
|
|
import Data.Text.Lazy.Builder qualified as L
|
2021-02-23 20:37:27 +03:00
|
|
|
import Database.ODBC.SQLServer
|
2021-12-15 16:55:41 +03:00
|
|
|
import Hasura.Backends.MSSQL.Types
|
2023-04-13 19:10:38 +03:00
|
|
|
import Hasura.NativeQuery.Metadata (InterpolatedItem (..), InterpolatedQuery (..))
|
2021-03-19 15:42:09 +03:00
|
|
|
import Hasura.Prelude hiding (GT, LT)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-01-03 20:16:24 +03:00
|
|
|
|
|
|
|
-- * Types
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
data Printer
|
|
|
|
= SeqPrinter [Printer]
|
|
|
|
| SepByPrinter Printer [Printer]
|
|
|
|
| NewlinePrinter
|
|
|
|
| QueryPrinter Query
|
|
|
|
| IndentPrinter Int Printer
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance IsString Printer where
|
|
|
|
fromString = QueryPrinter . fromString
|
|
|
|
|
|
|
|
(<+>) :: Printer -> Printer -> Printer
|
|
|
|
(<+>) x y = SeqPrinter [x, y]
|
|
|
|
|
|
|
|
(<+>?) :: Printer -> Maybe Printer -> Printer
|
|
|
|
(<+>?) x Nothing = x
|
|
|
|
(<+>?) x (Just y) = SeqPrinter [x, y]
|
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
(?<+>) :: Maybe Printer -> Printer -> Printer
|
|
|
|
(?<+>) Nothing x = x
|
|
|
|
(?<+>) (Just x) y = SeqPrinter [x, y]
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2022-01-03 20:16:24 +03:00
|
|
|
|
|
|
|
-- * Instances
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-06-15 18:05:41 +03:00
|
|
|
-- This is a debug instance, only here because it avoids a circular
|
|
|
|
-- dependency between this module and Types/Instances.
|
|
|
|
instance ToJSON Expression where
|
|
|
|
toJSON = toJSON . T.toTxt . toQueryFlat . fromExpression
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-01-03 20:16:24 +03:00
|
|
|
|
|
|
|
-- * Printer generators
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromExpression :: Expression -> Printer
|
|
|
|
fromExpression =
|
|
|
|
\case
|
2022-01-05 14:09:45 +03:00
|
|
|
CastExpression e t dataLength ->
|
2021-05-21 15:27:44 +03:00
|
|
|
"CAST("
|
|
|
|
<+> fromExpression e
|
|
|
|
<+> " AS "
|
2022-02-17 17:11:44 +03:00
|
|
|
<+> fromString (T.unpack $ scalarTypeDBName dataLength t)
|
2021-05-21 15:27:44 +03:00
|
|
|
<+> ")"
|
2021-02-23 20:37:27 +03:00
|
|
|
JsonQueryExpression e -> "JSON_QUERY(" <+> fromExpression e <+> ")"
|
|
|
|
JsonValueExpression e path ->
|
2021-05-10 13:17:54 +03:00
|
|
|
"JSON_VALUE(" <+> fromExpression e <+> fromPath path <+> ")"
|
2022-04-26 16:47:30 +03:00
|
|
|
ValueExpression value -> QueryPrinter $ toSql value
|
2021-02-23 20:37:27 +03:00
|
|
|
AndExpression xs ->
|
2021-05-21 15:27:44 +03:00
|
|
|
case xs of
|
|
|
|
[] -> truePrinter
|
|
|
|
_ ->
|
|
|
|
SepByPrinter
|
|
|
|
(NewlinePrinter <+> "AND ")
|
|
|
|
(fmap (\x -> "(" <+> fromExpression x <+> ")") (toList xs))
|
2021-02-23 20:37:27 +03:00
|
|
|
OrExpression xs ->
|
2021-05-21 15:27:44 +03:00
|
|
|
case xs of
|
|
|
|
[] -> falsePrinter
|
|
|
|
_ ->
|
|
|
|
SepByPrinter
|
|
|
|
(NewlinePrinter <+> "OR ")
|
|
|
|
(fmap (\x -> "(" <+> fromExpression x <+> ")") (toList xs))
|
2021-10-01 15:52:19 +03:00
|
|
|
NotExpression expression -> "NOT " <+> fromExpression expression
|
2021-06-08 06:50:24 +03:00
|
|
|
ExistsExpression sel -> "EXISTS (" <+> fromSelect sel <+> ")"
|
2021-02-23 20:37:27 +03:00
|
|
|
IsNullExpression expression ->
|
|
|
|
"(" <+> fromExpression expression <+> ") IS NULL"
|
|
|
|
IsNotNullExpression expression ->
|
|
|
|
"(" <+> fromExpression expression <+> ") IS NOT NULL"
|
|
|
|
ColumnExpression fieldName -> fromFieldName fieldName
|
|
|
|
ToStringExpression e -> "CONCAT(" <+> fromExpression e <+> ", '')"
|
|
|
|
SelectExpression s -> "(" <+> IndentPrinter 1 (fromSelect s) <+> ")"
|
|
|
|
OpExpression op x y ->
|
|
|
|
"("
|
|
|
|
<+> fromExpression x
|
|
|
|
<+> ") "
|
|
|
|
<+> fromOp op
|
|
|
|
<+> " ("
|
|
|
|
<+> fromExpression y
|
|
|
|
<+> ")"
|
2022-01-05 14:09:45 +03:00
|
|
|
MethodApplicationExpression ex methodAppExp -> fromMethodApplicationExpression ex methodAppExp
|
|
|
|
FunctionApplicationExpression funAppExp -> fromFunctionApplicationExpression funAppExp
|
2021-03-19 15:42:09 +03:00
|
|
|
ListExpression xs -> SepByPrinter ", " $ fromExpression <$> xs
|
2021-03-24 16:43:40 +03:00
|
|
|
STOpExpression op e str ->
|
|
|
|
"("
|
|
|
|
<+> fromExpression e
|
|
|
|
<+> ")."
|
|
|
|
<+> fromString (show op)
|
|
|
|
<+> "("
|
|
|
|
<+> fromExpression str
|
|
|
|
<+> ") = 1"
|
2021-10-01 15:52:19 +03:00
|
|
|
ConditionalExpression condition trueExpression falseExpression ->
|
2021-07-08 23:49:10 +03:00
|
|
|
"(CASE WHEN("
|
2021-10-01 15:52:19 +03:00
|
|
|
<+> fromExpression condition
|
2021-07-08 23:49:10 +03:00
|
|
|
<+> ") THEN "
|
2021-10-01 15:52:19 +03:00
|
|
|
<+> fromExpression trueExpression
|
|
|
|
<+> " ELSE "
|
|
|
|
<+> fromExpression falseExpression
|
|
|
|
<+> " END)"
|
|
|
|
DefaultExpression -> "DEFAULT"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2022-01-05 14:09:45 +03:00
|
|
|
fromMethodApplicationExpression :: Expression -> MethodApplicationExpression -> Printer
|
|
|
|
fromMethodApplicationExpression ex methodAppExp =
|
|
|
|
case methodAppExp of
|
|
|
|
MethExpSTAsText -> fromApp "STAsText" []
|
|
|
|
where
|
|
|
|
fromApp :: Text -> [Expression] -> Printer
|
|
|
|
fromApp method args =
|
|
|
|
fromExpression ex
|
|
|
|
<+> "."
|
|
|
|
<+> fromString (T.unpack method)
|
|
|
|
<+> "("
|
|
|
|
<+> SeqPrinter (map fromExpression args)
|
|
|
|
<+> ")"
|
|
|
|
|
|
|
|
fromFunctionApplicationExpression :: FunctionApplicationExpression -> Printer
|
|
|
|
fromFunctionApplicationExpression funAppExp = case funAppExp of
|
|
|
|
(FunExpISNULL x y) -> fromApp "ISNULL" [x, y]
|
|
|
|
where
|
|
|
|
fromApp :: Text -> [Expression] -> Printer
|
|
|
|
fromApp function args =
|
|
|
|
fromString (T.unpack function)
|
|
|
|
<+> "("
|
|
|
|
<+> SepByPrinter ", " (map fromExpression args)
|
|
|
|
<+> ")"
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
fromOp :: Op -> Printer
|
|
|
|
fromOp =
|
|
|
|
\case
|
2021-03-19 15:42:09 +03:00
|
|
|
LT -> "<"
|
|
|
|
GT -> ">"
|
|
|
|
GTE -> ">="
|
|
|
|
LTE -> "<="
|
|
|
|
IN -> "IN"
|
|
|
|
NIN -> "NOT IN"
|
|
|
|
LIKE -> "LIKE"
|
|
|
|
NLIKE -> "NOT LIKE"
|
2021-05-21 15:27:44 +03:00
|
|
|
EQ' -> "="
|
|
|
|
NEQ' -> "!="
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromPath :: JsonPath -> Printer
|
2021-05-10 13:17:54 +03:00
|
|
|
fromPath path =
|
|
|
|
", " <+> string path
|
|
|
|
where
|
|
|
|
string =
|
|
|
|
fromExpression
|
|
|
|
. ValueExpression
|
|
|
|
. TextValue
|
|
|
|
. L.toStrict
|
|
|
|
. L.toLazyText
|
|
|
|
. go
|
|
|
|
go =
|
|
|
|
\case
|
|
|
|
RootPath -> "$"
|
|
|
|
IndexPath r i -> go r <> "[" <> L.fromString (show i) <> "]"
|
|
|
|
FieldPath r f -> go r <> ".\"" <> L.fromText f <> "\""
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromFieldName :: FieldName -> Printer
|
|
|
|
fromFieldName (FieldName {..}) =
|
|
|
|
fromNameText fieldNameEntity <+> "." <+> fromNameText fieldName
|
|
|
|
|
2021-11-22 12:11:32 +03:00
|
|
|
fromInserted :: Inserted -> Printer
|
|
|
|
fromInserted Inserted = "INSERTED"
|
|
|
|
|
|
|
|
fromDeleted :: Deleted -> Printer
|
|
|
|
fromDeleted Deleted = "DELETED"
|
|
|
|
|
|
|
|
fromOutputColumn :: Printer -> OutputColumn -> Printer
|
|
|
|
fromOutputColumn prefix (OutputColumn columnName) =
|
|
|
|
prefix <+> "." <+> fromNameText (columnNameText columnName)
|
|
|
|
|
|
|
|
fromOutput :: (t -> Printer) -> Output t -> Printer
|
|
|
|
fromOutput typePrinter (Output ty outputColumns) =
|
|
|
|
"OUTPUT " <+> SepByPrinter ", " (map (fromOutputColumn (typePrinter ty)) outputColumns)
|
2021-10-01 15:52:19 +03:00
|
|
|
|
|
|
|
fromInsertOutput :: InsertOutput -> Printer
|
2021-11-22 12:11:32 +03:00
|
|
|
fromInsertOutput = fromOutput fromInserted
|
|
|
|
|
|
|
|
fromDeleteOutput :: DeleteOutput -> Printer
|
|
|
|
fromDeleteOutput = fromOutput fromDeleted
|
2021-10-01 15:52:19 +03:00
|
|
|
|
2021-12-15 16:55:41 +03:00
|
|
|
fromUpdateOutput :: UpdateOutput -> Printer
|
|
|
|
fromUpdateOutput = fromOutput fromInserted
|
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
fromValues :: Values -> Printer
|
|
|
|
fromValues (Values values) =
|
|
|
|
"( " <+> SepByPrinter ", " (map fromExpression values) <+> " )"
|
|
|
|
|
2021-12-31 13:56:06 +03:00
|
|
|
fromValuesList :: [Values] -> Printer
|
|
|
|
fromValuesList valuesList =
|
|
|
|
"VALUES " <+> SepByPrinter ", " (map fromValues valuesList)
|
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
fromInsert :: Insert -> Printer
|
|
|
|
fromInsert Insert {..} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
2022-05-11 19:00:10 +03:00
|
|
|
$ ["INSERT INTO " <+> fromTableName insertTable]
|
2022-05-30 15:17:37 +03:00
|
|
|
<> [ "(" <+> SepByPrinter ", " (map (fromNameText . columnNameText) insertColumns) <+> ")"
|
|
|
|
| not (null insertColumns)
|
|
|
|
]
|
2022-05-11 19:00:10 +03:00
|
|
|
<> [ fromInsertOutput insertOutput,
|
|
|
|
"INTO " <+> fromTempTable insertTempTable,
|
|
|
|
if null insertColumns
|
2022-05-30 15:17:37 +03:00
|
|
|
then "VALUES " <+> SepByPrinter ", " (map (const "(DEFAULT)") insertValues)
|
2022-05-11 19:00:10 +03:00
|
|
|
else fromValuesList insertValues
|
|
|
|
]
|
2021-10-01 15:52:19 +03:00
|
|
|
|
|
|
|
fromSetValue :: SetValue -> Printer
|
|
|
|
fromSetValue = \case
|
|
|
|
SetON -> "ON"
|
|
|
|
SetOFF -> "OFF"
|
|
|
|
|
2021-11-19 20:05:01 +03:00
|
|
|
fromSetIdentityInsert :: SetIdentityInsert -> Printer
|
|
|
|
fromSetIdentityInsert SetIdentityInsert {..} =
|
2021-10-01 15:52:19 +03:00
|
|
|
SepByPrinter
|
|
|
|
" "
|
|
|
|
[ "SET IDENTITY_INSERT",
|
2021-12-31 13:56:06 +03:00
|
|
|
tableName,
|
2021-10-01 15:52:19 +03:00
|
|
|
fromSetValue setValue
|
|
|
|
]
|
2021-12-31 13:56:06 +03:00
|
|
|
where
|
|
|
|
tableName =
|
|
|
|
case setTable of
|
|
|
|
RegularTableName name -> fromTableName name
|
|
|
|
TemporaryTableName name -> fromTempTableName name
|
|
|
|
|
|
|
|
-- | Generate a statement to insert values into temporary table.
|
|
|
|
fromInsertValuesIntoTempTable :: InsertValuesIntoTempTable -> Printer
|
|
|
|
fromInsertValuesIntoTempTable InsertValuesIntoTempTable {..} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "INSERT INTO " <+> fromTempTableName ivittTempTableName,
|
|
|
|
"(" <+> SepByPrinter ", " (map (fromNameText . columnNameText) ivittColumns) <+> ")",
|
|
|
|
fromValuesList ivittValues
|
|
|
|
]
|
|
|
|
|
|
|
|
-- | Alias for the source table in a MERGE statement. Used when pretty printing MERGE statments.
|
|
|
|
mergeSourceAlias :: Text
|
|
|
|
mergeSourceAlias = "source"
|
|
|
|
|
|
|
|
-- | Alias for the target table in a MERGE statement. Used when pretty printing MERGE statments.
|
|
|
|
mergeTargetAlias :: Text
|
|
|
|
mergeTargetAlias = "target"
|
|
|
|
|
|
|
|
-- | USING section of a MERGE statement. Used in 'fromMerge'.
|
|
|
|
fromMergeUsing :: MergeUsing -> Printer
|
|
|
|
fromMergeUsing MergeUsing {..} =
|
|
|
|
"USING (" <+> fromSelect selectSubQuery <+> ") AS " <+> fromNameText mergeSourceAlias
|
|
|
|
where
|
|
|
|
selectSubQuery :: Select
|
|
|
|
selectSubQuery =
|
|
|
|
let alias = "merge_temptable"
|
|
|
|
columnNameToProjection ColumnName {columnNameText} =
|
|
|
|
-- merge_temptable.column_name AS column_name
|
|
|
|
FieldNameProjection $
|
|
|
|
Aliased
|
|
|
|
{ aliasedThing = FieldName columnNameText alias,
|
|
|
|
aliasedAlias = columnNameText
|
|
|
|
}
|
|
|
|
in emptySelect
|
|
|
|
{ selectProjections = map columnNameToProjection mergeUsingColumns,
|
|
|
|
selectFrom = Just (FromTempTable $ Aliased mergeUsingTempTable alias) -- FROM temp_table AS merge_temptable
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | ON section of a MERGE statement. Used in 'fromMerge'.
|
|
|
|
fromMergeOn :: MergeOn -> Printer
|
|
|
|
fromMergeOn MergeOn {..} =
|
|
|
|
"ON (" <+> onExpression <+> ")"
|
|
|
|
where
|
|
|
|
onExpression
|
|
|
|
| null mergeOnColumns =
|
|
|
|
falsePrinter
|
|
|
|
| otherwise =
|
|
|
|
(fromExpression . AndExpression) (map matchColumn mergeOnColumns)
|
|
|
|
|
|
|
|
matchColumn :: ColumnName -> Expression
|
|
|
|
matchColumn ColumnName {..} =
|
|
|
|
let sourceColumn = ColumnExpression $ FieldName columnNameText mergeSourceAlias
|
|
|
|
targetColumn = ColumnExpression $ FieldName columnNameText mergeTargetAlias
|
|
|
|
in OpExpression EQ' sourceColumn targetColumn
|
|
|
|
|
|
|
|
-- | WHEN MATCHED section of a MERGE statement. Used in 'fromMerge'.
|
|
|
|
fromMergeWhenMatched :: MergeWhenMatched -> Printer
|
|
|
|
fromMergeWhenMatched (MergeWhenMatched updateColumns updateCondition updatePreset) =
|
|
|
|
if null updates
|
|
|
|
then ""
|
|
|
|
else
|
|
|
|
"WHEN MATCHED AND "
|
|
|
|
<+> fromExpression updateCondition
|
|
|
|
<+> " THEN UPDATE "
|
|
|
|
<+> fromUpdateSet updates
|
|
|
|
where
|
|
|
|
updates = updateSet <> HM.map UpdateSet updatePreset
|
|
|
|
|
|
|
|
updateSet :: UpdateSet
|
|
|
|
updateSet =
|
|
|
|
HM.fromList $
|
|
|
|
map
|
|
|
|
( \cn@ColumnName {..} ->
|
|
|
|
( cn,
|
|
|
|
UpdateSet $ ColumnExpression $ FieldName columnNameText mergeSourceAlias
|
|
|
|
)
|
|
|
|
)
|
|
|
|
updateColumns
|
|
|
|
|
|
|
|
-- | WHEN NOT MATCHED section of a MERGE statement. Used in 'fromMerge'.
|
|
|
|
fromMergeWhenNotMatched :: MergeWhenNotMatched -> Printer
|
|
|
|
fromMergeWhenNotMatched (MergeWhenNotMatched insertColumns) =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "WHEN NOT MATCHED THEN INSERT (" <+> SepByPrinter ", " (map fromColumnName insertColumns) <+> ")",
|
|
|
|
fromValuesList [Values columnsFromSource]
|
|
|
|
]
|
|
|
|
where
|
|
|
|
columnsFromSource =
|
|
|
|
insertColumns <&> \ColumnName {..} -> ColumnExpression $ FieldName columnNameText mergeSourceAlias
|
|
|
|
|
|
|
|
-- | Generate a MERGE SQL statement
|
|
|
|
fromMerge :: Merge -> Printer
|
|
|
|
fromMerge Merge {..} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "MERGE " <+> fromAliased (fmap fromTableName mergeTableAsTarget),
|
|
|
|
fromMergeUsing mergeUsing,
|
|
|
|
fromMergeOn mergeOn,
|
|
|
|
fromMergeWhenMatched mergeWhenMatched,
|
|
|
|
fromMergeWhenNotMatched mergeWhenNotMatched,
|
|
|
|
fromInsertOutput mergeInsertOutput,
|
|
|
|
"INTO " <+> fromTempTable mergeOutputTempTable,
|
|
|
|
";" -- Always, a Merge statement should end with a ";"
|
|
|
|
]
|
|
|
|
where
|
|
|
|
mergeTableAsTarget :: Aliased TableName
|
|
|
|
mergeTableAsTarget = Aliased mergeTargetTable mergeTargetAlias
|
2021-10-01 15:52:19 +03:00
|
|
|
|
2021-11-19 20:05:01 +03:00
|
|
|
-- | 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))
|
2021-11-22 12:11:32 +03:00
|
|
|
fromDelete :: Delete -> Printer
|
|
|
|
fromDelete Delete {deleteTable, deleteOutput, deleteTempTable, deleteWhere} =
|
2021-02-23 20:37:27 +03:00
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "DELETE " <+> fromNameText (aliasedAlias deleteTable),
|
2021-11-22 12:11:32 +03:00
|
|
|
fromDeleteOutput deleteOutput,
|
|
|
|
"INTO " <+> fromTempTable deleteTempTable,
|
2021-02-23 20:37:27 +03:00
|
|
|
"FROM " <+> fromAliased (fmap fromTableName deleteTable),
|
|
|
|
fromWhere deleteWhere
|
|
|
|
]
|
|
|
|
|
2021-12-15 16:55:41 +03:00
|
|
|
-- | 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)
|
|
|
|
fromUpdate :: Update -> Printer
|
|
|
|
fromUpdate Update {..} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "UPDATE " <+> fromNameText (aliasedAlias updateTable),
|
|
|
|
fromUpdateSet updateSet,
|
|
|
|
fromUpdateOutput updateOutput,
|
|
|
|
"INTO " <+> fromTempTable updateTempTable,
|
|
|
|
"FROM " <+> fromAliased (fmap fromTableName updateTable),
|
|
|
|
fromWhere updateWhere
|
|
|
|
]
|
|
|
|
|
2021-12-31 13:56:06 +03:00
|
|
|
fromUpdateSet :: UpdateSet -> Printer
|
|
|
|
fromUpdateSet setColumns =
|
|
|
|
let updateColumnValue (column, updateOp) =
|
|
|
|
fromColumnName column <+> fromUpdateOperator (fromExpression <$> updateOp)
|
|
|
|
in "SET " <+> SepByPrinter ", " (map updateColumnValue (HM.toList setColumns))
|
|
|
|
where
|
2021-12-15 16:55:41 +03:00
|
|
|
fromUpdateOperator :: UpdateOperator Printer -> Printer
|
|
|
|
fromUpdateOperator = \case
|
|
|
|
UpdateSet p -> " = " <+> p
|
|
|
|
UpdateInc p -> " += " <+> p
|
|
|
|
|
2021-11-19 20:05:01 +03:00
|
|
|
-- | 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.
|
|
|
|
fromSelectIntoTempTable :: SelectIntoTempTable -> Printer
|
2021-12-31 13:56:06 +03:00
|
|
|
fromSelectIntoTempTable SelectIntoTempTable {sittTempTableName, sittColumns, sittFromTableName, sittConstraints} =
|
2021-11-19 20:05:01 +03:00
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
2021-12-31 13:56:06 +03:00
|
|
|
$ [ "SELECT "
|
|
|
|
<+> columns,
|
|
|
|
"INTO " <+> fromTempTableName sittTempTableName,
|
|
|
|
"FROM " <+> fromTableName sittFromTableName,
|
|
|
|
"WHERE " <+> falsePrinter
|
|
|
|
]
|
|
|
|
<> case sittConstraints of
|
|
|
|
RemoveConstraints ->
|
|
|
|
[ "UNION ALL SELECT " <+> columns,
|
|
|
|
"FROM " <+> fromTableName sittFromTableName,
|
|
|
|
"WHERE " <+> falsePrinter
|
|
|
|
]
|
|
|
|
KeepConstraints ->
|
|
|
|
[]
|
2021-11-19 20:05:01 +03:00
|
|
|
where
|
|
|
|
-- column names separated by commas
|
|
|
|
columns =
|
|
|
|
SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
|
|
|
(map columnNameFromUnifiedColumn sittColumns)
|
|
|
|
|
|
|
|
-- column name with potential modifications of types
|
|
|
|
columnNameFromUnifiedColumn (UnifiedColumn columnName columnType) =
|
|
|
|
case columnType of
|
|
|
|
-- The "timestamp" is type synonym for "rowversion" and it is just an incrementing number and does not preserve a date or a time.
|
|
|
|
-- So, the "timestamp" type is neither insertable nor explicitly updatable. Its values are unique binary numbers within a database.
|
|
|
|
-- We're using "binary" type instead so that we can copy a timestamp row value safely into the temporary table.
|
|
|
|
-- See https://docs.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql for more details.
|
2022-03-18 13:04:52 +03:00
|
|
|
TimestampType -> "CAST(" <+> fromColumnName columnName <+> " AS binary(8)) AS " <+> fromColumnName columnName
|
|
|
|
_ -> fromColumnName columnName
|
2021-11-19 20:05:01 +03:00
|
|
|
|
|
|
|
-- | @TempTableName "deleted"@ becomes @\#deleted@
|
|
|
|
fromTempTableName :: TempTableName -> Printer
|
|
|
|
fromTempTableName (TempTableName v) = QueryPrinter (fromString . T.unpack $ "#" <> v)
|
|
|
|
|
2021-11-22 12:11:32 +03:00
|
|
|
fromTempTable :: TempTable -> Printer
|
|
|
|
fromTempTable (TempTable table columns) =
|
|
|
|
fromTempTableName table <+> parens (SepByPrinter ", " (map fromColumnName columns))
|
|
|
|
|
2021-12-15 16:55:41 +03:00
|
|
|
-- | @TempTableName "temp_table" is converted to "DROP TABLE #temp_table"
|
|
|
|
dropTempTableQuery :: TempTableName -> Printer
|
|
|
|
dropTempTableQuery tempTableName =
|
|
|
|
QueryPrinter "DROP TABLE " <+> fromTempTableName tempTableName
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
fromSelect :: Select -> Printer
|
2022-02-21 11:52:05 +03:00
|
|
|
fromSelect Select {..} = fmap fromWith selectWith ?<+> result
|
2021-02-25 12:57:22 +03:00
|
|
|
where
|
|
|
|
result =
|
|
|
|
SepByPrinter
|
2021-06-08 06:50:24 +03:00
|
|
|
NewlinePrinter
|
2021-02-25 12:57:22 +03:00
|
|
|
$ [ "SELECT "
|
|
|
|
<+> IndentPrinter
|
|
|
|
7
|
|
|
|
( SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
|
|
|
(map fromProjection (toList selectProjections))
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2021-06-08 06:50:24 +03:00
|
|
|
]
|
|
|
|
<> ["FROM " <+> IndentPrinter 5 (fromFrom f) | Just f <- [selectFrom]]
|
|
|
|
<> [ SepByPrinter
|
2021-02-25 12:57:22 +03:00
|
|
|
NewlinePrinter
|
|
|
|
( map
|
|
|
|
( \Join {..} ->
|
|
|
|
SeqPrinter
|
|
|
|
[ "OUTER APPLY (",
|
|
|
|
IndentPrinter 13 (fromJoinSource joinSource),
|
|
|
|
") ",
|
|
|
|
NewlinePrinter,
|
|
|
|
"AS ",
|
|
|
|
fromJoinAlias joinJoinAlias
|
|
|
|
]
|
|
|
|
)
|
|
|
|
selectJoins
|
2021-09-24 01:56:37 +03:00
|
|
|
),
|
2021-02-25 12:57:22 +03:00
|
|
|
fromWhere selectWhere,
|
|
|
|
fromOrderBys selectTop selectOffset selectOrderBy,
|
|
|
|
fromFor selectFor
|
|
|
|
]
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
fromWith :: With -> Printer
|
|
|
|
fromWith (With withSelects) =
|
|
|
|
"WITH " <+> SepByPrinter ", " (map fromAliasedSelect (toList withSelects)) <+> NewlinePrinter
|
|
|
|
where
|
2023-03-27 19:54:27 +03:00
|
|
|
fromAliasedSelect (Aliased {..}) =
|
2023-04-19 17:52:43 +03:00
|
|
|
fromNameText aliasedAlias
|
|
|
|
<+> " AS "
|
|
|
|
<+> "( "
|
|
|
|
<+> ( case aliasedThing of
|
|
|
|
CTESelect select ->
|
|
|
|
fromSelect select
|
|
|
|
CTEUnsafeRawSQL nativeQuery ->
|
|
|
|
renderInterpolatedQuery nativeQuery <+> "\n"
|
|
|
|
)
|
|
|
|
<+> " )"
|
2021-10-01 15:52:19 +03:00
|
|
|
|
2023-03-27 19:54:27 +03:00
|
|
|
renderInterpolatedQuery :: InterpolatedQuery Expression -> Printer
|
|
|
|
renderInterpolatedQuery = foldr (<+>) "" . renderedParts
|
|
|
|
where
|
|
|
|
renderedParts :: InterpolatedQuery Expression -> [Printer]
|
|
|
|
renderedParts (InterpolatedQuery parts) =
|
|
|
|
( \case
|
|
|
|
IIText t -> fromRawUnescapedText t
|
|
|
|
IIVariable v -> fromExpression v
|
|
|
|
)
|
|
|
|
<$> parts
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
fromJoinSource :: JoinSource -> Printer
|
|
|
|
fromJoinSource =
|
|
|
|
\case
|
2021-06-08 06:50:24 +03:00
|
|
|
JoinSelect sel -> fromSelect sel
|
2021-02-23 20:37:27 +03:00
|
|
|
JoinReselect reselect -> fromReselect reselect
|
|
|
|
|
|
|
|
fromReselect :: Reselect -> Printer
|
2022-02-21 11:52:05 +03:00
|
|
|
fromReselect Reselect {..} = result
|
2021-04-28 14:05:33 +03:00
|
|
|
where
|
|
|
|
result =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "SELECT "
|
|
|
|
<+> IndentPrinter
|
|
|
|
7
|
|
|
|
( SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
|
|
|
(map fromProjection (toList reselectProjections))
|
2021-09-24 01:56:37 +03:00
|
|
|
),
|
2021-04-28 14:05:33 +03:00
|
|
|
fromWhere reselectWhere,
|
2021-05-25 17:57:58 +03:00
|
|
|
fromFor reselectFor
|
2021-04-28 14:05:33 +03:00
|
|
|
]
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromOrderBys ::
|
|
|
|
Top -> Maybe Expression -> Maybe (NonEmpty OrderBy) -> Printer
|
|
|
|
fromOrderBys NoTop Nothing Nothing = "" -- An ORDER BY is wasteful if not needed.
|
|
|
|
fromOrderBys top moffset morderBys =
|
|
|
|
SeqPrinter
|
|
|
|
[ "ORDER BY ",
|
|
|
|
IndentPrinter
|
|
|
|
9
|
|
|
|
( SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ case morderBys of
|
2021-05-21 15:27:44 +03:00
|
|
|
-- If you ORDER BY 1, a text field will signal an
|
|
|
|
-- error. What we want instead is to just order by
|
|
|
|
-- nothing, but also satisfy the syntactic
|
|
|
|
-- requirements. Thus ORDER BY (SELECT NULL).
|
|
|
|
--
|
|
|
|
-- This won't create consistent orderings, but that's
|
|
|
|
-- why you should specify an order_by in your GraphQL
|
|
|
|
-- query anyway, to define the ordering.
|
|
|
|
Nothing -> "(SELECT NULL) /* ORDER BY is required for OFFSET */"
|
2021-02-23 20:37:27 +03:00
|
|
|
Just orderBys ->
|
|
|
|
SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
|
|
|
(concatMap fromOrderBy (toList orderBys)),
|
|
|
|
case (top, moffset) of
|
|
|
|
(NoTop, Nothing) -> ""
|
|
|
|
(NoTop, Just offset) ->
|
|
|
|
"OFFSET " <+> fromExpression offset <+> " ROWS"
|
|
|
|
(Top n, Nothing) ->
|
|
|
|
"OFFSET 0 ROWS FETCH NEXT "
|
2021-05-21 15:27:44 +03:00
|
|
|
<+> QueryPrinter (toSql (IntValue n))
|
|
|
|
<+> " ROWS ONLY"
|
2021-02-23 20:37:27 +03:00
|
|
|
(Top n, Just offset) ->
|
|
|
|
"OFFSET "
|
|
|
|
<+> fromExpression offset
|
2021-05-21 15:27:44 +03:00
|
|
|
<+> " ROWS FETCH NEXT "
|
|
|
|
<+> QueryPrinter (toSql (IntValue n))
|
|
|
|
<+> " ROWS ONLY"
|
2021-02-23 20:37:27 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
fromOrderBy :: OrderBy -> [Printer]
|
|
|
|
fromOrderBy OrderBy {..} =
|
|
|
|
[ fromNullsOrder orderByFieldName orderByNullsOrder,
|
2021-05-21 15:27:44 +03:00
|
|
|
-- Above: This doesn't do anything when using text, ntext or image
|
|
|
|
-- types. See below on CAST commentary.
|
|
|
|
wrapNullHandling (fromFieldName orderByFieldName)
|
|
|
|
<+> " "
|
|
|
|
<+> fromOrder orderByOrder
|
2021-02-23 20:37:27 +03:00
|
|
|
]
|
2021-05-21 15:27:44 +03:00
|
|
|
where
|
|
|
|
wrapNullHandling inner =
|
|
|
|
case orderByType of
|
|
|
|
Just TextType -> castTextish inner
|
|
|
|
Just WtextType -> castTextish inner
|
|
|
|
-- Above: For some types, we have to do null handling manually
|
|
|
|
-- ourselves:
|
|
|
|
_ -> inner
|
|
|
|
-- Direct quote from SQL Server error response:
|
|
|
|
--
|
|
|
|
-- > The text, ntext, and image data types cannot be compared or
|
|
|
|
-- > sorted, except when using IS NULL or LIKE operator.
|
|
|
|
--
|
|
|
|
-- So we cast it to a varchar, maximum length.
|
|
|
|
castTextish inner = "CAST(" <+> inner <+> " AS VARCHAR(MAX))"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromOrder :: Order -> Printer
|
|
|
|
fromOrder =
|
|
|
|
\case
|
|
|
|
AscOrder -> "ASC"
|
|
|
|
DescOrder -> "DESC"
|
|
|
|
|
|
|
|
fromNullsOrder :: FieldName -> NullsOrder -> Printer
|
|
|
|
fromNullsOrder fieldName =
|
|
|
|
\case
|
|
|
|
NullsAnyOrder -> ""
|
|
|
|
NullsFirst -> "IIF(" <+> fromFieldName fieldName <+> " IS NULL, 0, 1)"
|
|
|
|
NullsLast -> "IIF(" <+> fromFieldName fieldName <+> " IS NULL, 1, 0)"
|
|
|
|
|
|
|
|
fromJoinAlias :: JoinAlias -> Printer
|
|
|
|
fromJoinAlias JoinAlias {..} =
|
|
|
|
fromNameText joinAliasEntity
|
|
|
|
<+>? fmap (\name -> "(" <+> fromNameText name <+> ")") joinAliasField
|
|
|
|
|
|
|
|
fromFor :: For -> Printer
|
|
|
|
fromFor =
|
|
|
|
\case
|
|
|
|
NoFor -> ""
|
2021-04-28 14:05:33 +03:00
|
|
|
JsonFor ForJson {jsonCardinality} ->
|
2021-07-08 23:49:10 +03:00
|
|
|
"FOR JSON PATH, INCLUDE_NULL_VALUES"
|
2021-02-23 20:37:27 +03:00
|
|
|
<+> case jsonCardinality of
|
2021-04-28 14:05:33 +03:00
|
|
|
JsonArray -> ""
|
|
|
|
JsonSingleton -> ", WITHOUT_ARRAY_WRAPPER"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromProjection :: Projection -> Printer
|
|
|
|
fromProjection =
|
|
|
|
\case
|
|
|
|
ExpressionProjection aliasedExpression ->
|
|
|
|
fromAliased (fmap fromExpression aliasedExpression)
|
|
|
|
FieldNameProjection aliasedFieldName ->
|
|
|
|
fromAliased (fmap fromFieldName aliasedFieldName)
|
|
|
|
AggregateProjection aliasedAggregate ->
|
|
|
|
fromAliased (fmap fromAggregate aliasedAggregate)
|
|
|
|
StarProjection -> "*"
|
|
|
|
|
|
|
|
fromAggregate :: Aggregate -> Printer
|
|
|
|
fromAggregate =
|
|
|
|
\case
|
|
|
|
CountAggregate countable -> "COUNT(" <+> fromCountable countable <+> ")"
|
2021-09-03 02:00:23 +03:00
|
|
|
OpAggregate op args ->
|
|
|
|
QueryPrinter (rawUnescapedText op)
|
2021-02-23 20:37:27 +03:00
|
|
|
<+> "("
|
|
|
|
<+> SepByPrinter ", " (map fromExpression (toList args))
|
|
|
|
<+> ")"
|
|
|
|
TextAggregate text -> fromExpression (ValueExpression (TextValue text))
|
|
|
|
|
|
|
|
fromCountable :: Countable FieldName -> Printer
|
|
|
|
fromCountable =
|
|
|
|
\case
|
|
|
|
StarCountable -> "*"
|
2022-01-18 17:53:44 +03:00
|
|
|
NonNullFieldCountable field -> fromFieldName field
|
|
|
|
DistinctCountable field -> "DISTINCT " <+> fromFieldName field
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromWhere :: Where -> Printer
|
|
|
|
fromWhere =
|
|
|
|
\case
|
2021-07-08 23:49:10 +03:00
|
|
|
Where expressions
|
|
|
|
| Just whereExp <- collapseWhere (AndExpression expressions) ->
|
|
|
|
"WHERE " <+> IndentPrinter 6 (fromExpression whereExp)
|
|
|
|
| otherwise -> ""
|
|
|
|
|
2022-01-03 20:16:24 +03:00
|
|
|
-- | Drop useless examples like this from the output:
|
2021-07-08 23:49:10 +03:00
|
|
|
--
|
|
|
|
-- WHERE (((1<>1))
|
|
|
|
-- AND ((1=1)))
|
|
|
|
-- AND ((1=1))
|
|
|
|
--
|
|
|
|
-- And
|
|
|
|
--
|
|
|
|
-- WHERE ((1<>1))
|
|
|
|
--
|
|
|
|
-- They're redundant, but make the output less readable.
|
|
|
|
collapseWhere :: Expression -> Maybe Expression
|
|
|
|
collapseWhere = go
|
|
|
|
where
|
|
|
|
go =
|
|
|
|
\case
|
|
|
|
ValueExpression (BoolValue True) -> Nothing
|
|
|
|
AndExpression xs ->
|
|
|
|
case mapMaybe go xs of
|
|
|
|
[] -> Nothing
|
|
|
|
ys -> pure (AndExpression ys)
|
|
|
|
e -> pure e
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromFrom :: From -> Printer
|
|
|
|
fromFrom =
|
|
|
|
\case
|
|
|
|
FromQualifiedTable aliasedQualifiedTableName ->
|
|
|
|
fromAliased (fmap fromTableName aliasedQualifiedTableName)
|
|
|
|
FromOpenJson openJson -> fromAliased (fmap fromOpenJson openJson)
|
2021-07-08 23:49:10 +03:00
|
|
|
FromSelect select -> fromAliased (fmap (parens . fromSelect) select)
|
2021-10-01 15:52:19 +03:00
|
|
|
FromIdentifier identifier -> fromNameText identifier
|
2021-11-19 20:05:01 +03:00
|
|
|
FromTempTable aliasedTempTable -> fromAliased (fmap fromTempTableName aliasedTempTable)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromOpenJson :: OpenJson -> Printer
|
|
|
|
fromOpenJson OpenJson {openJsonExpression, openJsonWith} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ "OPENJSON("
|
|
|
|
<+> IndentPrinter 9 (fromExpression openJsonExpression)
|
|
|
|
<+> ")",
|
2021-07-08 23:49:10 +03:00
|
|
|
case openJsonWith of
|
|
|
|
Nothing -> ""
|
|
|
|
Just openJsonWith' ->
|
|
|
|
"WITH ("
|
|
|
|
<+> IndentPrinter
|
|
|
|
5
|
|
|
|
( SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
2021-10-01 15:52:19 +03:00
|
|
|
(fromJsonFieldSpec <$> toList openJsonWith')
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2021-07-08 23:49:10 +03:00
|
|
|
<+> ")"
|
2021-02-23 20:37:27 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
fromJsonFieldSpec :: JsonFieldSpec -> Printer
|
|
|
|
fromJsonFieldSpec =
|
|
|
|
\case
|
2021-04-20 19:57:14 +03:00
|
|
|
StringField name mPath -> fromNameText name <+> " NVARCHAR(MAX)" <+> quote mPath
|
|
|
|
JsonField name mPath -> fromJsonFieldSpec (StringField name mPath) <+> " AS JSON"
|
2022-04-06 10:18:59 +03:00
|
|
|
ScalarField fieldType fieldLength name mPath ->
|
|
|
|
fromNameText name
|
|
|
|
<+> " "
|
|
|
|
<+> fromString (T.unpack $ scalarTypeDBName fieldLength fieldType)
|
|
|
|
<+> quote mPath
|
2021-04-20 19:57:14 +03:00
|
|
|
where
|
2021-05-10 13:17:54 +03:00
|
|
|
quote mPath = maybe "" ((\p -> " '" <+> p <+> "'") . go) mPath
|
|
|
|
go = \case
|
|
|
|
RootPath -> "$"
|
|
|
|
IndexPath r i -> go r <+> "[" <+> fromString (show i) <+> "]"
|
|
|
|
FieldPath r f -> go r <+> ".\"" <+> fromString (T.unpack f) <+> "\""
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
fromTableName :: TableName -> Printer
|
2022-02-24 11:13:19 +03:00
|
|
|
fromTableName (TableName tableName (SchemaName tableSchema)) =
|
2021-02-23 20:37:27 +03:00
|
|
|
fromNameText tableSchema <+> "." <+> fromNameText tableName
|
|
|
|
|
|
|
|
fromAliased :: Aliased Printer -> Printer
|
|
|
|
fromAliased Aliased {..} =
|
|
|
|
aliasedThing
|
|
|
|
<+> ((" AS " <+>) . fromNameText) aliasedAlias
|
|
|
|
|
2021-11-19 20:05:01 +03:00
|
|
|
fromColumnName :: ColumnName -> Printer
|
2023-02-16 14:18:46 +03:00
|
|
|
fromColumnName (ColumnName colname) = quoteIdentifier colname
|
2021-11-19 20:05:01 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
fromNameText :: Text -> Printer
|
2023-02-16 14:18:46 +03:00
|
|
|
fromNameText = quoteIdentifier
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2022-04-28 22:33:33 +03:00
|
|
|
fromRawUnescapedText :: Text -> Printer
|
|
|
|
fromRawUnescapedText t = QueryPrinter (rawUnescapedText t)
|
|
|
|
|
2023-02-16 14:18:46 +03:00
|
|
|
-- | In Sql Server identifiers can be quoted using square brackets or double
|
|
|
|
-- quotes, "Delimited Identifiers" in T-SQL parlance, which gives full freedom
|
|
|
|
-- in what can syntactically constitute a name of a thing.
|
|
|
|
--
|
|
|
|
-- The delimiting characters may themselves appear in a delimited identifier,
|
|
|
|
-- in which case they are quoted by duplication of the terminal delimiter. This
|
|
|
|
-- is the only character escaping that happens within a delimited identifier.
|
|
|
|
--
|
|
|
|
-- (TODO: That fact does not seem to be documented anywhere I could find, but
|
|
|
|
-- seems to be folklore. I verified it myself at any rate)
|
|
|
|
--
|
|
|
|
-- Reference: https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver16
|
|
|
|
quoteIdentifier :: Text -> Printer
|
|
|
|
quoteIdentifier ident = QueryPrinter (rawUnescapedText ("[" <> duplicateBrackets ident <> "]"))
|
|
|
|
where
|
|
|
|
duplicateBrackets :: Text -> Text
|
|
|
|
duplicateBrackets = T.replace "]" "]]"
|
|
|
|
|
2021-05-21 15:27:44 +03:00
|
|
|
truePrinter :: Printer
|
|
|
|
truePrinter = "(1=1)"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-05-21 15:27:44 +03:00
|
|
|
falsePrinter :: Printer
|
|
|
|
falsePrinter = "(1<>1)"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-07-08 23:49:10 +03:00
|
|
|
parens :: Printer -> Printer
|
|
|
|
parens p = "(" <+> IndentPrinter 1 p <+> ")"
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-01-03 20:16:24 +03:00
|
|
|
-- * Basic printing API
|
|
|
|
|
|
|
|
-- | Pretty-prints a 'Printer' as one line, converting 'NewlinePrinter' to space.
|
2021-02-23 20:37:27 +03:00
|
|
|
toQueryFlat :: Printer -> Query
|
|
|
|
toQueryFlat = go 0
|
|
|
|
where
|
|
|
|
go level =
|
|
|
|
\case
|
|
|
|
QueryPrinter q -> q
|
|
|
|
SeqPrinter xs -> mconcat (filter notEmpty (map (go level) xs))
|
|
|
|
SepByPrinter x xs ->
|
|
|
|
mconcat
|
|
|
|
(intersperse (go level x) (filter notEmpty (map (go level) xs)))
|
|
|
|
NewlinePrinter -> " "
|
|
|
|
IndentPrinter n p -> go (level + n) p
|
|
|
|
notEmpty = (/= mempty) . renderQuery
|
|
|
|
|
2022-01-03 20:16:24 +03:00
|
|
|
-- | Pretty-prints a 'Printer' as multiple lines as defined by the printer.
|
2021-02-23 20:37:27 +03:00
|
|
|
toQueryPretty :: Printer -> Query
|
|
|
|
toQueryPretty = go 0
|
|
|
|
where
|
|
|
|
go level =
|
|
|
|
\case
|
|
|
|
QueryPrinter q -> q
|
|
|
|
SeqPrinter xs -> mconcat (filter notEmpty (map (go level) xs))
|
|
|
|
SepByPrinter x xs ->
|
|
|
|
mconcat
|
|
|
|
(intersperse (go level x) (filter notEmpty (map (go level) xs)))
|
|
|
|
NewlinePrinter -> "\n" <> indentation level
|
|
|
|
IndentPrinter n p -> go (level + n) p
|
|
|
|
indentation n = rawUnescapedText (T.replicate n " ")
|
|
|
|
notEmpty = (/= mempty) . renderQuery
|