0001 function out_st = mysql_check_attribute_x_attribute(attrib_dict, params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
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
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
0073
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
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
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
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
0119 out_st = ensemble_concat_datastruct({out_st, curr_st});
0120 end
0121
0122
0123 if tmp_conn_id
0124 mysql(conn_id,'close');
0125 params.conn_id = [];
0126 end
0127
0128 return