1 | function f = BasicDB(varargin) |
---|
2 | % BASICDB - create a basic database |
---|
3 | % BasicDB('USER', user, 'PASSWORD', password, 'SCHEMA', schema) connects |
---|
4 | % to the database on localhost.'schema' using user name 'user', |
---|
5 | % password 'password'. The user name and password are loaded into |
---|
6 | % private members of the class instance; if for security reason this is |
---|
7 | % to be avoided use the switch 'SECURE' (parented database cannot be |
---|
8 | % used). |
---|
9 | % |
---|
10 | % BasicDB(..., 'SERVER', server) works as above but on the server |
---|
11 | % `serverŽ. |
---|
12 | % |
---|
13 | % BasicDB(..., 'ENGINE', engine) works as above but the tables are |
---|
14 | % created using the engine 'engine' (should be one of 'MyISAM', 'HEAP', |
---|
15 | % 'MERGE', 'InnoDB', 'BDB', 'NDBCluster'). |
---|
16 | % |
---|
17 | % BasicDB(db) use the same connection information as these used in db |
---|
18 | % (an instance of a BasicDB class). |
---|
19 | |
---|
20 | %% connection information |
---|
21 | if numel(varargin)==1&&isa(varargin{1}, 'BasicDB') |
---|
22 | f.conInfo = varargin{1}.conInfo; |
---|
23 | else |
---|
24 | % get server |
---|
25 | idx = find(strcmp(varargin, 'SERVER')); |
---|
26 | if ~isempty(idx) |
---|
27 | f.conInfo.server = varargin{idx+1}; |
---|
28 | else |
---|
29 | f.conInfo.server = 'localhost'; |
---|
30 | end |
---|
31 | % get user |
---|
32 | idx = find(strcmp(varargin, 'USER')); |
---|
33 | if ~isempty(idx) |
---|
34 | f.conInfo.user = varargin{idx+1}; |
---|
35 | else |
---|
36 | error('username: none provided') |
---|
37 | end |
---|
38 | % get password |
---|
39 | idx = find(strcmp(varargin, 'PASSWORD')); |
---|
40 | if ~isempty(idx) |
---|
41 | f.conInfo.pass = varargin{idx+1}; |
---|
42 | else |
---|
43 | error('password: none provided') |
---|
44 | end |
---|
45 | % get schema |
---|
46 | idx = find(strcmp(varargin, 'SCHEMA')); |
---|
47 | if ~isempty(idx) |
---|
48 | f.conInfo.schema = varargin{idx+1}; |
---|
49 | else |
---|
50 | error('schema: none provided') |
---|
51 | end |
---|
52 | % get engine |
---|
53 | idx = find(strcmp(varargin, 'ENGINE')); |
---|
54 | if ~isempty(idx) |
---|
55 | f.conInfo.engine = varargin{idx+1}; |
---|
56 | if any(strcmp(f.conInfo.engine, {'MyISAM', 'HEAP', 'MERGE', 'InnoDB', 'BDB', 'NDBCluster'})) |
---|
57 | error('engine: unknown') |
---|
58 | end |
---|
59 | else |
---|
60 | f.conInfo.engine = 'InnoDB'; |
---|
61 | end |
---|
62 | % secure? |
---|
63 | if ~any(strcmp(varargin, 'SECURE')) |
---|
64 | f.conInfo.secure = false; |
---|
65 | else |
---|
66 | f.conInfo.secure = true; |
---|
67 | end |
---|
68 | end |
---|
69 | % connection id |
---|
70 | f.conInfo.id = 0; |
---|
71 | %% current context |
---|
72 | f.ctx.collection = []; |
---|
73 | f.ctx.nParents = 0; |
---|
74 | f.ctx.itemRepresentation = []; |
---|
75 | f.ctx.parent = []; |
---|
76 | f.ctx.readonly = false; |
---|
77 | %% timer (to refresh connection from time to time) |
---|
78 | f.timer = []; |
---|
79 | %% create the imageDB class |
---|
80 | f = class(f, 'BasicDB'); |
---|
81 | % create schema and allLists table, if needed |
---|
82 | createBasicDB_(f); |
---|
83 | %% connect to the MySQL Server |
---|
84 | try |
---|
85 | f.conInfo.id = mym(-1, 'open', f.conInfo.server, f.conInfo.user, f.conInfo.pass); |
---|
86 | mym(f.conInfo.id, 'use', f.conInfo.schema); |
---|
87 | catch |
---|
88 | error(['Impossible to connect to ' f.conInfo.schema ' database on ' f.conInfo.server]); |
---|
89 | end |
---|
90 | %% to refresh connection from time to time |
---|
91 | f.timer = timer('TimerFcn', {@refreshConnection, f.conInfo.id}, 'Period', 300.0, 'ExecutionMode', 'fixedRate'); |
---|
92 | start(f.timer); |
---|
93 | %% clear user name and password |
---|
94 | if f.conInfo.secure |
---|
95 | f.conInfo.user = []; |
---|
96 | f.conInfo.pass = []; |
---|
97 | end |
---|