Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning UNIQUEIDENTIFIER column data as a string #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dblib.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type col_type =
| SYBMONEY4 (* 17 *) | SYBMONEY (* 18 *) | SYBMONEYN (* 19 *)
| SYBREAL (* 20 *)
| SYBBINARY (* 21 *) | SYBVARBINARY (* 22 *)
| SYBUNIQUE (* 23 *)

let string_of_col_type = function
| SYBCHAR -> "CHAR"
Expand All @@ -168,6 +169,7 @@ let string_of_col_type = function
| SYBBINARY -> "BINARY" | SYBVARBINARY -> "VARBINARY"
| SYBNUMERIC -> "NUMERIC"
| SYBDECIMAL -> "DECIMAL"
| SYBUNIQUE -> "UNIQUEIDENTIFIER"
;;
external coltype : dbprocess -> int -> col_type = "ocaml_freetds_dbcoltype"

Expand Down
1 change: 1 addition & 0 deletions src/dblib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type col_type =
| SYBMONEY4 | SYBMONEY | SYBMONEYN
| SYBREAL
| SYBBINARY | SYBVARBINARY
| SYBUNIQUE

val string_of_col_type : col_type -> string
(** Returns a string description of the column type. *)
Expand Down
8 changes: 8 additions & 0 deletions src/dblib_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ CAMLexport value ocaml_freetds_dbcoltype(value vdbproc, value vc)
case SYBREAL: CAMLreturn(Val_int(20));
case SYBBINARY: CAMLreturn(Val_int(21));
case SYBVARBINARY: CAMLreturn(Val_int(22));
case 36 /* SYBUNIQUE */: CAMLreturn(Val_int(23));
}
if (ty == -1)
/* The handler catches this exception. Raise a compatible one. */
Expand Down Expand Up @@ -713,6 +714,13 @@ CAMLexport value ocaml_freetds_get_data(value vdbproc, value vcol,
COPY_STRING(vres, data, len);
CONSTRUCTOR(0, vres);
break;
// SYBUNIQUE is not consistently exported in FreeTDS headers due to "an oversight"
case 36 /* SYBUNIQUE */:
// Unique identifier should be 16 bytes
// When converting to a string, the result is 36 characters (32 hex characters + 4 dashes)
CONVERT_STRING(36);
CONSTRUCTOR(0, vres);
break;
case SYBIMAGE:
case SYBBINARY:
case SYBVARBINARY:
Expand Down
19 changes: 17 additions & 2 deletions test/test_dblib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,28 @@ let test_data params _ =
CAST('a' AS CHAR(10)) AS c, \
CAST('abc' AS TEXT) AS txt, \
CAST(1 AS INT) AS i, \
CAST(3.4 AS DOUBLE PRECISION) AS d";
CAST(3.4 AS DOUBLE PRECISION) AS d, \
CAST('b6702d1b-8082-4475-90c9-07e51aef9e7c' AS UNIQUEIDENTIFIER) AS u";
Dblib.results conn
|> assert_bool "query has results";
Dblib.numcols conn
|> assert_equal ~printer:string_of_int 6;
Dblib.coltype conn 1
|> assert_equal ~printer:Dblib.string_of_col_type SYBCHAR;
Dblib.coltype conn 2
|> assert_equal ~printer:Dblib.string_of_col_type SYBCHAR;
Dblib.coltype conn 3
|> assert_equal ~printer:Dblib.string_of_col_type SYBTEXT;
Dblib.coltype conn 4
|> assert_equal ~printer:Dblib.string_of_col_type SYBINT4;
Dblib.coltype conn 5
|> assert_equal ~printer:Dblib.string_of_col_type SYBFLT8;
Dblib.coltype conn 6
|> assert_equal ~printer:Dblib.string_of_col_type SYBUNIQUE;
Dblib.nextrow conn
|> assert_equal ~printer:string_of_row
Dblib.([STRING "a"; STRING "a "; STRING "abc";
INT 1; FLOAT 3.4])
INT 1; FLOAT 3.4 ; STRING "B6702D1B-8082-4475-90C9-07E51AEF9E7C"])
)

let test_insert params _ =
Expand Down