Home > database > mysql_check_attribute_x_attribute.m

mysql_check_attribute_x_attribute

PURPOSE ^

Checks to see whether a mapping exists in the attribute_x_attribute table

SYNOPSIS ^

function out_st = mysql_check_attribute_x_attribute(attrib_dict, params)

DESCRIPTION ^

 Checks to see whether a mapping exists in the attribute_x_attribute table
 and creates it if necessary

 out_st = mysql_check_attribute_x_attribute(attrib_dict, params);
 
 INPUT:
 
 attrib_dict is a cell array of structures containing the following fields to match/insert:
   'attribute_id_child' - the ID of the attribute considered to be the child
   'attribute_id_parent' - the ID of the attribute in the attribute table
                           considered to be the parent
   'mapping_name' - the name for this mapping (equivalent to
                    attribute_value_text in other tables)
   'mapping_value_double', - a numeric value 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_attribute_x_attribute(attrib_dict, params)
0002 % Checks to see whether a mapping exists in the attribute_x_attribute table
0003 % and creates it if necessary
0004 %
0005 % out_st = mysql_check_attribute_x_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 %   'attribute_id_child' - the ID of the attribute considered to be the child
0011 %   'attribute_id_parent' - the ID of the attribute in the attribute table
0012 %                           considered to be the parent
0013 %   'mapping_name' - the name for this mapping (equivalent to
0014 %                    attribute_value_text in other tables)
0015 %   'mapping_value_double', - a numeric value that is optionally associated with the record
0016 %
0017 % If attrib_dict is an array, a vector of attribute IDs is returned.
0018 %
0019 % params.conn_id - specify the mysql connection to use
0020 % params.create - indicate whether attribute should be created if not
0021 %                  present [default: false]
0022 %
0023 % If create is true and the attribute is not found, the attribute is created.
0024 %
0025 % OUTPUT:
0026 % An Ensemble data struct
0027 
0028 % 23May2015 Petr Janata - adapted from mysql_check_stimulus_x_attribute()
0029 
0030 % Check for connection to database
0031 try 
0032   conn_id = params.conn_id;
0033   tmp_conn_id = 0;
0034 catch
0035   tmp_conn_id = 1;
0036   conn_id = 0;
0037   params.conn_id = conn_id;
0038 end
0039 
0040 if mysql_check_conn(conn_id)
0041   mysql_make_conn(params);
0042 end
0043 
0044 out_st = ensemble_init_data_struct;
0045 
0046 % Check to see if we are creating missing attributes
0047 if isfield(params,'create')
0048   create = params.create; 
0049 else
0050   create=false; 
0051 end
0052 
0053 if isstruct(attrib_dict) && length(attrib_dict)==1
0054   attrib_dict = {attrib_dict};
0055 end
0056 
0057 num_entries = length(attrib_dict);
0058 tbl = mysql_describe_table('attribute_x_attribute',conn_id);
0059 vars = tbl.flds(:)';
0060 data = cell(1,length(vars));
0061 out_st.vars = vars;
0062 out_st.data = data;
0063 
0064 for ientry = 1:num_entries
0065   curr_st = ensemble_init_data_struct;
0066   curr_st.vars = vars;
0067   
0068   curr_entry = attrib_dict{ientry};
0069   flds = fieldnames(curr_entry);
0070   nflds = length(flds);
0071   
0072   % Construct the where clause for the query based on the information we
0073   % have
0074   insertColsStrCells = cell(1,nflds);
0075   whereStrCells = cell(1,nflds);
0076   valuesStrCells = cell(1,nflds);
0077   
0078   for ifld = 1:nflds
0079     currFld = flds{ifld};
0080     insertColsStrCells{ifld} = currFld;
0081     
0082     switch currFld
0083       case 'mapping_name'
0084         typstr = '%s';
0085       otherwise
0086         typstr = '%d';
0087     end
0088     fmtstr = sprintf('%%s="%s"', typstr);
0089     
0090     currVal = curr_entry.(flds{ifld});
0091     whereStrCells{ifld} = sprintf(fmtstr, flds{ifld}, currVal);
0092     valuesStrCells{ifld} = sprintf(['"' typstr '"'], currVal);
0093   end
0094   whereStr = cell2str(whereStrCells,' AND ');
0095   
0096   % Check to see if the attribute tag already exists
0097   mysql_str = sprintf('SELECT * FROM attribute_x_attribute WHERE %s;', whereStr);
0098   [data{:}] = mysql(conn_id, mysql_str);
0099   foundEntries = ~isempty(data{1});
0100 
0101   if ~foundEntries && create
0102     % Create the attribute if necessary
0103     mysql_str = sprintf(['INSERT INTO attribute_x_attribute (%s) ' ...
0104       'VALUES (%s);'], cell2str(insertColsStrCells,','), cell2str(valuesStrCells,','));
0105     mysql(conn_id,mysql_str);
0106 
0107     % Get the attribute ID
0108     mysql_str = sprintf('SELECT * FROM attribute_x_attribute WHERE %s;', whereStr);
0109     [data{:}] = mysql(conn_id, mysql_str);
0110     fprintf('Created entry: %s\n', whereStr);
0111   elseif ~foundEntries && ~create
0112     data = {};
0113   else
0114     fprintf('Found entry: %s\n', whereStr);
0115   end
0116   curr_st.data = data;
0117   
0118   % Append the datastruct
0119   out_st = ensemble_concat_datastruct({out_st, curr_st});
0120 end
0121 
0122 % Close connection if necessary
0123 if tmp_conn_id
0124   mysql(conn_id,'close');
0125   params.conn_id = [];
0126 end
0127 
0128 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003