示例#1
0
 private function inlineForeignKey(MidataConstraint $constraint)
 {
     // CREATE TABLE `books_authors` (
     //   `book_id` int(10) unsigned NOT NULL,
     //   `author_id` int(10) unsigned NOT NULL,
     //   `type_id` int(10) unsigned NOT NULL,
     //   PRIMARY KEY (`book_id`,`author_id`,`type_id`),
     //   KEY `books_authors_author` (`author_id`,`type_id`),
     //   CONSTRAINT `books_authors_author` FOREIGN KEY (`author_id`, `type_id`) REFERENCES `authors` (`author_id`, `type_id`),
     //   CONSTRAINT `books_authors_book` FOREIGN KEY (`book_id`) REFERENCES `books` (`book_id`)
     // ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci
     $baseTable = $constraint->baseTable();
     $refTable = $constraint->refTable();
     $columns = $constraint->columns();
     $name = $constraint->name();
     $onUpdate = $constraint->onUpdate();
     $onDelete = $constraint->onDelete();
     if ($onUpdate == MysqlConstraint::RESTRICT) {
         $onUpdate = "";
     } else {
         $onUpdate = "ON UPDATE {$onUpdate}";
     }
     if ($onDelete == MysqlConstraint::RESTRICT) {
         $onDelete = "";
     } else {
         $onDelete = "ON DELETE {$onDelete}";
     }
     $tableColumn = "";
     $refTableColumn = "";
     foreach ($columns as $def) {
         $base = $def['base'];
         $ref = $def['ref'];
         $tableColumn .= "`{$base}`,";
         $refTableColumn .= "`{$ref}`,";
     }
     $tableColumn = trim($tableColumn, ',');
     $refTableColumn = trim($refTableColumn, ',');
     $sql = "CONSTRAINT `{$name}` FOREIGN KEY ({$tableColumn}) REFERENCES `{$refTable}` ({$refTableColumn}) {$onUpdate} {$onDelete}";
     $sql = trim($sql);
     return $sql;
 }