-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathconn.m
118 lines (103 loc) · 3.51 KB
/
conn.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
% dj.conn - constructs and returns a persistent dj.Connection object.
%
% This function can be used in cases when all datajoint schemas connect to
% the same database server with the same credentials. If this is not so,
% you may wish to create multiple functions patterned after dj.conn to
% manage multiple persistent connections.
%
% The first time dj.conn is called, it must establish a conection. The
% connection parameters may be specified by input arguments. Any values omitted
% from the input arguments will be taken from the environment variables
% DJ_HOST, DJ_USER, DJ_PASS, and DJ_INIT are used.
% Finally, if the required parameters are still missing, dj.conn will prompt the
% user to enter them manually.
%
% The last parameter, initQuery (or the environemnt variable DJ_INIT) specify
% the query to be executed everytime a new connection session is established.
%
% Once established during the first invocation, the connection object cannot
% be changed. To reset the connection, use 'clear functions' or 'clear classes'.
function connObj = conn(host, user, pass, initQuery, reset, use_tls, nogui)
persistent CONN
if nargin < 5 || isempty(reset)
reset = false;
end
% get password prompt option
if nargin < 7 || isempty(nogui)
nogui = ~usejava('desktop');
end
if nargin<1 || isempty(host)
host = dj.config('databaseHost');
end
if ~contains(host, ':')
host = [host ':' num2str(dj.config('databasePort'))];
end
if isa(CONN, 'dj.Connection') && ~reset
if nargin>0
if strcmp(CONN.host,host)
warning('DataJoint:Connection:AlreadyInstantiated', sprintf([...
'Connection already instantiated.\n' ...
'Will use existing connection to "' CONN.host '".\n' ...
'To reconnect, set reset to true']));
else
error('DataJoint:Connection:AlreadyInstantiated', sprintf([...
'Connection already instantiated to "' CONN.host '".\n' ...
'To reconnect, set reset to true']));
end
end
else
% get host address
if isempty(host)
host = input('Enter datajoint host address> ','s');
end
% get username
if nargin<2 || isempty(user)
user = dj.config('databaseUser');
end
if isempty(user)
user = input('Enter datajoint username> ', 's');
end
% get password
if nargin<3 || isempty(pass)
pass = dj.config('databasePassword');
end
if isempty(pass)
if nogui
pass = input('Enter datajoint password> ','s');
else
pass = dj.lib.getpass('Enter datajoint password');
end
end
% get initial query (if any) to execute when a connection is (re)established
if nargin<4 || isempty(initQuery)
initQuery = dj.config('connectionInit_function');
end
% get tls option
if nargin<6 || isempty(use_tls)
use_tls = dj.config('databaseUse_tls');
end
if islogical(use_tls) && ~use_tls
use_tls = 'false';
elseif islogical(use_tls) && use_tls
use_tls = 'true';
elseif ~isstruct(use_tls)
use_tls = 'none';
else
use_tls = jsonencode(use_tls);
end
CONN = dj.Connection(host, user, pass, initQuery, use_tls);
end
connObj = CONN;
% if connection fails to establish, remove the persistent connection object
if ~connObj.isConnected
try
query(connObj, 'status')
catch e
CONN = [];
rethrow(e)
end
end
if nargout==0
fprintf('database connection id: %d\n', connObj.serverId);
end
end