0 | module Oracle.Internal.Query
 1 |
 2 | import Data.String
 3 | import Oracle.Types.Query
 4 | import Oracle.Types.QueryColumn
 5 |
 6 | ||| Render a single query column into its SQL representation.
 7 | |||
 8 | ||| `Column` leaves the supplied expression unchanged.
 9 | |||
10 | ||| `JSONColumn` wraps the supplied expression with `JSON_SERIALIZE(... RETURNING CLOB)` so that Oracle returns the JSON value as CLOB text rather than as a native Oracle JSON value.
11 | |||
12 | public export
13 | renderQueryColumn : QueryColumn -> String
14 | renderQueryColumn (Column expression)     =
15 |   expression
16 | renderQueryColumn (JSONColumn expression) =
17 |   "JSON_SERIALIZE(" ++
18 |   expression        ++
19 |   " RETURNING CLOB)"
20 |
21 | ||| Construct the SQL statement represented by a Query.
22 | |||
23 | ||| The query columns are rendered into the SELECT projection and joined with commas.
24 | |||
25 | ||| The query body is appended after FROM and is expected to contain the table expression and any additional SQL clauses.
26 | |||
27 | ||| For example:
28 | |||
29 | |||   buildQuerySQL $
30 | |||     MkQuery
31 | |||       [Column "id", JSONColumn "profile"]
32 | |||       "people"
33 | |||       []
34 | |||
35 | ||| Produces:
36 | |||
37 | |||   SELECT id, JSON_SERIALIZE(profile RETURNING CLOB) FROM people
38 | |||
39 | public export
40 | buildQuerySQL : Query -> String
41 | buildQuerySQL query =
42 |   "SELECT "                                           ++
43 |   joinBy ", " (map renderQueryColumn (columns query)) ++
44 |   " FROM "                                            ++
45 |   querybody query
46 |