MCPcopy Create free account
hub / github.com/cloudwan/gohan / genTableCols

Method genTableCols

db/sql/sql.go:426–510  ·  view source on GitHub ↗
(s *schema.Schema, cascade bool, exclude []string)

Source from the content-addressed store, hash-verified

424}
425
426func (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 := ""

Callers 2

AlterTableDefMethod · 0.95
GenTableDefMethod · 0.95

Implementers 4

DBdb/sql/sql.go
MockDBdb/mocks/db.go
MockDBMockRecorderdb/mocks/db.go
DBdb/file/file.go

Calls 7

foreignKeyNameFunction · 0.85
GetDbTableNameMethod · 0.80
JoinMethod · 0.80
quoteFunction · 0.70
dataTypeMethod · 0.65
SchemaMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected