Home > database > make_form_name_defs.m

make_form_name_defs

PURPOSE ^

Generates the form_name_defs file if params.write2file is true. Otherwise it

SYNOPSIS ^

function [form_id_const, form_name_id_const_map, form_name_list] = make_form_name_defs(params)

DESCRIPTION ^

 Generates the form_name_defs file if params.write2file is true. Otherwise it
 returns a set of variables that allow one to get form IDs by name.

 [form_id_const, form_name_id_const_map, form_name_list] = make_form_name_defs(params);

  Constants are now fields of a form_name_const structure.  form_name_list is a
  corresponding cell array that contains the original (Ensemble) name of the form
  and the new constant name.  In order to get at the form ID field from the
  original name, one can write form_name_const.(form_name_list{form_idx,2})

 params is a structure with fields
  .ensemble - structure containing information for connecting to database.
              See mysql_make_conn()
  .mysql - same as .ensemble
  .write2file - whether mappings should be written to a file name
                form_name_defs.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [form_id_const, form_name_id_const_map, form_name_list] = make_form_name_defs(params)
0002 % Generates the form_name_defs file if params.write2file is true. Otherwise it
0003 % returns a set of variables that allow one to get form IDs by name.
0004 %
0005 % [form_id_const, form_name_id_const_map, form_name_list] = make_form_name_defs(params);
0006 %
0007 %  Constants are now fields of a form_name_const structure.  form_name_list is a
0008 %  corresponding cell array that contains the original (Ensemble) name of the form
0009 %  and the new constant name.  In order to get at the form ID field from the
0010 %  original name, one can write form_name_const.(form_name_list{form_idx,2})
0011 %
0012 % params is a structure with fields
0013 %  .ensemble - structure containing information for connecting to database.
0014 %              See mysql_make_conn()
0015 %  .mysql - same as .ensemble
0016 %  .write2file - whether mappings should be written to a file name
0017 %                form_name_defs.m
0018 
0019 % 10/28/06 Petr Janata - updated from an earlier version.
0020 % 01/03/07 PJ - fixed to implement stated functionality and deal with bad
0021 %          characters
0022 % 10/15/07 PJ - Turned into a function, fixed handling of multiple databases
0023 %          and added returning of the variables
0024 % 12/11/08 PJ - Turned off writing of form_name_defs.m file by default
0025 % 8/17/09 Stefan Tomic - added flag to only close db connection if
0026 %                        the ID wasn't passed into the function
0027 %                        (otherwise, this is a conn that we may want to use later)
0028 % 06/15/10 PJ - sanitized mysql_make_conn
0029 % 08/31/10 PJ - cleaned up handling of ensemble/mysql structures and
0030 %               associated connection formation
0031 
0032 close_conn = 0;
0033 
0034 if nargin < 1
0035   error('Must pass in params structure with database information')
0036 end
0037 
0038 params_struct_names = {'mysql','ensemble'};
0039 params_struct_idx = find(isfield(params,params_struct_names));
0040 if isempty(params_struct_idx)
0041   error('%s: Do not have sufficient database connection information', mfilename);
0042 elseif length(params_struct_idx > 1)
0043   fprintf('WARNING %s: both ensemble and mysql structures present. Using %s\n', mfilename, params_struct_names{params_struct_idx(1)});
0044   params_struct_idx = params_struct_idx(1);
0045 end
0046 
0047 try 
0048   CONN_ID = params.(params_struct_names{params_struct_idx}).conn_id; 
0049 catch
0050   CONN_ID = 0; 
0051   params.(params_struct_names{params_struct_idx}).conn_id = CONN_ID;
0052   close_conn = 1; 
0053 end
0054 try DATABASE_SCRIPT_PATH = params.paths.project_root; catch DATABASE_SCRIPT_PATH = '.'; end
0055 try write2file = params.write2file; catch write2file = 0; end
0056   
0057 CONN_ID = mysql_make_conn(params.(params_struct_names{params_struct_idx(1)}));
0058 
0059 mysql_str = sprintf('SELECT form_name, form_id FROM form');
0060 [names, ids] = mysql(CONN_ID, mysql_str);
0061 
0062 if write2file
0063   fname = fullfile(DATABASE_SCRIPT_PATH, 'form_name_defs.m');
0064 
0065   % Make a backup copy of the defs file if it already exists
0066   if exist(fname)
0067     unix_str = sprintf('mv form_name_defs.m form_name_defs.m.%s', datestr(datenum(now),29));
0068     unix(unix_str);
0069   end
0070 
0071   fid = fopen(fname, 'wt');
0072   if fid == -1
0073     error(sprintf('Could not open file: %s\n', fname))
0074   end
0075 
0076   fprintf('Writing %s\n', fname);
0077 
0078   fprintf(fid, ...
0079       ['%% Automatically generated mapping of form names to form numbers.\n%%\n%%'...
0080     ' form_name_defs.m\n%%\n%% Generated on %s by make_form_name_defs.m\n\n'], datestr(datenum(now)));
0081 end
0082 
0083 nforms = length(ids);
0084 
0085 form_name_list = cell(nforms,2);
0086 for iform = 1:nforms
0087   orig_name = names{iform};
0088   
0089   % Replace any characters in the name that won't work as variable names
0090   
0091   curr_name = regexprep(orig_name, {'-',' ','/',','}, '_');
0092   curr_name = regexprep(curr_name, {'''','(',')','\.'}, '');
0093   new_name = upper(curr_name);
0094   
0095   form_name_list{iform,1} = orig_name;
0096   form_name_list{iform,2} = new_name;
0097   if write2file
0098     fprintf(fid,'form_id_const.%s = %d;\n', new_name, ids(iform));
0099   end
0100   form_id_const.(new_name) = ids(iform);
0101 end
0102 
0103 % Write a list of form name mappings
0104 if write2file
0105   fprintf(fid, '\n\nform_name_id_const_map = { ...\n');
0106 end
0107 
0108 for iform = 1:nforms
0109   if write2file
0110     fprintf(fid,'''%s'', ''%s''; ...\n', strrep(form_name_list{iform,1},'''',''''''), form_name_list{iform,2});
0111   end
0112   form_name_id_const_map{iform,1} = ...
0113       strrep(form_name_list{iform,1},'''','''''');
0114   form_name_id_const_map{iform,2} = form_name_list{iform,2};
0115 end
0116 if write2file
0117   fprintf(fid, '};\n');
0118 end
0119 
0120 % Write the list of form names to a variable at the end of the file
0121 if write2file
0122   fprintf(fid, '\n\nform_name_list = { ...\n');
0123 end
0124 for iform = 1:nforms
0125   if write2file
0126     fprintf(fid,'''%s''; ...\n', strrep(form_name_list{iform,1},'''',''''''));
0127   end
0128   new_form_name_list{iform,1} = strrep(form_name_list{iform,1},'''','''''');
0129 end
0130 
0131 if write2file
0132   fprintf(fid, '};\n');
0133   fclose(fid);
0134 end
0135 
0136 form_name_list = new_form_name_list;
0137 
0138 
0139 % Close the mysql connection
0140 if(close_conn)
0141   mysql('close');
0142 end

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003