0001 function attrib_id = mysql_stimfld2attrib(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 narg = length(varargin);
0020 for iarg = 1:2:narg
0021 switch varargin{iarg}
0022 case 'conn_id'
0023 conn_id = varargin{iarg+1};
0024 case {'attrib','attribute'}
0025 attrib_tag = varargin{iarg+1};
0026 case 'stimtbl_fld'
0027 fldname = varargin{iarg+1};
0028 case 'stimtbl_vals'
0029 fldvals = varargin{iarg+1};
0030 otherwise
0031 fprintf('%s: Unknown input argument: %s\n', mfilename, varargin{iarg});
0032 end
0033 end
0034
0035
0036 if ~exist('conn_id','var') || isempty(conn_id) || mysql(conn_id,'status')
0037 error('%s: Do not have a valid connection ID', mfilename);
0038 end
0039
0040 params.conn_id = conn_id;
0041 params.create = true;
0042 attrib_id = mysql_check_attribute(attrib_tag, params);
0043
0044
0045
0046
0047 switch fldname
0048 case {'name','description','playlist','artist','album','genre','location'}
0049 if ~iscell(fldvals)
0050 fldvals = {fldvals};
0051 end
0052 stim_str = sprintf('"%s"',cell2str(fldvals,'","'));
0053 otherwise
0054 stim_str = sprintf('%d,', fldvals);
0055 stim_str(end) = '';
0056 end
0057 mysql_str = sprintf('SELECT stimulus_id FROM stimulus WHERE %s IN (%s);', fldname, stim_str);
0058 stim_ids = mysql(conn_id,mysql_str);
0059
0060
0061
0062
0063
0064 mysql_str = sprintf(['SELECT stimulus_id FROM stimulus_x_attribute ' ...
0065 'WHERE attribute_id=%d'], attrib_id);
0066 existing_stim_ids = mysql(conn_id, mysql_str);
0067
0068
0069 insert_stim_ids = setdiff(stim_ids, existing_stim_ids);
0070 insert_stim_ids = insert_stim_ids(:);
0071
0072
0073 if ~isempty(insert_stim_ids)
0074 value_str = sprintf('("%d","%d"),', ...
0075 [insert_stim_ids ones(size(insert_stim_ids))*attrib_id]');
0076 value_str(end) = [];
0077 mysql_str = sprintf(['INSERT INTO stimulus_x_attribute (stimulus_id,' ...
0078 ' attribute_id) VALUES %s;'], value_str);
0079 mysql(conn_id, mysql_str);
0080 else
0081 fprintf('No stimulus IDs to be inserted\n');
0082 end
0083
0084 return