0001 function out_st = mysql_check_stimulus_x_attribute(attrib_dict, params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 try
0030 conn_id = params.conn_id;
0031 tmp_conn_id = 0;
0032 catch
0033 tmp_conn_id = 1;
0034 conn_id = 0;
0035 params.conn_id = conn_id;
0036 end
0037
0038 if mysql_check_conn(conn_id)
0039 mysql_make_conn(params);
0040 end
0041
0042 out_st = ensemble_init_data_struct;
0043
0044
0045 if isfield(params,'create')
0046 create = params.create;
0047 else
0048 create=false;
0049 end
0050
0051 if isstruct(attrib_dict) && length(attrib_dict)==1
0052 attrib_dict = {attrib_dict};
0053 end
0054
0055 num_entries = length(attrib_dict);
0056 tbl = mysql_describe_table('stimulus_x_attribute',conn_id);
0057 vars = tbl.flds(:)';
0058 data = cell(1,length(vars));
0059 out_st.vars = vars;
0060 out_st.data = data;
0061
0062 for ientry = 1:num_entries
0063 curr_st = ensemble_init_data_struct;
0064 curr_st.vars = vars;
0065
0066 curr_entry = attrib_dict{ientry};
0067 flds = fieldnames(curr_entry);
0068 nflds = length(flds);
0069
0070
0071
0072 insertColsStrCells = cell(1,nflds);
0073 whereStrCells = cell(1,nflds);
0074 valuesStrCells = cell(1,nflds);
0075
0076 for ifld = 1:nflds
0077 currFld = flds{ifld};
0078 insertColsStrCells{ifld} = currFld;
0079
0080 switch currFld
0081 case 'attribute_value_text'
0082 typstr = '%s';
0083 otherwise
0084 typstr = '%d';
0085 end
0086 fmtstr = sprintf('%%s="%s"', typstr);
0087
0088 currVal = curr_entry.(flds{ifld});
0089 whereStrCells{ifld} = sprintf(fmtstr, flds{ifld}, currVal);
0090 valuesStrCells{ifld} = sprintf(['"' typstr '"'], currVal);
0091 end
0092 whereStr = cell2str(whereStrCells,' AND ');
0093
0094
0095 mysql_str = sprintf('SELECT * FROM stimulus_x_attribute WHERE %s;', whereStr);
0096 [data{:}] = mysql(conn_id, mysql_str);
0097 foundEntries = ~isempty(data{1});
0098
0099 if ~foundEntries && create
0100
0101 mysql_str = sprintf(['INSERT INTO stimulus_x_attribute (%s) ' ...
0102 'VALUES (%s);'], cell2str(insertColsStrCells,','), cell2str(valuesStrCells,','));
0103 mysql(conn_id,mysql_str);
0104
0105
0106 mysql_str = sprintf('SELECT * FROM stimulus_x_attribute WHERE %s;', whereStr);
0107 [data{:}] = mysql(conn_id, mysql_str);
0108 fprintf('Created entry: %s\n', whereStr);
0109 elseif ~foundEntries && ~create
0110 data = {};
0111 else
0112 fprintf('Found entry: %s\n', whereStr);
0113 end
0114 curr_st.data = data;
0115
0116
0117 out_st = ensemble_concat_datastruct({out_st, curr_st});
0118 end
0119
0120
0121 if tmp_conn_id
0122 mysql(conn_id,'close');
0123 params.conn_id = [];
0124 end
0125
0126 return