Home > database > mysql_group_attribs.m

mysql_group_attribs

PURPOSE ^

Groups attributes together under a parent attribute in the attribute_x_attribute table

SYNOPSIS ^

function mysql_group_attribs(parent_attrib_name,attrib_names,conn_id)

DESCRIPTION ^

 Groups attributes together under a parent attribute in the attribute_x_attribute table
 mysql_group_attribs(parent_attrib_name,attrib_names,conn_id)

 conn_id - database connection ID - required

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mysql_group_attribs(parent_attrib_name,attrib_names,conn_id)
0002 % Groups attributes together under a parent attribute in the attribute_x_attribute table
0003 % mysql_group_attribs(parent_attrib_name,attrib_names,conn_id)
0004 %
0005 % conn_id - database connection ID - required
0006 %
0007 
0008 % 08/18/05 Petr Janata
0009 % 06/15/10 PJ sanitized mysql_make_conn handling
0010 % 05Feb2013 PJ added handling of missing child attributes
0011 
0012 % Do some input parameter checking.
0013 min_arg = 2;
0014 max_arg = 3;
0015 
0016 msg = nargchk(min_arg,max_arg,nargin);
0017 if ~isempty(msg)
0018   disp(msg)
0019   return
0020 end
0021 
0022 % Check for valid connection to database
0023 if ~exist('conn_id','var') || isempty(conn_id) || mysql(conn_id,'status')
0024   error('%s: Do not have a valid connection ID', mfilename);
0025 end
0026 
0027 %
0028 % Check to see if the parent attribute exists in the attribute table
0029 %
0030 
0031 mysql_str = sprintf(['SELECT attribute_id FROM attribute ' ...
0032       ' WHERE attribute.name="%s";'], parent_attrib_name);
0033 parent_id = mysql(conn_id,mysql_str);
0034 
0035 if isempty(parent_id)
0036   fprintf('Creating new parent attribute in attribute table ...\n');
0037   mysql_str = sprintf('INSERT INTO attribute (class,name) VALUES ("parent_category","%s");', parent_attrib_name);
0038   status = mysql(conn_id, mysql_str);
0039 end
0040 
0041 %
0042 % Get the child attribute IDs
0043 %
0044 
0045 child_str = sprintf('"%s",',attrib_names{:});
0046 child_str(end) = [];
0047 mysql_str = sprintf(['SELECT attribute_id, name FROM attribute ' ...
0048       'WHERE name in (%s);'], child_str);
0049 [child_ids, child_names] = mysql(conn_id, mysql_str);
0050 
0051 %
0052 % Handle insertion of missing child attributes
0053 %
0054 missing_children = setdiff(attrib_names, child_names);
0055 if ~isempty(missing_children)
0056   numMissing = length(missing_children);
0057   fprintf('Creating attribute entries for %d child attributes:\n%s\n', ...
0058     numMissing, cell2str(missing_children,'\n'));
0059   value_str_array = cell(1,numMissing);
0060   for ichild = 1:numMissing
0061     value_str_array{ichild} = sprintf('("stim_set","%s")', missing_children{ichild});
0062   end
0063   value_str = cell2str(value_str_array,',');
0064   mysql_str = sprintf(['INSERT INTO attribute (class, name) ' ...
0065     'VALUES %s;'], value_str);
0066   mysql(conn_id, mysql_str);
0067   
0068   % Select all the children again
0069   child_str = sprintf('"%s",',attrib_names{:});
0070   child_str(end) = '';
0071   mysql_str = sprintf(['SELECT attribute_id FROM attribute ' ...
0072     'WHERE name in (%s);'], child_str);
0073   child_ids = mysql(conn_id, mysql_str);
0074 end
0075 
0076 %
0077 % Now insert a bunch of child/parent relationships into the
0078 % attribute_x_attribute table
0079 %
0080 
0081 value_str = sprintf('("%d","%d"),', [child_ids(:) ones(length(child_ids),1)*parent_id]');
0082 value_str(end) = [];
0083 
0084 mysql_str = sprintf(['INSERT IGNORE INTO attribute_x_attribute' ...
0085       ' (attribute_id_child,attribute_id_parent) VALUES %s;'], value_str);
0086 status = mysql(conn_id, mysql_str);
0087 
0088 %
0089 % Close the mysql connection if this was a temporary opening of the database
0090 %
0091 
0092 if exist('tmp_conn_id','var')
0093   mysql(conn_id,'close');
0094 end

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