Package functions not loaded after successful `Get` (`<<`)

1

I have the following package:

(* Wolfram Language Package *)

(* Created by the Wolfram Workbench Aug 13, 2015 *)

BeginPackage["XxDataLink`",{"DatabaseLink`"}]
(* Exported symbols added here with SymbolName::usage *) 

OpenXxSQLConnection::usage="Open the connection to the Xx database."
CloseXxSQLConnection::usage="Closes the connection to the Xx database."

Begin["`Private`"]
(* Implementation of the package *)
XxSQLConnection;

OpenXxSQLConnection[]:=
 Module[{},
  If[! SQLConnectionUsableQ[XxSQLConnection],
   XxSQLConnection = OpenSQLConnection[JDBC["Microsoft SQL Server(jTDS)", "server"], "Catalog" -> "Xx"];
  ];
  XxSQLConnection
 ]

CloseXxSQLConnection[]:=
 CloseSQLConnection[XxSQLConnection]


End[]

EndPackage[]

This is installed to the $BaseDirectory in an XxDataLink directory. There is a sub directory called Kernel with an init.m file with the following line Get[ "XxDataLink`XxDataLink`"].

In a new instance of Mathematica I do a Get (<<) on this package and try to open the database connection (no error messages).

<< "XxDataLink`"
conn = OpenXxSQLConnection[]
(* OpenXxSQLConnection[] *)

What am I doing wrong? I am expecting to get a connection to the database. I've added the jTDS DLL file so that can use SQL Server integrated security for the connection and the code works outside of the package.

OpenSQLConnection[JDBC["Microsoft SQL Server(jTDS)", "server"], "Catalog" -> "Xx"]
{SQLConnectionUsableQ[#], SQLConnectionOpenQ[#]} &[%]
(* {True, True} *)

Returns an open usable connection when run directly in a notebook. Not certain why I'm not getting one when I run it through the package. The package and DatabaseLink are loading with the Get as I can run OpenSQLConnection after I load XxDataLink without explicitly loading DatabaseLink in the notebook.

Mathematica 10.1 and Windows 7

Update:

Also tried in 10.2 and Windows 7 with the same results. Can anyone repeat this behavior? Could it be some sort of bug with the Mathematica DatabaseLink package? I'm thinking it wouldn't be since tons of people must have done this before me and had it work.

Edmund

Posted 2015-08-17T17:03:03.730

Reputation: 35 657

Question was closed 2015-08-18T03:49:55.163

It might be that SQLConnectionUsableQ yields a definite a true or False only when given a real connection link as parameter. It might return unevaluated when not, causing the If branch to return unevaluated. – Sjoerd C. de Vries – 2015-08-17T18:01:22.183

@SjoerdC.deVries Nope. It returns false when not given connection. Tried that before I made function in the package. I just tested again to be certain with SQLConnectionUsableQ[x] returns (* Flase *) for uninitialised x. – Edmund – 2015-08-17T18:14:03.743

Is XxDataLink directory directly inside $BaseDirectory? Are you sure you have $BaseDirectory in your $Path, by default it's not. But if it's not, then you should get Get::noopen message, so there must be something else wrong. What does FindFile["XxDataLink`"] return? – jkuczm – 2015-08-17T18:20:59.733

@jkuczm Yes, $BaseDirectory is in $Path. XxDataLink.m is in XxDataLink directory in $BaseDirectory. The XxDataLink directory has the Kernel subdirectory. FindFile["XxDataLink`"] returns the init.m filename ( C:\ProgramData\Mathematica\Applications\XxDataLink\Kernel\init.m ). – Edmund – 2015-08-17T18:26:31.340

I just reproduced your package setup and everything "works", i.e. after getting the package and calling OpenXxSQLConnection[] I get JDBC::error: Unknown server host name 'server'. $Failed. I'm using v10.2 on Linux. – jkuczm – 2015-08-17T18:38:26.770

Is OpenXxSQLConnection symbol blue or black after loading the package? What does ?? OpenXxSQLConnection return? Can you add something like Print["I was loaded."] at the end of package file? Is this text printed when loading the package? – jkuczm – 2015-08-17T18:48:08.410

@jkuczm ??OpenXxSQLConnection returns the usage message. The Print does show in the notebook when I load the package. OpenXxSQLConnection is black when I type it it after loading the package. All of this in a new instance of Mathematica. – Edmund – 2015-08-17T18:58:44.567

"??OpenXxSQLConnection returns the usage message." Only usage message, or usage message together with proper definition? – jkuczm – 2015-08-17T19:06:16.373

@jkuczm Only the usage message. Should it be returning more for ??? – Edmund – 2015-08-17T19:08:17.203

Single ? returns only usage message, double ?? should return usage message and definition, in this case I get whole OpenXxSQLConnection[] := blah blah printed after the usage message. – jkuczm – 2015-08-17T19:10:44.367

@jkuczm I tried deleting the directory, restarting Mathematica, tried to load the package then exited. Then created the directories and files, then started Mathematica, and loaded the package. Same results. Functions not loaded and only usage message with ??. Any idea how to get around this? – Edmund – 2015-08-17T19:20:13.557

2Maybe you have some typo in your package, that you accidentally corrected when posting code in your question? Try copying the code from your post and paste it to your package, that's what I did and package worked. – jkuczm – 2015-08-17T19:25:07.307

2@jkuczm Oh, for the love of Zeus!!! What I wouldn't give for some peer review in times like these. The gruesome pain has come to a close. It was a forsaken typo (and that f is well placed). – Edmund – 2015-08-17T19:49:34.493

It happens to the best of us ;) – jkuczm – 2015-08-17T19:54:08.430

@jkuczm That's a heckuva guess! – Michael E2 – 2015-08-17T22:18:10.447

@MichaelE2 As can be seen above, I started with a lot of wrong guesses. – jkuczm – 2015-08-17T22:39:27.090

It was a great help. Thanks for working through it with me. – Edmund – 2015-08-17T23:01:14.037

Answers

1

Maybe try

If[! TrueQ[SQLConnectionUsableQ[XxSQLConnection]], ...

Chris Degnen

Posted 2015-08-17T17:03:03.730

Reputation: 27 033

No dice. Still does not work. I would not have expected that to work since SQLConnectionUsableQ returns True or False. – Edmund – 2015-08-17T17:19:07.623

Maybe if conn is defined. What happens if it isn't defined? – Chris Degnen – 2015-08-17T17:20:11.597

I'm starting from a fresh loading of Mathematica (only have one instance open) so nothing is previously defined. – Edmund – 2015-08-17T17:22:16.740

1How about << "XxDataLink\XxDataLink`"? Although it shouldn't be necessary at all if your package is loaded ininit.m`. – Chris Degnen – 2015-08-17T17:37:00.713

Nope. That is not doing the trick either. The package syntax and locations appear correct. It is very odd. – Edmund – 2015-08-17T17:47:05.537