(s *schema.Schema, cascade bool, exclude []string)
| 424 | } |
| 425 | |
| 426 | func (db *DB) genTableCols(s *schema.Schema, cascade bool, exclude []string) ([]string, []string, []string) { |
| 427 | var cols []string |
| 428 | var relations []string |
| 429 | var indices []string |
| 430 | schemaManager := schema.GetManager() |
| 431 | for _, property := range s.Properties { |
| 432 | if util.ContainsString(exclude, property.ID) { |
| 433 | continue |
| 434 | } |
| 435 | handler := db.handlers[property.Type] |
| 436 | sqlDataType := property.SQLType |
| 437 | sqlDataProperties := "" |
| 438 | if db.sqlType == "sqlite3" { |
| 439 | sqlDataType = strings.Replace(sqlDataType, "auto_increment", "autoincrement", 1) |
| 440 | } |
| 441 | if sqlDataType == "" { |
| 442 | sqlDataType = handler.dataType(&property) |
| 443 | if property.ID == "id" { |
| 444 | sqlDataProperties = " primary key" |
| 445 | } |
| 446 | } |
| 447 | if property.ID != "id" { |
| 448 | if property.Nullable { |
| 449 | sqlDataProperties = " null" |
| 450 | } else { |
| 451 | sqlDataProperties = " not null" |
| 452 | } |
| 453 | if property.Unique { |
| 454 | sqlDataProperties = " unique" |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | query := "`" + property.ID + "` " + sqlDataType + sqlDataProperties |
| 459 | |
| 460 | cols = append(cols, query) |
| 461 | if property.Relation != "" { |
| 462 | foreignSchema, _ := schemaManager.Schema(property.Relation) |
| 463 | if foreignSchema != nil { |
| 464 | cascadeString := "" |
| 465 | if cascade || |
| 466 | property.OnDeleteCascade || |
| 467 | (property.Relation == s.Parent && s.OnParentDeleteCascade) { |
| 468 | cascadeString = "on delete cascade" |
| 469 | } |
| 470 | |
| 471 | relationColumn := "id" |
| 472 | if property.RelationColumn != "" { |
| 473 | relationColumn = property.RelationColumn |
| 474 | } |
| 475 | |
| 476 | relations = append(relations, fmt.Sprintf("constraint %s foreign key(`%s`) REFERENCES `%s`(%s) %s", |
| 477 | quote(foreignKeyName(s.GetDbTableName(), property.ID, foreignSchema.GetDbTableName(), relationColumn)), |
| 478 | property.ID, foreignSchema.GetDbTableName(), relationColumn, cascadeString)) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if property.Indexed { |
| 483 | prefix := "" |
no test coverage detected