source: MML/trunk/applications/database/mym/dbm/@BasicDB/BasicDB.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 2.8 KB
Line 
1function 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
21if numel(varargin)==1&&isa(varargin{1}, 'BasicDB')
22  f.conInfo = varargin{1}.conInfo;
23else
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
68end
69% connection id
70f.conInfo.id = 0;
71%% current context
72f.ctx.collection = [];
73f.ctx.nParents = 0;
74f.ctx.itemRepresentation = [];
75f.ctx.parent = [];
76f.ctx.readonly = false;
77%% timer (to refresh connection from time to time)
78f.timer = [];
79%% create the imageDB class
80f = class(f, 'BasicDB');
81% create schema and allLists table, if needed
82createBasicDB_(f);
83%% connect to the MySQL Server
84try
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);
87catch
88  error(['Impossible to connect to ' f.conInfo.schema ' database on ' f.conInfo.server]);
89end
90%% to refresh connection from time to time
91f.timer = timer('TimerFcn', {@refreshConnection, f.conInfo.id}, 'Period', 300.0, 'ExecutionMode', 'fixedRate');
92start(f.timer);
93%% clear user name and password
94if f.conInfo.secure
95  f.conInfo.user = [];
96  f.conInfo.pass = [];
97end
Note: See TracBrowser for help on using the repository browser.