|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace XoopsModules\Wggithub\Common; |
| 3 | +namespace XoopsModules\Wgtransifex\Common; |
4 | 4 |
|
5 | 5 | /* |
6 | 6 | You may not change or alter any portion of this comment or credits |
@@ -79,34 +79,53 @@ public function createSchemaFromSqlfile(): bool |
79 | 79 | } |
80 | 80 | } |
81 | 81 |
|
| 82 | + $skip = true; |
| 83 | + $skipWords = ['CREATE DATABASE ', 'CREATE VIEW ', 'INSERT INTO ', 'SELECT ', 'DELETE ', 'UPDATE ', 'ALTER ', 'DROP ']; |
| 84 | + $options = ''; |
82 | 85 | // read remaining lines line by line and create new schema |
83 | 86 | foreach ($lines as $key => $value) { |
84 | 87 | $line = \trim($value); |
| 88 | + foreach ($skipWords as $skipWord) { |
| 89 | + if ($skipWord === \mb_strtoupper(\substr($line, 0, \strlen($skipWord)))) { |
| 90 | + $skip = true; |
| 91 | + } |
| 92 | + } |
85 | 93 | if ('CREATE TABLE' === \mb_strtoupper(\substr($line, 0, 12))) { |
| 94 | + $skip = false; |
| 95 | + $options = ''; |
86 | 96 | // start table definition |
87 | 97 | $tableName = $this->getTableName ($line); |
88 | 98 | $tables[$tableName] = []; |
89 | 99 | $tables[$tableName]['options'] = ''; |
90 | 100 | $tables[$tableName]['columns'] = []; |
91 | 101 | $tables[$tableName]['keys'] = []; |
92 | 102 | } else { |
93 | | - if (')' === \mb_strtoupper(\substr($line, 0, 1))) { |
94 | | - // end of table definition |
95 | | - // get options |
96 | | - $tables[$tableName]['options'] = $this->getOptions($line); |
97 | | - } else { |
98 | | - // get keys and fields |
99 | | - switch (\mb_strtoupper(\substr($line, 0, 3))) { |
100 | | - case 'KEY': |
101 | | - case 'PRI': |
102 | | - case 'UNI': |
103 | | - $tables[$tableName]['keys'][] = $this->getKey($line); |
104 | | - break; |
105 | | - case 'else': |
106 | | - default: |
107 | | - $columns = $this->getColumns($line); |
108 | | - $tables[$tableName]['columns'][] = $columns; |
109 | | - break; |
| 103 | + if (false == $skip) { |
| 104 | + if (')' === \mb_strtoupper(\substr($line, 0, 1))) { |
| 105 | + // end of table definition |
| 106 | + // get options |
| 107 | + $this->getOptions($line, $options); |
| 108 | + $tables[$tableName]['options'] = $options; |
| 109 | + } elseif ('ENGINE ' === \mb_strtoupper(\substr($line, 0, 7))) { |
| 110 | + $this->getOptions($line, $options); |
| 111 | + $tables[$tableName]['options'] = $options; |
| 112 | + } elseif ('DEFAULT CHARSET ' === \mb_strtoupper(\substr($line, 0, 16))) { |
| 113 | + $this->getOptions($line, $options); |
| 114 | + $tables[$tableName]['options'] = $options; |
| 115 | + } else { |
| 116 | + // get keys and fields |
| 117 | + switch (\mb_strtoupper(\substr($line, 0, 3))) { |
| 118 | + case 'KEY': |
| 119 | + case 'PRI': |
| 120 | + case 'UNI': |
| 121 | + $tables[$tableName]['keys'][] = $this->getKey($line); |
| 122 | + break; |
| 123 | + case 'else': |
| 124 | + default: |
| 125 | + $columns = $this->getColumns($line); |
| 126 | + $tables[$tableName]['columns'][] = $columns; |
| 127 | + break; |
| 128 | + } |
110 | 129 | } |
111 | 130 | } |
112 | 131 | } |
@@ -212,24 +231,27 @@ private function getColumns (string $line) |
212 | 231 | * Extract options of table from given line |
213 | 232 | * |
214 | 233 | * @param string $line |
215 | | - * @return string |
| 234 | + * @param string $options |
| 235 | + * @return void |
216 | 236 | */ |
217 | | - private function getOptions (string $line): string |
| 237 | + private function getOptions (string $line, string &$options): void |
218 | 238 | { |
219 | 239 |
|
220 | | - $options = \str_replace([')', ';'], '', $line); |
221 | | - $options = \trim($options); |
222 | | - $options = "'" . $options . "'"; |
223 | | - |
224 | | - return $options; |
| 240 | + $lineText = \trim(\str_replace([')', ';'], '', $line)); |
| 241 | + // remove all existing ' |
| 242 | + $options = \str_replace("'", '', $options); |
| 243 | + if ('' != $options) { |
| 244 | + $options .= ' '; |
| 245 | + } |
| 246 | + $options = "'" . $options . $lineText . "'"; |
225 | 247 |
|
226 | 248 | } |
227 | 249 |
|
228 | 250 | /** |
229 | 251 | * Extract keys of table from given line |
230 | 252 | * |
231 | 253 | * @param string $line |
232 | | - * @return array|bool |
| 254 | + * @return array |
233 | 255 | */ |
234 | 256 | private function getKey (string $line) |
235 | 257 | { |
|
0 commit comments