Home > database > mysql_insert_trial_mtx.m

mysql_insert_trial_mtx

PURPOSE ^

Inserts trial information into the trial table.

SYNOPSIS ^

function result = mysql_insert_trial_mtx(trial_mtx,params)

DESCRIPTION ^

 Inserts trial information into the trial table.

 USAGE: result = mysql_insert_trial_mtx(trial_mtx,params);

 trial_mtx: stimulus_id1, stimulus_id2, question_id, data_format_id, correct_response_enum

 params should be a structure containing database information so that it can connect to the
 database: 
 Either: host,database,user,passwd
 or conn_id

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function result = mysql_insert_trial_mtx(trial_mtx,params)
0002 % Inserts trial information into the trial table.
0003 %
0004 % USAGE: result = mysql_insert_trial_mtx(trial_mtx,params);
0005 %
0006 % trial_mtx: stimulus_id1, stimulus_id2, question_id, data_format_id, correct_response_enum
0007 %
0008 % params should be a structure containing database information so that it can connect to the
0009 % database:
0010 % Either: host,database,user,passwd
0011 % or conn_id
0012 
0013 % 07/18/09 Petr Janata
0014 % 06/15/10 PJ - sanitized mysql_make_conn
0015 
0016 result = [];
0017 
0018 if mysql_check_conn(params.conn_id)
0019   if ~all(isfield(params,{'host','database'}))
0020     error('%s: Insufficient information to establish database connection', mfilename);
0021   else
0022     params.login_type = 'researcher';
0023     params = mysql_login(params);
0024   end
0025   params = mysql_make_conn(params);
0026   tmp_conn_id = 1;
0027 else
0028   tmp_conn_id = 0;
0029 end
0030 
0031 %
0032 % Check to see if trial IDs have already been created in the trial table
0033 %
0034 stim1_str = sprintf('%d,', unique(trial_mtx(:,1)));
0035 stim1_str(end) = '';
0036 
0037 stim2_str = sprintf('%d,', unique(trial_mtx(:,2)));
0038 stim2_str(end) = '';
0039 
0040 qid_str = sprintf('%d,', unique(trial_mtx(:,3)));
0041 qid_str(end) = '';
0042 
0043 dfid_str = sprintf('%d,', unique(trial_mtx(:,4)));
0044 dfid_str(end) = '';
0045 
0046 mysql_str = sprintf(['SELECT trial_id, stimulus_id1, stimulus_id2, question_id, data_format_id '  ...
0047       'FROM trial WHERE ' ...
0048       'stimulus_id1 IN (%s) AND ' ...
0049       'stimulus_id2 IN (%s) AND ' ...
0050       'question_id IN (%s) AND ' ...
0051       'data_format_id IN (%s);'],...
0052     stim1_str, stim2_str, qid_str, dfid_str);
0053 [trial_ids, stim1_ids, stim2_ids, question_ids, dfids] = mysql(params.conn_id,mysql_str);
0054 
0055 %
0056 % Need to check if any trial IDs need to be inserted
0057 %
0058 insert_mtx = [];
0059 if isempty(trial_ids)
0060   insert_mtx = trial_mtx;
0061 elseif length(trial_ids) < size(trial_mtx,1)
0062   % Remove existing trials
0063   for itr = 1:length(trial_ids)
0064     existing_idxs = ...
0065     trial_mtx(:,1)==stim1_ids(itr) & ...
0066     trial_mtx(:,2)==stim2_ids(itr) & ...
0067     trial_mtx(:,3)==question_ids(itr) & ...
0068     trial_mtx(:,4)==dfids(itr);
0069     
0070     trial_mtx(existing_idxs,:) = [];
0071   end
0072   insert_mtx = trial_mtx;
0073 end
0074 
0075 if ~isempty(insert_mtx)
0076   fprintf('Inserting %d trials into trial table\n', size(insert_mtx,1));
0077   value_str = sprintf('("%d","%d","%d","%d","%d"),', insert_mtx');
0078   value_str(end) = '';
0079 
0080   mysql_str = sprintf(['INSERT INTO trial (' ...
0081     'stimulus_id1, ' ...
0082     'stimulus_id2, ' ...
0083     'question_id, ' ...
0084     'data_format_id, ' ...
0085     'correct_response_enum) ' ...
0086     'VALUES %s;'], value_str);
0087   
0088   result = mysql(params.conn_id, mysql_str);
0089 end
0090 
0091 % Close connection if necessary
0092 if tmp_conn_id
0093   mysql(params.conn_id,'close');
0094 end

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