Skip to content

Commit

Permalink
Merge pull request #92 from JuliaDB/jq/updates
Browse files Browse the repository at this point in the history
Support DataFrames 0.11
  • Loading branch information
quinnj authored Dec 19, 2017
2 parents b9ef4a3 + eca7bd0 commit 5fa2ad2
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 247 deletions.
21 changes: 19 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
services:
- mysql
os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("MySQL"); Pkg.test("MySQL"; coverage=true)'
after_success:
# push coverage results to Coveralls
- julia -e 'cd(Pkg.dir("MySQL")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("MySQL")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
before_script:
- export OLD_PATH=$LD_LIBRARY_PATH
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`mariadb_config --libs | cut -d ' ' -f1 | sed 's/-L//'`
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`mysql_config --libs | cut -d ' ' -f1 | sed 's/-L//'`
after_script:
- export LD_LIBRARY_PATH=$OLD_PATH
- unset OLD_PATH
7 changes: 3 additions & 4 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
julia 0.5
DataFrames
Compat 0.17.0
ConfParser
julia 0.6
DataFrames 0.11
Compat 0.39.0
5 changes: 3 additions & 2 deletions src/MySQL.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
__precompile__()

__precompile__(true)
module MySQL
using Compat
using DataFrames
using Missings
using Compat.Dates

include("config.jl")
include("consts.jl")
Expand Down
16 changes: 11 additions & 5 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
#
# TODO: Need to update lib_choices for Mac OS X and Windows.

@static if !isdefined(Base, Symbol("@isdefined"))
macro isdefined(x)
esc(:(isdefined($x)))
end
end

