Skip to content

Commit 0146f7b

Browse files
load tables with auto increment key
1 parent acf7f83 commit 0146f7b

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
@@ -34,13 +34,14 @@ const SQLTYPES = Dict{Type, String}(
3434

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

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

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

0 commit comments

Comments
 (0)