Context of SQLBinary in Package

I have the following code for a simple key value store for binary data:

BeginPackage["keyValueStore"];
{open, insert, get, close};
Begin["Private"];

open[] := (
$connection = OpenSQLConnection[JDBC["SQLite(Memory)",""]]; SQLCreateTable[$connection, SQLTable["data"], {
SQLColumn["key", "DataTypeName" -> "TEXT"],
SQLColumn["value", "DataTypeName" -> "BLOB", "Nullable" -> True]
}];
);

insert[key_, data_] := SQLInsert[$connection, "data", {"key", "value"}, {key, SQLBinary[data]}]; get[key_] := Replace[ SQLSelect[$connection, "data", "value", SQLColumn["key"] == key],
{{SQLBinary[data_]}} :> data
];

close[] := CloseSQLConnection[$connection]; End[]; EndPackage[];  which one could use like this: open[]; insert["one", {1, 2, 3}] get["one"] close[];  if you try this, you will find that inserting data will not work but throw a message like: DatabaseLinkJDBC::error: "Illegal value: DatabaseLinkSQLBinary[{1, 2, 3}]"  now I can repair that by either doing: BeginPackage["keyValueStore",{"DatabaseLink"}]  (which I don't want) or by replacing SQLBinary with Symbol["SQLBinary"] in the code. Both seem to repair a behavior considering the context of SQLBinary that I would consider a bug, but maybe just don't understand correctly what is going on. Would anyone agree that this is not as one would expect and should be repaired? Or can someone enlighten me why this is what I should expect? • What happens if you move the Needs to before the Begin? – Szabolcs Sep 12 '17 at 9:12 • But is DatabaseLinkSQLBinary[{1, 2, 3}] illegal or what? What should be the context of SQLBinary? – Kuba Sep 12 '17 at 9:29 • @Szabolcs: that will also not work. It seems to be necessary that either Databaselink  is on $ContextPath or that SQLBinary is created in the current $Context... – Albert Retey Sep 12 '17 at 9:35 • So it looks like DatabaseLink works only when it is in $ContextPath. The reason could be that there's some string-to-code translation somewhere in the package. Maybe through J/Link? A workaround could be to temporarily add "DatabaseLink" to the context path while your functions is being evaluated (using InheritedBlock). But this still bothers me ... – Szabolcs Sep 12 '17 at 9:57
• @Szabolcs: impressive, what you have detected seems to be the explanation -- and for me looks like a bug, which even could be corrected easily. I think I should file a bug report, would you aggree? – Albert Retey Sep 12 '17 at 10:10

In short, the reason for the behaviour you observe is this:

Some DatabaseLink functions will only work if "DatabaseLink" is in $ContextPath at the time of their evaluation. It does not matter how DatabaseLink was loaded. All that matters is whether its context is in $ContextPath at the time when SQLInsert is called.

The detailed reason is that part of DatabaseLink is implemented in Java, and some Mathematica expressions are being sent to Java through MathLink (J/Link). This involves encoding the symbols making up those expressions as strings. Whether they are encoded with or without a context prepended depends on the value of the current $ContextPath and $Context.

If "DatabaseLink" is in $ContextPath, Needs["DatabaseLink"] ToString[DatabaseLinkSQLBinary] (* "SQLBinary" *)  If it is not: Block[{$ContextPath},
]
(* "DatabaseLinkSQLBinary" *)


The Java code does some explicit string comparisons of some symbol names with "SQLBinary", and fails if it finds "DatabaseLinkSQLBinary" instead.

The relevant code is lines 23, 110 and 266 of this file:

FileNameJoin[{DirectoryName@FindFile["DatabaseLink"], "..", "java",


A possible workaround is to temporarily insert the required context in the context path in every relevant function in your package.

For example,

dbl = Function[expr,
InternalInheritedBlock[{$ContextPath}, PrependTo[$ContextPath, "DatabaseLink"]; expr], HoldAllComplete];

insert[key_, data_] := dbl@SQLInsert[...]
`

Related:

• thanks for the thorough inspection, I accepted it. It seems now very clear what happens and how we can implement workarounds. But I think you would agree that this has a strong smell of a bug, wouldn't you? – Albert Retey Sep 12 '17 at 19:45