public static function parse($name, $def) { if (Table::match('(?<type>PRIMARY KEY|UNIQUE) \\((?<columns>[^)]+)\\)', $def, $m)) { // primary/unique key (add conflict clause) $constraintType = strtoupper($m['type']); $constraintColumns = array_map([Table::class, 'trim'], explode(',', $m['columns'])); return new self($constraintColumns, $constraintType === 'PRIMARY KEY'); } return NULL; }
public static function parse($name, $def) { if (Table::match('FOREIGN KEY \\((?<local>[^)]+)\\) REFERENCES (?<table>[^\\s]+) \\((?<foreign>[^)]+)\\) ON DELETE (?<delete>SET NULL|CASCADE|RESTRICT)', $def, $m)) { // foreign key (add fk clause) $localField = Table::trim($m['local']); $foreignTable = Table::trim($m['table']); $foreignField = Table::trim($m['foreign']); $foreignDelete = $m['delete']; return new self($localField, $foreignTable, $foreignField, $foreignDelete); } return NULL; }