1 | function 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 | |
---|
11 | if a_f.ctx.readonly |
---|
12 | error('read only instance') |
---|
13 | end |
---|
14 | |
---|
15 | if nargin<5||isempty(a_fct) |
---|
16 | a_fct = @(x,varargin)x; |
---|
17 | elseif ~isa(a_fct, 'function_handle') |
---|
18 | error('expected a function handle or an empty input') |
---|
19 | end |
---|
20 | |
---|
21 | if nargin<4 |
---|
22 | a_parents = []; |
---|
23 | end |
---|
24 | |
---|
25 | if numel(a_parents)~=a_f.ctx.nParents |
---|
26 | error(['expected ' num2str(a_f.ctx.nParents) ' parent(s)']) |
---|
27 | end |
---|
28 | |
---|
29 | parent = cell(a_f.ctx.nParents, 1); |
---|
30 | for i = 1:a_f.ctx.nParents |
---|
31 | parent(i) = itemGet(a_f.ctx.parent{i}, a_parents(i)); |
---|
32 | end |
---|
33 | item = a_fct(a_item, parent{:}); |
---|
34 | |
---|
35 | q = ['INSERT INTO {S} (item) VALUES ("' a_f.ctx.item_rep '")']; |
---|
36 | mym(a_f.conInfo.id, q, a_f.ctx.collection, item); |
---|
37 | q = 'SELECT id FROM {S} WHERE id=LAST_INSERT_ID()'; |
---|
38 | id = mym(a_f.conInfo.id, q, a_f.ctx.collection); |
---|
39 | q = 'UPDATE {S} SET parent{Si}_id={Si} WHERE id={Si}'; |
---|
40 | for i = 1:a_f.ctx.nParents |
---|
41 | mym(a_f.conInfo.id, q, a_f.ctx.collection, i, a_parents(i), id); |
---|
42 | end |
---|
43 | if 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); |
---|
49 | end |
---|