Skip to content

Commit e9ee406

Browse files
authored
add strict option to create_table! and load!
1 parent ccdb8b0 commit e9ee406

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/tables.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function DBInterface.execute(
179179
end
180180

181181
"""
182-
SQLite.createtable!(db::SQLite.DB, table_name, schema::Tables.Schema; temp=false, ifnotexists=true)
182+
SQLite.createtable!(db::SQLite.DB, table_name, schema::Tables.Schema; temp=false, ifnotexists=true, strict=false)
183183
184184
Create a table in `db` with name `table_name`, according to `schema`, which is a set of column names and types, constructed like `Tables.Schema(names, types)`
185185
where `names` can be a vector or tuple of String/Symbol column names, and `types` is a vector or tuple of sqlite-compatible types (`Int`, `Float64`, `String`, or unions of `Missing`).
@@ -193,6 +193,7 @@ function createtable!(
193193
::Tables.Schema{names,types};
194194
temp::Bool = false,
195195
ifnotexists::Bool = true,
196+
strict::Bool = false
196197
) where {names,types}
197198
temp = temp ? "TEMP" : ""
198199
ifnotexists = ifnotexists ? "IF NOT EXISTS" : ""
@@ -203,7 +204,7 @@ function createtable!(
203204
sqlitetype(types !== nothing ? fieldtype(types, i) : Any),
204205
) for i in eachindex(names)
205206
]
206-
sql = "CREATE $temp TABLE $ifnotexists $(esc_id(string(name))) ($(join(columns, ',')))"
207+
sql = "CREATE $temp TABLE $ifnotexists $(esc_id(string(name))) ($(join(columns, ','))) $(strict ? "STRICT" : "")"
207208
return execute(db, sql)
208209
end
209210

@@ -303,6 +304,7 @@ function load!(
303304
st = nothing;
304305
temp::Bool = false,
305306
ifnotexists::Bool = false,
307+
strict::Bool = false,
306308
on_conflict::Union{String,Nothing} = nothing,
307309
replace::Bool = false,
308310
analyze::Bool = false,
@@ -313,7 +315,7 @@ function load!(
313315
if db_tableinfo !== nothing
314316
checknames(sch, db_tableinfo.name)
315317
else
316-
createtable!(db, name, sch; temp = temp, ifnotexists = ifnotexists)
318+
createtable!(db, name, sch; temp = temp, ifnotexists = ifnotexists, strict = strict)
317319
end
318320
# build insert statement
319321
columns = join(esc_id.(string.(sch.names)), ",")

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,18 @@ end
799799
@test_throws SQLiteException SQLite.load!(tbl3, db, "data")
800800
end
801801

802+
@testset "PR #XXX: strict (and only strict) tables should error if types don't match" begin
803+
db = SQLite.DB()
804+
805+
tbl1 = (a = [1, 2, 3], b = [4, 5, 6])
806+
SQLite.load!(tbl1, db, "data_default")
807+
SQLite.load!(tbl1, db, "data_strict", strict=true)
808+
809+
tbl2 = (a = ["a", "b", "c"], b=[7, 8, 9]
810+
SQLite.load!(tbl2, db, "data_default")
811+
@test_throws SQLiteException SQLite.load!(tbl2, db, "data_strict")
812+
end
813+
802814
@testset "Test busy_timeout" begin
803815
db = SQLite.DB()
804816
@test SQLite.busy_timeout(db, 300) == 0

0 commit comments

Comments
 (0)