@@ -175,14 +175,15 @@ tableinfo(db::DB, name::AbstractString) =
175175 end
176176
177177"""
178- source |> SQLite.load!(db::SQLite.DB, tablename::String; temp::Bool=false, ifnotexists::Bool=false, analyze::Bool=false)
179- SQLite.load!(source, db, tablename; temp=false, ifnotexists=false, analyze::Bool=false)
178+ source |> SQLite.load!(db::SQLite.DB, tablename::String; temp::Bool=false, ifnotexists::Bool=false, replace::Bool=false, analyze::Bool=false)
179+ SQLite.load!(source, db, tablename; temp=false, ifnotexists=false, replace::Bool=false, analyze::Bool=false)
180180
181181Load a Tables.jl input `source` into an SQLite table that will be named `tablename` (will be auto-generated if not specified).
182182
183- `temp=true` will create a temporary SQLite table that will be destroyed automatically when the database is closed
184- `ifnotexists=false` will throw an error if `tablename` already exists in `db`
185- `analyze=true` will execute `ANALYZE` at the end of the insert
183+ * `temp=true` will create a temporary SQLite table that will be destroyed automatically when the database is closed
184+ * `ifnotexists=false` will throw an error if `tablename` already exists in `db`
185+ * `replace=false` controls whether an `INSERT INTO ...` statement is generated or a `REPLACE INTO ...`
186+ * `analyze=true` will execute `ANALYZE` at the end of the insert
186187"""
187188function load! end
188189
@@ -222,7 +223,7 @@ function checknames(::Tables.Schema{names}, db_names::AbstractVector{String}) wh
222223end
223224
224225function load! (sch:: Tables.Schema , rows, db:: DB , name:: AbstractString , db_tableinfo:: Union{NamedTuple, Nothing} , row= nothing , st= nothing ;
225- temp:: Bool = false , ifnotexists:: Bool = false , analyze:: Bool = false )
226+ temp:: Bool = false , ifnotexists:: Bool = false , replace :: Bool = false , analyze:: Bool = false )
226227 # check for case-insensitive duplicate column names (sqlite doesn't allow)
227228 checkdupnames (sch. names)
228229 # check if `rows` column names match the existing table, or create the new one
@@ -234,7 +235,8 @@ function load!(sch::Tables.Schema, rows, db::DB, name::AbstractString, db_tablei
234235 # build insert statement
235236 columns = join (esc_id .(string .(sch. names)), " ," )
236237 params = chop (repeat (" ?," , length (sch. names)))
237- stmt = _Stmt (db, " INSERT INTO $(esc_id (string (name))) ($columns ) VALUES ($params )" )
238+ kind = replace ? " REPLACE" : " INSERT"
239+ stmt = _Stmt (db, " $kind INTO $(esc_id (string (name))) ($columns ) VALUES ($params )" )
238240 # start a transaction for inserting rows
239241 transaction (db) do
240242 if row === nothing
@@ -246,8 +248,14 @@ function load!(sch::Tables.Schema, rows, db::DB, name::AbstractString, db_tablei
246248 Tables. eachcolumn (sch, row) do val, col, _
247249 bind! (stmt, col, val)
248250 end
249- sqlite3_step (stmt. handle)
250- sqlite3_reset (stmt. handle)
251+ r = sqlite3_step (stmt. handle)
252+ if r == SQLITE_DONE
253+ sqlite3_reset (stmt. handle)
254+ elseif r != SQLITE_ROW
255+ e = sqliteexception (db)
256+ sqlite3_reset (stmt. handle)
257+ throw (e)
258+ end
251259 state = iterate (rows, st)
252260 state === nothing && break
253261 row, st = state
0 commit comments