Skip to content

Commit 02de40f

Browse files
committed
fix
1 parent 97a7fa4 commit 02de40f

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/database.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ struct Database{Source}
2828
end
2929

3030
function Database(filename::AbstractString)
31-
if endswith(filename, "sqlite")
31+
if endswith(filename, ".sqlite")
3232
Database(SQLite.DB(filename))
3333
else
34-
error("Unsupported database type for $filename")
34+
throw(ArgumentError("Unsupported database type for $filename"))
3535
end
3636
end
3737

src/show.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ function prefix(io, call, argument1)
55
print(io, argument1)
66
end
77

8+
function parenthesize_prefix(io, call, argument1)
9+
print(io, call)
10+
print(io, ' ')
11+
print(io, '(')
12+
print(io, argument1)
13+
print(io, ')')
14+
end
15+
816
function postfix(io, call, argument1)
917
print(io, argument1)
1018
print(io, ' ')
@@ -17,6 +25,14 @@ function tight_infix(io, call, argument1, argument2)
1725
print(io, argument2)
1826
end
1927

28+
function parenthesize_infix(io, call, argument1, argument2)
29+
print(io, argument1)
30+
print(io, ' ')
31+
print(io, call)
32+
print(io, ' ')
33+
print(io, argument2)
34+
end
35+
2036
function infix(io, call, argument1, argument2, arguments...)
2137
print(io, argument1)
2238
print(io, ' ')
@@ -89,13 +105,13 @@ function show(io::IO, sql_expression::SQLExpression)
89105
elseif call === :AND
90106
infix(io, call, arguments...)
91107
elseif call === :AS
92-
infix(io, call, arguments...)
108+
parenthesize_infix(io, call, arguments...)
93109
elseif call === :CASE
94110
case(io, call, arguments...)
95111
elseif call === :DESC
96112
postfix(io, call, arguments...)
97113
elseif call === :FROM
98-
prefix(io, call, arguments...)
114+
parenthesize_prefix(io, call, arguments...)
99115
elseif call === Symbol("GROUP BY")
100116
head_call_tail(io, call, arguments...)
101117
elseif call === :IN

src/translate.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ function translate_call(::typeof(QueryOperators.groupby), ungrouped, group_funct
9595
# TODO: map_selector
9696
model = model_row(ungrouped)
9797
SQLExpression(Symbol("GROUP BY"),
98-
translate(ungrouped; primary = primary),
98+
SQLExpression(:AS, SQLExpression(:FROM, translate_call(
99+
QueryOperators.map,
100+
ungrouped,
101+
map_selector, map_function_expression,
102+
primary = primary
103+
)), :__TABLE__),
99104
translate(group_function(model).code; primary = primary)
100105
)
101106
end

test/runtests.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ database2 = Database(DB(filename))
5757
@map({AlbumId = key(_), Count = length(_.AlbumId)}) |>
5858
collect |>
5959
first).Count == 10
60+
6061
end
6162

6263
@testset "Systematic tests" begin
6364

6465
filename = joinpath(@__DIR__, "test.sqlite")
6566
connection = DB(filename)
66-
67+
execute!(Stmt(connection, """DROP TABLE IF EXISTS test"""))
6768
execute!(Stmt(connection, """
6869
CREATE TABLE test (
6970
a Int,
@@ -74,7 +75,6 @@ execute!(Stmt(connection, """
7475
INSERT INTO test VALUES(0, 1, -1)
7576
"""))
7677
database = Database(connection)
77-
7878
result =
7979
database.test |>
8080
@map({
@@ -86,7 +86,8 @@ result =
8686
h = _.a * _.b,
8787
i = _.a + _.b,
8888
j = _.a % _.b,
89-
k = abs(_.c)
89+
k = abs(_.c),
90+
l = _.a in (0, 1)
9091
}) |>
9192
collect |>
9293
first
@@ -100,7 +101,10 @@ result =
100101
@test result.i == 1
101102
@test result.j == 0
102103
@test result.k == 1
104+
@test result.l == 1
103105

104106
drop!(connection, "test")
105107

108+
@test_throws ArgumentError Database("file.not_sqlite")
109+
106110
end

test/test.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)