orangehrm /lib/utils/ConstraintHandler.php

Language PHP Lines 435
MD5 Hash 093e0774e26459281606ab39041b6e25 Estimated Cost $6,328 (why?)
Repository https://bitbucket.org/wildanm/orangehrm View Raw File View Project SPDX
  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
<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM 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.
 *
 * OrangeHRM 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */

/*
 * Class that is used by upgrader to apply foreign key constraints to the database.
 * Handles failures when applying constraints and attempts to recover by restoring
 * referential integrity to the database.
 *
 * NOTE: This class does not attempt to connect to mysql.
 */
class ConstraintHandler {

	const TABLE_NDX = 0;
	const FIELDS_NDX = 1;
	const CHILD_TABLE_NDX = 2;
	const CHILD_FIELDS_NDX = 3;
	const DEL_CLAUSE_NDX = 4;

	protected $logFile;

	/**
	 * Set the file to log to. If not set, no logging is done
	 */
	public function setLogFile($logFile) {
		$this->logFile = $logFile;
	}

	/**
	 * Get the log file.
	 */
	public function getLogFile() {
		return $this->logFile;
	}

	/**
	 * Apply the supplied array of constraints to the database.
	 *
	 * TODO: Pass an array of Constraint objects. But this would make
	 * dbscripts/constrainst.php a bit more difficult to read.
	 *
	 * @param array $constraints Array of constraints
	 * @param string $database The database to connect to.
	 *
	 * @return array Array of failed constraints. Empty array if none failed.
	 */
	public function applyConstraints($constraints, $database = null) {
		$maxTries = 10;
		$tries = 0;
		$cleaned = 0;

		do {
			$cleaned = $this->_cleanup($constraints);
			$tries++;
		} while (($tries < $maxTries) && ($cleaned > 0));

		// Handle case where unable to clean up
		if ($cleaned > 0) {
			$this->_log("Some constraint failures may not have been cleaned (after {$maxTries} tries)");
		}

		$failedConstraints = array();

		foreach ($constraints as $constraint) {

			$result = $this->_addConstraint($constraint);
			if (!$result) {
				$failedConstraints[] = $constraint;
			}
		}

		return $failedConstraints;
	}

	/**
	 * Get Alter table SQL to apply the given constraint.
	 * (Made public for ease of testing)
	 *
	 * @param string constraint apply constraint
	 * @return string Alter table statement to apply constraint to table
	 */
	public function getConstraintSQL($constraint) {

		$table = $constraint[ConstraintHandler::TABLE_NDX];
		$fields = implode(",", $constraint[ConstraintHandler::FIELDS_NDX]);
		$childTable = $constraint[ConstraintHandler::CHILD_TABLE_NDX];
		$childFields = implode(",", $constraint[ConstraintHandler::CHILD_FIELDS_NDX]);
		$onDelete = $constraint[ConstraintHandler::DEL_CLAUSE_NDX];

		$delClause = "";
		switch ($onDelete) {
			case "null":
				$delClause = "ON DELETE SET NULL";
				break;
			case "cascade":
				$delClause = "ON DELETE CASCADE";
				break;
			case "restrict":
				$delClause = "ON DELETE RESTRICT";
				break;
			default:
				$delClause = "";
				break;
		}

		$sql = sprintf("ALTER TABLE %s ADD CONSTRAINT FOREIGN KEY (%s) REFERENCES %s(%s) %s",
						$table, $fields, $childTable, $childFields, $delClause);
		return $sql;
	}

	/**
	 * Add the given constraint to the database
	 * @param array $constraint Constraint as an array
	 * @return boolean True if successfully added. False if failed to add
	 */
	private function _addConstraint($constraint) {

		$sql = $this->getConstraintSQL($constraint);
		$result = mysql_query($sql);
		if ($result) {
			return true;
		} else {
			$this->_log("Failed to apply constraint: $sql");
			return false;
		}
	}

