/** * 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; }
/** * Compares two lines, and returns any errors of non matches * * @param Line $line * @param string $prefix * * @return string[] * @throws \Exception */ public function equals(Line $line, $prefix) { $prefix .= ': Column (' . $this->getName() . '):'; $errors = []; if ($line->getName() !== $this->getName()) { throw new \Exception('Comparing Incomparable columns'); } if ($line->getType() !== $this->getType()) { $errors[] = $prefix . 'Type Does Not Match:' . "\n" . $line->getType() . "\n" . $this->getType(); } if ($line->isUnsigned() !== $this->isUnsigned()) { $errors[] = $prefix . 'Unsigned Does Not Match:' . "\n" . $line->isUnsigned() . "\n" . $this->isUnsigned(); } if ($line->isNullable() !== $this->isNullable()) { $errors[] = $prefix . 'Nullable Does Not Match:' . "\n" . $line->isNullable() . "\n" . $this->isNullable(); } if ($line->isAutoIncrement() !== $this->isAutoIncrement()) { $errors[] = $prefix . 'AutoIncrement Does Not Match:' . "\n" . $line->isAutoIncrement() . "\n" . $this->isAutoIncrement(); } if ($line->getCollate() !== $this->getCollate()) { $errors[] = $prefix . 'Collate Does Not Match:' . "\n" . $line->getCollate() . "\n" . $this->getCollate(); } if ($line->getComment() !== $this->getComment()) { $errors[] = $prefix . 'Comment Does Not Match:' . "\n" . $line->getComment() . "\n" . $this->getComment(); } if ($line->getDefault() !== $this->getDefault()) { $errors[] = $prefix . 'Default Does Not Match:' . "\n" . $line->getDefault() . "\n" . $this->getDefault(); } return $errors; }