salimkanoun /
GaelO
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | Copyright (C) 2018-2020 KANOUN Salim |
||
| 5 | This program is free software; you can redistribute it and/or modify |
||
| 6 | it under the terms of the Affero GNU General Public v.3 License as published by |
||
| 7 | the Free Software Foundation; |
||
| 8 | This program is distributed in the hope that it will be useful, |
||
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 11 | Affero GNU General Public Public for more details. |
||
| 12 | You should have received a copy of the Affero GNU General Public Public along |
||
| 13 | with this program; if not, write to the Free Software Foundation, Inc., |
||
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||
| 15 | */ |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Performs DB Structure Alteration for the specific forms |
||
| 19 | */ |
||
| 20 | class Visit_Builder |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Get specific PDO object in order to perform db table alteration |
||
| 25 | * on the visit type tables |
||
| 26 | * @return PDO |
||
| 27 | */ |
||
| 28 | private static function getLinkpdo(): PDO |
||
| 29 | { |
||
| 30 | return Session::getLinkpdo(); |
||
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Check if the specific db table of a given visit type is empty |
||
| 36 | * @param Visit_Type $vt |
||
| 37 | * @return boolean |
||
| 38 | */ |
||
| 39 | public static function isTableEmpty(Visit_Type $vt): bool |
||
| 40 | { |
||
| 41 | $linkpdo=Visit_Builder::getLinkpdo(); |
||
| 42 | $table=$vt->tableReviewSpecificName; |
||
| 43 | $pdoSt=$linkpdo->query('SELECT * FROM '.$table.';'); |
||
| 44 | return count($pdoSt->fetchAll()) == 0; |
||
| 45 | } |
||
| 46 | |||
| 47 | public static function dropColumn(Visit_Type $vt, string $column) : PDOStatement |
||
| 48 | { |
||
| 49 | $linkpdo=Visit_Builder::getLinkpdo(); |
||
| 50 | $table=$vt->tableReviewSpecificName; |
||
| 51 | $sql='ALTER TABLE `'.$table.'` DROP COLUMN `'.$column.'`;'; |
||
| 52 | return $linkpdo->query($sql); |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | public static function alterColumn(Visit_Type $vt, $columnNameBefore, $columnNameAfter, $dataType) : PDOStatement |
||
| 57 | { |
||
| 58 | $linkpdo=Visit_Builder::getLinkpdo(); |
||
| 59 | $table=$vt->tableReviewSpecificName; |
||
| 60 | $sql='ALTER TABLE `'.$table.'` CHANGE `'.$columnNameBefore.'` `'.$columnNameAfter.'` '.$dataType.';'; |
||
| 61 | return $linkpdo->query($sql); |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | public static function addColumn(Visit_Type $vt, $columnName, $dataType) : PDOStatement |
||
| 66 | { |
||
| 67 | $linkpdo=Visit_Builder::getLinkpdo(); |
||
| 68 | $table=$vt->tableReviewSpecificName; |
||
| 69 | $sql='ALTER TABLE `'.$table.'` ADD `'.$columnName.'` '.$dataType.';'; |
||
| 70 | return $linkpdo->query($sql); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Format mysql data type string for mysql query |
||
| 76 | * @param string $type mysql data type without parameters (e.g. 'decimal') |
||
| 77 | * @param array|null $typeParam list of the data type paramaters |
||
| 78 | * @return string mysql column datatype |
||
| 79 | */ |
||
| 80 | public static function formatDataType(string $typeLabel, $typeParams): string |
||
| 81 | { |
||
| 82 | $res=''; |
||
| 83 | switch ($typeLabel) { |
||
| 84 | case 'int': |
||
| 85 | $res='INT(11)'; |
||
| 86 | break; |
||
| 87 | case 'tinyint': |
||
| 88 | $res='TINYINT(1)'; |
||
| 89 | break; |
||
| 90 | case 'tinytext': |
||
| 91 | $res='TINYTEXT'; |
||
| 92 | break; |
||
| 93 | case 'date': |
||
| 94 | $res='DATE'; |
||
| 95 | break; |
||
| 96 | case 'varchar': |
||
| 97 | $param=Visit_Builder::escape($typeParams[0]); |
||
| 98 | // Format params array into string e.g. '(123)' |
||
| 99 | $res='VARCHAR('.$param.')'; |
||
| 100 | break; |
||
| 101 | case 'decimal': |
||
| 102 | $param1=Visit_Builder::escape($typeParams[0]); |
||
| 103 | $param2=Visit_Builder::escape($typeParams[1]); |
||
| 104 | // Format params array into string e.g. '(12,3)' |
||
| 105 | $res='DECIMAL('.$param1.','.$param2.')'; |
||
| 106 | break; |
||
| 107 | case 'enum': |
||
| 108 | $params=[]; |
||
| 109 | foreach ($typeParams as $tp) { |
||
| 110 | array_push($params, Visit_Builder::escape($tp)); |
||
| 111 | } |
||
| 112 | // Format params array into string e.g. '("abc","def","ghi")' |
||
| 113 | $res='ENUM("'.implode('","', $params).'")'; |
||
| 114 | break; |
||
| 115 | default: |
||
| 116 | throw new Exception('Unknown datatype'); |
||
| 117 | } |
||
| 118 | return $res; |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the mysql data type label of a string containing a mysql data |
||
| 124 | * type label (e.g. 'decimal') and data type parameter (e.g. '(10,2)') |
||
| 125 | * @param string $type |
||
| 126 | * @return string data typpe label |
||
| 127 | */ |
||
| 128 | public static function extractDataTypeLabel(string $datatype): string |
||
| 129 | { |
||
| 130 | return substr($datatype, 0, strpos($datatype, '(')); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Get mysql column data type of a given column from a given visit type |
||
| 136 | * @param Visit_Type $vt |
||
| 137 | * @param string $columnName |
||
| 138 | * @return string mysql data type with parameters |
||
| 139 | */ |
||
| 140 | public static function getColumnDataType(Visit_Type $vt, string $columnName): string |
||
| 141 | { |
||
| 142 | $columns=$vt->getSpecificTableInputType(); |
||
| 143 | foreach ($columns as $c) { |
||
| 144 | // Retrieving original data type |
||
| 145 | if ($c['COLUMN_NAME'] == $columnName) { |
||
| 146 | return $c['COLUMN_TYPE']; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | throw new Exception('Cannot find column '.$columnName.' for visit type '.$vt); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Escape special chars from a given string |
||
| 155 | * @param string $s string to escape chars from |
||
| 156 | * @return string escaped string |
||
| 157 | */ |
||
| 158 | public static function escape(string $s): string |
||
| 159 | { |
||
| 160 | return preg_replace('/[^A-Za-z0-9\-_]/', '', $s); |
||
| 161 | } |
||
| 162 | } |
||
| 163 |