From 5afa048d9696d062170fa00981426af239ffbebb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 13 Sep 2020 03:18:42 +0000 Subject: [PATCH] Format files using DocumentFormat --- coverage/submit.jl | 4 +- docs/make.jl | 4 +- src/functions.jl | 2 +- src/iterate.jl | 29 ++++---- src/model_row.jl | 6 +- src/translate.jl | 116 ++++++++++++++++---------------- src/utilities.jl | 8 +-- test/runtests.jl | 162 ++++++++++++++++++++++----------------------- 8 files changed, 164 insertions(+), 167 deletions(-) diff --git a/coverage/submit.jl b/coverage/submit.jl index 3a8e529..d69293c 100644 --- a/coverage/submit.jl +++ b/coverage/submit.jl @@ -9,8 +9,8 @@ submit( filter( let prefix = joinpath(pwd(), "src", "") - coverage -> startswith(coverage.filename, prefix) - end, + coverage -> startswith(coverage.filename, prefix) + end, readfolder(".") ) ) diff --git a/docs/make.jl b/docs/make.jl index f1fb719..78ae082 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,5 +1,5 @@ using Documenter: deploydocs, makedocs using QuerySQLite -makedocs(sitename = "QuerySQLite.jl", modules = [QuerySQLite], doctest = false) -deploydocs(repo = "github.com/queryverse/QuerySQLite.jl.git") +makedocs(sitename="QuerySQLite.jl", modules=[QuerySQLite], doctest=false) +deploydocs(repo="github.com/queryverse/QuerySQLite.jl.git") diff --git a/src/functions.jl b/src/functions.jl index 86dfc88..dd25e1a 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -80,7 +80,7 @@ julia> hex("hello") ``` """ function hex(it::Number) - uppercase(string(it, base = 16)) + uppercase(string(it, base=16)) end function hex(it::AbstractString) join(hex(byte) for byte in codeunits(it)) diff --git a/src/iterate.jl b/src/iterate.jl index d0cacdf..8e61b6f 100644 --- a/src/iterate.jl +++ b/src/iterate.jl @@ -1,9 +1,9 @@ -function generate_namedtuple(::Type{NamedTuple{names, types}}, q) where {names, types} +function generate_namedtuple(::Type{NamedTuple{names,types}}, q) where {names,types} if @generated vals = Tuple(:(getvalue(q, $i, $(fieldtype(types, i)))) for i = 1:fieldcount(types)) - return :(NamedTuple{names, types}(($(vals...),))) + return :(NamedTuple{names,types}(($(vals...),))) else - return NamedTuple{names, types}(Tuple(getvalue(q, i, fieldtype(types, i)) for i = 1:fieldcount(types))) + return NamedTuple{names,types}(Tuple(getvalue(q, i, fieldtype(types, i)) for i = 1:fieldcount(types))) end end @@ -44,14 +44,14 @@ function getvalue(cursor::SQLiteCursor, column_number::Int, ::Type{Value}) where julia_type = juliatype(column_type) # native SQLite Int, Float, and Text types sqlitevalue( if julia_type === Any - if !isbitstype(Value) - Value - else - julia_type - end + if !isbitstype(Value) + Value else julia_type - end, handle, column_number) + end + else + julia_type + end, handle, column_number) end end @@ -94,7 +94,7 @@ function second((value_1, value_2)) value_2 end -function name_and_type(handle, column_number, nullable = true, strict_types = true) +function name_and_type(handle, column_number, nullable=true, strict_types=true) Symbol(unsafe_string(sqlite3_column_name(handle, column_number))), if strict_types julia_type = juliatype(handle, column_number) @@ -118,14 +118,11 @@ function getiterator(source_code::SourceCode) handle = statement.handle schema = ntuple( let handle = handle - column_number -> name_and_type(handle, column_number) - end, + column_number -> name_and_type(handle, column_number) + end, sqlite3_column_count(handle) ) - SQLiteCursor{NamedTuple{ - Tuple(map(first, schema)), - Tuple{map(second, schema)...} - }}(statement, Ref(status), Ref(0)) + SQLiteCursor{NamedTuple{Tuple(map(first, schema)),Tuple{map(second, schema)...}}}(statement, Ref(status), Ref(0)) end # Use default show methods from the queryverse diff --git a/src/model_row.jl b/src/model_row.jl index ce6fb7b..34abf62 100644 --- a/src/model_row.jl +++ b/src/model_row.jl @@ -28,8 +28,8 @@ function model_row_call(::typeof(getproperty), source_tables::Database, table_na source_row = SourceRow(source, table_name) NamedTuple{column_names}( map(column_names) do column_name - get_column(source_row, column_name) - end + get_column(source_row, column_name) + end ) end @@ -39,7 +39,7 @@ function model_row_call(::typeof(QueryOperators.map), iterator, call, call_expre end # Grouped rows have their own dedicated model type -struct GroupRow{Group, Row} +struct GroupRow{Group,Row} group::Group row::Row end diff --git a/src/translate.jl b/src/translate.jl index 587bb06..42504f3 100644 --- a/src/translate.jl +++ b/src/translate.jl @@ -10,30 +10,30 @@ function nest(sql_expression) SQLExpression(:AS, SQLExpression(:FROM, sql_expression), :__TABLE__) end -function translate(something::Union{Char, AbstractString}; _primary = true) +function translate(something::Union{Char,AbstractString}; _primary=true) repr(something) end -function translate(something; _primary = true) +function translate(something; _primary=true) something end -function translate(source_row::SourceRow; _primary = true) +function translate(source_row::SourceRow; _primary=true) source_row.table_name end -function translate(call::Expr; _primary = true) +function translate(call::Expr; _primary=true) arguments, keywords = split_call(call) - translate_call(arguments...; _primary = _primary, keywords...) + translate_call(arguments...; _primary=_primary, keywords...) end # A 1-1 mapping between Julia functions and SQL functions function translate_default(location, function_type, SQL_call) result = :( - function translate_call($function_type, arguments...; _primary = true) - $SQLExpression($SQL_call, $map( - argument -> $translate(argument; _primary = _primary), + function translate_call($function_type, arguments...; _primary=true) + $SQLExpression($SQL_call, $map( + argument -> $translate(argument; _primary=_primary), arguments )...) - end + end ) result.args[2].args[1] = location result @@ -65,9 +65,9 @@ end @translate_default ::typeof(abs) :ABS -function as(pair; _primary = true) +function as(pair; _primary=true) SQLExpression(:AS, - translate(pair.second.code; _primary = _primary), + translate(pair.second.code; _primary=_primary), pair.first ) end @@ -76,7 +76,7 @@ end @translate_default ::typeof(char) :CHAR -function translate_call(::typeof(convert), ::Type{Int}, it; _primary = true) +function translate_call(::typeof(convert), ::Type{Int}, it; _primary=true) SQLExpression(:UNICODE, translate(it)) end @@ -84,31 +84,31 @@ end @translate_default ::typeof(QueryOperators.drop) :OFFSET -function translate_call(::typeof(QueryOperators.filter), iterator, call, call_expression; _primary = true) +function translate_call(::typeof(QueryOperators.filter), iterator, call, call_expression; _primary=true) SQLExpression(:WHERE, - translate(iterator; _primary = _primary), - translate(call(model_row(iterator)).code; _primary = _primary) + translate(iterator; _primary=_primary), + translate(call(model_row(iterator)).code; _primary=_primary) ) end -function translate_call(::typeof(format), time_type, format_string; _primary = true) +function translate_call(::typeof(format), time_type, format_string; _primary=true) SQLExpression( :STRFTIME, - translate(format_string; _primary = _primary), - translate(time_type; _primary = _primary) + translate(format_string; _primary=_primary), + translate(time_type; _primary=_primary) ) end -function translate_call(::typeof(getproperty), source_tables::Database, table_name; _primary = true) - translated = translate(table_name; _primary = _primary) +function translate_call(::typeof(getproperty), source_tables::Database, table_name; _primary=true) + translated = translate(table_name; _primary=_primary) if _primary SQLExpression(:FROM, translated) else translated end end -function translate_call(::typeof(getproperty), source_row::SourceRow, column_name; _primary = true) - translated = translate(column_name; _primary = _primary) +function translate_call(::typeof(getproperty), source_row::SourceRow, column_name; _primary=true) + translated = translate(column_name; _primary=_primary) if _primary translated else @@ -116,16 +116,16 @@ function translate_call(::typeof(getproperty), source_row::SourceRow, column_nam end end -function translate_call(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; _primary = true) +function translate_call(::typeof(QueryOperators.groupby), ungrouped, group_function, group_function_expression, map_selector, map_function_expression; _primary=true) model = model_row(ungrouped) SQLExpression(Symbol("GROUP BY"), nest(translate_call( QueryOperators.map, ungrouped, map_selector, map_function_expression, - _primary = _primary + _primary=_primary )), - translate(group_function(model).code; _primary = _primary) + translate(group_function(model).code; _primary=_primary) ) end @@ -143,7 +143,7 @@ end @translate_default ::typeof(join) :GROUP_CONCAT -function translate_call(::typeof(QueryOperators.join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; _primary = true) +function translate_call(::typeof(QueryOperators.join), source1, source2, key1, key1_expression, key2, key2_expression, combine, combine_expression; _primary=true) model_row_1 = model_row(source1) model_row_2 = model_row(source2) SQLExpression(:SELECT, @@ -151,17 +151,17 @@ function translate_call(::typeof(QueryOperators.join), source1, source2, key1, k SQLExpression(Symbol("INNER JOIN"), translate(source1), # mark as not _primary to suppress FROM - translate(source2; _primary = false) + translate(source2; _primary=false) ), # mark both as not _primary to always be explicit about table SQLExpression(:(=), - translate(key1(model_row_1).code; _primary = false), - translate(key2(model_row_2).code; _primary = false) + translate(key1(model_row_1).code; _primary=false), + translate(key2(model_row_2).code; _primary=false) ) ), # mark both as not _primary to always be explicit about table Generator( - pair -> as(pair; _primary = false), + pair -> as(pair; _primary=false), pairs(combine(model_row_1, model_row_2)) )... ) @@ -171,15 +171,15 @@ end @translate_default ::typeof(lowercase) :LOWER -function translate_call(::typeof(QueryOperators.map), select_table, call, call_expression; _primary = true) - inner = translate(select_table; _primary = _primary) +function translate_call(::typeof(QueryOperators.map), select_table, call, call_expression; _primary=true) + inner = translate(select_table; _primary=_primary) if inner.call == :SELECT inner = nest(inner) end SQLExpression( :SELECT, inner, Generator( - pair -> as(pair; _primary = _primary), + pair -> as(pair; _primary=_primary), pairs(call(model_row(select_table))) )... ) @@ -193,49 +193,49 @@ end @translate_default ::typeof(min) :min @translate_default ::typeof(minimum) :min -translate_call(::typeof(occursin), needle, haystack; _primary = true) = +translate_call(::typeof(occursin), needle, haystack; _primary=true) = SQLExpression( :LIKE, - translate(haystack; _primary = _primary), - translate(needle; _primary = _primary) + translate(haystack; _primary=_primary), + translate(needle; _primary=_primary) ) -function translate_call(::typeof(QueryOperators.orderby), unordered, key_function, key_function_expression; _primary = true) +function translate_call(::typeof(QueryOperators.orderby), unordered, key_function, key_function_expression; _primary=true) SQLExpression(Symbol("ORDER BY"), - translate(unordered; _primary = _primary), - translate(key_function(model_row(unordered)).code; _primary = _primary) + translate(unordered; _primary=_primary), + translate(key_function(model_row(unordered)).code; _primary=_primary) ) end -function translate_call(::typeof(QueryOperators.orderby_descending), unordered, key_function, key_function_expression; _primary = true) +function translate_call(::typeof(QueryOperators.orderby_descending), unordered, key_function, key_function_expression; _primary=true) SQLExpression(Symbol("ORDER BY"), - translate(unordered; _primary = _primary), + translate(unordered; _primary=_primary), SQLExpression(:DESC, - translate(key_function(model_row(unordered)).code; _primary = _primary) + translate(key_function(model_row(unordered)).code; _primary=_primary) ) ) end @translate_default ::typeof(repr) :QUOTE -function translate_call(::typeof(rand), ::Type{Int}; _primary = true) +function translate_call(::typeof(rand), ::Type{Int}; _primary=true) SQLExpression(:RANDOM) end @translate_default ::typeof(randstring) :RANDOMBLOB -function translate_call(::typeof(replace), it, pair; _primary = true) +function translate_call(::typeof(replace), it, pair; _primary=true) SQLExpression(:REPLACE, - translate(it; _primary = _primary), - translate(pair.first; _primary = _primary), - translate(pair.second; _primary = _primary) + translate(it; _primary=_primary), + translate(pair.first; _primary=_primary), + translate(pair.second; _primary=_primary) ) end -function translate_call(::typeof(round), it; _primary = true, digits = 0) +function translate_call(::typeof(round), it; _primary=true, digits=0) SQLExpression(:ROUND, - translate(it; _primary = _primary), - translate(digits; _primary = _primary) + translate(it; _primary=_primary), + translate(digits; _primary=_primary) ) end @@ -249,26 +249,26 @@ end @translate_default ::typeof(QueryOperators.take) :LIMIT -function translate_call(::typeof(QueryOperators.thenby), unordered, key_function, key_function_expression; _primary = true) - original = translate(unordered; _primary = _primary) +function translate_call(::typeof(QueryOperators.thenby), unordered, key_function, key_function_expression; _primary=true) + original = translate(unordered; _primary=_primary) SQLExpression(original.call, original.arguments..., - translate(key_function(model_row(unordered)).code; _primary = _primary) + translate(key_function(model_row(unordered)).code; _primary=_primary) ) end -function translate_call(::typeof(QueryOperators.thenby_descending), unordered, key_function, key_function_expression; _primary = true) - original = translate(unordered; _primary = _primary) +function translate_call(::typeof(QueryOperators.thenby_descending), unordered, key_function, key_function_expression; _primary=true) + original = translate(unordered; _primary=_primary) SQLExpression(original.call, original.arguments..., SQLExpression(:DESC, - translate(key_function(model_row(unordered)).code; _primary = _primary) + translate(key_function(model_row(unordered)).code; _primary=_primary) ) ) end @translate_default ::typeof(type_of) :TYPEOF -function translate_call(::typeof(QueryOperators.unique), repeated, key_function, key_function_expression; _primary = true) - result = translate(repeated; _primary = _primary) +function translate_call(::typeof(QueryOperators.unique), repeated, key_function, key_function_expression; _primary=true) + result = translate(repeated; _primary=_primary) SQLExpression(Symbol(string(result.call, " DISTINCT")), result.arguments...) end diff --git a/src/utilities.jl b/src/utilities.jl index 389a92a..805aeef 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -4,10 +4,10 @@ end split_keyword(keyword::Expr) = if keyword.head === :kw - Pair(keyword.args[1], keyword.args[2]) - else - error("Cannot split keyword $keyword") - end + Pair(keyword.args[1], keyword.args[2]) +else + error("Cannot split keyword $keyword") +end # Split a function call into its pieces # Normalize non-function-like patterns into function calls diff --git a/test/runtests.jl b/test/runtests.jl index f8cf3b0..be0bf33 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,45 +13,45 @@ doctest(QuerySQLite) @testset "SQLite tutorial" begin -filename = joinpath(@__DIR__, "Chinook_Sqlite.sqlite") -database = Database(filename) -database2 = Database(DB(filename)) + filename = joinpath(@__DIR__, "Chinook_Sqlite.sqlite") + database = Database(filename) + database2 = Database(DB(filename)) -@test (database.Track |> + @test (database.Track |> @filter(_.AlbumId == 1) |> collect |> first).Name == "For Those About To Rock (We Salute You)" -@test database.Track |> + @test database.Track |> @map({_.TrackId, _.Name, _.Composer, _.UnitPrice}) |> collect |> first |> propertynames == (:TrackId, :Name, :Composer, :UnitPrice) -@test (database2.Customer |> + @test (database2.Customer |> @map({_.City, _.Country}) |> @orderby(_.Country) |> DataTable).Country[1] == "Argentina" -@test database.Customer |> + @test database.Customer |> @map({_.City}) |> @unique() |> collect |> length == 53 -@test database.Track |> + @test database.Track |> @map({_.TrackId, _.Name}) |> @take(10) |> collect |> length == 10 -@test first((database.Track |> + @test first((database.Track |> @map({_.TrackId, _.Name}) |> @take(10) |> @drop(10) |> DataTable).Name) == "C.O.D." -@test first(( + @test first(( @from i in database.Track begin @orderby descending(i.Bytes), i.Name @select {i.TrackId, i.Name, i.Bytes} @@ -59,7 +59,7 @@ database2 = Database(DB(filename)) end ).Bytes) == 1059546140 -@test first(( + @test first(( @from i in database.Track begin @orderby i.Bytes, descending(i.Name) @select {i.TrackId, i.Name, i.Bytes} @@ -67,18 +67,18 @@ database2 = Database(DB(filename)) end ).Bytes) == 38747 -@test database.Artist |> + @test database.Artist |> @join(database.Album, _.ArtistId, _.ArtistId, {_.ArtistId, __.AlbumId}) |> DataTable |> length == 347 -@test (database.Track |> + @test (database.Track |> @map({_.Name, _.Milliseconds, _.Bytes, _.AlbumId}) |> @filter(_.AlbumId == 1) |> collect |> first).Name == "For Those About To Rock (We Salute You)" -group_by_row = + group_by_row = database.Track |> @groupby(_.AlbumId) |> @map({ @@ -92,13 +92,13 @@ group_by_row = collect |> first -@test group_by_row.AlbumId == 1 -@test group_by_row.length == 10 -@test group_by_row.sum == 2400415 -@test group_by_row.min == 199836 -@test group_by_row.mean == 240041.5 + @test group_by_row.AlbumId == 1 + @test group_by_row.length == 10 + @test group_by_row.sum == 2400415 + @test group_by_row.min == 199836 + @test group_by_row.mean == 240041.5 -@test collect( + @test collect( database.Track |> @select(:AlbumId, :Composer) |> @mutate(bar = _.AlbumId * 2) @@ -108,13 +108,13 @@ end @testset "Systematic tests" begin -filename = joinpath(@__DIR__, "tmp", "test.sqlite") -isfile(filename) && rm(filename) -cp(joinpath(@__DIR__, "test.sqlite"), filename) + filename = joinpath(@__DIR__, "tmp", "test.sqlite") + isfile(filename) && rm(filename) + cp(joinpath(@__DIR__, "test.sqlite"), filename) -connection = DB(filename) -execute(Stmt(connection, """DROP TABLE IF EXISTS test""")) -execute(Stmt(connection, """ + connection = DB(filename) + execute(Stmt(connection, """DROP TABLE IF EXISTS test""")) + execute(Stmt(connection, """ CREATE TABLE test ( zero Int, one Int, @@ -131,12 +131,12 @@ execute(Stmt(connection, """ datetime_text Text, format Text )""")) -execute(Stmt(connection, """ + execute(Stmt(connection, """ 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") """)) -small = Database(connection) + small = Database(connection) -@test (small.test |> + @test (small.test |> @groupby(_.zero) |> @map({ join_test = join(_.ab) @@ -144,7 +144,7 @@ small = Database(connection) collect |> first).join_test == "ab" -result = + result = small.test |> @map({ equals_test = _.zero == _.one, @@ -202,58 +202,58 @@ result = collect |> first -@test result.equals_test == 0 -@test result.not_equals_test == 1 -@test result.not_test == 1 -@test result.and_test == 0 -@test result.or_test == 1 -@test result.times_test == 0 -@test result.divide_test == 0 -@test result.plus_test == 1 -@test result.minus_test == 1 -@test result.mod_test == 0 -@test result.abs_test == 1 -@test result.in_test == 1 -@test result.coalesce_test == 0 -@test result.if_else_test_1 == 1 -@test result.if_else_test_2 == 1 -@test result.if_else_test_3 == 0 -@test result.if_else_test_4 == 0 -@test result.if_else_test_5 == 0 -@test result.if_else_test_6 == 1 -@test result.if_else_test_7 == 1 -@test result.ismissing_test == 1 -@test result.max_test == 1 -@test result.min_test == 0 -@test result.occursin_test == 1 -@test result.occursin_test_2 == 1 -@test result.occursin_test_3 == 1 -@test result.uppercase_test == "AB" -@test result.lowercase_test == "ab" -@test result.char_test == "A" -@test result.instr_test_1 == 2 -@test result.instr_test_2 == 2 -@test result.instr_test_3 == 2 -@test result.hex_test == "6162" -@test result.strip_test == "a" -@test result.strip_test_2 == "a" -@test result.repr_test == "\'ab\'" -@test result.replace_test == "aa" -@test result.round_test_1 == 1.1 -@test result.round_test_2 == 1.0 -@test result.SubString_test_1 == "b" -@test result.SubString_test_2 == "b" -@test result.random_test isa DataValue{Int} -@test_broken length(result.randomstring_test) == 4 -@test_broken length(result.randomstring_test2) == 1 -@test result.format_test == "2019-12-08 11:09:00" -@test result.format_test_2 == "2019-12-08 11:09:00" -@test result.format_test_3 == "2019-12-08 11:09:00" -@test result.type_of_test == "integer" -@test result.string_test == "ab" + @test result.equals_test == 0 + @test result.not_equals_test == 1 + @test result.not_test == 1 + @test result.and_test == 0 + @test result.or_test == 1 + @test result.times_test == 0 + @test result.divide_test == 0 + @test result.plus_test == 1 + @test result.minus_test == 1 + @test result.mod_test == 0 + @test result.abs_test == 1 + @test result.in_test == 1 + @test result.coalesce_test == 0 + @test result.if_else_test_1 == 1 + @test result.if_else_test_2 == 1 + @test result.if_else_test_3 == 0 + @test result.if_else_test_4 == 0 + @test result.if_else_test_5 == 0 + @test result.if_else_test_6 == 1 + @test result.if_else_test_7 == 1 + @test result.ismissing_test == 1 + @test result.max_test == 1 + @test result.min_test == 0 + @test result.occursin_test == 1 + @test result.occursin_test_2 == 1 + @test result.occursin_test_3 == 1 + @test result.uppercase_test == "AB" + @test result.lowercase_test == "ab" + @test result.char_test == "A" + @test result.instr_test_1 == 2 + @test result.instr_test_2 == 2 + @test result.instr_test_3 == 2 + @test result.hex_test == "6162" + @test result.strip_test == "a" + @test result.strip_test_2 == "a" + @test result.repr_test == "\'ab\'" + @test result.replace_test == "aa" + @test result.round_test_1 == 1.1 + @test result.round_test_2 == 1.0 + @test result.SubString_test_1 == "b" + @test result.SubString_test_2 == "b" + @test result.random_test isa DataValue{Int} + @test_broken length(result.randomstring_test) == 4 + @test_broken length(result.randomstring_test2) == 1 + @test result.format_test == "2019-12-08 11:09:00" + @test result.format_test_2 == "2019-12-08 11:09:00" + @test result.format_test_3 == "2019-12-08 11:09:00" + @test result.type_of_test == "integer" + @test result.string_test == "ab" -drop!(connection, "test") + drop!(connection, "test") -@test_throws ArgumentError Database("file.not_sqlite") + @test_throws ArgumentError Database("file.not_sqlite") end