0001 function mysql_group_attribs(parent_attrib_name,attrib_names,conn_id)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 min_arg = 2;
0014 max_arg = 3;
0015
0016 msg = nargchk(min_arg,max_arg,nargin);
0017 if ~isempty(msg)
0018 disp(msg)
0019 return
0020 end
0021
0022
0023 if ~exist('conn_id','var') || isempty(conn_id) || mysql(conn_id,'status')
0024 error('%s: Do not have a valid connection ID', mfilename);
0025 end
0026
0027
0028
0029
0030
0031 mysql_str = sprintf(['SELECT attribute_id FROM attribute ' ...
0032 ' WHERE attribute.name="%s";'], parent_attrib_name);
0033 parent_id = mysql(conn_id,mysql_str);
0034
0035 if isempty(parent_id)
0036 fprintf('Creating new parent attribute in attribute table ...\n');
0037 mysql_str = sprintf('INSERT INTO attribute (class,name) VALUES ("parent_category","%s");', parent_attrib_name);
0038 status = mysql(conn_id, mysql_str);
0039 end
0040
0041
0042
0043
0044
0045 child_str = sprintf('"%s",',attrib_names{:});
0046 child_str(end) = [];
0047 mysql_str = sprintf(['SELECT attribute_id, name FROM attribute ' ...
0048 'WHERE name in (%s);'], child_str);
0049 [child_ids, child_names] = mysql(conn_id, mysql_str);
0050
0051
0052
0053
0054 missing_children = setdiff(attrib_names, child_names);
0055 if ~isempty(missing_children)
0056 numMissing = length(missing_children);
0057 fprintf('Creating attribute entries for %d child attributes:\n%s\n', ...
0058 numMissing, cell2str(missing_children,'\n'));
0059 value_str_array = cell(1,numMissing);
0060 for ichild = 1:numMissing
0061 value_str_array{ichild} = sprintf('("stim_set","%s")', missing_children{ichild});
0062 end
0063 value_str = cell2str(value_str_array,',');
0064 mysql_str = sprintf(['INSERT INTO attribute (class, name) ' ...
0065 'VALUES %s;'], value_str);
0066 mysql(conn_id, mysql_str);
0067
0068
0069 child_str = sprintf('"%s",',attrib_names{:});
0070 child_str(end) = '';
0071 mysql_str = sprintf(['SELECT attribute_id FROM attribute ' ...
0072 'WHERE name in (%s);'], child_str);
0073 child_ids = mysql(conn_id, mysql_str);
0074 end
0075
0076
0077
0078
0079
0080
0081 value_str = sprintf('("%d","%d"),', [child_ids(:) ones(length(child_ids),1)*parent_id]');
0082 value_str(end) = [];
0083
0084 mysql_str = sprintf(['INSERT IGNORE INTO attribute_x_attribute' ...
0085 ' (attribute_id_child,attribute_id_parent) VALUES %s;'], value_str);
0086 status = mysql(conn_id, mysql_str);
0087
0088
0089
0090
0091
0092 if exist('tmp_conn_id','var')
0093 mysql(conn_id,'close');
0094 end