1 | function tbwrite(table_tbwrite,vecs_tbwrite,varargin) |
---|
2 | % TBWRITE Write to a MySQL table |
---|
3 | % INPUTS : TABLE - table name, string |
---|
4 | % VECS - list of input vectors, (m x 1) or (1 x m) cell array |
---|
5 | % COLS - list of output columns, (m x 1) or (1 x m) cell array |
---|
6 | % (optional, VECS if COLS = {} or omitted) |
---|
7 | % BUFFER - rows per INSERT VALUES command, scalar (optional, 1000 default) |
---|
8 | % OUTPUTS : None |
---|
9 | % EXAMPLE : global name dob age |
---|
10 | % vecs = {'name','dob','age'}; |
---|
11 | % cols = {'employee_name','employee_dob','employee_age'}; |
---|
12 | % types = {'varchar(30)','date','double'}; |
---|
13 | % tbadd('staff',cols,types,'replace') |
---|
14 | % name = {'Brad','Angelina'}; |
---|
15 | % dob = {'1963-12-18',''}; |
---|
16 | % age = [42 NaN]; |
---|
17 | % tbwrite('staff',vecs,cols) |
---|
18 | % name = []; % cannot CLEAR the variables |
---|
19 | % dob = []; |
---|
20 | % age = []; |
---|
21 | % tbread('staff',vecs,cols) |
---|
22 | % name, dob, age |
---|
23 | % |
---|
24 | % NOTES : 1. Variables referenced in VECS must be (n x 1) numeric or cell arrays. |
---|
25 | % 2. Variables referenced in VECS must be declared global before TBWRITE |
---|
26 | % is invoked. |
---|
27 | % 3. VECS elements are case-sensitive. COLS elements are case-insensitive |
---|
28 | % if MySQL runs on Windows, but case-sensitive under Unix. Leading and |
---|
29 | % trailing blanks are removed from strings in both arrays. |
---|
30 | % 4. TABLE columns not in COLS will have NULL values in appended rows. |
---|
31 | % 5. Numeric values except dates are converted to strings when written to |
---|
32 | % MySQL, as follows: s = num2str(x,8). Edit line 135 to change digits- |
---|
33 | % of-precision parameter. |
---|
34 | % 6. Use mym.m to work with BLOB columns. |
---|
35 | % 7. Use DBASE.TABLE syntax to refer to tables not in current database. |
---|
36 | % AUTHOR : Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 8/7/06 |
---|
37 | error(nargchk(2,4,nargin)) |
---|
38 | if ~mycheck |
---|
39 | error('No MySQL instance detected. Use MYOPEN to connect.') |
---|
40 | end |
---|
41 | try |
---|
42 | [a,b,c,d,e,f] = mym(['describe ' table_tbwrite]); %#ok |
---|
43 | catch |
---|
44 | error(['Table ' table_tbwrite ' not found. Use TBLIST to list available tables.']) |
---|
45 | end |
---|
46 | if nargin > 2 |
---|
47 | string_tbwrite = strtrim(varargin{1}); |
---|
48 | if length(string_tbwrite) |
---|
49 | cols_tbwrite = string_tbwrite; |
---|
50 | else |
---|
51 | cols_tbwrite = vecs_tbwrite; |
---|
52 | end |
---|
53 | else |
---|
54 | cols_tbwrite = vecs_tbwrite; |
---|
55 | end |
---|
56 | if ~iscell(cols_tbwrite) |
---|
57 | error('COLS must be a cell array.') |
---|
58 | end |
---|
59 | if ~iscell(vecs_tbwrite) |
---|
60 | error('VECS must be a cell array.') |
---|
61 | end |
---|
62 | if ~isvector(cols_tbwrite) |
---|
63 | error('COLS must be a cell vector.') |
---|
64 | end |
---|
65 | if ~isvector(vecs_tbwrite) |
---|
66 | error('VECS must be a cell vector.') |
---|
67 | end |
---|
68 | K_tbwrite = length(cols_tbwrite); |
---|
69 | if K_tbwrite ~= length(vecs_tbwrite) |
---|
70 | error('COLS and VECS have different lengths.') |
---|
71 | end |
---|
72 | if any(cellfun('isempty',cols_tbwrite)) |
---|
73 | error('COLS contains empty cells.') |
---|
74 | end |
---|
75 | if any(cellfun('isempty',vecs_tbwrite)) |
---|
76 | error('VECS contains empty cells.') |
---|
77 | end |
---|
78 | if nargin > 3 && ~isempty(varargin{2}) |
---|
79 | buff_tbwrite = varargin{2}; |
---|
80 | if buff_tbwrite ~= floor(buff_tbwrite) || buff_tbwrite < 1 |
---|
81 | error('BUFFER must be a positive integer.') |
---|
82 | end |
---|
83 | else |
---|
84 | buff_tbwrite = 1000; |
---|
85 | end |
---|
86 | collist_tbwrite = cols_tbwrite{1}; |
---|
87 | for k_tbwrite = 2:K_tbwrite |
---|
88 | collist_tbwrite = [collist_tbwrite ',' cols_tbwrite{k_tbwrite}]; |
---|
89 | end |
---|
90 | [allcols_tbwrite,alltypes_tbwrite] = tbattr(table_tbwrite); |
---|
91 | n_tbwrite = zeros(K_tbwrite,1); |
---|
92 | for k_tbwrite = 1:K_tbwrite |
---|
93 | vec_tbwrite = vecs_tbwrite{k_tbwrite}; |
---|
94 | par_tbwrite = findstr(vec_tbwrite,'('); |
---|
95 | if par_tbwrite > 0 |
---|
96 | vec_reduced_tbwrite = vec_tbwrite(1:par-1); |
---|
97 | else |
---|
98 | vec_reduced_tbwrite = vec_tbwrite; |
---|
99 | end |
---|
100 | warning('off','MATLAB:declareGlobalBeforeUse') |
---|
101 | eval(['global ' vec_reduced_tbwrite]); |
---|
102 | if ~exist(vec_reduced_tbwrite,'var') |
---|
103 | cleanup |
---|
104 | error(['Vector ' vec_reduced_tbwrite ' not accessible to TBWRITE. Check if it was declared global.']) |
---|
105 | end |
---|
106 | x_tbwrite = eval(vec_reduced_tbwrite); |
---|
107 | n_tbwrite(k_tbwrite) = length(x_tbwrite); |
---|
108 | if ~(islogical(x_tbwrite) || ... |
---|
109 | isnumeric(x_tbwrite) || ... |
---|
110 | iscell(x_tbwrite)) |
---|
111 | cleanup |
---|
112 | error(['Type mismatch. ' vec_tbwrite ' is not a numeric or cell vector.']) |
---|
113 | end |
---|
114 | if ~any(strcmpi(allcols_tbwrite,cols_tbwrite{k_tbwrite})) |
---|
115 | cleanup |
---|
116 | error(['Column ' cols_tbwrite{k_tbwrite} ' not found in table ' table_tbwrite '. Use TBATTR to list available columns.']) |
---|
117 | end |
---|
118 | if iscell(x_tbwrite) |
---|
119 | actual_maxlength_tbwrite = max(cellfun('length',x_tbwrite)); |
---|
120 | coltype_tbwrite = alltypes_tbwrite{k_tbwrite}; |
---|
121 | par_tbwrite = findstr(coltype_tbwrite,'char'); |
---|
122 | if par_tbwrite |
---|
123 | p1_tbwrite = findstr(coltype_tbwrite,'(') + 1; |
---|
124 | p2_tbwrite = findstr(coltype_tbwrite,')') - 1; |
---|
125 | declared_maxlength_tbwrite = str2double(coltype_tbwrite(p1_tbwrite:p2_tbwrite)); |
---|
126 | if declared_maxlength_tbwrite < actual_maxlength_tbwrite |
---|
127 | cleanup |
---|
128 | error(['Maximum string length in ' vec_tbwrite ' exceeds declared limit. Use ALTER TABLE to resize the column.']) |
---|
129 | end |
---|
130 | end |
---|
131 | end |
---|
132 | end |
---|
133 | if var(n_tbwrite) > 0 |
---|
134 | cleanup |
---|
135 | error('Vectors with different lengths referenced in VECS.') |
---|
136 | else |
---|
137 | n_tbwrite = n_tbwrite(1); |
---|
138 | end |
---|
139 | flushes_tbwrite= ceil(n_tbwrite/buff_tbwrite); |
---|
140 | S_tbwrite = cell(1,K_tbwrite); |
---|
141 | mym(['lock tables ' table_tbwrite ' write']) |
---|
142 | for flush_tbwrite = 1:flushes_tbwrite |
---|
143 | firstrow_tbwrite = 1 + buff_tbwrite*(flush_tbwrite - 1); |
---|
144 | lastrow_tbwrite = min(firstrow_tbwrite + buff_tbwrite,n_tbwrite); |
---|
145 | for k_tbwrite = 1:K_tbwrite |
---|
146 | y_tbwrite = eval(vecs_tbwrite{k_tbwrite}); |
---|
147 | x_tbwrite = y_tbwrite(firstrow_tbwrite:lastrow_tbwrite); |
---|
148 | x_tbwrite = reshape(x_tbwrite,[],1); |
---|
149 | if isnumeric(x_tbwrite) |
---|
150 | S_tbwrite{k_tbwrite} = {num2str(x_tbwrite,8)}; |
---|
151 | end |
---|
152 | if iscell(x_tbwrite) |
---|
153 | S_tbwrite{k_tbwrite} = {strcat('''',char(x_tbwrite),'''')}; |
---|
154 | end |
---|
155 | end |
---|
156 | Z_tbwrite = strcat('(',char(S_tbwrite{1})); |
---|
157 | for k_tbwrite = 2:K_tbwrite |
---|
158 | Z_tbwrite = strcat(Z_tbwrite,',',char(S_tbwrite{k_tbwrite})); |
---|
159 | end |
---|
160 | Z_tbwrite = strcat(Z_tbwrite,'),'); |
---|
161 | Z_tbwrite = reshape(Z_tbwrite',1,[]); |
---|
162 | Z_tbwrite = strrep(Z_tbwrite,'NaN','NULL'); |
---|
163 | Z_tbwrite = strrep(Z_tbwrite,'''''','NULL'); |
---|
164 | s_tbwrite = ['insert into ' table_tbwrite '(' collist_tbwrite ') values ' Z_tbwrite]; |
---|
165 | s_tbwrite = strtrim(s_tbwrite); |
---|
166 | s_tbwrite = s_tbwrite(1:length(s_tbwrite)-1); |
---|
167 | try |
---|
168 | mym(s_tbwrite) |
---|
169 | catch |
---|
170 | cleanup |
---|
171 | error(['Values could not be inserted into table ' table_tbwrite '. Try a smaller value of BUFFER parameter.']) |
---|
172 | end |
---|
173 | end |
---|
174 | |
---|
175 | |
---|
176 | function cleanup |
---|
177 | mym('unlock tables') |
---|
178 | warning('on','MATLAB:declareGlobalBeforeUse') |
---|