Skip to content

Commit e22212e

Browse files
TianChengJiang9911quinnjclaude
authored
load tables with auto increment key (#230)
* load tables with auto increment key * Fix syntax error: add missing ) in function signature Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 49a062f commit e22212e

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/load.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ const SQLTYPES = Dict{Type, String}(
3535

3636
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")
3737

38-
function createtable(conn::Connection, nm::AbstractString, sch::Tables.Schema; debug::Bool=false, quoteidentifiers::Bool=true, createtableclause::AbstractString="CREATE TABLE", coltypes=Dict(), columnsuffix=Dict())
38+
function createtable(conn::Connection, nm::AbstractString, sch::Tables.Schema; debug::Bool=false, quoteidentifiers::Bool=true, createtableclause::AbstractString="CREATE TABLE", coltypes=Dict(), columnsuffix=Dict(), auto_increment_primary_key_name::Union{Nothing,AbstractString}=nothing)
3939
names = sch.names
4040
checkdupnames(names)
4141
types = [sqltype(T, coltypes, names[i]) for (i, T) in enumerate(sch.types)]
4242
columns = (string(quoteidentifiers ? quoteid(String(names[i])) : names[i], ' ', types[i], ' ', get(columnsuffix, names[i], "")) for i = 1:length(names))
43-
debug && @info "executing create table statement: `$createtableclause $nm ($(join(columns, ", ")))`"
44-
return DBInterface.execute(conn, "$createtableclause $nm ($(join(columns, ", ")))")
43+
auto_increment_column = (auto_increment_primary_key_name === nothing || isempty(auto_increment_primary_key_name)) ? "" : "$(auto_increment_primary_key_name) INT AUTO_INCREMENT PRIMARY KEY, "
44+
debug && @info "executing create table statement: `$createtableclause $nm ($(auto_increment_column)$(join(columns, ", ")))`"
45+
return DBInterface.execute(conn, "$createtableclause $nm ($(auto_increment_column)$(join(columns, ", ")))")
4546
end
4647

4748
"""
@@ -97,7 +98,7 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
9798
# start a transaction for inserting rows
9899
DBInterface.transaction(conn) do
99100
params = chop(repeat("?,", length(sch.names)))
100-
stmt = DBInterface.prepare(conn, "INSERT INTO $name VALUES ($params)")
101+
stmt = DBInterface.prepare(conn, "INSERT INTO $name ($(join(sch.names .|> string .|> quoteid,", "))) VALUES ($params)")
101102
for (i, row) in enumerate(rows)
102103
i > limit && break
103104
debug && @info "inserting row $i; $(Tables.Row(row))"

0 commit comments

Comments
 (0)