Skip to content

Commit cb72071

Browse files
committed
fix #86 : default DateTime and SysTime to currTime when NULL
1 parent 0bf65a2 commit cb72071

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pull requests are welcome. Please ensure your local branch is up to date and all
161161
The examples should also run, make sure to change to the _example_ directory and run `dub build` then make sure that the compiled executable will run with each supported database (you'll need to install relevant libs and create databases and users with relevant permissions):
162162

163163
```
164+
./ddbctest --connection=sqlite::memory:
164165
./ddbctest --connection=mysql:127.0.0.1 --database=testdb --user=travis --password=bbk4k77JKH88g54
165166
./ddbctest --connection=postgresql:127.0.0.1 --database=testdb --user=postgres
166167
./ddbctest --connection=odbc://localhost --database=ddbctest --user=SA --password=bbk4k77JKH88g54 --driver="ODBC Driver 17 for SQL Server"

example/source/testddbc.d

+30-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,13 @@ int main(string[] args)
286286
break;
287287
case "mysql": // MySQL has an underscore in 'AUTO_INCREMENT'
288288
stmt.executeUpdate("DROP TABLE IF EXISTS ddbct1");
289-
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ddbct1 (`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(250), `comment` MEDIUMTEXT, `ts` TIMESTAMP)");
289+
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ddbct1 (
290+
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
291+
`name` VARCHAR(250),
292+
`comment` MEDIUMTEXT,
293+
`ts` TIMESTAMP NULL DEFAULT NULL
294+
)");
295+
290296
stmt.executeUpdate("INSERT INTO ddbct1 (`name`, `comment`, `ts`) VALUES ('name1', 'comment for line 1', CURRENT_TIMESTAMP), ('name2','comment for line 2 - can be very long', CURRENT_TIMESTAMP)");
291297

292298
stmt.executeUpdate("DROP TABLE IF EXISTS employee");
@@ -380,6 +386,29 @@ int main(string[] args)
380386
}
381387
assert(2 == i, "There should be 2 results but instead there was " ~ to!string(i));
382388

389+
// make sure that a timestamp can handle being NULL
390+
stmt.executeUpdate("UPDATE ddbct1 SET ts=NULL");
391+
i = 0;
392+
rs = stmt.executeQuery("SELECT id, name, comment, ts FROM ddbct1 WHERE ts IS NULL");
393+
while (rs.next()) {
394+
SysTime now = Clock.currTime();
395+
DateTime dtNow = cast(DateTime) now;
396+
// if the column on the table is NULL ddbc will create a DateTime using Clock.currTime()
397+
// https://github.com/buggins/ddbc/issues/86
398+
assert(rs.getDateTime(4).year == dtNow.year);
399+
assert(rs.getDateTime(4).month == dtNow.month);
400+
assert(rs.getDateTime(4).day == dtNow.day);
401+
assert(rs.getDateTime(4).hour == dtNow.hour);
402+
writeln("\tid: " ~ to!string(rs.getLong(1)) ~ "\t" ~ rs.getString(2) ~ "\t" ~ to!string(rs.getDateTime(4)));
403+
404+
// ddbc should also allow you to retrive the timestamp as a SysTime (defaulting UTC if no zone info given)
405+
assert(rs.getSysTime(4).year == now.year);
406+
assert(rs.getSysTime(4).month == now.month);
407+
assert(rs.getSysTime(4).day == now.day);
408+
//assert(rs.getSysTime(4).hour == now.hour);
409+
i++;
410+
}
411+
assert(2 == i, "There should be 2 results but instead there was " ~ to!string(i));
383412

384413
writeln("\n > Testing prepared SQL statements");
385414
PreparedStatement ps2 = conn.prepareStatement("SELECT id, name name_alias, comment, ts FROM ddbct1 WHERE id >= ?");

source/ddbc/drivers/mysqlddbc.d

+2-3
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,8 @@ public:
11371137
scope(exit) unlock();
11381138

