Skip to content

Commit 4ecbe29

Browse files
committed
more functions
1 parent e3486d8 commit 4ecbe29

7 files changed

Lines changed: 69 additions & 60 deletions

File tree

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
1010
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
1111
DataValues = "e7dc6d0d-1eca-5fa6-8ad6-5aecde8b7ea5"
1212
TableShowUtils = "5e66a065-1f0a-5976-b372-e0b8c017ca10"
13+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1314

1415
[extras]
1516
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/QuerySQLite.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import SQLite: getvalue
1616
using SQLite: columns, DB, execute!, generate_namedtuple, juliatype,
1717
SQLITE_DONE, SQLITE_NULL, SQLITE_ROW, sqlite3_column_count, sqlite3_column_name,
1818
sqlite3_column_type, sqlite3_step, sqlitevalue, Stmt, tables
19+
import Statistics: mean, sum
1920
using TableShowUtils: printdataresource, printHTMLtable, printtable
2021
import TableTraits: isiterabletable
2122

2223
export Database
2324

25+
include("functions.jl")
2426
include("utilities.jl")
2527
include("database.jl")
2628
include("code_instead.jl")

src/code_instead.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ end
107107

108108
@code_instead abs SourceCode
109109

110+
@code_instead char SourceCode Vararg{Any}
111+
110112
@code_instead coalesce SourceCode Vararg{Any}
111113

112114
@code_instead QueryOperators.drop SourceCode Integer
@@ -126,6 +128,10 @@ end
126128
# TODO: add more methods
127129
@code_instead in SourceCode Any
128130

131+
@code_instead instr SourceCode Any
132+
@code_instead instr Any SourceCode
133+
@code_instead instr SourceCode SourceCode
134+
129135
@code_instead isequal SourceCode Any
130136
@code_instead isequal Any SourceCode
131137
@code_instead isequal SourceCode SourceCode
@@ -140,10 +146,14 @@ end
140146

141147
@code_instead max SourceCode Vararg{Any}
142148

149+
@code_instead mean SourceCode
150+
143151
@code_instead min SourceCode Vararg{Any}
144152

145153
@code_instead length SourceCode
146154

155+
@code_instead lowercase SourceCode
156+
147157
@code_instead QueryOperators.map SourceCode Any Expr
148158

149159
@code_instead occursin Regex SourceCode
@@ -160,21 +170,15 @@ end
160170

161171
@code_instead QueryOperators.thenby_descending SourceCode Any Expr
162172

173+
@code_instead sum SourceCode
174+
163175
# TODO: add more methods
164176
@code_instead QueryOperators.unique SourceCode Any Expr
165177

166178
@code_instead uppercase SourceCode
167179

168180
# TODO: add
169-
# char
170-
# glob
171181
# hex
172-
# ifnull
173-
# instr
174-
# like
175-
# lower
176-
# ltrim
177-
# nullif
178182
# printf
179183
# quote
180184
# random
@@ -185,14 +189,13 @@ end
185189
# trim
186190
# typeof
187191
# unicode
188-
# upper
189192
# zeroblob
190-
# avg
191193
# group_concat
192-
# sum
193194
# total
194195
# date
195196
# time
196197
# datetime
197198
# julianday
198199
# strftime
200+
201+
# TODO: regex start and end

src/translate.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ struct SQLExpression
66
SQLExpression(call, arguments...) = new(call, arguments)
77
end
88

9+
function translate(something::AbstractString; primary = true)
10+
repr(something)
11+
end
12+
913
function translate(something; primary = true)
1014
something
1115
end
@@ -61,7 +65,7 @@ function as(pair; primary = true)
6165
)
6266
end
6367

64-
@translate_default ::Type{Char} :CHAR
68+
@translate_default ::typeof(char) :CHAR
6569

6670
@translate_default ::typeof(coalesce) :COALESCE
6771

@@ -109,6 +113,8 @@ end
109113

110114
@translate_default ::typeof(in) :IN
111115

116+
@translate_default ::typeof(instr) :INSTR
117+
112118
@translate_default ::typeof(isequal) Symbol("IS NOT DISTINCT FROM")
113119

114120
@translate_default ::typeof(isless) :<
@@ -155,6 +161,8 @@ end
155161

156162
@translate_default ::typeof(max) :max
157163

164+
@translate_default ::typeof(mean) :AVG
165+
158166
@translate_default ::typeof(min) :min
159167

160168
translate_call(::typeof(occursin), needle::Regex, haystack; primary = true) =
@@ -183,6 +191,8 @@ end
183191

184192
@translate_default ::typeof(string) :||
185193

194+
@translate_default ::typeof(sum) :SUM
195+
186196
@translate_default ::typeof(QueryOperators.take) :LIMIT
187197

