Timestamps in ODBC

Andrea Bedini andrea@REDACTED
Wed Aug 5 04:54:05 CEST 2020


Hi everybody!

I have noticed that the ODBC driver in the OTP, encodes the SQL type TIMESTAMP as a tuple {{YY, MM, DD}, {HH, MM, SS}} (see [1], [2]). But according to the Microsoft ODBC documentation [3] the structure SQL_C_TYPE_TIMESTAMP also has a "fraction" field for sub-second resolution.

Would it be possible to change how the ODBC data types maps to Erlang terms? A quick hack as the one listed below seems to do the job for my specific use case.

Best wishes,
Andrea

diff --git a/lib/odbc/c_src/odbcserver.c b/lib/odbc/c_src/odbcserver.c
index ee5dc9cd0a..dbd49b2836 100644
--- a/lib/odbc/c_src/odbcserver.c
+++ b/lib/odbc/c_src/odbcserver.c
@@ -1490,10 +1490,11 @@ static void encode_column_dyn(db_column column, int column_nr,
             ei_x_encode_ulong(&dynamic_buffer(state), ts->year);
             ei_x_encode_ulong(&dynamic_buffer(state), ts->month);
             ei_x_encode_ulong(&dynamic_buffer(state), ts->day);
-            ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
+            ei_x_encode_tuple_header(&dynamic_buffer(state), 4);
             ei_x_encode_ulong(&dynamic_buffer(state), ts->hour);
             ei_x_encode_ulong(&dynamic_buffer(state), ts->minute);
             ei_x_encode_ulong(&dynamic_buffer(state), ts->second);
+            ei_x_encode_ulong(&dynamic_buffer(state), ts->fraction);
             break;
        case SQL_C_CHAR:
                if binary_strings(state) {


[1] https://erlang.org/doc/apps/odbc/databases.html#type
[2] https://github.com/erlang/otp/blob/3102ec010e74508f53e9ff0233b090b8c0334a76/lib/odbc/c_src/odbcserver.c#L1486
[3] https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/c-data-types?view=sql-server-ver15

--
Andrea Bedini
http://www.andreabedini.com
use keybase.io/andreabedini to send me private messages


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200805/e5c32628/attachment.htm>


More information about the erlang-questions mailing list