Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_utils_SimpleSQLParser | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |
0.00% |
0 / 1 |
| parse | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |||
| 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 | * A simple XML parser that is able to parse SQL files with Simple statements. |
| 29 | * Please note that complex statements like Procedure or Functions are not |
| 30 | * supported by this implementation. |
| 31 | * |
| 32 | * @author Jerome BOGAERTS <jerome.bogaerts@tudor.lu> |
| 33 | * @package tao |
| 34 | |
| 35 | */ |
| 36 | class tao_install_utils_SimpleSQLParser extends tao_install_utils_SQLParser |
| 37 | { |
| 38 | /** |
| 39 | * Parses a SQL file containing simple statements. |
| 40 | * @return void |
| 41 | * @throws tao_install_utils_SQLParsingException |
| 42 | */ |
| 43 | public function parse() |
| 44 | { |
| 45 | $this->setStatements([]); |
| 46 | |
| 47 | //common file checks |
| 48 | $file = $this->getFile(); |
| 49 | if (!file_exists($file)) { |
| 50 | throw new tao_install_utils_SQLParsingException("SQL file '${file}' does not exist."); |
| 51 | } elseif (!is_readable($file)) { |
| 52 | throw new tao_install_utils_SQLParsingException("SQL file '${file}' is not readable."); |
| 53 | } elseif (!preg_match("/\.sql$/", basename($file))) { |
| 54 | throw new tao_install_utils_SQLParsingException( |
| 55 | "File '${file}' is not a valid SQL file. Extension '.sql' not found." |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | if ($handler = fopen($file, "r")) { |
| 60 | //parse file and get only usefull lines |
| 61 | $ch = ""; |
| 62 | while (!feof($handler)) { |
| 63 | $line = utf8_decode(fgets($handler)); |
| 64 | |
| 65 | if (isset($line[0]) && ($line[0] != '#') && ($line[0] != '-')) { |
| 66 | $ch = $ch . $line; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | //explode and execute |
| 71 | $requests = explode(";", $ch); |
| 72 | |
| 73 | try { |
| 74 | foreach ($requests as $index => $request) { |
| 75 | $requestTrim = trim($request); |
| 76 | if (!empty($requestTrim)) { |
| 77 | $this->addStatement($request); |
| 78 | } |
| 79 | } |
| 80 | } catch (Exception $e) { |
| 81 | throw new tao_install_utils_SQLParsingException( |
| 82 | "Error executing query #$index : $request . " . $e->getMessage() |
| 83 | ); |
| 84 | } |
| 85 | fclose($handler); |
| 86 | } |
| 87 | } |
| 88 | } |