let
global mysql_lib
succeeded = false
if !isdefined(:mysql_lib)
@static is_linux() ? (lib_choices = ["libmysql.so", "libmysqlclient.so",
if !@isdefined(:mysql_lib)
@static Compat.Sys.islinux() ? (lib_choices = ["libmysql.so", "libmysqlclient.so",
"libmysqlclient_r.so", "libmariadb.so",
"libmysqlclient_r.so.16"]) : nothing
@static is_apple() ? (lib_choices = ["libmysqlclient.dylib", "libperconaserverclient.dylib"]) : nothing
@static is_windows() ? (lib_choices = ["libmysql.dll", "libmariadb.dll"]) : nothing
@static Compat.Sys.isapple() ? (lib_choices = ["libmysqlclient.dylib", "libperconaserverclient.dylib"]) : nothing
@static Compat.Sys.iswindows() ? (lib_choices = ["libmysql.dll", "libmariadb.dll"]) : nothing
local lib
for lib in lib_choices
try
Expand All @@ -22,7 +28,7 @@ let
break
end
end
succeeded || error("MYSQL library not found")
succeeded || error("MySQL library not found")
@eval const mysql_lib = $lib
end
end
2 changes: 1 addition & 1 deletion src/consts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const MYSQL_NO_DATA = 100

const MYSQL_DEFAULT_PORT = 3306

if is_windows()
if Compat.Sys.iswindows()
MYSQL_DEFAULT_SOCKET = "MySQL"
else
MYSQL_DEFAULT_SOCKET = "/tmp/mysql.sock"
Expand Down
13 changes: 3 additions & 10 deletions src/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ import Base.==
const MYSQL_DATE_FORMAT = Dates.DateFormat("yyyy-mm-dd")
const MYSQL_DATETIME_FORMAT = Dates.DateFormat("yyyy-mm-dd HH:MM:SS")

function Base.convert(::Type{Date}, datestr::String)
Date(datestr, MYSQL_DATE_FORMAT)
end

function Base.convert(::Type{DateTime}, dtimestr::String)
if !contains(dtimestr, " ")
dtimestr = "1970-01-01 " * dtimestr
end
DateTime(dtimestr, MYSQL_DATETIME_FORMAT)
end
mysql_date(str) = Dates.Date(str, MYSQL_DATE_FORMAT)
mysql_datetime(str) = Dates.DateTime(contains(str, " ") ? str : "1970-01-01 " * str, MYSQL_DATETIME_FORMAT)
export mysql_date, mysql_datetime

function Base.convert(::Type{DateTime}, mtime::MYSQL_TIME)
if mtime.year == 0 || mtime.month == 0 || mtime.day == 0
Expand Down
52 changes: 33 additions & 19 deletions src/handy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ function mysql_next_result(hndl::MySQLHandle)
end

for func = (:mysql_field_count, :mysql_error, :mysql_insert_id)
eval(quote
@eval begin
function ($func)(hndl::MySQLHandle, args...)
hndl.mysqlptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL connection."))
return ($func)(hndl.mysqlptr, args...)
end
end)
end
end

"""
Expand All @@ -93,14 +93,14 @@ mysql_insert_id

# wrappers to take MySQLHandle as input as well as check for NULL pointer.
for func = (:mysql_query, :mysql_options)
eval(quote
@eval begin
function ($func)(hndl::MySQLHandle, args...)
hndl.mysqlptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL connection."))
val = ($func)(hndl.mysqlptr, args...)
val != 0 && throw(MySQLInternalError(hndl))
return val
end
end)
end
end

"""
Expand Down Expand Up @@ -166,7 +166,7 @@ function mysql_execute(hndl, command; opformat=MYSQL_DATA_FRAME)
else
throw(MySQLInterfaceError("Query expected to produce results but did not."))
end

status = mysql_next_result(hndl.mysqlptr)
if status > 0
throw(MySQLInternalError(hndl))
Expand Down Expand Up @@ -212,12 +212,12 @@ end

for func = (:mysql_stmt_num_rows, :mysql_stmt_affected_rows,
:mysql_stmt_result_to_dataframe, :mysql_stmt_error)
eval(quote
@eval begin
function ($func)(hndl::MySQLHandle, args...)
hndl.stmtptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL statement handle."))
return ($func)(hndl.stmtptr, args...)
end
end)
end
end

"""
Expand Down Expand Up @@ -254,23 +254,23 @@ function mysql_stmt_bind_result(hndl::MySQLHandle, bindarr::Vector{MYSQL_BIND})
end

for func = (:mysql_stmt_store_result, :mysql_stmt_bind_param)
eval(quote
@eval begin
function ($func)(hndl, args...)
hndl.stmtptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL statement handle."))
val = ($func)(hndl.stmtptr, args...)
val != 0 && throw(MySQLStatementError(hndl))
return val
end
end)
end
end

for func = (:mysql_num_rows, :mysql_fetch_row)
eval(quote
@eval begin
function ($func)(hndl, args...)
hndl.resptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL result set."))
return ($func)(hndl.resptr, args...)
end
end)
end
end

"""
Expand All @@ -279,8 +279,14 @@ Get a `MYSQL_BIND` instance given the mysql type `typ` and a `value`.
mysql_bind_init(typ::MYSQL_TYPE, value) =
mysql_bind_init(mysql_get_julia_type(typ), typ, value)

mysql_bind_init(jtype::Union{Type{Date}, Type{DateTime}}, typ, value) =
MYSQL_BIND([convert(MYSQL_TIME, convert(jtype, value))], typ)
mysql_bind_init(jtype::Type{Date}, typ, value::Date) =
MYSQL_BIND([convert(MYSQL_TIME, value)], typ)
mysql_bind_init(jtype::Type{Date}, typ, value::String) =
MYSQL_BIND([convert(MYSQL_TIME, mysql_date(value))], typ)
mysql_bind_init(jtype::Type{DateTime}, typ, value::DateTime) =
MYSQL_BIND([convert(MYSQL_TIME, value)], typ)
mysql_bind_init(jtype::Type{DateTime}, typ, value::String) =
MYSQL_BIND([convert(MYSQL_TIME, mysql_datetime(value))], typ)

mysql_bind_init(::Type{String}, typ, value) = MYSQL_BIND(value, typ)
mysql_bind_init(jtype, typ, value) = MYSQL_BIND([convert(jtype, value)], typ)
Expand All @@ -295,13 +301,13 @@ Returns an array of `MYSQL_BIND`.
function mysql_bind_array(typs, params)
length(typs) != length(params) && throw(MySQLInterfaceError("Length of `typs` and `params` must be same."))
bindarr = MYSQL_BIND[]
for (typ, val) in zip(typs, params)
#Is the value one of three different versions of Null?
if (isdefined(:DataArrays)&&(typeof(val)==DataArrays.NAtype))||(isdefined(:NullableArrays)&&(typeof(val)<:Nullable)&&(val.isnull))||(val==nothing)
for (typ, val) in zip(typs, params)
#Is the value missing or equal to `nothing`?
if ismissing(val) || val === nothing
push!(bindarr, mysql_bind_init(MYSQL_TYPE_NULL, "NULL"))
else
push!(bindarr, mysql_bind_init(typ, val)) #Otherwise
end
end
end
return bindarr
end
Expand Down Expand Up @@ -332,14 +338,22 @@ end
Escapes a string using `mysql_real_escape_string()`, returns the escaped string.
"""
function mysql_escape(hndl::MySQLHandle, str::String)
output = Vector{UInt8}(length(str)*2 + 1)
output_len = mysql_real_escape_string(hndl.mysqlptr, output, str, UInt64(length(str)))
output = Vector{UInt8}(uninitialized, length(str) * 2 + 1)
output_len = mysql_real_escape_string(hndl.mysqlptr, output, str, Culong(length(str)))
if output_len == typemax(Cuint)
throw(MySQLInternalError(hndl))
end
return String(output[1:output_len])
end

"""
mysql_subtype(typ::DataType) -> DataType
Convenience function for working with missing values. If `typ` is of the form `Union{Missing, T}` it returns `T`, otherwise it returns `typ`. Not exported.
"""
mysql_subtype{T}(typ::Type{Union{Missing, T}})=T
mysql_subtype(typ::DataType)=typ

export mysql_options, mysql_connect, mysql_disconnect, mysql_execute,
mysql_insert_id, mysql_store_result, mysql_metadata, mysql_query,
mysql_stmt_prepare, mysql_escape
Loading

0 comments on commit 5fa2ad2

Please sign in to comment.