private function __construct($sql) { preg_match('/^CREATE TABLE (?<name>[^\\s\\(]+) \\((?<defs>.*)\\)\\s*$/is', $sql, $matches); $this->m_name = $matches['name']; $defs = array_map('trim', preg_split('/,[\\t ]*[\\r\\n]/', $matches['defs'])); $this->m_columns = []; $this->m_foreign = []; $this->m_unique = []; $constraints = []; foreach ($defs as $def) { if (count($constraints) || ($matched = self::match('(?:CONSTRAINT|PRIMARY|UNIQUE|FOREIGN|CHECK)\\b', $def, $m))) { if (isset($matched) && !$matched) { throw new \Exception('Failed to parse start of constraint'); } // table constraint // check for optional constraint name $constraintName = NULL; if (self::match('CONSTRAINT (?<name>' . Column::R_NAME . ')', $def, $m)) { $constraintName = $m['name']; $def = ltrim(substr($def, strlen($m[0]))); } if ($uniqueKey = UniqueKey::parse($constraintName, $def)) { $this->m_unique[] = $uniqueKey; } else { if ($foreignKey = ForeignKey::parse($constraintName, $def)) { $this->m_foreign[$foreignKey->localField()] = $foreignKey; } else { throw new \Exception('Failed to parse constraint'); } } } else { $column = Column::parse($def); $this->m_columns[$column->name()] = $column; } } }
/** * @param string $str * @param string $ordering * @param string|null $alias * @return OrderingColumn * @throws \Trinity\Bundle\SearchBundle\Exception\SyntaxErrorException */ public static function parse($str, $ordering = 'ASC', $alias = null) { $column = self::wrap(parent::parse($str, $alias)); $column->setOrdering($ordering); return $column; }