Home > database > mysql_get_child_attribs.m

mysql_get_child_attribs

PURPOSE ^

Returns child attribute IDs and names associated with one or more parent

SYNOPSIS ^

function [attribIDs, attribNames] = mysql_get_child_attribs(parentAttribs, params)

DESCRIPTION ^

 Returns child attribute IDs and names associated with one or more parent
 attributes (specified either by name or ID) specified in the input. The
 attributes are found in the attribute_x_attribute table

 [attribIDs, attribNames] = mysql_get_child_attribs(parentAttribs, params);

 INPUTS: 
    parentAttribs - can be a string, cell array of strings, or numeric
                    vector specifying the name(s) or IDs, respectively of
                    the parent attribute(s)
    params - structure containing, most importantly, a mysql
             field/structure with database information

 OUTPUTS:
    attribIDs - a vector of attribute IDs
    attribNames - a cell array of strings containing the child attribute
                  names

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [attribIDs, attribNames] = mysql_get_child_attribs(parentAttribs, params)
0002 % Returns child attribute IDs and names associated with one or more parent
0003 % attributes (specified either by name or ID) specified in the input. The
0004 % attributes are found in the attribute_x_attribute table
0005 %
0006 % [attribIDs, attribNames] = mysql_get_child_attribs(parentAttribs, params);
0007 %
0008 % INPUTS:
0009 %    parentAttribs - can be a string, cell array of strings, or numeric
0010 %                    vector specifying the name(s) or IDs, respectively of
0011 %                    the parent attribute(s)
0012 %    params - structure containing, most importantly, a mysql
0013 %             field/structure with database information
0014 %
0015 % OUTPUTS:
0016 %    attribIDs - a vector of attribute IDs
0017 %    attribNames - a cell array of strings containing the child attribute
0018 %                  names
0019 
0020 % 27Jan2013 Petr Janata
0021 
0022 attribIDs = [];
0023 attribNames = {};
0024 
0025 % Make sure our database connection is intact
0026 if ~isfield(params,'mysql')
0027   error('params.mysql is required')
0028 else
0029   conn_id = mysql_make_conn(params.mysql);
0030 end
0031 
0032 % Handle the input
0033 if ischar(parentAttribs)
0034   parentAttribs = {parentAttribs};
0035 end
0036 
0037 if isnumeric(parentAttribs)
0038   attribStr = sprintf('%d,',parentAttribs);
0039   attribStr(end) = '';
0040   mysql_str = sprintf('SELECT attribute_id FROM attribute WHERE attribute_id IN (%s) AND class="parent_category";', attribStr);
0041   parentAttribIDs = mysql(conn_id, mysql_str);
0042 else
0043   attribStr = sprintf('"%s",', parentAttribs{:});
0044   attribStr(end) = '';
0045   mysql_str = sprintf('SELECT attribute_id FROM attribute WHERE name IN (%s) AND class="parent_category";', attribStr);
0046   parentAttribIDs = mysql(conn_id, mysql_str);
0047 end
0048 
0049 if isempty(parentAttribIDs)
0050   fprintf('Could not find any parent_category attribute IDs matching: %s\n', attribStr);
0051   return
0052 end
0053 
0054 parentStr = sprintf('%d,', parentAttribIDs);
0055 parentStr(end) = '';
0056 
0057 % Now consult the attribute_x_attribute table to retrieve the children
0058 mysql_str = sprintf(['SELECT a.attribute_id, a.name FROM ' ...
0059   'attribute AS a, attribute_x_attribute AS axa ' ...
0060   'WHERE a.attribute_id = axa.attribute_id_child AND axa.attribute_id_parent IN (%s);'], parentStr);
0061 [attribIDs, attribNames] = mysql(conn_id, mysql_str);
0062 
0063 return

Generated on Sat 24-Aug-2019 04:00:39 by m2html © 2003