source: MML/trunk/applications/database/mym/utilities/tbadd.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 1.9 KB
Line 
1function tbadd(table,cols,types,varargin)
2% TBADD     Create a MySQL table
3% INPUTS  : TABLE  - table name, string
4%           COLS   - list of column names, (m x 1) or (1 x m) cell array
5%           TYPES  - list of column types, (m x 1) or (1 x m) cell array
6%           OVER   - string 'replace' if overwrites are allowed; if  the
7%                    argument is omitted and TABLE already exists, TBADD
8%                    call will trigger an error
9% OUTPUTS : None
10% NOTES   : Use DBASE.TABLE syntax to refer to a table not in the current
11%           database
12% EXAMPLE : cols  = {'customer','date','price'};
13%           types = {'varchar(30)', 'date', 'double'}
14%           tbadd('orders',cols,types,'replace')
15% AUTHOR  : Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 8/7/06
16error(nargchk(3,4,nargin))
17if nargin == 4
18   if ~strcmpi(varargin{1},'replace')
19       error('Overwrites-allowed argument not recognized.')
20   else
21       overwrite = true;
22   end   
23else
24   overwrite = false;
25end   
26if ~mycheck
27   error('No MySQL instance detected. Use MYOPEN to connect.')
28end
29try   %#ok
30  exists = false;
31  [a,b,c,d,e,f] = mym(['describe ' table]);  %#ok
32  exists = true;
33end 
34if exists
35   if overwrite
36      tbdrop(table)
37   else   
38      error(['Table ' table ' already exists. Use ''replace'' to overwrite, or TBDROP to delete a table.'])
39   end   
40end
41K = length(cols);
42if ~iscell(cols)
43   error('COLS must be a cell array.')
44end
45if ~iscell(types)
46   error('TYPES must be a cell array.')
47end
48if ~isvector(cols)
49   error('COLS must be a cell vector.')
50end
51if ~isvector(types)
52   error('TYPES must be a cell vector.')
53end
54if K ~= length(types)
55   error('COLS and TYPES have different lengths.')
56end
57if any(cellfun('isempty',cols))
58   error('COLS contains empty cells.')
59end
60if any(cellfun('isempty',types))
61   error('TYPES contains empty cells.')
62end
63s = ['create table ' table '('];
64for k = 1:K
65  s = [s cols{k} ' ' types{k} ', '];
66end
67mym([s(1:length(s)-2) ')'])
Note: See TracBrowser for help on using the repository browser.