Skip to content

Commit 06fe87a

Browse files
authored
add analyze kwarg to load! (#208)
* add analyze kwarg to load!
1 parent 6068678 commit 06fe87a

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ version = "1.0.2"
55

66
[deps]
77
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
8-
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
98
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
9+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1010
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1111
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
12-
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1312
SQLite_jll = "76ed43ae-9a5d-5a62-8c75-30186b810ce8"
13+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1414
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1515
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1616
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

src/tables.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ function createtable!(db::DB, nm::AbstractString, ::Tables.Schema{names, types};
120120
end
121121

122122
"""
123-
source |> SQLite.load!(db::SQLite.DB, tablename::String; temp::Bool=false, ifnotexists::Bool=false)
124-
SQLite.load!(source, db, tablename; temp=false, ifnotexists=false)
123+
source |> SQLite.load!(db::SQLite.DB, tablename::String; temp::Bool=false, ifnotexists::Bool=false, analyze::Bool=false)
124+
SQLite.load!(source, db, tablename; temp=false, ifnotexists=false, analyze::Bool=false)
125125
126126
Load a Tables.jl input `source` into an SQLite table that will be named `tablename` (will be auto-generated if not specified).
127127
128128
`temp=true` will create a temporary SQLite table that will be destroyed automatically when the database is closed
129129
`ifnotexists=false` will throw an error if `tablename` already exists in `db`
130+
`analyze=true` will execute `ANALYZE` at the end of the insert
130131
"""
131132
function load! end
132133

@@ -143,7 +144,7 @@ end
143144

144145
checkdupnames(names) = length(unique(map(x->lowercase(String(x)), names))) == length(names) || error("duplicate case-insensitive column names detected; sqlite doesn't allow duplicate column names and treats them case insensitive")
145146

146-
function load!(sch::Tables.Schema, rows, db::DB, nm::AbstractString, name, shouldcreate; temp::Bool=false, ifnotexists::Bool=false)
147+
function load!(sch::Tables.Schema, rows, db::DB, nm::AbstractString, name, shouldcreate; temp::Bool=false, ifnotexists::Bool=false, analyze::Bool=false)
147148
# check for case-insensitive duplicate column names (sqlite doesn't allow)
148149
checkdupnames(sch.names)
149150
# create table if needed
@@ -161,12 +162,12 @@ function load!(sch::Tables.Schema, rows, db::DB, nm::AbstractString, name, shoul
161162
sqlite3_reset(stmt.handle)
162163
end
163164
end
164-
execute(db, "ANALYZE $nm")
165+
analyze && execute(db, "ANALYZE $nm")
165166
return name
166167
end
167168

168169
# unknown schema case
169-
function load!(::Nothing, rows, db::DB, nm::AbstractString, name, shouldcreate; temp::Bool=false, ifnotexists::Bool=false)
170+
function load!(::Nothing, rows, db::DB, nm::AbstractString, name, shouldcreate; temp::Bool=false, ifnotexists::Bool=false, analyze::Bool=false)
170171
state = iterate(rows)
171172
state === nothing && return nm
172173
row, st = state
@@ -191,6 +192,7 @@ function load!(::Nothing, rows, db::DB, nm::AbstractString, name, shouldcreate;
191192
row, st = state
192193
end
193194
end
194-
execute(db, "ANALYZE $nm")
195+
analyze && execute(db, "ANALYZE $nm")
196+
195197
return name
196198
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ SQLite.drop!(db2, "nonexistant", ifexists=true)
177177
SQLite.drop!(db2, "tab2", ifexists=true)
178178
@test !in("tab2", SQLite.tables(db2)[1])
179179

180-
SQLite.drop!(db, "sqlite_stat1")
180+
SQLite.drop!(db, "sqlite_stat1", ifexists=true)
181181
tables = SQLite.tables(db)
182182
@test length(tables[1]) == 11
183183

0 commit comments

Comments
 (0)