Home > database > mysql_check_stimulus_x_attribute.m

mysql_check_stimulus_x_attribute

PURPOSE ^

Checks to see whether an attribute exists in the attribute table and

SYNOPSIS ^

function out_st = mysql_check_stimulus_x_attribute(attrib_dict, params)

DESCRIPTION ^

 Checks to see whether an attribute exists in the attribute table and
 optionally creates it if necessary.

 out_st = mysql_check_attribute(attrib_dict, params);
 
 INPUT:
 
 attrib_dict is a cell array of structures containing the following fields to match/insert:
   'stimulus_id' - the ID of the stimulus in the stimulus table
   'attribute_id' - the ID of the attribute in the attribute table
   'attribute_value_double' - a numeric value that is optionally associated with the record
   'attribute_value_text', - a string (maxlen=128) that is optionally associated with the record

 If attrib_dict is an array, a vector of attribute IDs is returned.

 params.conn_id - specify the mysql connection to use
 params.create - indicate whether attribute should be created if not
                  present [default: false]

 If create is true and the attribute is not found, the attribute is created.

 OUTPUT:
 An Ensemble data struct

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function out_st = mysql_check_stimulus_x_attribute(attrib_dict, params)
0002 % Checks to see whether an attribute exists in the attribute table and
0003 % optionally creates it if necessary.
0004 %
0005 % out_st = mysql_check_attribute(attrib_dict, params);
0006 %
0007 % INPUT:
0008 %
0009 % attrib_dict is a cell array of structures containing the following fields to match/insert:
0010 %   'stimulus_id' - the ID of the stimulus in the stimulus table
0011 %   'attribute_id' - the ID of the attribute in the attribute table
0012 %   'attribute_value_double' - a numeric value that is optionally associated with the record
0013 %   'attribute_value_text', - a string (maxlen=128) that is optionally associated with the record
0014 %
0015 % If attrib_dict is an array, a vector of attribute IDs is returned.
0016 %
0017 % params.conn_id - specify the mysql connection to use
0018 % params.create - indicate whether attribute should be created if not
0019 %                  present [default: false]
0020 %
0021 % If create is true and the attribute is not found, the attribute is created.
0022 %
0023 % OUTPUT:
0024 % An Ensemble data struct
0025 
0026 % 23May2015 Petr Janata - adapted from mysql_check_attribute()
0027 
0028 % Check for connection to database
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 % Check to see if we are creating missing attributes
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   % Construct the where clause for the query based on the information we
0071   % have
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   % Check to see if the attribute tag already exists
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     % Create the attribute if necessary
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     % Get the attribute ID
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   % Append the datastruct
0117   out_st = ensemble_concat_datastruct({out_st, curr_st});
0118 end
0119 
0120 % Close connection if necessary
0121 if tmp_conn_id
0122   mysql(conn_id,'close');
0123   params.conn_id = [];
0124 end
0125 
0126 return

Generated on Tue 16-Jul-2019 04:00:51 by m2html © 2003