Skip to content

Commit 063d7a6

Browse files
committed
more remaining TODOs
1 parent eaea1a4 commit 063d7a6

8 files changed

Lines changed: 37 additions & 37 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.jl.mem
44
.vscode
55
docs/build
6+
test/test.sqlite

docs/src/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Patterns like `if` and functions like `ifelse` and `typeof` can't be overloaded.
1818

1919
If you would like to translate code to SQL, but you do not pass any SQL arguments, you will need to use [`BySQL`](@ref) to pass a dummy SQL object instead. See the `BySQL` docstring for more information.
2020

21+
### Pattern matching
22+
23+
Use SQLite syntax, not Julia syntax, for pattern matching for regular expressions and date formats.
24+
2125
## Developer documentation
2226

2327
QuerySQLite hijacks Julia's multiple dispatch to translate external database commands to SQL instead of evaluating them. To do this, it constructs a "model_row" that represents the structure of a row of data. If you would like to add support for a new function, there are only a few steps:

src/QuerySQLite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Base: Generator, NamedTuple, RefValue, SizeUnknown, tail
77
using Base.Meta: quot
88
import Base.Multimedia: showable
99
using DataValues: DataValue
10-
import Dates: Date, DateTime, format, Time
10+
import Dates: format
1111
import IteratorInterfaceExtensions: getiterator, isiterable
1212
import MacroTools
1313
using MacroTools: @capture

src/code_instead.jl

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,13 @@ end
159159

160160
@code_instead convert Type{Int} SourceCode
161161

162-
@code_instead Date SourceCode
163-
164-
@code_instead DateTime SourceCode
165-
166162
@code_instead QueryOperators.drop SourceCode Integer
167163

168164
@code_instead QueryOperators.filter SourceCode Any Expr
169165

170-
# TODO: support more methods
171166
@code_instead format SourceCode AbstractString
167+
@code_instead format AbstractString SourceCode
168+
@code_instead format SourceCode SourceCode
172169

173170
@code_instead QueryOperators.groupby SourceCode Any Expr Any Expr
174171

@@ -215,18 +212,19 @@ end
215212

216213
@code_instead QueryOperators.map SourceCode Any Expr
217214

218-
@code_instead occursin Regex SourceCode
215+
@code_instead occursin AbstractString SourceCode
216+
@code_instead occursin SourceCode AbstractString
217+
@code_instead occursin SourceCode SourceCode
219218

220219
@code_instead QueryOperators.orderby SourceCode Any Expr
221220

222221
@code_instead QueryOperators.orderby_descending SourceCode Any Expr
223222

224223
@code_instead rand BySQL Type{Int}
225224

226-
# TODO: add more methods
227225
@code_instead randstring BySQL Integer
226+
@code_instead randstring SourceCode
228227

229-
# TODO: add more methods
230228
@code_instead replace SourceCode Pair
231229

232230
@code_instead repr SourceCode
@@ -235,6 +233,9 @@ end
235233

236234
@code_instead secondary SourceCode
237235

236+
# TODO: add more methods
237+
@code_instead string SourceCode Vararg{Any}
238+
238239
@code_instead strip SourceCode
239240
@code_instead strip SourceCode Char
240241

@@ -249,20 +250,12 @@ end
249250

250251
@code_instead QueryOperators.thenby_descending SourceCode Any Expr
251252

252-
# TODO: support dateformat
253-
@code_instead Time SourceCode
254-
255253
@code_instead type_of SourceCode
256254

257-
# TODO: add more methods
258255
@code_instead QueryOperators.unique SourceCode Any Expr
259256

260257
@code_instead uppercase SourceCode
261258

262259
# TODO: add
263260
# printf
264261
# zeroblob
265-
# total
266-
# julianday
267-
268-
# TODO: regex start and end

src/show.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ function show(io::IO, sql_expression::SQLExpression)
100100
infix(io, call, arguments...)
101101
elseif call === :%
102102
infix(io, call, arguments...)
103+
elseif call === :||
104+
infix(io, call, arguments...)
103105
elseif call === :AND
104106
infix(io, call, arguments...)
105107
elseif call === :AS

src/translate.jl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ function translate_call(::typeof(convert), ::Type{Int}, it; _primary = true)
8080
SQLExpression(:UNICODE, translate(it))
8181
end
8282

83-
@translate_default ::Type{Date} :DATE
84-
85-
@translate_default ::Type{DateTime} :DATETIME
86-
8783
@translate_default ::typeof(hex) :HEX
8884

8985
@translate_default ::typeof(QueryOperators.drop) :OFFSET
@@ -195,12 +191,11 @@ end
195191

196192
@translate_default ::typeof(min) :min
197193

