Commit cfc431c2 authored by Michael Hanus 's avatar Michael Hanus
Browse files

Language tag "sql*" for raw DBAction added

parent 64883de4
......@@ -566,6 +566,21 @@ allStudents :: IO [Student]
allStudents = liftIO fromSQLResult ``sql Select * From Student;''
\end{curry}
%
In order to get more control on executing the SQL statement,
one can add a star character after the language tag.
In this case, the SQL statement is translated into a database action,
i.e., into the type \code{DBAction} defined in the \CYS library
\code{Database.CDBI.Connection}:
%
\begin{curry}
allStudentsAction :: DBAction [Student]
allStudentsAction = ``sql* Select * From Student;''
\end{curry}
%
Then one can put \code{allStudentsAction} inside a database transaction
or combine it with other database actions (see \code{Database.CDBI.Connection}
for operations for this purpose).
In order to select students with an age between 20 and 25,
one can put a condition as usual:
%
......
......@@ -26,15 +26,16 @@ testS1 = queryS1 `returns` Right
testS2 :: IO() -- SQLResult [(Student,Uni_CDBI.Result)])
testS2 = do
result <- ``sql Select * From Student as s Inner Join Result as r On Satisfies s has_a r;''
result <- ``sql Select * From Student as s Inner Join Result as r
On Satisfies s has_a r;''
printResult result
queryS3 :: IO (SQLResult [String])
queryS3 = ``sql Select Distinct s.Name From Student as s;''
queryS3 :: IO [String]
queryS3 = liftIO fromSQLResult
``sql Select Distinct s.Name From Student as s;''
testS3 :: PropIO
testS3 = queryS3 `returns`
Right ["Muster","Arndt","Ziege","Sonne","Mond","Stern"]
testS3 = queryS3 `returns` ["Muster","Arndt","Ziege","Sonne","Mond","Stern"]
testS4 :: IO() -- SQLResult [String])
testS4 = do
......
......@@ -31,15 +31,16 @@ import SQLTyper
--- Converts a string representing SQL-requests into functions defined
--- in the CDBI interface by calling the different stage of transformation.
---@parserInfo - either the parser information or an error message
---@param pos - Position of the integrated SQL-String in the orginal file
---@param code - the SQL-request as string
---@return A String in Curry-Syntax (CDBI-functions).
parse :: Either String ParserInfo -> LangParser
parse parserInfo pos code =
--- @param withrundb - decorate target code with `runWithDB`
--- @param parserInfo - either the parser information or an error message
--- @param pos - Position of the integrated SQL-String in the orginal file
--- @param code - the SQL-request as string
--- @return A String in Curry-Syntax (CDBI-functions).
parse :: Bool -> Either String ParserInfo -> LangParser
parse withrundb parserInfo pos code =
case parserInfo of
Left err -> return (throwPM pos err)
Right pi -> processCompilation pi pos code
Right pi -> processCompilation withrundb pi pos code
--- Reader for parser information file.
--- @param verb - verbosity level
......@@ -63,8 +64,8 @@ checkResult pm@(WM (OK _ ) _) = Right pm
-- Calls the stages of parsing process in the right order
-- and passes corresponding parts of the parser information.
-- Aborts process in case a stage returns with an error.
processCompilation :: ParserInfo -> LangParser
processCompilation parserInfo pos code =
processCompilation :: Bool -> ParserInfo -> LangParser
processCompilation withrundb parserInfo pos code =
let parsedCode = parseTkLs pos (scan code)
in case checkResult parsedCode of
Left pm -> return pm
......@@ -88,7 +89,7 @@ processCompilation parserInfo pos code =
in case checkResult typeCheckedCode of
Left pm -> return pm
Right _ -> return (translate typeCheckedCode
(dbName parserInfo)
withrundb
(cdbiModule parserInfo)
pos)
-- ---------------------------------------------------------
--- This module translates the abstract syntax tree
--- into a curry programm using the AbstractCurry libraries.
--- Errors are thrown for limitations caused by CDBI.
--- The translation is provided as a single line string as
--- required by currypp.
---
--- @author: Julia Krone
--- @version: 0.1
-- ---------------------------------------------------------
module SQLTranslator(translate) where
......@@ -26,30 +27,32 @@ mCDBI = "Database.CDBI.ER"
--- Invokes the translation of the AST into a string of curry code
--- in case a valid AST is given, does nothing otherwise.
translate :: PM[Statement] -> String -> String -> Pos -> PM String
translate :: PM [Statement] -> Bool -> String -> Pos -> PM String
translate (WM (Errors err) ws) _ _ _ = WM (throwPR err) ws
translate (WM (OK stats) ws) dbPath mModel pos =
translate (WM (OK stats) ws) withrundb mModel pos =
let (WM resPR warns) = sequencePM (map (transStatement pos mModel) stats )
in liftPM (writeFunction dbPath) (WM resPR (warns++ws))
-- The list of CExpr representing the statements is concatenated and pretty
-- printed to obtain a single-line-translation line feeds are replaced by
-- space characters and indentation is removed
writeFunction :: String -> [CExpr] -> String
writeFunction db stats =
let finExpr = (applyF (mCDBI, "runWithDB")
[(string2ac db), (concatStatements stats)])
finStr = (pPrint (ppCExpr defaultOptions finExpr))
newLines = splitOn ['\n'] finStr
in '(' : removeIndents (intercalate [' '] newLines) ++ ")"
in liftPM showFunction (WM resPR (warns++ws))
where
-- The list of CExpr representing the statements is concatenated and pretty
-- printed. To obtain a single-line translation, line feeds are replaced by
-- space characters and indentation is removed
showFunction stats =
let finExpr = if withrundb
then applyF (mCDBI, "runWithDB")
[constF (mModel, "sqliteDBFile"),
concatStatements stats]
else concatStatements stats
finStr = (pPrint (ppCExpr defaultOptions finExpr))
newLines = splitOn ['\n'] finStr
in '(' : removeIndents (intercalate [' '] newLines) ++ ")"
removeIndents :: String -> String
removeIndents [] = []
removeIndents (s:str) =
case s of
' ' -> let (spaces, rest) = span (\c -> c == ' ') str
in s:(removeIndents rest)
_ -> s:(removeIndents str)
' ' -> let (_, rest) = span (\c -> c == ' ') str
in s : removeIndents rest
_ -> s : removeIndents str
concatStatements :: [CExpr] -> CExpr
concatStatements [] = CLambda [cpvar "conn"]
......
......@@ -26,7 +26,7 @@
--- sql - see the SQLConverter and CDBI-library
---
--- @author Jasper Sikorra (with changes by Michael Hanus)
--- @version June 2016
--- @version January 2018
------------------------------------------------------------------------------
module TransICode where
......@@ -61,7 +61,10 @@ parsers = maybe iden pars
case l of
"sql" -> case model of
Left err -> const (return $ throwPM p err)
_ -> SQLParser.parse model p
_ -> SQLParser.parse True model p
"sql*" -> case model of
Left err -> const (return $ throwPM p err)
_ -> SQLParser.parse False model p
"dummy" -> DummyParser.parse p
"format" -> FormatParser.parse "" p
"printf" -> FormatParser.parse "putStr" p
......
......@@ -7,7 +7,7 @@
--- is supported (option `foreigncode`, see module `Translator`).
---
--- @author Michael Hanus
--- @version July 2017
--- @version January 2018
------------------------------------------------------------------------------
import AbstractCurry.Types
......@@ -28,7 +28,7 @@ import TransContracts (transContracts)
cppBanner :: String
cppBanner = unlines [bannerLine,bannerText,bannerLine]
where
bannerText = "Curry Preprocessor (version of 05/01/2018)"
bannerText = "Curry Preprocessor (version of 09/01/2018)"
bannerLine = take (length bannerText) (repeat '=')
--- Preprocessor targets, i.e., kind of entities to be preprocessed:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment