Skip to content

Commit a8ae1c7

Browse files
authored
Support zero_date (#200)
1 parent a7f19d6 commit a8ae1c7

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/execute.jl

+5
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ function cast(::Type{T}, ptr, len) where {T}
6262
end
6363

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

6667
function cast(::Type{DateTime}, ptr, len)
6768
buf = unsafe_wrap(Array, ptr, len)
6869
try
6970
x, code, pos = Parsers.typeparser(DateTime, buf, 1, len, buf[1], Int16(0), DATETIME_OPTIONS)
7071
if code > 0
7172
return x
73+
elseif buf == ZERO_DATE
74+
return DateTime(0)
7275
end
7376
catch e
7477
e isa InexactError && API.dateandtime_warning()
@@ -89,6 +92,8 @@ function cast(::Type{DateAndTime}, ptr, len)
8992
tm += Dates.Microsecond(y)
9093
end
9194
return DateAndTime(dt, tm)
95+
elseif buf == ZERO_DATE
96+
return DateAndTime(Date(0), Time(0))
9297
end
9398
casterror(DateAndTime, ptr, len)
9499
end

test/runtests.jl

+7
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ ct = (x = UInt8[1, 2, 3],)
336336
MySQL.load(ct, conn, "test194")
337337
ct2 = columntable(DBInterface.execute(conn, "select * from test194"))
338338

339+
# https://github.com/JuliaDatabases/MySQL.jl/issues/186
340+
DBInterface.execute(conn, "SET SESSION SQL_MODE=''")
341+
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt ") |> Tables.columntable
342+
@test dt.dt[1] == DateTime(0)
343+
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt "; mysql_date_and_time=true) |> Tables.columntable
344+
@test dt.dt[1].date == DateTime(0)
345+
339346
# 156
340347
res = DBInterface.execute(conn, "select * from Employee")
341348
DBInterface.close!(conn)

0 commit comments

Comments
 (0)