Skip to content

Commit

Permalink
Support zero_date (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj authored Oct 10, 2022
1 parent a7f19d6 commit a8ae1c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/execute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ function cast(::Type{T}, ptr, len) where {T}
end

const DATETIME_OPTIONS = Parsers.Options(dateformat=dateformat"yyyy-mm-dd HH:MM:SS.s")
const ZERO_DATE = Vector{UInt8}("0000-00-00 00:00:00")

function cast(::Type{DateTime}, ptr, len)
buf = unsafe_wrap(Array, ptr, len)
try
x, code, pos = Parsers.typeparser(DateTime, buf, 1, len, buf[1], Int16(0), DATETIME_OPTIONS)
if code > 0
return x
elseif buf == ZERO_DATE
return DateTime(0)
end
catch e
e isa InexactError && API.dateandtime_warning()
Expand All @@ -89,6 +92,8 @@ function cast(::Type{DateAndTime}, ptr, len)
tm += Dates.Microsecond(y)
end
return DateAndTime(dt, tm)
elseif buf == ZERO_DATE
return DateAndTime(Date(0), Time(0))
end
casterror(DateAndTime, ptr, len)
end
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ ct = (x = UInt8[1, 2, 3],)
MySQL.load(ct, conn, "test194")
ct2 = columntable(DBInterface.execute(conn, "select * from test194"))

# https://github.com/JuliaDatabases/MySQL.jl/issues/186
DBInterface.execute(conn, "SET SESSION SQL_MODE=''")
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt ") |> Tables.columntable
@test dt.dt[1] == DateTime(0)
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt "; mysql_date_and_time=true) |> Tables.columntable
@test dt.dt[1].date == DateTime(0)

# 156
res = DBInterface.execute(conn, "select * from Employee")
DBInterface.close!(conn)
Expand Down

0 comments on commit a8ae1c7

Please sign in to comment.