Skip to content

Commit

Permalink
Support for Repo.delete! with multiple ids (fix #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
wookay committed Sep 7, 2023
1 parent fcdd9ea commit 51d5046
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
21 changes: 19 additions & 2 deletions src/Repo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,32 @@ function delete!(M::Type, nt::NamedTuple; db::Connection=current_connection())
end

"""
Repo.delete!(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection())
Repo.delete!(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection())
"""
function delete!(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function delete!(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
a = db.adapter
table = a.from(M)
key = _field_for_primary_key(db, M)
execute([a.DELETE a.FROM table a.WHERE key a.BETWEEN pk_range.start a.AND pk_range.stop]; db=db)
end

"""
Repo.delete!(M::Type, pk_tuple::(Tuple{Vararg{Int, N}} where N); db::Connection=current_connection())
"""
function delete!(M::Type, pk_tuple::(Tuple{Vararg{Int, N}} where N); db::Connection=current_connection()) # throw Schema.PrimaryKeyError
delete!(M, collect(pk_tuple); db)
end

"""
Repo.delete!(M::Type, pk_vector::Vector{Int}; db::Connection=current_connection())
"""
function delete!(M::Type, pk_vector::Vector{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
a = db.adapter
table = a.from(M)
key = _field_for_primary_key(db, M)
execute([a.DELETE a.FROM table a.WHERE key a.IN a.Enclosed(pk_vector)]; db=db)
end

function sql_startswith_insert_update_delete(sql::String)::Bool
lowercase(first(split(lstrip(sql), ' '))) in ("insert", "update", "delete")
end
Expand Down
12 changes: 12 additions & 0 deletions test/adapters/postgresql/repo_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ df = Repo.query([SELECT * FROM em WHERE em.Name == "Tim"])
@test size(df) == (1,)
@test df[1].name == "Tim"

result = Repo.delete!(Employee, [1, 3])
@test result.num_affected_rows == 2

result = Repo.delete!(Employee, (4, 5))
@test result.num_affected_rows == 2

result = Repo.delete!(Employee, 6:7)
@test result.num_affected_rows == 2

df = Repo.query(Employee)
@test size(df) == (0,)

# coverage up
Schema.tables[Base.typename(Employee)] = Dict(:table_name => "Employee")
@test_throws Schema.PrimaryKeyError Repo.get(Employee, 2)
Expand Down
37 changes: 24 additions & 13 deletions test/adapters/sqlite/repo_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,54 @@ df = Repo.query([SELECT * FROM em WHERE em.EmployeeId == 2])
# using Octo.Adapters.SQLite # CREATE TABLE IF NOT EXISTS
Repo.query([CREATE TABLE IF NOT EXISTS :temp AS SELECT * FROM :Album])

struct Temp
struct Album
end
Schema.model(Temp, table_name="temp", primary_key="AlbumId")
Schema.model(Album, table_name="temp", primary_key="AlbumId")

df = Repo.query(Temp)
df = Repo.query(Album)
@test size(df) == (347,)

changes = (AlbumId=0, Title="Test Album", ArtistId=0)
result = Repo.insert!(Temp, changes)
result = Repo.insert!(Album, changes)
@test result.num_affected_rows == 1

df = Repo.query(Temp)
df = Repo.query(Album)
@test size(df) == (348,)

df = Repo.get(Temp, 6)
df = Repo.get(Album, 6)
@test df[1].Title == "Jagged Little Pill"

df = Repo.get(Temp, (Title="Jagged Little Pill",))
df = Repo.get(Album, (Title="Jagged Little Pill",))
@test df[1].Title == "Jagged Little Pill"

changes = (AlbumId=6, Title="Texas")
result = Repo.update!(Temp, changes)
result = Repo.update!(Album, changes)
@test result.num_affected_rows == 1

df = Repo.get(Temp, 6)
df = Repo.get(Album, 6)
@test df[1].Title == "Texas"

result = Repo.delete!(Temp, changes)
result = Repo.delete!(Album, changes)
@test result.num_affected_rows == 1

df = Repo.get(Temp, 6)
@test size(df) == (0,)
result = Repo.delete!(Album, [1, 2, 3])
@test result.num_affected_rows == 3

result = Repo.delete!(Album, (4, 5))
@test result.num_affected_rows == 2

result = Repo.delete!(Album, 11:15)
@test result.num_affected_rows == 5

df = Repo.query(Album)
@test size(df) == (337,)

df = Repo.get(Album, 6)
@test size(df) == (0,)

= Octo.PlaceHolder

a = from(Temp)
a = from(Album)
df = Repo.query([SELECT * FROM a WHERE a.Title == ❔ AND a.ArtistId == ❔ ], ["The Doors", 140])
@test size(df) == (1,)

Expand Down

0 comments on commit 51d5046

Please sign in to comment.