-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMSSQLconn.m
154 lines (137 loc) · 6.12 KB
/
MSSQLconn.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function [Out, Settings] = MSSQLconn(dbname, userpassmethod, varargin)
% MSSQLCONN Establishes connection with MS SQL
%
% MSSQLCONN(DBNAME) Every other input by default
% - dbname: database name to which you want to connect (char format)
%
% MSSQLCONN(DBNAME, USERPASSMETHOD) Supply username and password, else by default
% - userpassmethod --> {'username','password'}; 1st cell username,2nd cell password
% - userpassmethod --> '-win'; use windows authentication (only for R2008b)
% - userpassmethod --> '-manual'; supply credentials through inputdlg
%
% MSSQLCONN(DBNAME,USERPASSMETHOD,OPTIONAL1...3) Supply variable number of optionals.
% - servername: string with the server where the database is hosted
% - portnumber: integer of the port
% - oldversion: if MS SQL is older than 2005 ed. supply '-old';
%
% [OUT,SETTINGS] = ...
% - Class 'database': connection to the server.
% - Class 'structure' with the settings used to connect (except password).
%
% DEFAULT SETTINGS:
% - user = '';
% - pass = '';
% - server = 'localhost';
% - port = 1433;
% - driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' (MS SQL 2005 and above);
% - windows authentication: false;
%
% Examples:
% - MSSQLconn('MyDB') % use the whole set of default settings
% - MSSQLconn('MyDB',{'Oleg','****'}) % supply username and password
% - MSSQLconn('MyDB',{'Oleg',''}) % supply just the username, password by inputdlg
% - MSSQLconn('MyDB','-win') % use windows authentication
% - MSSQLconn('MyDB','-manual') % supply both username and password through inputdlg
% - MSSQLconn(...,'myserver') % supply servername with any combination of dbname and userpassmethod
% - MSSQLconn(...,1433, 'myserver','-old') % full set of optionals
%
% Additional features:
% - <a href="matlab: web('http://msdn.microsoft.com/en-us/data/aa937724.aspx','-browser')">MS SQL server JDBC Driver Webpage</a>
% - <a href="matlab: web('http://www.mathworks.com/matlabcentral/fileexchange/25577-ms-sql-jdbc-connection','-browser')">FEX MSSQLconn page</a>
% - <a href="matlab: web('http://www.mathworks.com/support/solutions/en/data/1-9SHNAT/','-browser')">TMW Support Win Authentication</a>
%
% See also DATABASE
% Author: Oleg Komarov ([email protected])
% Date: 13 oct 2009 - created
% 14 oct 2009 - added links to MS JDBC drivers and to FEX submission page
% 28 oct 2009 - reorganized input checks and added single input syntax
% 11 nov 2009 - changed input syntax; added win authentication, TMW support link and settings output
%-------------------------------------------------------------------------------------
% CHECK part
%-------------------------------------------------------------------------------------
% 1. # of inputs
error(nargchk(1,5,nargin))
% 2. dbname
if ~ischar(dbname); error('MSSQLconn:strFmt', 'dbname must be char'); end
% 3. userpassmethod
if nargin == 1 || isempty(userpassmethod);
method = '-default';
elseif iscell(userpassmethod)
method = '-cell';
else method = userpassmethod;
end
winAuth = 'false'; user = ''; pass = ''; % Default values
switch method
case '-default' % Use default values
case '-cell'
if numel(userpassmethod) == 2 % [1] IF 2 cells
user = userpassmethod{1}; pass = userpassmethod{2};
if ~isempty(user) && ischar(user) && isempty(pass) % [2] IF pass empty
while isempty(pass)
pass = inputdlg('Supply password: ', 'Empty not admitted',1);
pass = pass{:};
end
elseif ~isempty(pass) && ischar(pass) && isempty(user) % [2] IF user empty
while isempty(user)
user = inputdlg('Supply username: ', 'Empty not admitted',1);
user = user{:};
end
end
else error('MSSQLconn:upmFmt', 'userpassmethod wrong format');
end
case '-win'
if any(str2double(struct2cell(ver('database'))) < 3.5) ;
error('MSSQLconn:wauMth', 'Feature unavailable for Database Toolbox release older than 3.5 (R2008b)')
else winAuth = 'true';
end
case '-manual'
while isempty(user) || isempty(pass)
userpass = inputdlg({'Supply username: '; 'Supply password: '}, 'ENTER BOTH',1,{'',''},'on');
user = userpass{1}; pass = userpass{2};
end
end
% 4. Oldver
IDXo = strcmp('-old',varargin);
if any(IDXo)
drv = 'com.microsoft.jdbc.sqlserver.SQLServerDriver';
else
drv = 'com.microsoft.sqlserver.jdbc.SQLServerDriver';
end
% 5. Port
IDXn = cellfun(@isnumeric,varargin);
if nnz(IDXn) == 1 && mod(varargin{IDXn},1) == 0
port = num2str(varargin{IDXn});
elseif nnz(IDXn) > 1
error('MSSQLconn:prtFmt', 'Only one numeric integer port is accepted')
else port = '1433';
end
% 6. Server
IDXs = cellfun(@ischar, varargin) & ~IDXo;
if any(IDXs); server = varargin{IDXs}; else server = 'localhost'; end
%-------------------------------------------------------------------------------------
% ENGINE part
%-------------------------------------------------------------------------------------
% Url concatenation
URL = ['jdbc:sqlserver://' server ':' port ';database=' dbname ';integratedSecurity=' winAuth ';'];
% Set connection timeout (s)
logintimeout(drv, 10);
% Connect
Out = database('', user, pass, drv, URL);
% Settings
if nargout == 2
Settings = cell2struct({dbname; user; drv; server; port; ~strcmp(winAuth,';');Out.Message},...
{'databaseName'; 'user'; 'driver'; 'server'; 'port'; 'windowsAuthentication'; 'errorMsg'});
end
% [1] IF connected
if isconnection(Out)
% Initialize Status
Status = '.';
% [2] IF readonly
if isreadonly(Out); Status = ' in "READONLY" mode.'; end % [2]
% Display connection status
sprintf('Connected%s', Status)
else % [1] IF not connected
% Display error
error('MSSQLconn:conInv',Out.Message)
end % [1]
end