	/**
	 * Checks if any data exists that violate the given constraints and
	 * attempt to clean them up.
	 *
	 * @param string $constraints The constraints to cleanup
	 * @return int Number of constraints which needed to be cleaned
	 */
	private function _cleanup($constraints) {

		$cleaned = 0;
		foreach ($constraints as $constraint) {

			$type = $constraint[self::DEL_CLAUSE_NDX];

			$fields = $constraint[ConstraintHandler::FIELDS_NDX];
			$childFields = $constraint[ConstraintHandler::CHILD_FIELDS_NDX];
			if (count($fields) != count($childFields)) {
				throw new ConstraintHandlerException("Invalid constraint: ". getConstraintSQL($constraint));
			}

			switch ($type) {
				case "null":
					$result = $this->_cleanSetNullConstraint($constraint);
					break;
				case "cascade":
					$result = $this->_cleanCascadeConstraint($constraint);
					break;
				case "restrict":
					$result = $this->_cleanRestrictConstraint($constraint);
					break;
				case null:
					$result = $this->_cleanEmptyConstraint($constraint);
					break;
				default:
					/* Unsupported */
					$this->_log("Unsupported constraint type: $type");
					$result = false;
					break;
			}

			if ($result) {
				$cleaned++;
			}
		}

		return $cleaned;
	}

	/**
	 * Attempts to clean up the given constraint
	 *
	 * TODO: Change naming. child actually refers to parent table
	 *
	 * @param array Constraint
	 * @return int number of rows affected (cleaned).
	 */
	private function _cleanSetNullConstraint($constraint) {

		$numAffected = 0;

		$table = $constraint[ConstraintHandler::TABLE_NDX];
		$fields = $constraint[ConstraintHandler::FIELDS_NDX];
		$childTable = $constraint[ConstraintHandler::CHILD_TABLE_NDX];
		$childFields = $constraint[ConstraintHandler::CHILD_FIELDS_NDX];

		for ($i=0; $i< count($fields); $i++) {
			$field = $fields[$i];
			$childField = $childFields[$i];

			$setArray[] = "`$field` = NULL";
			$checkNullArray[] = " (`{$table}`.`{$field}` IS NULL)";
			$whereArray[] = "`$table`.`$field` = `$childTable`.`$childField`";
		}
		$setClause = implode(",", $setArray);
		$checkNull = implode(" OR ", $checkNullArray);
		$whereClause = implode(" AND ", $whereArray);

		/* Self references need to be handled differently since we can't select and update
		 * from the same table in a multi table update.
		 */
		if ($table == $childTable) {

			/* This happens only for hs_hr_users - modified_user_id and created_by fields.
			 * So we are considering only the case where one field is in the constraint.
			 */
			if (count($fields) == 1) {

				$field = $fields[0];
				$childField = $childFields[0];
 				$sql = "SELECT DISTINCT A.`{$field}` FROM `{$table}` As A WHERE A.`{$field}` IS NOT NULL AND A.`{$field}` NOT IN " .
 						"(SELECT B.id FROM `{$table}` as B)";
				$result = mysql_query($sql);
				if ($result) {
					if (mysql_num_rows($result) > 0) {
						while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
							$ids[] = "'" . $row[0] . "'";
						}

						$idList = implode(",", $ids);
						$sql = "UPDATE `{$table}` SET `{$field}` = NULL WHERE `{$field}` IN ({$idList})";
						$result = mysql_query($sql);
						if (!$result) {
							throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
						}
						$numAffected = mysql_affected_rows();
					}
				} else {
					throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
				}
			}

		} else {

			$sql = sprintf("UPDATE `{$table}` SET {$setClause} WHERE NOT ({$checkNull}) AND NOT EXISTS(SELECT 1 FROM `{$childTable}` " .
					" WHERE {$whereClause} )");
			$result = mysql_query($sql);
			if (!$result) {
				throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
			}

			$numAffected = mysql_affected_rows();
		}

