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

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

Initial import--MML version from SOLEIL@2013

File size: 1.5 KB
Line 
1function id = itemNew(a_f, a_item, a_tag, a_parents, a_fct)
2% ITEMNEW - add a new item
3%  itemNew(f, item, [tag, pid, fct]) adds the new item 'item' to the
4%   collection. Optionally, a tag can be specified. If the tabel as
5%   parents, the parent ids must be given. Finally, a function can be given
6%   such that the item stored in the db is given by ftc(item, parent1,
7%   parent2, ...).
8%
9%  id = itemNew(...) returns the new item id.
10
11if a_f.ctx.readonly
12  error('read only instance')
13end
14
15if nargin<5||isempty(a_fct)
16  a_fct = @(x,varargin)x;
17elseif ~isa(a_fct, 'function_handle')
18  error('expected a function handle or an empty input')
19end
20
21if nargin<4
22  a_parents = [];
23end
24
25if numel(a_parents)~=a_f.ctx.nParents
26  error(['expected ' num2str(a_f.ctx.nParents) ' parent(s)'])
27end
28
29parent = cell(a_f.ctx.nParents, 1);
30for i = 1:a_f.ctx.nParents
31  parent(i) = itemGet(a_f.ctx.parent{i}, a_parents(i));
32end
33item = a_fct(a_item, parent{:});
34
35q = ['INSERT INTO {S} (item) VALUES ("' a_f.ctx.item_rep '")'];
36mym(a_f.conInfo.id, q, a_f.ctx.collection, item);
37q = 'SELECT id FROM {S} WHERE id=LAST_INSERT_ID()';
38id = mym(a_f.conInfo.id, q, a_f.ctx.collection);
39q = 'UPDATE {S} SET parent{Si}_id={Si} WHERE id={Si}';
40for i = 1:a_f.ctx.nParents
41  mym(a_f.conInfo.id, q, a_f.ctx.collection, i, a_parents(i), id);
42end
43if nargin>=3&&~isempty(a_tag)
44  if ~ischar(a_tag)
45    error('expected a string')
46  end
47  q = 'UPDATE {S} SET tag="{S}" WHERE id={Si}';
48  mym(a_f.conInfo.id, q, a_f.ctx.collection, a_tag, id);
49end
Note: See TracBrowser for help on using the repository browser.