-- | A quasiquoter for SQL text that spans several lines of a Haskell-- source file. A migration writes---- > migrationUp =-- > runSql-- > [sqlText|-- > CREATE TABLE saved_views (-- > name text PRIMARY KEY-- > );-- > |]---- and 'sqlText' gives the 'Text'-- @"CREATE TABLE saved_views (\\n name text PRIMARY KEY\\n);\\n"@. The-- module that uses 'sqlText' turns on the extension @QuasiQuotes@.module Database.DagMigrations.PostgreSQL.SqlText ( sqlText, unquoteSqlText, SqlTextError (..), )whereimport Data.List.NonEmpty (NonEmpty (..))import Data.List.NonEmpty qualified as NonEmptyimport Data.Maybe (fromMaybe)import Data.Text (Text)import Data.Text qualified as Textimport Language.Haskell.TH.Quote (QuasiQuoter (..))import Language.Haskell.TH.Syntax (lift)-- | The quasiquoter for SQL text. GHC calls it at compile time for each-- quotation @[sqlText| ... |]@, which then stands for a 'Text'.---- Preconditions: the quotation follows the layout that 'unquoteSqlText'-- describes, and is in the place of an expression.---- Inputs: the characters between @[sqlText|@ and @|]@.---- Output: the 'Text' that 'unquoteSqlText' gives. When the quotation does-- not follow the layout, or stands in the place of a pattern, a type or a-- declaration, compilation fails with a message that names the problem.---- Side effects: none at run time.sqlText :: QuasiQuotersqlText = QuasiQuoter { quoteExp = either (fail . describeSqlTextError) lift . unquoteSqlText, quotePat = const (fail "sqlText: a quotation [sqlText| ... |] stands for an expression, not a pattern"), quoteType = const (fail "sqlText: a quotation [sqlText| ... |] stands for an expression, not a type"), quoteDec = const (fail "sqlText: a quotation [sqlText| ... |] stands for an expression, not a declaration") }-- | Gives the text for which a quotation @[sqlText| ... |]@ stands.---- Preconditions: none.---- Inputs: the characters between @[sqlText|@ and @|]@. The quotation-- follows this layout:---- * The opening line, from @[sqlText|@ to the first line break, holds-- only spaces.-- * The closing line, from the last line break to @|]@, holds only-- spaces.-- * The lines between the opening line and the closing line hold the-- SQL.---- A carriage return before a line break is part of the line break.-- Backslashes and quotation marks stand for themselves. The characters-- @|]@ end the quotation, so the SQL cannot hold them.---- Output: the lines of the SQL, each followed by a line feed. The-- function removes the indentation from each line. The indentation is-- the least number of spaces at the start of a line that holds a-- character other than a space. A tab is such a character, not-- indentation. A line that holds only spaces keeps the spaces beyond the-- indentation. When no line holds a character other than a space, each-- line gives an empty line. When the quotation does not follow the-- layout, the result is the first problem that the function finds, as a-- 'SqlTextError'.---- Side effects: none.unquoteSqlText :: String -> Either SqlTextError TextunquoteSqlText quotation = case Text.splitOn "\n" (Text.pack quotation) of opening : afterOpening : rest | not (onlySpaces (withoutCarriageReturn opening)) -> Left SqlTextAfterOpening | not (onlySpaces closing) -> Left SqlTextBeforeClosing | otherwise -> Right (foldMap (\line -> Text.drop indentation line <> "\n") sqlLines) where linesAfterOpening = afterOpening :| rest closing = NonEmpty.last linesAfterOpening sqlLines = map withoutCarriageReturn (NonEmpty.init linesAfterOpening) -- With no line that holds a character other than a space, the -- indentation is maxBound, and each line gives an empty line. indentation = minimum (maxBound :| [Text.length (Text.takeWhile (== ' ') line) | line <- sqlLines, not (onlySpaces line)]) _ -> Left SqlTextOnOneLine where onlySpaces = Text.all (== ' ') withoutCarriageReturn line = fromMaybe line (Text.stripSuffix "\r" line)-- | A reason why 'unquoteSqlText' refuses a quotation.data SqlTextError = -- | The quotation holds no line break, so it has no opening line and -- no closing line. SqlTextOnOneLine | -- | The opening line holds a character other than a space after -- @[sqlText|@. SqlTextAfterOpening | -- | The closing line holds a character other than a space before -- @|]@. SqlTextBeforeClosing deriving (Eq, Show)-- | The message of a compilation error for a quotation that does not-- follow the layout.describeSqlTextError :: SqlTextError -> StringdescribeSqlTextError problem = case problem of SqlTextOnOneLine -> "sqlText: the quotation holds no line break. Start the SQL on the line after [sqlText|, and put |] on a line of its own." SqlTextAfterOpening -> "sqlText: the line of [sqlText| holds more than spaces after it. Start the SQL on the next line." SqlTextBeforeClosing -> "sqlText: the line of |] holds more than spaces before it. Put |] on a line of its own, after spaces only."