예제 #1
0
파일: Table.php 프로젝트: jdauie/bramble
 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;
         }
     }
 }