Skip to content

Commit

Permalink
Merge pull request #35 from emdash/select_distinct
Browse files Browse the repository at this point in the history
Add a `SELECT_DISTINCT` query variant
  • Loading branch information
stefan-hoeck authored Dec 5, 2023
2 parents 421547f + 0aed00d commit 5f9de3e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Sqlite3/Cmd.idr
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ record Query (t : Type) where
[noHints]
constructor Q
{auto fromRow : FromRow t}
distinct : Bool
schema : Schema
from : From schema
columns : LAll (NamedExpr schema) (FromRowTypes t)
Expand Down Expand Up @@ -376,7 +377,16 @@ SELECT :
-> LAll (NamedExpr s) (FromRowTypes t)
-> From s
-> Query t
SELECT xs from = Q s from xs TRUE TRUE [] [] Nothing 0
SELECT xs from = Q False s from xs TRUE TRUE [] [] Nothing 0

public export %inline
SELECT_DISTINCT :
{s : _}
-> {auto fromRow : FromRow t}
-> LAll (NamedExpr s) (FromRowTypes t)
-> From s
-> Query t
SELECT_DISTINCT xs from = Q True s from xs TRUE TRUE [] [] Nothing 0

public export %inline
GROUP_BY : (q : Query t) -> List (GroupingTerm $ ExprSchema q.columns) -> Query t
Expand Down
7 changes: 5 additions & 2 deletions src/Sqlite3/Parameter.idr
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,14 @@ encodeHaving x = do
||| inserted as placeholders for literal values where appropriate.
export
encodeQuery : Query ts -> ParamStmt
encodeQuery (Q _ from vs where_ having group_by order_by lim off) = do
encodeQuery (Q distinct _ from vs where_ having group_by order_by lim off) = do
vstr <- namedExprs [<] vs
fstr <- encodeFrom from
wh <- encodeExprP where_
hav <- encodeHaving having
grp <- encodeOrd "GROUP BY" (map ord group_by)
ord <- encodeOrd "ORDER BY" order_by
pure "SELECT \{vstr} \{fstr} WHERE \{wh} \{grp} \{hav} \{ord} \{limit lim off}"
let rest = "\{vstr} \{fstr} WHERE \{wh} \{grp} \{hav} \{ord} \{limit lim off}"
pure $ case distinct of
True => "SELECT DISTINCT \{rest}"
False => "SELECT \{rest}"
11 changes: 11 additions & 0 deletions test/src/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ app = withDB ":memory:" $ do
, IF_NOT_EXISTS createFiles
, IF_NOT_EXISTS createUnits
, IF_NOT_EXISTS createEmployees
, IF_NOT_EXISTS createEdges
, insertMol $ M "Ethanol" (Just "64-17-5") (Just 46.069) Compound
, insertMol $ M "Strychnine" (Just "57-24-9") (Just 334.419) Compound
, insertMol $ M "Atropine" (Just "51-55-8") (Just 289.375) Compound
Expand All @@ -55,6 +56,14 @@ app = withDB ":memory:" $ do
, insertEmployee $ E "Gundi" 2050.0 1
, insertEmployee $ E "Valeri" 5010.0 1
, insertEmployee $ E "Ronja" 4010.0 1
, insertEdge "A" "B"
, insertEdge "B" "C"
, insertEdge "B" "D"
, insertEdge "C" "E"
, insertEdge "C" "F"
, insertEdge "D" "F"
, insertEdge "F" "G"
, insertEdge "B" "F"
] ++ fromList (insertFile . file <$> [0..255])

queryTable (mol TRUE) 1000 >>= printTable
Expand All @@ -78,5 +87,7 @@ app = withDB ":memory:" $ do
queryTable unitStats 1000 >>= printTable
putStrLn ""

queryTable parents 1000 >>= printTable

main : IO ()
main = runApp handlers app
28 changes: 28 additions & 0 deletions test/src/Schema.idr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Files =
, C "content" BLOB
]

public export
Edges : SQLTable
Edges =
table "edges"
[ C "u" TEXT
, C "v" TEXT
]

export
createMolecules : Cmd TCreate
createMolecules =
Expand Down Expand Up @@ -92,6 +100,15 @@ createEmployees =
, UNIQUE ["name"]
]

export
createEdges : Cmd TCreate
createEdges =
CREATE_TABLE Edges
[ PRIMARY_KEY ["u", "v"]
, NOT_NULL "u"
, NOT_NULL "v"
]

--------------------------------------------------------------------------------
-- Idris Types
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -157,6 +174,10 @@ export
insertFile : File -> Cmd TInsert
insertFile = insert Files ["content"]

export
insertEdge : String -> String -> Cmd TInsert
insertEdge u v = INSERT Edges ["u", "v"] [val u, val v]

--------------------------------------------------------------------------------
-- Query
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -232,3 +253,10 @@ tuples =
]
`WHERE` ("m1.molweight" < "m2.molweight")
`OFFSET` 2

public export
parents : LQuery [String]
parents =
SELECT_DISTINCT
["u"]
[< FROM $ Edges `AS` "e"]

0 comments on commit 5f9de3e

Please sign in to comment.