		if ($numAffected > 0) {
			$this->_log($sql . "NUM = " . $numAffected);
		}
		return $numAffected;
	}

	/**
	 * Attempts to clean up the given constraint
	 *
	 * @param array Constraint
	 * @return int number of rows affected (cleaned).
	 */
	private function _cleanCascadeConstraint($constraint) {
		$numAffected = 0;

		$table = $constraint[ConstraintHandler::TABLE_NDX];
		$fields = $constraint[ConstraintHandler::FIELDS_NDX];
		$childTable = $constraint[ConstraintHandler::CHILD_TABLE_NDX];
		$childFields = $constraint[ConstraintHandler::CHILD_FIELDS_NDX];

		for ($i=0; $i< count($fields); $i++) {
			$field = $fields[$i];
			$childField = $childFields[$i];
			$checkNullArray[] = " (`{$table}`.`{$field}` IS NULL)";
			$whereArray[] = "`$table`.`$field` = `$childTable`.`$childField`";
		}
		$checkNull = implode(" OR ", $checkNullArray);
		$whereClause = implode(" AND ", $whereArray);

		$countSql = "SELECT COUNT(*) FROM `{$childTable}`";
		$result = mysql_query($countSql);
		if (!$result) {
			throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
		}
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$count = $row[0];

		/* If no parents all the children have to be deleted */
		if ($count == 0) {
			$sql = sprintf("DELETE FROM `{$table}` WHERE NOT ({$checkNull})");
		} else {

			$sql = sprintf("DELETE FROM `{$table}` USING `{$table}`, `{$childTable}` WHERE NOT ({$checkNull}) AND NOT EXISTS(SELECT 1 FROM `{$childTable}` " .
					" WHERE {$whereClause} )");
		}
		$result = mysql_query($sql);

		if (!$result) {
			throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
		}

		$numAffected = mysql_affected_rows();
		if ($numAffected > 0) {
			$this->_log($sql . "NUM = " . $numAffected);
		}
		return $numAffected;
	}

	/**
	 * Attempts to clean up the given constraint
	 *
	 * @param array Constraint
	 * @return int number of rows affected (cleaned).
	 */
	private function _cleanRestrictConstraint($constraint) {

		/*
		 * The only restrict constraints are hs_hr_project and hs_hr_compstructtree
		 * Entries in hs_hr_project are not deleted, in hs_hr_compstructtree
		 * loc_code can be set to null (but shouldn't be necessary
		 * since we prevent deletion of locations if in use),
		 * so consider this the same as a null constraint.
		 */
		$numAffected = $this->_cleanSetNullConstraint($constraint);
		return $numAffected;
	}

	/**
	 * Attempts to clean up the given constraint
	 *
	 * @param array Constraint
	 * @return int number of rows affected (cleaned).
	 */
	private function _cleanEmptyConstraint($constraint) {
		$numAffected = 0;

		/* Not implemented yet. We don't have empty constraints */
		return $numAffected;
	}

	/**
	 * Verifies that the given constraints have been set in the database.
	 * @param array $constraints constraint array.
	 *
	 * @return array Array of constraints that were missing.
	 */
	public function getMissingConstraints($constraints) {

		$failedConstraints = array();

		foreach ($constraints as $constraint) {

			$found = false;

			$table = $constraint[ConstraintHandler::TABLE_NDX];
			$fields = implode("[\s,`]+", $constraint[ConstraintHandler::FIELDS_NDX]);
			$childTable = $constraint[ConstraintHandler::CHILD_TABLE_NDX];
			$childFields = implode("[\s,`]+", $constraint[ConstraintHandler::CHILD_FIELDS_NDX]);
			$onDelete = $constraint[ConstraintHandler::DEL_CLAUSE_NDX];

			$delClause = "ON\s+DELETE";
			switch ($onDelete) {
				case "null":
					$delClause = "ON\s+DELETE\s+SET\s+NULL";
					break;
				case "cascade":
					$delClause = "ON\s+DELETE\s+CASCADE";
					break;
				default:
					$delClause = "";
				break;
			}

			$sql = "SHOW CREATE TABLE $table";
			$result = mysql_query($sql);

			if ($result === false) {
				throw new ConstraintHandlerException("Error when running query: $sql. MysqlError:" . mysql_error());
			}

			$row = mysql_fetch_array($result, MYSQL_NUM);
			$createTable = $row[1];
			$lines = explode("\n", $createTable);

			foreach ($lines as $line) {
				$regexp = "/\w*CONSTRAINT\b.*\bFOREIGN\s+KEY[\s(`]+{$fields}[\s)`]+REFERENCES[\s`)]+{$childTable}[\s`(]+{$childFields}[\s`)]+{$delClause}.*/";
				$count = preg_match($regexp, $line);

				if ($count === 1) {
					$found = true;
					break;
				}
			}

			if (!$found) {
				$failedConstraints[] = $constraint;
			}
		}

		return $failedConstraints;
	}

	/**
	 * Log the given message to the log file
	 * @param string Log message
	 *
	 */
	private function _log($message) {
		if (!empty($this->logFile)) {
			error_log (date("r") . " " . $message ."\n", 3, $this->logFile);
		}
	}
}

class ConstraintHandlerException extends Exception {

}
?>
Back to Top