99
1010sqltype (:: Type{Union{T, Missing}} ) where {T} = sqltype (T)
1111sqltype (T) = get (SQLTYPES, T, " VARCHAR(255)" )
12+ sqltype (T, coltypes, name) = get (coltypes, name, sqltype (T))
1213
1314const SQLTYPES = Dict {Type, String} (
1415 Int8 => " TINYINT" ,
@@ -34,27 +35,30 @@ const SQLTYPES = Dict{Type, String}(
3435
3536checkdupnames (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 (), auto_increment_primary_key_name:: AbstractString = " " )
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
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))
42- auto_increment_column = isempty (auto_increment_primary_key_name) ? " " : " $(auto_increment_primary_key_name) INT AUTO_INCREMENT PRIMARY KEY, "
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, "
4344 debug && @info " executing create table statement: `$createtableclause $nm ($(auto_increment_column)$(join (columns, " , " )) )`"
4445 return DBInterface. execute (conn, " $createtableclause $nm ($(auto_increment_column)$(join (columns, " , " )) )" )
4546end
4647
4748"""
48- MySQL.load(table, conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, columnsuffix=Dict(), debug=false)
49- table |> MySQL.load(conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, columnsuffix=Dict(), debug=false)
49+ MySQL.load(table, conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, coltypes=Dict(), columnsuffix=Dict(), debug=false)
50+ table |> MySQL.load(conn, name; append=true, quoteidentifiers=true, limit=typemax(Int64), createtableclause=nothing, coltypes=Dict(), columnsuffix=Dict(), debug=false)
5051
5152Attempts to take a Tables.jl source `table` and load into the database represented by `conn` with table name `name`.
5253
5354It first detects the `Tables.Schema` of the table source and generates a `CREATE TABLE` statement
54- with the appropriate column names and types. If no table name is provided, one will be autogenerated, like `odbcjl_xxxxx `.
55+ with the appropriate column names and types. If no table name is provided, one will be autogenerated, like `mysql_xxxxx `.
5556The `CREATE TABLE` clause can be provided manually by passing the `createtableclause` keyword argument, which
5657would allow specifying a temporary table or `if not exists`.
57- Column definitions can also be enhanced by providing arguments to `columnsuffix` as a `Dict` of
58+ Column types can be overridden by providing the `coltypes` keyword argument as a `Dict` of
59+ column name (given as a `Symbol`) to a string of the SQL type. This allows, for example, using
60+ a `LONGBLOB` instead of `BLOB` for large binary data by doing `coltypes=Dict(:Photo => "LONGBLOB")`.
61+ Column definitions can also be enhanced by providing arguments to `columnsuffix` as a `Dict` of
5862column name (given as a `Symbol`) to a string of the enhancement that will come after name and type like
5963`[column name] [column type] enhancements`. This allows, for example, specifying the charset of a string column
6064by doing something like `columnsuffix=Dict(:Name => "CHARACTER SET utf8mb4")`.
0 commit comments