0001 function [data, vars] = mysql_get_stim_by_attribute(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 data = {};
0025 vars = {};
0026 inputargs = varargin;
0027
0028
0029 tables = {'stimulus','attribute','stimulus_x_attribute'};
0030
0031
0032 str_idxs = find(cellfun(@isstr,inputargs));
0033 params_idx = strmatch('params',inputargs(str_idxs));
0034
0035 if ~isempty(params_idx)
0036 params = inputargs{str_idxs(params_idx)+1};
0037 rm_idxs = str_idxs(params_idx):str_idxs(params_idx)+1;
0038 inputargs(rm_idxs) = [];
0039 end
0040
0041
0042 narg = length(inputargs);
0043 for iarg = 1:2:narg
0044 switch inputargs{iarg}
0045 case {'params'}
0046 params = inputargs{iarg+1};
0047
0048 case 'mysql'
0049 params.mysql = inputargs{iarg+1};
0050
0051 case 'conn_id'
0052 params.mysql.conn_id = inputargs{iarg+1};
0053
0054 case 'host'
0055 params.mysql.host = inputargs{iarg+1};
0056
0057 case 'database'
0058 params.mysql.database = inputargs{iarg+1};
0059
0060 case {'name','attrib_name'}
0061 params.attrib_names = check_cell(inputargs{iarg+1});
0062
0063 case {'extract_flds','extract_vars'}
0064 params.extract_vars = check_cell(inputargs{iarg+1});
0065
0066 end
0067 end
0068
0069 try
0070 attrib_names = check_cell(params.attrib_names);
0071 catch
0072
0073 if isfield(params,'stimselect') && isfield(params.stimselect,'attrib_names')
0074 attrib_names = params.stimselect.attrib_names;
0075 elseif isfield(params,'selection') && isfield(params.selection,'attrib_names')
0076 attrib_names = params.selection.attrib_names;
0077 else
0078 attrib_names = {};
0079 end
0080 end
0081 if isempty(attrib_names)
0082 sprintf('no attribute names specified\n')
0083 return
0084 end
0085
0086
0087 try conn_id = params.mysql.conn_id;
0088 catch
0089 try conn_id = params.conn_id;
0090 catch
0091 tmp_conn_id = 1;
0092 conn_id = 0;
0093 params.conn_id = 0;
0094 end
0095 end
0096
0097 if mysql_check_conn(conn_id)
0098 params.mysql.conn_id = mysql_make_conn(params.mysql);
0099 end
0100
0101
0102 if iscell(attrib_names)
0103 attrib_name_str = sprintf('"%s"', cell2str(attrib_names,'","'));
0104 else
0105 attrib_name_str = sprintf('"%s"', attrib_names);
0106 end
0107
0108
0109
0110
0111
0112 if (~isfield(params,'extract_vars') || isempty(params.extract_vars))
0113
0114
0115 extract_vars = {'stimulus.stimulus_id','stimulus.name','attribute.name'};
0116
0117
0118
0119 elseif (isequal(params.extract_vars,{'*'}) || isequal(params.extract_vars,'*'))
0120
0121 extract_vars = {};
0122
0123 for itbls = 1:length(tables)
0124
0125 tbl_stub = sprintf('%s.',tables{itbls});
0126
0127 tbl_desc = mysql_describe_table(tables{itbls},conn_id);
0128 tbl_desc.flds = strcat(tbl_stub,tbl_desc.flds);
0129 extract_vars = [extract_vars transpose(tbl_desc.flds)];
0130 end
0131
0132 else
0133
0134
0135 extract_vars = check_cell(params.extract_vars);
0136 tbl_members = {};
0137 valid = 1;
0138 for itbls = 1:length(tables)
0139
0140
0141 tbl_stub = sprintf('%s.',tables{itbls});
0142
0143
0144 tbl_idxs = strmatch(tbl_stub,extract_vars);
0145 extract_vars{tbl_idxs};
0146
0147 if (length(tbl_idxs) > 0)
0148
0149
0150 tbl_desc = mysql_describe_table(tables{itbls},conn_id);
0151 for tidx = 1:length(tbl_idxs)
0152 tbl_members = [tbl_members extract_vars{tbl_idxs(tidx)}];
0153 candidate = strrep(extract_vars{tbl_idxs(tidx)},tbl_stub,'');
0154 if(~all(ismember(candidate,tbl_desc.flds)))
0155 sprintf('%s does not exist within %s',candidate,tables{itbls})
0156 valid = 0;
0157 end
0158 end
0159 end
0160 end
0161
0162
0163
0164 if (length(tbl_members) ~= length(extract_vars))
0165 missing = setdiff(params.extract_vars,tbl_members);
0166 for midx = 1:length(missing)
0167 sprintf('%s is not within the allowed search tables\n',missing{midx})
0168 end
0169 valid = 0;
0170 end
0171
0172
0173 if (~valid) return; end
0174
0175 end
0176
0177 extract_vars_str = cell2str(extract_vars,',');
0178
0179 mysql_str = sprintf(['SELECT %s FROM stimulus ' ...
0180 'LEFT JOIN stimulus_x_attribute USING (stimulus_id) ' ...
0181 'LEFT JOIN attribute USING (attribute_id) ' ...
0182 'WHERE attribute.name IN (%s);'], extract_vars_str, attrib_name_str);
0183
0184 mysql_str = sprintf(['SELECT %s FROM stimulus, attribute, stimulus_x_attribute ' ...
0185 'WHERE stimulus.stimulus_id = stimulus_x_attribute.stimulus_id ' ...
0186 'AND attribute.attribute_id = stimulus_x_attribute.attribute_id ' ...
0187 'AND attribute.name IN (%s);'], extract_vars_str, attrib_name_str);
0188
0189 [data{1:length(extract_vars)}] = mysql(conn_id, mysql_str);
0190
0191
0192 for ivar = 1:length(extract_vars)
0193 varname = extract_vars{ivar};
0194
0195
0196
0197 vars{ivar} = strrep(varname,'.','__');
0198 end
0199
0200 if exist('tmp_conn_id','var')
0201 mysql(conn_id,'close');
0202 params.conn_id = [];
0203 end
0204 end
0205
0206 function var = check_cell(var)
0207 if ~iscell(var)
0208 var = {var};
0209 end
0210 end
0211
0212