Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_utils_MysqlProceduresParser | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
| parse | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 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; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * This SQL Parser is able to deal with Stored Procedures and Functions |
| 29 | * for mySQL server in a compliant SQL file. The following statements are supported. |
| 30 | * |
| 31 | * - DROP PROCEDURE |
| 32 | * - DROP FUNCTION |
| 33 | * - CREATE PROCEDURE |
| 34 | * - CREATE FUNCTION |
| 35 | * |
| 36 | * @author Jerome BOGAERTS <jerome.bogaerts@tudor.lu> |
| 37 | * @package tao |
| 38 | |
| 39 | * |
| 40 | */ |
| 41 | class tao_install_utils_MysqlProceduresParser extends tao_install_utils_SQLParser |
| 42 | { |
| 43 | /** |
| 44 | * Parse a SQL file containing mySQL compliant Procedures or Functions. |
| 45 | * @return void |
| 46 | * @throws tao_install_utils_SQLParsingException |
| 47 | */ |
| 48 | public function parse() |
| 49 | { |
| 50 | $this->setStatements([]); |
| 51 | $file = $this->getFile(); |
| 52 | |
| 53 | if (!file_exists($file)) { |
| 54 | throw new tao_install_utils_SQLParsingException("SQL file '${file}' does not exist."); |
| 55 | } elseif (!is_readable($file)) { |
| 56 | throw new tao_install_utils_SQLParsingException("SQL file '${file}' is not readable."); |
| 57 | } elseif (!preg_match("/\.sql$/", basename($file))) { |
| 58 | throw new tao_install_utils_SQLParsingException( |
| 59 | "File '${file}' is not a valid SQL file. Extension '.sql' not found." |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | $content = @file_get_contents($file); |
| 64 | if ($content !== false) { |
| 65 | $matches = []; |
| 66 | $patterns = [ |
| 67 | 'DROP\s+FUNCTION\s+IF\s+EXISTS\s*\w+\s*;', |
| 68 | 'DROP\s+PROCEDURE\s+IF\s+EXISTS\s*\w+\s*;', |
| 69 | 'CREATE\s+PROCEDURE\s+\w+\s*\(.*\)\s*(?:(?:NOT\s+){0,1}DETERMINISTIC){0,1}\s*BEGIN\s+(?:.*\s*;\s*)*END' |
| 70 | . '\s*;', |
| 71 | 'CREATE\s+(DEFINER = \w+\s+)FUNCTION\s+\w+\s*\(.*\)\s*RETURNS\s+(?:.*)\s*(?:(?:NOT\s+){0,1}' |
| 72 | . 'DETERMINISTIC){0,1}\s*(?:CONTAINS SQL\s+|NO SQL\s+|READS SQL DATA\s+|MODIFIES SQL DATA\s+|SQL ' |
| 73 | . 'SECURITY INVOKER\s+)*\s*BEGIN\s+(?:.*\s*;\s*)*END\s*;' |
| 74 | ]; |
| 75 | |
| 76 | if (preg_match_all('/' . implode($patterns, '|') . '/i', $content, $matches)) { |
| 77 | foreach ($matches[0] as $match) { |
| 78 | $this->addStatement($match); |
| 79 | } |
| 80 | } |
| 81 | } else { |
| 82 | throw new tao_install_utils_SQLParsingException( |
| 83 | "SQL file '${file}' cannot be read. An unknown error occured while reading it." |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | } |