Home > classes > @ao > submit_fastinsert.m

submit_fastinsert

PURPOSE ^

SUBMIT AO(s) to the MySQL server.

SYNOPSIS ^

function [AOids, Cid] = submit_fastinsert(varargin)

DESCRIPTION ^

 SUBMIT AO(s) to the MySQL server.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: SUBMIT AO(s) to the MySQL server.
              The input AOs will be submitted to the MySQL server configured in
              ltpda_startup.m. If multiple AOs are submitted together,
              a corresponding AO Collection entry will be made.

 CALL:        submit(a1, 'some comment')
              submit(a1,a2, 'some comment')
              submit([a1 a2], a3, 'some comment')

 VERSION:     $Id: submit_fastinsert.html,v 1.5 2008/01/22 20:43:15 hewitson Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = submit('Params')

 HISTORY: 09-05-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [AOids, Cid] = submit_fastinsert(varargin)
0002 % SUBMIT AO(s) to the MySQL server.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: SUBMIT AO(s) to the MySQL server.
0007 %              The input AOs will be submitted to the MySQL server configured in
0008 %              ltpda_startup.m. If multiple AOs are submitted together,
0009 %              a corresponding AO Collection entry will be made.
0010 %
0011 % CALL:        submit(a1, 'some comment')
0012 %              submit(a1,a2, 'some comment')
0013 %              submit([a1 a2], a3, 'some comment')
0014 %
0015 % VERSION:     $Id: submit_fastinsert.html,v 1.5 2008/01/22 20:43:15 hewitson Exp $
0016 %
0017 % The following call returns a parameter list object that contains the
0018 % default parameter values:
0019 %
0020 % >> pl = submit('Params')
0021 %
0022 % HISTORY: 09-05-07 M Hewitson
0023 %             Creation
0024 %
0025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0026 
0027 % Check if this is a call for parameters
0028 if nargin == 2
0029   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0030     in = char(varargin{2});
0031     if strcmp(in, 'Params')
0032       AOids = getDefaultPL();
0033       return
0034     end
0035   end
0036 end
0037 
0038 %--------------------------------------------------------------------------
0039 % collect all AOs from input
0040 as      = [];
0041 comment = [];
0042 for j=1:nargin
0043   a = varargin{j};
0044   if isa(a, 'ao')
0045     as = [as a];
0046   end
0047   if ischar(a)
0048     comment = varargin{j};
0049   end
0050 end
0051 na = length(as);
0052 if na < 1
0053   error('### Incorrect inputs: please supply at least one AO to submit.');
0054 end
0055 if isempty(comment) || length(comment)<10
0056   error('### Incorrect inputs: please supply a comment of at least 10 characters in length.');
0057 end
0058 disp(sprintf('** Submitting %d AOs.', na));
0059 
0060 %--------------------------------------------------------------------------
0061 % open mysql connection
0062 
0063 conn = mysql_connect();
0064 
0065 %--------------------------------------------------------------------------
0066 % Now process each AO
0067 
0068 AOids = [];
0069 Cid   = -1;
0070 
0071 for j=1:na
0072 
0073   a = as(j);
0074   % Tag this AO
0075   a = tag(a, conn);
0076   
0077   disp(sprintf('** Submitting AO: %06d/%s', a.tag, a.name));
0078 
0079   % Get UserID from Username
0080   [userid, dbuser] = mysql_getUserID(conn);
0081   disp(sprintf('** submitting as user %s/%d', dbuser, userid));
0082 
0083   % Collect together all info for this entry
0084   name      = a.name;
0085   data      = a.data;
0086   dinfo     = whos('data');
0087   dtype     = dinfo.class;
0088   dname     = data.name;
0089   submitted = ltpda_reformat_date(datestr(now, 31));
0090   prov      = a.provenance;
0091 %   created   = ltpda_reformat_date(datestr(get(prov, 'created'), 31));
0092   created   = format(get(prov, 'created'), 'mm-dd-yyyy HH:MM:SS');
0093   ip        = get(prov, 'ip');
0094   hostname  = get(prov, 'hostname');
0095   os        = get(prov, 'os');
0096   filename  = sprintf('AO%06d.xml', a.tag);
0097 
0098   % convert ao to text
0099   x = xml(a); % first to DOM
0100   atxt = xmlwrite(x.docNode); % then to string
0101 
0102   % Insert into aos table
0103   colnames = {'name','dtype','dname','ndata',...
0104               'UserID','Comment1','Creation',...
0105               'IP','Hostname','OS','Filename'};
0106   exdata   = {name,dtype,dname,len(a),...
0107               userid,comment,created,...
0108               ip,hostname,os,filename};
0109 
0110   fastinsert(conn, 'aos', colnames, exdata);    
0111   
0112   colnames = {'AO'};
0113   exdata   = {atxt};
0114   insert(conn, 'aofiles', colnames, exdata);
0115   
0116   
0117   % Update transactions table
0118   colnames = {'aoid','UserId','Tdate','Direction'};
0119   exdata   = {a.tag,userid,submitted,'in'};
0120   try
0121     fastinsert(conn, 'transactions', colnames, exdata);
0122   catch
0123     warning('### error updating transaction table for submission of AO %s/%d', a.name, a.tag);
0124   end
0125 
0126   % collect submitted IDs
0127   AOids = [AOids a.tag];
0128 
0129 end
0130 
0131 % Update collections table.
0132 colnames = {'num','aoids'};
0133 exdata   = {length(AOids),csv(AOids)};
0134 if ~isempty(AOids)
0135   try
0136     fastinsert(conn, 'collections', colnames, exdata);
0137     % get assigned collection ID
0138     curs = exec(conn, 'select max(id) from collections');
0139     curs = fetch(curs);
0140     Cid  = curs.Data{1};
0141     close(curs);
0142   catch
0143     warning('### error submitting AO collection.');
0144   end
0145 end
0146 
0147 % Cid = [];
0148 
0149 %--------------------------------------------------------------------------
0150 % close MySQL connection
0151 close(conn);
0152 
0153 %--------------------------------------------------------------------------
0154 % make comma separated list of numbers
0155 function s = csv(x)
0156 
0157 s = sprintf('%g,', x);
0158 s = s(1:end-1);
0159 
0160 %--------------------------------------------------------------------------
0161 % Get default params
0162 function plo = getDefaultPL()
0163 
0164 disp('* creating default plist...');
0165 plo = plist();
0166 disp('* done.');
0167 
0168 
0169 %--------------------------------------------------------------------------
0170 % A faster insert than fastinsert (at least for this case)
0171 function message = minsert(conn, table, varargin)
0172 
0173 % MINSERT inserts values into a single row of a table using JDBC driver
0174 % specified by the input connection.
0175 %
0176 % Usage: message = minsert(conn, table, 'field1', value1, 'field2',
0177 %                               value2,...)
0178 %
0179 % M Hewitson
0180 %
0181 % 31-07-07
0182 %
0183 % $Id: submit_fastinsert.html,v 1.5 2008/01/22 20:43:15 hewitson Exp $
0184 %
0185 
0186 %   q = sprintf('INSERT INTO aofiles(AO) VALUES(''%s'');', atxt);
0187 %   curs = exec(conn, q);
0188 %   disp(curs.Message)
0189 %   close(curs);
0190 
0191 q = sprintf('INSERT INTO %s(',  table);
0192 for j=1:2:length(varargin)
0193   q = [q varargin{j} ','];
0194 end
0195 q = q(1:end-1);
0196 
0197 q = [q ') VALUES('];
0198 
0199 for j=2:2:length(varargin)
0200   val = varargin{j};
0201   if ischar(val)
0202     q = [q '''' val '''' ','];
0203   elseif isnumeric(val)
0204     q = [q mat2str(val) ','];
0205   else
0206     error('unknown data type');
0207   end
0208   
0209 end
0210 
0211 q = q(1:end-1);
0212 q = [q ');'];
0213 
0214 curs = exec(conn, q);
0215 message = curs.Message;
0216 disp(message);
0217 close(curs);
0218 
0219 
0220 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003