11391139
immutable string s = getString(columnIndex);
1140-
SysTime st;
11411140
if (s is null)
1142-
return st;
1141+
return Clock.currTime();
11431142
try {
11441143
import ddbc.drivers.utils : parseSysTime;
11451144
return parseSysTime(s);
@@ -1154,7 +1153,7 @@ public:
11541153
scope(exit) unlock();
11551154
Variant v = getValue(columnIndex);
11561155
if (lastIsNull)
1157-
return DateTime();
1156+
return cast(DateTime) Clock.currTime();
11581157
if (v.convertsTo!(DateTime)) {
11591158
return v.get!DateTime();
11601159
}

source/ddbc/drivers/odbcddbc.d

+10-14
Original file line numberDiff line numberDiff line change
@@ -1585,14 +1585,13 @@ version (USE_ODBC)
15851585
return stmt.getColumn(columnIndex).value.get!(string);
15861586
}
15871587

1588-
override SysTime getSysTime(int columnIndex) {
1589-
checkClosed();
1590-
lock();
1591-
scope (exit) unlock();
1592-
1588+
override SysTime getSysTime(int columnIndex)
1589+
{
15931590
Variant v = stmt.getColumn(columnIndex).value;
1594-
if (lastIsNull)
1595-
return SysTime();
1591+
if (v.peek!(SysTime) is null) {
1592+
return Clock.currTime();
1593+
}
1594+
15961595
if (v.convertsTo!(SysTime)) {
15971596
return v.get!(SysTime);
15981597
}
@@ -1601,14 +1600,11 @@ version (USE_ODBC)
16011600

16021601
override DateTime getDateTime(int columnIndex)
16031602
{
1604-
checkClosed();
1605-
lock();
1606-
scope (exit)
1607-
unlock();
1608-
16091603
Variant v = stmt.getColumn(columnIndex).value;
1610-
if (lastIsNull)
1611-
return DateTime();
1604+
if (v.peek!(DateTime) is null) {
1605+
return cast(DateTime) Clock.currTime();
1606+
}
1607+
16121608
if (v.convertsTo!(DateTime)) {
16131609
return v.get!(DateTime);
16141610
}

source/ddbc/drivers/pgsqlddbc.d

+2-2
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ version(USE_PGSQL) {
13291329
scope(exit) unlock();
13301330
Variant v = getValue(columnIndex);
13311331
if (lastIsNull)
1332-
return SysTime();
1332+
return Clock.currTime();
13331333
if (v.convertsTo!(SysTime)) {
13341334
return v.get!SysTime();
13351335
}
@@ -1342,7 +1342,7 @@ version(USE_PGSQL) {
13421342
scope(exit) unlock();
13431343
Variant v = getValue(columnIndex);
13441344
if (lastIsNull)
1345-
return DateTime();
1345+
return cast(DateTime) Clock.currTime();
13461346
if (v.convertsTo!(DateTime)) {
13471347
return v.get!DateTime();
13481348
}

source/ddbc/drivers/sqliteddbc.d

+2-4
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,8 @@ version(USE_SQLITE) {
10221022

10231023
override SysTime getSysTime(int columnIndex) {
10241024
immutable string s = getString(columnIndex);
1025-
SysTime st;
10261025
if (s is null)
1027-
return st;
1026+
return Clock.currTime();
10281027
try {
10291028
return parseSysTime(s);
10301029
} catch (Throwable e) {
@@ -1034,9 +1033,8 @@ version(USE_SQLITE) {
10341033

10351034
override DateTime getDateTime(int columnIndex) {
10361035
string s = getString(columnIndex);
1037-
DateTime dt;
10381036
if (s is null)
1039-
return dt;
1037+
return cast(DateTime) Clock.currTime();
10401038
try {
10411039
return fromResultSet(s);
10421040
} catch (Throwable e) {

0 commit comments

Comments
 (0)