188198
function translate_call(::typeof(QueryOperators.thenby), unordered, key_function, key_function_expression; primary = true)

src/utilities.jl

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,3 @@ function split_call(call_expression::Expr)
5858
error("$call_expression is not a function call")
5959
end
6060
end
61-
62-
"""
63-
if_else(switch, yes, no)
64-
65-
`ifelse` that you can add methods to.
66-
67-
```jldoctest
68-
julia> using QuerySQLite
69-
70-
julia> if_else(true, 1, 0)
71-
1
72-
73-
julia> if_else(false, 1, 0)
74-
0
75-
```
76-
"""
77-
function if_else(switch, yes, no)
78-
ifelse(switch, yes, no)
79-
end
80-
export if_else
81-
82-
"""
83-
type_of(it)
84-
85-
`typeof` that you can add methods to.
86-
87-
```jldoctest
88-
julia> using QuerySQLite
89-
90-
julia> type_of('a')
91-
Char
92-
```
93-
"""
94-
function type_of(it)
95-
typeof(it)
96-
end
97-
export type_of

test/runtests.jl

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using QuerySQLite
12
using Query
23
using Test
3-
using QuerySQLite
44
using SQLite: DB, drop!, execute!, Stmt
55
using QueryTables
6+
using Statistics: mean
67

78
@testset "SQLite tutorial" begin
89

@@ -60,18 +61,32 @@ database2 = Database(DB(filename))
6061
DataTable |>
6162
length == 347
6263

63-
@test (database.Track |>
64-
@groupby(_.AlbumId) |>
65-
@map({AlbumId = key(_), Count = length(_.AlbumId)}) |>
66-
collect |>
67-
first).Count == 10
68-
6964
@test (database.Track |>
7065
@map({_.Name, _.Milliseconds, _.Bytes, _.AlbumId}) |>
7166
@filter(_.AlbumId == 1) |>
7267
collect |>
7368
first).Name == "For Those About To Rock (We Salute You)"
7469

70+
group_by_row =
71+
database.Track |>
72+
@groupby(_.AlbumId) |>
73+
@map({
74+
AlbumId = key(_),
75+
length = length(_.AlbumId),
76+
sum = sum(_.Milliseconds),
77+
min = min(_.Milliseconds),
78+
max = max(_.Milliseconds),
79+
mean = mean(_.Milliseconds)
80+
}) |>
81+
collect |>
82+
first
83+
84+
@test group_by_row.AlbumId == 1
85+
@test group_by_row.length == 10
86+
@test group_by_row.sum == 2400415
87+
@test group_by_row.min == 199836
88+
@test group_by_row.mean == 240041.5
89+
7590
end
7691

7792
@testset "Systematic tests" begin
@@ -85,10 +100,12 @@ execute!(Stmt(connection, """
85100
b Int,
86101
c Int,
87102
d Text,
88-
e Int
103+
e Int,
104+
f Int,
105+
g Text
89106
)"""))
90107
execute!(Stmt(connection, """
91-
INSERT INTO test VALUES(0, 1, -1, "ab", NULL)
108+
INSERT INTO test VALUES(0, 1, -1, "ab", NULL, 65, "b")
92109
"""))
93110
database = Database(connection)
94111
result =
@@ -113,10 +130,15 @@ result =
113130
if_else_test_6 = if_else(_.b, _.b, 0),
114131
if_else_test_7 = if_else(_.b, _.b, _.a),
115132
ismissing_test = ismissing(_.e),
133+
lowercase_test = lowercase(_.d),
116134
max_test = max(_.b, 0),
117135
min_test = min(_.a, 1),
118-
occursin_test = occursin(r"a.*", _.d),
119-
uppercase_test = uppercase(_.d)
136+
occursin_test = occursin(r"A.*", _.d),
137+
uppercase_test = uppercase(_.d),
138+
char_test = char(_.f),
139+
instr_test_1 = instr(_.d, "b"),
140+
instr_test_2 = instr("ab", _.g),
141+
instr_test_3 = instr(_.d, _.g)
120142
}) |>
121143
collect |>
122144
first
@@ -144,9 +166,17 @@ result =
144166
@test result.min_test == 0
145167
@test result.occursin_test == 1
146168
@test result.uppercase_test == "AB"
169+
@test result.lowercase_test == "ab"
170+
@test result.char_test == "A"
171+
# TODO: fix
172+
@test_broken result.instr_test_1 == 2
173+
@test result.instr_test_2 == 2
174+
@test result.instr_test_3 == 2
147175

148176
drop!(connection, "test")
149177

150178
@test_throws ArgumentError Database("file.not_sqlite")
151179

152180
end
181+
182+
# TODO: add doctests as tests

test/test.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)