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,26 +35,29 @@ 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 ())
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, " , " )) )" )
4445end
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
5051Attempts to take a Tables.jl source `table` and load into the database represented by `conn` with table name `name`.
5152
5253It 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 `.
5455The `CREATE TABLE` clause can be provided manually by passing the `createtableclause` keyword argument, which
5556would 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
5761column 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
5963by doing something like `columnsuffix=Dict(:Name => "CHARACTER SET utf8mb4")`.
0 commit comments