/** * parses the raw string and builds the parts * * @param string $rawString * * @return void * @throws \Exception */ private function build($rawString) { $parseHelper = new ParseHelper(); $rawString = trim($rawString); //Parse name if (preg_match('@`(?P<name>[^`]+)`@', $rawString, $matches)) { $this->setName($matches['name']); $rawString = substr($rawString, strlen($matches[0])); } $rawString = trim($rawString); $type = $parseHelper->parseSection($rawString); $this->setType($type); $rawString = substr($rawString, strlen($type)); $rawString = trim($rawString); if (preg_match('@^unsigned($| )@', $rawString, $matches)) { $this->setUnsigned(true); $rawString = substr($rawString, strlen($matches[0])); } $rawString = trim($rawString); if (preg_match('@^COLLATE (?P<collate>[^ ]+)($| )@', $rawString, $matches)) { $this->setCollate($matches['collate']); $rawString = substr($rawString, strlen($matches[0])); } $rawString = trim($rawString); if (preg_match('@^(?P<nullable>NOT NULL|NULL)($| )@', $rawString, $matches)) { $this->setNullable($matches['nullable'] == 'NULL'); $rawString = substr($rawString, strlen($matches[0])); } $rawString = trim($rawString); if (preg_match('@^AUTO_INCREMENT($| )@', $rawString, $matches)) { $this->setAutoIncrement(true); $rawString = substr($rawString, strlen($matches[0])); } $rawString = trim($rawString); if (preg_match('@^DEFAULT @', $rawString, $matches)) { $rawString = substr($rawString, strlen($matches[0])); $default = $parseHelper->parseSection($rawString); $this->setDefault($default); $rawString = substr($rawString, strlen($default)); } $rawString = trim($rawString); if (preg_match('@^COMMENT @', $rawString, $matches)) { $rawString = substr($rawString, strlen($matches[0])); $comment = $parseHelper->parseSection($rawString); $this->setComment($comment); $rawString = substr($rawString, strlen($comment)); } }
/** * buildLines * * @param string $rawResult * * @return array */ private function buildLines($rawResult) { if (preg_match('@^CREATE TABLE `[^`]+` \\(@', $rawResult, $matches)) { $rawResult = substr($rawResult, strlen($matches[0])); } $parseHelper = new ParseHelper(); $lines = array(); while (strlen($rawResult) > 0) { $section = $parseHelper->parseSection($rawResult, ','); $lines[] = $section; $rawResult = substr($rawResult, strlen($section) + 1); if ($parseHelper->isTooManyClosingParenthesis()) { break; } } $lineObjects = []; $keyObjects = []; $constraintObjects = []; foreach ($lines as $line) { $line = trim($line); if ($line[0] == '`') { $lineObject = new Line($line); $lineObjects[$lineObject->getName()] = $lineObject; } elseif (preg_match('@^(PRIMARY |UNIQUE |)KEY @', $line)) { $keyObject = new Key($line); $keyObjects[$keyObject->getName()] = $keyObject; } elseif (preg_match('@^CONSTRAINT @', $line)) { $constraintObject = new Constraint($line); $constraintObjects[$constraintObject->getName()] = $constraintObject; } } $this->setLines($lineObjects); $this->setKeys($keyObjects); $this->setConstraints($constraintObjects); return $rawResult; }