support spaces and colons as modifier separators, as suggest by yehster
[openemr.git] / library / sql_upgrade_fx.php
blobac265a53b6889fb16fde1b684680385d63dada95
1 <?php
2 // Copyright (C) 2008-2011 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // Functions to allow safe database and global modifications
10 // during upgrading and patches
13 function tableExists($tblname) {
14 $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
15 if (empty($row)) return false;
16 return true;
19 function columnExists($tblname, $colname) {
20 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
21 if (empty($row)) return false;
22 return true;
25 function columnHasType($tblname, $colname, $coltype) {
26 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
27 if (empty($row)) return true;
28 return (strcasecmp($row['Type'], $coltype) == 0);
31 function tableHasRow($tblname, $colname, $value) {
32 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
33 "$colname LIKE '$value'");
34 return $row['count'] ? true : false;
37 function tableHasRow2D($tblname, $colname, $value, $colname2, $value2) {
38 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
39 "$colname LIKE '$value' AND $colname2 LIKE '$value2'");
40 return $row['count'] ? true : false;
43 function tableHasRow3D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3) {
44 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
45 "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3'");
46 return $row['count'] ? true : false;
49 function tableHasRow4D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3, $colname4, $value4) {
50 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
51 "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3' AND $colname4 LIKE '$value4'");
52 return $row['count'] ? true : false;
55 function tableHasIndex($tblname, $colname) {
56 $row = sqlQuery("SHOW INDEX FROM `$tblname` WHERE `Key_name` = '$colname'");
57 return (empty($row)) ? false : true;
60 function upgradeFromSqlFile($filename) {
61 global $webserver_root;
63 flush();
64 echo "<font color='green'>Processing $filename ...</font><br />\n";
66 $fullname = "$webserver_root/sql/$filename";
68 $fd = fopen($fullname, 'r');
69 if ($fd == FALSE) {
70 echo "ERROR. Could not open '$fullname'.\n";
71 flush();
72 break;
75 $query = "";
76 $line = "";
77 $skipping = false;
79 while (!feof ($fd)){
80 $line = fgets($fd, 2048);
81 $line = rtrim($line);
83 if (preg_match('/^\s*--/', $line)) continue;
84 if ($line == "") continue;
86 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
87 $skipping = tableExists($matches[1]);
88 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
90 else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
91 $skipping = ! tableExists($matches[1]);
92 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
94 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
95 if (tableExists($matches[1])) {
96 $skipping = columnExists($matches[1], $matches[2]);
98 else {
99 // If no such table then the column is deemed not "missing".
100 $skipping = true;
102 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
104 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
105 if (tableExists($matches[1])) {
106 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
108 else {
109 // If no such table then the column type is deemed not "missing".
110 $skipping = true;
112 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
114 else if (preg_match('/^#IfNotIndex\s+(\S+)\s+(\S+)/', $line, $matches)) {
115 if (tableExists($matches[1])) {
116 $skipping = tableHasIndex($matches[1], $matches[2]);
118 else {
119 // If no such table then the index is deemed not "missing".
120 $skipping = true;
122 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
124 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
125 if (tableExists($matches[1])) {
126 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
128 else {
129 // If no such table then the row is deemed not "missing".
130 $skipping = true;
132 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
134 else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
135 if (tableExists($matches[1])) {
136 $skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
138 else {
139 // If no such table then the row is deemed not "missing".
140 $skipping = true;
142 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
144 else if (preg_match('/^#IfNotRow3D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
145 if (tableExists($matches[1])) {
146 $skipping = tableHasRow3D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7]);
148 else {
149 // If no such table then the row is deemed not "missing".
150 $skipping = true;
152 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
154 else if (preg_match('/^#IfNotRow4D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
155 if (tableExists($matches[1])) {
156 $skipping = tableHasRow4D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7], $matches[8], $matches[9]);
158 else {
159 // If no such table then the row is deemed not "missing".
160 $skipping = true;
162 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
164 else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
165 if (tableExists($matches[1])) {
166 // If either check exist, then will skip
167 $firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
168 $secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
169 if ($firstCheck || $secondCheck) {
170 $skipping = true;
172 else {
173 $skipping = false;
176 else {
177 // If no such table then the row is deemed not "missing".
178 $skipping = true;
180 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
182 else if (preg_match('/^#EndIf/', $line)) {
183 $skipping = false;
186 if (preg_match('/^\s*#/', $line)) continue;
187 if ($skipping) continue;
189 $query = $query . $line;
190 if (substr($query, -1) == ';') {
191 $query = rtrim($query, ';');
192 echo "$query<br />\n";
193 if (!sqlStatement($query)) {
194 echo "<font color='red'>The above statement failed: " .
195 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
197 $query = '';
200 flush();
201 } // end function