Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,31 +492,10 @@ function load!(
"$kind INTO $(esc_id(string(name))) ($columns) VALUES ($params)";
register = false,
)
handle = _get_stmt_handle(stmt)
# start a transaction for inserting rows
try
DBInterface.transaction(db) do
if row === nothing
state = iterate(rows)
state === nothing && return
row, st = state
end
while true
Tables.eachcolumn(sch, row) do val, col, _
bind!(stmt, col, val)
end
r = Base.@lock db.lock GC.@preserve row C.sqlite3_step(handle)
if r == C.SQLITE_DONE
Base.@lock db.lock C.sqlite3_reset(handle)
elseif r != C.SQLITE_ROW
e = sqliteexception(db, stmt)
Base.@lock db.lock C.sqlite3_reset(handle)
throw(e)
end
state = iterate(rows, st)
state === nothing && break
row, st = state
end
insertrows!(stmt, sch, rows, row, st)
end
finally
_close_stmt!(stmt)
Expand All @@ -525,6 +504,34 @@ function load!(
return name
end

# Insert each row of `rows` with `stmt`, starting at `row` and iteration state `st` if given.
# The loop sits outside the transaction closure, because a closure that reassigns `row` and `st` boxes them.
function insertrows!(stmt::Stmt, sch::Tables.Schema, rows, row, st)
if row === nothing
state = iterate(rows)
state === nothing && return
row, st = state
end
db = stmt.db
handle = _get_stmt_handle(stmt)
while true
Tables.eachcolumn(sch, row) do val, col, _
bind!(stmt, col, val)
end
r = Base.@lock db.lock GC.@preserve row C.sqlite3_step(handle)
if r == C.SQLITE_DONE
Base.@lock db.lock C.sqlite3_reset(handle)
elseif r != C.SQLITE_ROW
e = sqliteexception(db, stmt)
Base.@lock db.lock C.sqlite3_reset(handle)
throw(e)
end
state = iterate(rows, st)
state === nothing && return
row, st = state
end
end

# unknown schema case
function load!(
::Nothing,
Expand Down
Loading