198-
translate_call(::typeof(occursin), needle::Regex, haystack; _primary = true) =
194+
translate_call(::typeof(occursin), needle, haystack; _primary = true) =
199195
SQLExpression(
200196
:LIKE,
201197
translate(haystack; _primary = _primary),
202-
# * => %, . => _
203-
translate(replace(replace(needle.pattern, r"(?<!\\)\.\*" => "%"), r"(?<!\\)\." => "_"))
198+
translate(needle; _primary = _primary)
204199
)
205200

206201
function translate_call(::typeof(QueryOperators.orderby), unordered, key_function, key_function_expression; _primary = true)
@@ -268,8 +263,6 @@ function translate_call(::typeof(QueryOperators.thenby_descending), unordered, k
268263
)
269264
end
270265

271-
@translate_default ::Type{Time} :Time
272-
273266
@translate_default ::typeof(type_of) :TYPEOF
274267

275268
function translate_call(::typeof(QueryOperators.unique), repeated, key_function, key_function_expression; _primary = true)

test/runtests.jl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,17 @@ execute!(Stmt(connection, """
114114
ab Text,
115115
null_column Int,
116116
A_code Int,
117+
a Text,
117118
b Text,
118119
a_space Text,
119120
a_underscore Text,
121+
a_wild Text,
120122
one_point_one_one Real,
121-
date_text Text,
122123
datetime_text Text,
123-
time_text Text
124+
format Text
124125
)"""))
125126
execute!(Stmt(connection, """
126-
INSERT INTO test VALUES(0, 1, -1, "ab", NULL, 65, "b", " a ", "_a_", 1.11, "2019-12-08", "2019-12-08T11:09:00", "11:09:00")
127+
INSERT INTO test VALUES(0, 1, -1, "ab", NULL, 65, "a", "b", " a ", "_a_", "a%", 1.11, "2019-12-08T11:09:00", "%Y-%m-%d %H:%M:%S")
127128
"""))
128129
small = Database(connection)
129130

@@ -162,7 +163,9 @@ result =
162163
lowercase_test = lowercase(_.ab),
163164
max_test = max(_.one, 0),
164165
min_test = min(_.zero, 1),
165-
occursin_test = occursin(r"A.*", _.ab),
166+
occursin_test = occursin("a%", _.ab),
167+
occursin_test_2 = occursin(_.a_wild, "ab"),
168+
occursin_test_3 = occursin(_.a_wild, _.ab),
166169
uppercase_test = uppercase(_.ab),
167170
char_test = char(_.A_code),
168171
instr_test_1 = instr(_.ab, "b"),
@@ -180,12 +183,13 @@ result =
180183
random_test = rand(BySQL(_), Int),
181184
# TODO: fix
182185
# randstring_test = randstring(BySQL(_), 4),
183-
date_test = Date(_.date_text),
184-
datetime_test = DateTime(_.datetime_text),
185-
time_test = Time(_.time_text),
186+
# randstring_test2 = randstring(_.one),
186187
format_test = format(_.datetime_text, "%Y-%m-%d %H:%M:%S"),
188+
format_test_2 = format("2019-12-08T11:09:00", _.format),
189+
format_test_3 = format(_.datetime_text, _.format),
187190
type_of_test = type_of(_.zero),
188-
convert_test = convert(Int, _.b)
191+
convert_test = convert(Int, _.b),
192+
string_test = string(_.a, _.b)
189193
}) |>
190194
collect |>
191195
first
@@ -214,6 +218,8 @@ result =
214218
@test result.max_test == 1
215219
@test result.min_test == 0
216220
@test result.occursin_test == 1
221+
@test result.occursin_test_2 == 1
222+
@test result.occursin_test_3 == 1
217223
@test result.uppercase_test == "AB"
218224
@test result.lowercase_test == "ab"
219225
@test result.char_test == "A"
@@ -231,11 +237,12 @@ result =
231237
@test result.SubString_test_2 == "b"
232238
@test result.random_test isa DataValue{Int}
233239
@test_broken length(result.randomstring_test) == 4
234-
@test result.date_test == "2019-12-08"
235-
@test result.datetime_test == "2019-12-08 11:09:00"
240+
@test_broken length(result.randomstring_test2) == 1
236241
@test result.format_test == "2019-12-08 11:09:00"
237-
@test result.time_test == "11:09:00"
242+
@test result.format_test_2 == "2019-12-08 11:09:00"
243+
@test result.format_test_3 == "2019-12-08 11:09:00"
238244
@test result.type_of_test == "integer"
245+
@test result.string_test == "ab"
239246

240247
drop!(connection, "test")
241248

test/test.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)