From e80ff4910c8a481fbb0616cd0ef16d6329132da6 Mon Sep 17 00:00:00 2001 From: Bob Ham Date: Wed, 17 Jul 2019 14:44:15 +0100 Subject: [PATCH 12/29] gom: Store NULL GDateTimes in the database as NULL Currently Gom will store NULL GDateTimes in the database as a string representing the epoch, '1970-01-01T00:00:00Z', instead of NULL. This unneccessarily increases the storage requirements of every NULL GDateTime column in every row by 20 bytes. This also presents a loss of information: there's no way to distinguish between the valid input values of the epoch and NULL. The API user has to account for the way in which the library is transforming data values. To fix this, we store NULL GDateTime values in the database as NULL instead. --- gom/gom-command.c | 6 +++--- gom/gom-cursor.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gom/gom-command.c b/gom/gom-command.c index a66050c..abad299 100644 --- a/gom/gom-command.c +++ b/gom/gom-command.c @@ -106,13 +106,13 @@ gom_command_bind_param (GomCommand *command, break; default: if (G_VALUE_TYPE(value) == G_TYPE_DATE_TIME) { - GTimeVal tv = { 0 }; GDateTime *dt = g_value_get_boxed(value); - gchar *iso8601; + gchar *iso8601 = NULL; if (dt) { + GTimeVal tv = { 0 }; g_date_time_to_timeval(dt, &tv); + iso8601 = g_time_val_to_iso8601(&tv); } - iso8601 = g_time_val_to_iso8601(&tv); sqlite3_bind_text(priv->stmt, param, iso8601, -1, g_free); break; } else if (G_VALUE_TYPE(value) == G_TYPE_STRV) { diff --git a/gom/gom-cursor.c b/gom/gom-cursor.c index 6d21ae4..6ffdc28 100644 --- a/gom/gom-cursor.c +++ b/gom/gom-cursor.c @@ -86,13 +86,13 @@ gom_cursor_get_column (GomCursor *cursor, break; default: if (G_VALUE_TYPE(value) == G_TYPE_DATE_TIME) { - GTimeVal tv = { 0 }; - GDateTime *dt; const gchar *iso8601 = (gchar *)sqlite3_column_text(priv->stmt, column); + GDateTime *dt = NULL; if (iso8601) { + GTimeVal tv = { 0 }; g_time_val_from_iso8601(iso8601, &tv); + dt = g_date_time_new_from_timeval_utc(&tv); } - dt = g_date_time_new_from_timeval_utc(&tv); g_value_take_boxed(value, dt); break; } -- 2.21.1 (Apple Git-122.3)