Skip to content

Commit 5fa2ad2

Browse files
authored
Merge pull request #92 from JuliaDB/jq/updates
Support DataFrames 0.11
2 parents b9ef4a3 + eca7bd0 commit 5fa2ad2

File tree

15 files changed

+212
-247
lines changed

15 files changed

+212
-247
lines changed

.travis.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
# Documentation: http://docs.travis-ci.com/user/languages/julia/
12
language: julia
3+
services:
4+
- mysql
5+
os:
6+
- linux
7+
- osx
28
julia:
3-
- 0.5
49
- 0.6
10+
- nightly
11+
notifications:
12+
email: false
13+
# uncomment the following lines to override the default test script
14+
#script:
15+
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
16+
# - julia -e 'Pkg.clone(pwd()); Pkg.build("MySQL"); Pkg.test("MySQL"; coverage=true)'
17+
after_success:
18+
# push coverage results to Coveralls
19+
- julia -e 'cd(Pkg.dir("MySQL")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
20+
# push coverage results to Codecov
21+
- julia -e 'cd(Pkg.dir("MySQL")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
522
before_script:
623
- export OLD_PATH=$LD_LIBRARY_PATH
7-
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`mariadb_config --libs | cut -d ' ' -f1 | sed 's/-L//'`
24+
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`mysql_config --libs | cut -d ' ' -f1 | sed 's/-L//'`
825
after_script:
926
- export LD_LIBRARY_PATH=$OLD_PATH
1027
- unset OLD_PATH

REQUIRE

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
julia 0.5
2-
DataFrames
3-
Compat 0.17.0
4-
ConfParser
1+
julia 0.6
2+
DataFrames 0.11
3+
Compat 0.39.0

src/MySQL.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
__precompile__()
2-
1+
__precompile__(true)
32
module MySQL
43
using Compat
54
using DataFrames
5+
using Missings
6+
using Compat.Dates
67

78
include("config.jl")
89
include("consts.jl")

src/config.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
#
66
# TODO: Need to update lib_choices for Mac OS X and Windows.
77

