Home > m > mysql > ltpda_dbquery.m

ltpda_dbquery

PURPOSE ^

LTPDA_DBQUERY query an AO repository database.

SYNOPSIS ^

function varargout = ltpda_dbquery(varargin)

DESCRIPTION ^

 LTPDA_DBQUERY query an AO repository database.

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

 DESCRIPTION: LTPDA_DBQUERY query an AO repository database.

 CALL:        info = ltpda_dbquery(server, dbname, table, query);

 EXAPLES:

   >> info  = ltpda_dbquery(server, dbname, 'aos',          'id>1000 and id<2000');
   >> info  = ltpda_dbquery(server, dbname, 'aos',          'name like "x12"');
   >> info  = ltpda_dbquery(server, dbname, 'AOusers',      'name="aouser"');
   >> info  = ltpda_dbquery(server, dbname, 'collections',  'UserID=3');
   >> info  = ltpda_dbquery(server, dbname, 'collections',  'aoids="1 2"');
   >> info  = ltpda_dbquery(server, dbname, 'transactions', 'UserID=3');
   >> info  = ltpda_dbquery(server, dbname, 'transactions', 'aoid=56');

   The returned 'info' structure contains the fields from each mathcing
   record.

   >> info  = ltpda_dbquery(server, dbname)

   The 'info' structure will contain a list of the tables in the database.

   >> info  = ltpda_dbquery(server, dbname, query)

   The 'info' structure will contain a list of records resulting from the SQL query.

 VERSION:     $Id: ltpda_dbquery.m,v 1.4 2007/11/13 13:53:47 hewitson Exp $

 HISTORY:     10-05-2007 M Hewitson
                 Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_dbquery(varargin)
0002 % LTPDA_DBQUERY query an AO repository database.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: LTPDA_DBQUERY query an AO repository database.
0007 %
0008 % CALL:        info = ltpda_dbquery(server, dbname, table, query);
0009 %
0010 % EXAPLES:
0011 %
0012 %   >> info  = ltpda_dbquery(server, dbname, 'aos',          'id>1000 and id<2000');
0013 %   >> info  = ltpda_dbquery(server, dbname, 'aos',          'name like "x12"');
0014 %   >> info  = ltpda_dbquery(server, dbname, 'AOusers',      'name="aouser"');
0015 %   >> info  = ltpda_dbquery(server, dbname, 'collections',  'UserID=3');
0016 %   >> info  = ltpda_dbquery(server, dbname, 'collections',  'aoids="1 2"');
0017 %   >> info  = ltpda_dbquery(server, dbname, 'transactions', 'UserID=3');
0018 %   >> info  = ltpda_dbquery(server, dbname, 'transactions', 'aoid=56');
0019 %
0020 %   The returned 'info' structure contains the fields from each mathcing
0021 %   record.
0022 %
0023 %   >> info  = ltpda_dbquery(server, dbname)
0024 %
0025 %   The 'info' structure will contain a list of the tables in the database.
0026 %
0027 %   >> info  = ltpda_dbquery(server, dbname, query)
0028 %
0029 %   The 'info' structure will contain a list of records resulting from the SQL query.
0030 %
0031 % VERSION:     $Id: ltpda_dbquery.m,v 1.4 2007/11/13 13:53:47 hewitson Exp $
0032 %
0033 % HISTORY:     10-05-2007 M Hewitson
0034 %                 Creation
0035 %
0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0037 
0038 % get table list
0039 if nargin == 2
0040 
0041   info = getTableList(varargin{1}, varargin{2});
0042 
0043 % get field list
0044 elseif nargin == 3
0045 
0046 %   info = getFieldList(varargin{1});
0047   info = simpleQuery(varargin{1}, varargin{2}, varargin{3});
0048 
0049 elseif nargin == 4
0050 
0051   hostname = varargin{1};
0052   dbname   = varargin{2};
0053   table    = varargin{1};
0054   query    = varargin{2};
0055 
0056   info = runQuery(hostname, dbname, table, query);
0057 
0058 else
0059   error('### Incorrect inputs.');
0060 end
0061 
0062 varargout{1} = info;
0063 
0064 %--------------------------------------------------------------------------
0065 % Get table list
0066 function info = getTableList(hostname, dbname)
0067 
0068 % open a connection
0069 conn = mysql_connect(hostname, dbname);
0070 curs = exec(conn, 'show tables');
0071 curs = fetch(curs);
0072 info = curs.Data;
0073 close(curs);
0074 
0075 % Close MySQL connection
0076 close(conn);
0077 
0078 %--------------------------------------------------------------------------
0079 % Get field list
0080 function info = getFieldList(table)
0081 
0082 % open a connection
0083 conn = mysql_connect();
0084 
0085 curs = exec(conn, sprintf('describe %s', table));
0086 curs = fetch(curs);
0087 info = curs.Data;
0088 close(curs);
0089 
0090 % Close MySQL connection
0091 close(conn);
0092 
0093 %--------------------------------------------------------------------------
0094 % Get field list
0095 function info = simpleQuery(hostname, dbname, q)
0096 
0097 % open a connection
0098 conn = mysql_connect(hostname, dbname);
0099 
0100 curs = exec(conn, sprintf('%s', q));
0101 curs = fetch(curs);
0102 info = curs.Data;
0103 close(curs);
0104 
0105 % Close MySQL connection
0106 close(conn);
0107 
0108 
0109 %--------------------------------------------------------------------------
0110 % Run a query
0111 function info = runQuery(hostname, dbname, table, query)
0112 
0113 % open a connection
0114 conn = mysql_connect(hostname, dbname);
0115 
0116 % Run query
0117 info = [];
0118 
0119 fieldlist = getFieldList(table);
0120 fields  = fieldlist(:,1);
0121 f = '';
0122 fs = {};
0123 for j=1:length(fields)
0124   % special cases
0125   switch table
0126     case 'aos'
0127       if ~strcmp(fields{j}, 'AO')
0128         f=[f fields{j} ',' ];
0129         fs = [fs fields(j)];
0130       end
0131     otherwise
0132       f=[f fields{j} ',' ];
0133       fs = [fs fields(j)];
0134   end
0135 end
0136 q = sprintf('select %s  from %s where %s', f(1:end-1), table, query);
0137 disp(['** QUERY: ' q]);
0138 curs = exec(conn, q);
0139 curs = fetch(curs);
0140 info = curs.Data;
0141 close(curs);
0142 
0143 % Close MySQL connection
0144 close(conn);
0145 
0146 
0147 % END

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