forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_upgrade_fx.php
514 lines (492 loc) · 19.4 KB
/
sql_upgrade_fx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* Upgrading and patching functions of database.
*
* Functions to allow safe database modifications
* during upgrading and patches.
*
* Copyright (C) 2008-2012 Rod Roark <[email protected]>
*
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
*
* @package LibreHealth EHR
* @author Rod Roark <[email protected]>
* @author Brady Miller <[email protected]>
* @author Teny <[email protected]>
* @link http://librehealth.io
*/
/**
* Check if a Sql table exists.
*
* @param string $tblname Sql Table Name
* @return boolean returns true if the sql table exists
*/
function tableExists($tblname) {
$row = sqlQuery("SHOW TABLES LIKE '$tblname'");
if (empty($row)) return false;
return true;
}
/**
* Check if a Sql column exists in a selected table.
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name
* @return boolean returns true if the sql column exists
*/
function columnExists($tblname, $colname) {
$row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
if (empty($row)) return false;
return true;
}
/**
* Check if a Sql column has a certain type.
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name
* @param string $coltype Sql Column Type
* @return boolean returns true if the sql column is of the specified type
*/
function columnHasType($tblname, $colname, $coltype) {
$row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
if (empty($row)) return true;
return (strcasecmp($row['Type'], $coltype) == 0);
}
/**
* Check if a Sql row exists. (with one value)
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name
* @param string $value Sql value
* @return boolean returns true if the sql row does exist
*/
function tableHasRow($tblname, $colname, $value) {
$row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
"$colname LIKE '$value'");
return $row['count'] ? true : false;
}
/**
* Check if a Sql row exists. (with two values)
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name 1
* @param string $value Sql value 1
* @param string $colname2 Sql Column Name 2
* @param string $value2 Sql value 2
* @return boolean returns true if the sql row does exist
*/
function tableHasRow2D($tblname, $colname, $value, $colname2, $value2) {
$row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
"$colname LIKE '$value' AND $colname2 LIKE '$value2'");
return $row['count'] ? true : false;
}
/**
* Check if a Sql row exists. (with three values)
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name 1
* @param string $value Sql value 1
* @param string $colname2 Sql Column Name 2
* @param string $value2 Sql value 2
* @param string $colname3 Sql Column Name 3
* @param string $value3 Sql value 3
* @return boolean returns true if the sql row does exist
*/
function tableHasRow3D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3) {
$row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
"$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3'");
return $row['count'] ? true : false;
}
/**
* Check if a Sql row exists. (with four values)
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Column Name 1
* @param string $value Sql value 1
* @param string $colname2 Sql Column Name 2
* @param string $value2 Sql value 2
* @param string $colname3 Sql Column Name 3
* @param string $value3 Sql value 3
* @param string $colname4 Sql Column Name 4
* @param string $value4 Sql value 4
* @return boolean returns true if the sql row does exist
*/
function tableHasRow4D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3, $colname4, $value4) {
$row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
"$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3' AND $colname4 LIKE '$value4'");
return $row['count'] ? true : false;
}
/**
* Check if a Sql table has a certain index/key.
*
* @param string $tblname Sql Table Name
* @param string $colname Sql Index/Key
* @return boolean returns true if the sql tables has the specified index/key
*/
function tableHasIndex($tblname, $colname) {
$row = sqlQuery("SHOW INDEX FROM `$tblname` WHERE `Key_name` = '$colname'");
return (empty($row)) ? false : true;
}
/**
* Check if a list exists.
*
* @param string $option_id Sql List Option ID
* @return boolean returns true if the list exists
*/
function listExists($option_id) {
$row = sqlQuery("SELECT * FROM list_options WHERE list_id = 'lists' AND option_id = ?", array($option_id));
if (empty($row)) return false;
return true;
}
/**
* Function to create list Occupation.
* Note this function is only run once in the sql upgrade script if the list Occupation does not exist
*/
function CreateOccupationList() {
$res = sqlStatement("SELECT DISTINCT occupation FROM patient_data WHERE occupation <> ''");
while($row = sqlFetchArray($res)) {
$records[] = $row['occupation'];
}
sqlStatement("INSERT INTO list_options (list_id, option_id, title) VALUES('lists', 'Occupation', 'Occupation')");
if(count($records)>0) {
$seq = 0;
foreach ($records as $key => $value) {
sqlStatement("INSERT INTO list_options ( list_id, option_id, title, seq) VALUES ('Occupation', ?, ?, ?)", array($value, $value, ($seq+10)));
$seq = $seq + 10;
}
}
}
/**
* Function to create list reaction.
* Note this function is only run once in the sql upgrade script if the list reaction does not exist
*/
function CreateReactionList() {
$res = sqlStatement("SELECT DISTINCT reaction FROM lists WHERE reaction <> ''");
while($row = sqlFetchArray($res)) {
$records[] = $row['reaction'];
}
sqlStatement("INSERT INTO list_options (list_id, option_id, title) VALUES('lists', 'reaction', 'Reaction')");
if(count($records)>0) {
$seq = 0;
foreach ($records as $key => $value) {
sqlStatement("INSERT INTO list_options ( list_id, option_id, title, seq) VALUES ('reaction', ?, ?, ?)", array($value, $value, ($seq+10)));
$seq = $seq + 10;
}
}
}
/*
* Function to add existing values in the immunization table to the new immunization manufacturer list
* This function will be executed always, but only missing values will ne inserted to the list
*/
function CreateImmunizationManufacturerList() {
$res = sqlStatement("SELECT DISTINCT manufacturer FROM immunizations WHERE manufacturer <> ''");
while($row = sqlFetchArray($res)) {
$records[] = $row['manufacturer'];
}
sqlStatement("INSERT INTO list_options (list_id, option_id, title) VALUES ('lists','Immunization_Manufacturer','Immunization Manufacturer')");
if(count($records)>0) {
$seq = 0;
foreach ($records as $key => $value) {
sqlStatement("INSERT INTO list_options ( list_id, option_id, title, seq) VALUES ('Immunization_Manufacturer', ?, ?, ?)", array($value, $value, ($seq+10)));
$seq = $seq + 10;
}
}
}
/**
* Upgrade or patch the database with a selected upgrade/patch file.
*
* The following "functions" within the selected file will be processed:
*
* #IfNotTable
* argument: table_name
* behavior: if the table_name does not exist, the block will be executed
*
* #IfTable
* argument: table_name
* behavior: if the table_name does exist, the block will be executed
*
* #IfColumn
* arguments: table_name colname
* behavior: if the table and column exist, the block will be executed
*
* #IfMissingColumn
* arguments: table_name colname
* behavior: if the table exists but the column does not, the block will be executed
*
* #IfNotColumnType
* arguments: table_name colname value
* behavior: If the table table_name does not have a column colname with a data type equal to value, then the block will be executed
*
* #IfNotRow
* arguments: table_name colname value
* behavior: If the table table_name does not have a row where colname = value, the block will be executed.
*
* #IfNotRow2D
* arguments: table_name colname value colname2 value2
* behavior: If the table table_name does not have a row where colname = value AND colname2 = value2, the block will be executed.
*
* #IfNotRow3D
* arguments: table_name colname value colname2 value2 colname3 value3
* behavior: If the table table_name does not have a row where colname = value AND colname2 = value2 AND colname3 = value3, the block will be executed.
*
* #IfNotRow4D
* arguments: table_name colname value colname2 value2 colname3 value3 colname4 value4
* behavior: If the table table_name does not have a row where colname = value AND colname2 = value2 AND colname3 = value3 AND colname4 = value4, the block will be executed.
*
* #IfNotRow2Dx2
* desc: This is a very specialized function to allow adding items to the list_options table to avoid both redundant option_id and title in each element.
* arguments: table_name colname value colname2 value2 colname3 value3
* behavior: The block will be executed if both statements below are true:
* 1) The table table_name does not have a row where colname = value AND colname2 = value2.
* 2) The table table_name does not have a row where colname = value AND colname3 = value3.
*
* #IfRow2D
* arguments: table_name colname value colname2 value2
* behavior: If the table table_name does have a row where colname = value AND colname2 = value2, the block will be executed.
*
* #IfRow3D
* arguments: table_name colname value colname2 value2 colname3 value3
* behavior: If the table table_name does have a row where colname = value AND colname2 = value2 AND colname3 = value3, the block will be executed.
*
* #IfIndex
* desc: This function is most often used for dropping of indexes/keys.
* arguments: table_name colname
* behavior: If the table and index exist the relevant statements are executed, otherwise not.
*
* #IfNotIndex
* desc: This function will allow adding of indexes/keys.
* arguments: table_name colname
* behavior: If the index does not exist, it will be created
*
* #IfNotMigrateClickOptions
* Custom function for the importing of the Clickoptions settings (if exist) from the codebase into the database
*
* #IfNotListOccupation
* Custom function for creating Occupation List
*
* #IfNotListReaction
* Custom function for creating Reaction List
*
* #EndIf
* all blocks are terminated with a #EndIf statement.
*
* @param string $filename Sql upgrade/patch filename
*/
function upgradeFromSqlFile($filename) {
global $webserver_root;
flush();
echo "<font color='green'>Processing $filename ...</font><br />\n";
$fullname = "$webserver_root/sql/$filename";
$fd = fopen($fullname, 'r');
if ($fd == FALSE) {
echo "ERROR. Could not open '$fullname'.\n";
flush();
}
$query = "";
$line = "";
$skipping = false;
while (!feof ($fd)){
$line = fgets($fd, 2048);
$line = rtrim($line);
if (preg_match('/^\s*--/', $line)) continue;
if ($line == "") continue;
if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
$skipping = tableExists($matches[1]);
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
$skipping = ! tableExists($matches[1]);
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = !columnExists($matches[1], $matches[2]);
}
else {
// If no such table then the column is deemed "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = columnExists($matches[1], $matches[2]);
}
else {
// If no such table then the column is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = columnHasType($matches[1], $matches[2], $matches[3]);
}
else {
// If no such table then the column type is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfIndex\s+(\S+)\s+(\S+)/', $line, $matches)) {
if (tableExists($matches[1])) {
// If no such index then skip.
$skipping = !tableHasIndex($matches[1], $matches[2]);
}
else {
// If no such table then skip.
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotIndex\s+(\S+)\s+(\S+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = tableHasIndex($matches[1], $matches[2]);
}
else {
// If no such table then the index is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
}
else {
// If no such table then the row is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
}
else {
// If no such table then the row is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotRow3D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = tableHasRow3D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7]);
}
else {
// If no such table then the row is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotRow4D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = tableHasRow4D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7], $matches[8], $matches[9]);
}
else {
// If no such table then the row is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
// If either check exist, then will skip
$firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
$secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
if ($firstCheck || $secondCheck) {
$skipping = true;
}
else {
$skipping = false;
}
}
else {
// If no such table then the row is deemed not "missing".
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = !(tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]));
}
else {
// If no such table then should skip.
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfRow3D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
if (tableExists($matches[1])) {
$skipping = !(tableHasRow3D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7]));
}
else {
// If no such table then should skip.
$skipping = true;
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotListOccupation/', $line)) {
if ( (listExists("Occupation")) || (!columnExists('patient_data','occupation')) ) {
$skipping = true;
}
else {
// Create Occupation list
CreateOccupationList();
$skipping = false;
echo "<font color='green'>Built Occupation List</font><br />\n";
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotListReaction/', $line)) {
if ( (listExists("reaction")) || (!columnExists('lists','reaction')) ) {
$skipping = true;
}
else {
// Create Reaction list
CreateReactionList();
$skipping = false;
echo "<font color='green'>Built Reaction List</font><br />\n";
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#IfNotListImmunizationManufacturer/', $line)){
if ( listExists("Immunization_Manufacturer") ) {
$skipping = true;
}
else {
// Create Immunization Manufacturer list
CreateImmunizationManufacturerList();
$skipping = false;
echo "<font color='green'>Built Immunization Manufacturer List</font><br />\n";
}
if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
}
else if (preg_match('/^#EndIf/', $line)) {
$skipping = false;
}
if (preg_match('/^\s*#/', $line)) continue;
if ($skipping) continue;
$query = $query . $line;
if (substr($query, -1) == ';') {
$query = rtrim($query, ';');
echo "$query<br />\n";
if (!sqlStatement($query)) {
echo "<font color='red'>The above statement failed: " .
getSqlLastError() . "<br />Upgrading will continue.<br /></font>\n";
}
$query = '';
}
}
flush();
} // end function
?>