8+
@static if !isdefined(Base, Symbol("@isdefined"))
9+
macro isdefined(x)
10+
esc(:(isdefined($x)))
11+
end
12+
end
13+
814
let
915
global mysql_lib
1016
succeeded = false
11-
if !isdefined(:mysql_lib)
12-
@static is_linux() ? (lib_choices = ["libmysql.so", "libmysqlclient.so",
17+
if !@isdefined(:mysql_lib)
18+
@static Compat.Sys.islinux() ? (lib_choices = ["libmysql.so", "libmysqlclient.so",
1319
"libmysqlclient_r.so", "libmariadb.so",
1420
"libmysqlclient_r.so.16"]) : nothing
15-
@static is_apple() ? (lib_choices = ["libmysqlclient.dylib", "libperconaserverclient.dylib"]) : nothing
16-
@static is_windows() ? (lib_choices = ["libmysql.dll", "libmariadb.dll"]) : nothing
21+
@static Compat.Sys.isapple() ? (lib_choices = ["libmysqlclient.dylib", "libperconaserverclient.dylib"]) : nothing
22+
@static Compat.Sys.iswindows() ? (lib_choices = ["libmysql.dll", "libmariadb.dll"]) : nothing
1723
local lib
1824
for lib in lib_choices
1925
try
@@ -22,7 +28,7 @@ let
2228
break
2329
end
2430
end
25-
succeeded || error("MYSQL library not found")
31+
succeeded || error("MySQL library not found")
2632
@eval const mysql_lib = $lib
2733
end
2834
end

src/consts.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const MYSQL_NO_DATA = 100
152152

153153
const MYSQL_DEFAULT_PORT = 3306
154154

155-
if is_windows()
155+
if Compat.Sys.iswindows()
156156
MYSQL_DEFAULT_SOCKET = "MySQL"
157157
else
158158
MYSQL_DEFAULT_SOCKET = "/tmp/mysql.sock"

src/datetime.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ import Base.==
55
const MYSQL_DATE_FORMAT = Dates.DateFormat("yyyy-mm-dd")
66
const MYSQL_DATETIME_FORMAT = Dates.DateFormat("yyyy-mm-dd HH:MM:SS")
77

8-
function Base.convert(::Type{Date}, datestr::String)
9-
Date(datestr, MYSQL_DATE_FORMAT)
10-
end
11-
12-
function Base.convert(::Type{DateTime}, dtimestr::String)
13-
if !contains(dtimestr, " ")
14-
dtimestr = "1970-01-01 " * dtimestr
15-
end
16-
DateTime(dtimestr, MYSQL_DATETIME_FORMAT)
17-
end
8+
mysql_date(str) = Dates.Date(str, MYSQL_DATE_FORMAT)
9+
mysql_datetime(str) = Dates.DateTime(contains(str, " ") ? str : "1970-01-01 " * str, MYSQL_DATETIME_FORMAT)
10+
export mysql_date, mysql_datetime
1811

1912
function Base.convert(::Type{DateTime}, mtime::MYSQL_TIME)
2013
if mtime.year == 0 || mtime.month == 0 || mtime.day == 0

src/handy.jl

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ function mysql_next_result(hndl::MySQLHandle)
7575
end
7676

7777
for func = (:mysql_field_count, :mysql_error, :mysql_insert_id)
78-
eval(quote
78+
@eval begin
7979
function ($func)(hndl::MySQLHandle, args...)
8080
hndl.mysqlptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL connection."))
8181
return ($func)(hndl.mysqlptr, args...)
8282
end
83-
end)
83+
end
8484
end
8585

8686
"""
@@ -93,14 +93,14 @@ mysql_insert_id
9393

9494
# wrappers to take MySQLHandle as input as well as check for NULL pointer.
9595
for func = (:mysql_query, :mysql_options)
96-
eval(quote
96+
@eval begin
9797
function ($func)(hndl::MySQLHandle, args...)
9898
hndl.mysqlptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL connection."))
9999
val = ($func)(hndl.mysqlptr, args...)
100100
val != 0 && throw(MySQLInternalError(hndl))
101101
return val
102102
end
103-
end)
103+
end
104104
end
105105

106106
"""
@@ -166,7 +166,7 @@ function mysql_execute(hndl, command; opformat=MYSQL_DATA_FRAME)
166166
else
167167
throw(MySQLInterfaceError("Query expected to produce results but did not."))
168168
end
169-
169+
170170
status = mysql_next_result(hndl.mysqlptr)
171171
if status > 0
172172
throw(MySQLInternalError(hndl))
@@ -212,12 +212,12 @@ end
212212

213213
for func = (:mysql_stmt_num_rows, :mysql_stmt_affected_rows,
214214
:mysql_stmt_result_to_dataframe, :mysql_stmt_error)
215-
eval(quote
215+
@eval begin
216216
function ($func)(hndl::MySQLHandle, args...)
217217
hndl.stmtptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL statement handle."))
218218
return ($func)(hndl.stmtptr, args...)
219219
end
220-
end)
220+
end
221221
end
222222

223223
"""
@@ -254,23 +254,23 @@ function mysql_stmt_bind_result(hndl::MySQLHandle, bindarr::Vector{MYSQL_BIND})
254254
end
255255

256256
for func = (:mysql_stmt_store_result, :mysql_stmt_bind_param)
257-
eval(quote
257+
@eval begin
258258
function ($func)(hndl, args...)
259259
hndl.stmtptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL statement handle."))
260260
val = ($func)(hndl.stmtptr, args...)
261261
val != 0 && throw(MySQLStatementError(hndl))
262262
return val
263263
end
264-
end)
264+
end
265265
end
266266

267267
for func = (:mysql_num_rows, :mysql_fetch_row)
268-
eval(quote
268+
@eval begin
269269
function ($func)(hndl, args...)
270270
hndl.resptr == C_NULL && throw(MySQLInterfaceError($(string(func)) * " called with NULL result set."))
271271
return ($func)(hndl.resptr, args...)
272272
end
273-
end)
273+
end
274274
end
275275

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

282-
mysql_bind_init(jtype::Union{Type{Date}, Type{DateTime}}, typ, value) =
283-
MYSQL_BIND([convert(MYSQL_TIME, convert(jtype, value))], typ)
282+
mysql_bind_init(jtype::Type{Date}, typ, value::Date) =
283+
MYSQL_BIND([convert(MYSQL_TIME, value)], typ)
284+
mysql_bind_init(jtype::Type{Date}, typ, value::String) =
285+
MYSQL_BIND([convert(MYSQL_TIME, mysql_date(value))], typ)
286+
mysql_bind_init(jtype::Type{DateTime}, typ, value::DateTime) =
287+
MYSQL_BIND([convert(MYSQL_TIME, value)], typ)
288+
mysql_bind_init(jtype::Type{DateTime}, typ, value::String) =
289+
MYSQL_BIND([convert(MYSQL_TIME, mysql_datetime(value))], typ)
284290

285291
mysql_bind_init(::Type{String}, typ, value) = MYSQL_BIND(value, typ)
286292
mysql_bind_init(jtype, typ, value) = MYSQL_BIND([convert(jtype, value)], typ)
@@ -295,13 +301,13 @@ Returns an array of `MYSQL_BIND`.
295301
function mysql_bind_array(typs, params)
296302
length(typs) != length(params) && throw(MySQLInterfaceError("Length of `typs` and `params` must be same."))
297303
bindarr = MYSQL_BIND[]
298-
for (typ, val) in zip(typs, params)
299-
#Is the value one of three different versions of Null?
300-
if (isdefined(:DataArrays)&&(typeof(val)==DataArrays.NAtype))||(isdefined(:NullableArrays)&&(typeof(val)<:Nullable)&&(val.isnull))||(val==nothing)
304+
for (typ, val) in zip(typs, params)
305+
#Is the value missing or equal to `nothing`?
306+
if ismissing(val) || val === nothing
301307
push!(bindarr, mysql_bind_init(MYSQL_TYPE_NULL, "NULL"))
302308
else
303309
push!(bindarr, mysql_bind_init(typ, val)) #Otherwise
304-
end
310+
end
305311
end
306312
return bindarr
307313
end
@@ -332,14 +338,22 @@ end
332338
Escapes a string using `mysql_real_escape_string()`, returns the escaped string.
333339
"""
334340
function mysql_escape(hndl::MySQLHandle, str::String)
335-
output = Vector{UInt8}(length(str)*2 + 1)
336-
output_len = mysql_real_escape_string(hndl.mysqlptr, output, str, UInt64(length(str)))
341+
output = Vector{UInt8}(uninitialized, length(str) * 2 + 1)
342+
output_len = mysql_real_escape_string(hndl.mysqlptr, output, str, Culong(length(str)))
337343
if output_len == typemax(Cuint)
338344
throw(MySQLInternalError(hndl))
339345
end
340346
return String(output[1:output_len])
341347
end
342348

349+
"""
350+
mysql_subtype(typ::DataType) -> DataType
351+
352+
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.
353+
"""
354+
mysql_subtype{T}(typ::Type{Union{Missing, T}})=T
355+
mysql_subtype(typ::DataType)=typ
356+
343357
export mysql_options, mysql_connect, mysql_disconnect, mysql_execute,
344358
mysql_insert_id, mysql_store_result, mysql_metadata, mysql_query,
345359
mysql_stmt_prepare, mysql_escape

0 commit comments

Comments
 (0)