github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/table.go (about)

     1  package drivers
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Table metadata from the database schema.
     8  type Table struct {
     9  	Name string `json:"name"`
    10  	// For dbs with real schemas, like Postgres.
    11  	// Example value: "schema_name"."table_name"
    12  	SchemaName string   `json:"schema_name"`
    13  	Columns    []Column `json:"columns"`
    14  
    15  	PKey  *PrimaryKey  `json:"p_key"`
    16  	FKeys []ForeignKey `json:"f_keys"`
    17  
    18  	IsJoinTable bool `json:"is_join_table"`
    19  
    20  	ToOneRelationships  []ToOneRelationship  `json:"to_one_relationships"`
    21  	ToManyRelationships []ToManyRelationship `json:"to_many_relationships"`
    22  
    23  	// For views
    24  	IsView           bool             `json:"is_view"`
    25  	ViewCapabilities ViewCapabilities `json:"view_capabilities"`
    26  }
    27  
    28  type ViewCapabilities struct {
    29  	CanInsert bool `json:"can_insert"`
    30  	CanUpsert bool `json:"can_upsert"`
    31  }
    32  
    33  // GetTable by name. Panics if not found (for use in templates mostly).
    34  func GetTable(tables []Table, name string) (tbl Table) {
    35  	for _, t := range tables {
    36  		if t.Name == name {
    37  			return t
    38  		}
    39  	}
    40  
    41  	panic(fmt.Sprintf("could not find table name: %s", name))
    42  }
    43  
    44  // GetColumn by name. Panics if not found (for use in templates mostly).
    45  func (t Table) GetColumn(name string) (col Column) {
    46  	for _, c := range t.Columns {
    47  		if c.Name == name {
    48  			return c
    49  		}
    50  	}
    51  
    52  	panic(fmt.Sprintf("could not find column name: %s", name))
    53  }
    54  
    55  // CanLastInsertID checks the following:
    56  // 1. Is there only one primary key?
    57  // 2. Does the primary key column have a default value?
    58  // 3. Is the primary key column type one of uintX/intX?
    59  // If the above is all true, this table can use LastInsertId
    60  func (t Table) CanLastInsertID() bool {
    61  	if t.PKey == nil || len(t.PKey.Columns) != 1 {
    62  		return false
    63  	}
    64  
    65  	col := t.GetColumn(t.PKey.Columns[0])
    66  	if len(col.Default) == 0 {
    67  		return false
    68  	}
    69  
    70  	switch col.Type {
    71  	case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
    72  	default:
    73  		return false
    74  	}
    75  
    76  	return true
    77  }
    78  
    79  func (t Table) CanSoftDelete(deleteColumn string) bool {
    80  	if deleteColumn == "" {
    81  		deleteColumn = "deleted_at"
    82  	}
    83  
    84  	for _, column := range t.Columns {
    85  		if column.Name == deleteColumn && column.Type == "null.Time" {
    86  			return true
    87  		}
    88  	}
    89  	return false
    90  }
    91  
    92  func TablesHaveNullableEnums(tables []Table) bool {
    93  	for _, table := range tables {
    94  		for _, col := range table.Columns {
    95  			if col.Nullable && IsEnumDBType(col.DBType) {
    96  				return true
    97  			}
    98  		}
    99  	}
   100  	return false
   101  }