Skip to content

Commit e81e63c

Browse files
committed
update migratehelper
1 parent 474df49 commit e81e63c

1 file changed

Lines changed: 48 additions & 26 deletions

File tree

class/Common/MigrateHelper.php

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace XoopsModules\Wggithub\Common;
3+
namespace XoopsModules\Wgtransifex\Common;
44

55
/*
66
You may not change or alter any portion of this comment or credits
@@ -79,34 +79,53 @@ public function createSchemaFromSqlfile(): bool
7979
}
8080
}
8181

82+
$skip = true;
83+
$skipWords = ['CREATE DATABASE ', 'CREATE VIEW ', 'INSERT INTO ', 'SELECT ', 'DELETE ', 'UPDATE ', 'ALTER ', 'DROP '];
84+
$options = '';
8285
// read remaining lines line by line and create new schema
8386
foreach ($lines as $key => $value) {
8487
$line = \trim($value);
88+
foreach ($skipWords as $skipWord) {
89+
if ($skipWord === \mb_strtoupper(\substr($line, 0, \strlen($skipWord)))) {
90+
$skip = true;
91+
}
92+
}
8593
if ('CREATE TABLE' === \mb_strtoupper(\substr($line, 0, 12))) {
94+
$skip = false;
95+
$options = '';
8696
// start table definition
8797
$tableName = $this->getTableName ($line);
8898
$tables[$tableName] = [];
8999
$tables[$tableName]['options'] = '';
90100
$tables[$tableName]['columns'] = [];
91101
$tables[$tableName]['keys'] = [];
92102
} 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+
}
110129
}
111130
}
112131
}
@@ -212,24 +231,27 @@ private function getColumns (string $line)
212231
* Extract options of table from given line
213232
*
214233
* @param string $line
215-
* @return string
234+
* @param string $options
235+
* @return void
216236
*/
217-
private function getOptions (string $line): string
237+
private function getOptions (string $line, string &$options): void
218238
{
219239

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 . "'";
225247

226248
}
227249

228250
/**
229251
* Extract keys of table from given line
230252
*
231253
* @param string $line
232-
* @return array|bool
254+
* @return array
233255
*/
234256
private function getKey (string $line)
235257
{

0 commit comments

Comments
 (0)