Home > database > mysql_check_attribute.m

mysql_check_attribute

PURPOSE ^

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

SYNOPSIS ^

function attrib_id = mysql_check_attribute(attrib_tag, params)

DESCRIPTION ^

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

 attrib_id = mysql_check_attribute(attrib_tag, params);
 
 attrib_tag is the attribute name to look for.  attrib_tag can be a cell array
 of strings in which case each string is checked as a tag, and 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.
 If multiple tags are searched and the creation parameter is set to false, any
 missing attributes are indicated by NaNs in the attribute ID vector.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function attrib_id = mysql_check_attribute(attrib_tag, params)
0002 % Checks to see whether an attribute exists in the attribute table and
0003 % optionally creates it if necessary.
0004 %
0005 % attrib_id = mysql_check_attribute(attrib_tag, params);
0006 %
0007 % attrib_tag is the attribute name to look for.  attrib_tag can be a cell array
0008 % of strings in which case each string is checked as a tag, and a vector of
0009 % attribute IDs is returned.
0010 %
0011 % params.conn_id - specify the mysql connection to use
0012 % params.create - indicate whether attribute should be created if not
0013 %                  present [default: false]
0014 %
0015 % If create is true and the attribute is not found, the attribute is created.
0016 % If multiple tags are searched and the creation parameter is set to false, any
0017 % missing attributes are indicated by NaNs in the attribute ID vector.
0018 
0019 % 07/18/09 Petr Janata
0020 % 19May2015 PJ - added clean handling of class
0021 
0022 % Check for connection to database
0023 try 
0024   conn_id = params.conn_id;
0025   tmp_conn_id = 0;
0026 catch
0027   tmp_conn_id = 1;
0028   conn_id = 0;
0029   params.conn_id = conn_id;
0030 end
0031 
0032 
0033 if mysql_check_conn(conn_id)
0034   mysql_make_conn(params);
0035 end
0036 
0037 % Check to see if we are creating missing attributes
0038 if isfield(params,'create')
0039   create = params.create; 
0040 else
0041   create=false; 
0042 end
0043 
0044 if isfield(params,'class')
0045   attrib_class = params.class;
0046 else
0047   attrib_class = 'stim_set';
0048 end
0049 
0050 % Check to see if attrib_tag is a cell array
0051 if ~iscell(attrib_tag)
0052   attrib_tag = {attrib_tag};
0053 end
0054 
0055 num_attrib = length(attrib_tag);
0056 
0057 for iattrib = 1:num_attrib
0058   curr_tag = attrib_tag{iattrib};
0059         
0060   % Check to see if the attribute tag already exists
0061   mysql_str = sprintf('SELECT attribute_id FROM attribute WHERE name="%s" AND class="%s";', curr_tag, attrib_class);
0062   [curr_id] = mysql(conn_id, mysql_str);
0063 
0064   if isempty(curr_id) && create
0065     % Create the attribute if necessary
0066     mysql_str = sprintf(['INSERT INTO attribute (name,class) ' ...
0067       'VALUES ("%s","%s");'], curr_tag, attrib_class);
0068     mysql(conn_id,mysql_str);
0069 
0070     % Get the attribute ID
0071     mysql_str = sprintf('SELECT attribute_id FROM attribute WHERE name="%s" AND class="%s";', curr_tag, attrib_class);
0072     [attrib_id(iattrib)] = mysql(conn_id, mysql_str);
0073     fprintf('Created attribute (%s), ID=%d\n', curr_tag, attrib_id(iattrib));
0074   elseif isempty(curr_id) && ~create
0075     if num_attrib == 1
0076       attrib_id = [];
0077     else
0078       attrib_id(iattrib) = NaN;
0079     end
0080   else
0081     attrib_id(iattrib) = curr_id;
0082     fprintf('Found attribute (%s), ID=%d\n', curr_tag, attrib_id(iattrib));
0083   end
0084 end
0085 
0086 % Close connection if necessary
0087 if tmp_conn_id
0088   mysql(conn_id,'close');
0089   params.conn_id = [];
0090 end

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