Skip to content

Commit 164504d

Browse files
quinnjclaude
andcommitted
Add coltypes kwarg to MySQL.load for LONGBLOB support
Fixes #225. Users can now override the SQL column type when creating tables via MySQL.load() using the new `coltypes` keyword argument: MySQL.load(df, conn, "mytable"; coltypes=Dict(:Photo => "LONGBLOB")) This allows using LONGBLOB, MEDIUMBLOB, TEXT, or any other SQL type instead of the default type inference. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e795ea4 commit 164504d

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/load.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ end
99

1010
sqltype(::Type{Union{T, Missing}}) where {T} = sqltype(T)
1111
sqltype(T) = get(SQLTYPES, T, "VARCHAR(255)")
12+
sqltype(T, coltypes, name) = get(coltypes, name, sqltype(T))
1213

1314
const SQLTYPES = Dict{Type, String}(
1415
Int8 => "TINYINT",
@@ -34,26 +35,29 @@ const SQLTYPES = Dict{Type, String}(
3435

3536
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")
3637

37-
function createtable(conn::Connection, nm::AbstractString, sch::Tables.Schema; debug::Bool=false, quoteidentifiers::Bool=true, createtableclause::AbstractString="CREATE TABLE", 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())
3839
names = sch.names
3940
checkdupnames(names)
40-
types = [sqltype(T) for T in sch.types]
41+
types = [sqltype(T, coltypes, names[i]) for (i, T) in enumerate(sch.types)]
4142
columns = (string(quoteidentifiers ? quoteid(String(names[i])) : names[i], ' ', types[i], ' ', get(columnsuffix, names[i], "")) for i = 1:length(names))
4243
debug && @info "executing create table statement: `$createtableclause $nm ($(join(columns, ", ")))`"
4344
return DBInterface.execute(conn, "$createtableclause $nm ($(join(columns, ", ")))")
4445
end
4546

4647
"""
47-
MySQL.load(table, conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, columnsuffix=Dict(), debug=false)
48-
table |> MySQL.load(conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, columnsuffix=Dict(), debug=false)
48+
MySQL.load(table, conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, coltypes=Dict(), columnsuffix=Dict(), debug=false)
49+
table |> MySQL.load(conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, coltypes=Dict(), columnsuffix=Dict(), debug=false)
4950
5051
Attempts to take a Tables.jl source `table` and load into the database represented by `conn` with table name `name`.
5152
5253
It first detects the `Tables.Schema` of the table source and generates a `CREATE TABLE` statement
53-
with the appropriate column names and types. If no table name is provided, one will be autogenerated, like `odbcjl_xxxxx`.
54+
with the appropriate column names and types. If no table name is provided, one will be autogenerated, like `mysql_xxxxx`.
5455
The `CREATE TABLE` clause can be provided manually by passing the `createtableclause` keyword argument, which
5556
would allow specifying a temporary table or `if not exists`.
56-
Column definitions can also be enhanced by providing arguments to `columnsuffix` as a `Dict` of
57+
Column types can be overridden by providing the `coltypes` keyword argument as a `Dict` of
58+
column name (given as a `Symbol`) to a string of the SQL type. This allows, for example, using
59+
a `LONGBLOB` instead of `BLOB` for large binary data by doing `coltypes=Dict(:Photo => "LONGBLOB")`.
60+
Column definitions can also be enhanced by providing arguments to `columnsuffix` as a `Dict` of
5761
column name (given as a `Symbol`) to a string of the enhancement that will come after name and type like
5862
`[column name] [column type] enhancements`. This allows, for example, specifying the charset of a string column
5963
by doing something like `columnsuffix=Dict(:Name => "CHARACTER SET utf8mb4")`.

0 commit comments

Comments
 (0)