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

     1  package drivers
     2  
     3  import "fmt"
     4  
     5  // PrimaryKey represents a primary key constraint in a database
     6  type PrimaryKey struct {
     7  	Name    string   `json:"name"`
     8  	Columns []string `json:"columns"`
     9  }
    10  
    11  // ForeignKey represents a foreign key constraint in a database
    12  type ForeignKey struct {
    13  	Table    string `json:"table"`
    14  	Name     string `json:"name"`
    15  	Column   string `json:"column"`
    16  	Nullable bool   `json:"nullable"`
    17  	Unique   bool   `json:"unique"`
    18  
    19  	ForeignTable          string `json:"foreign_table"`
    20  	ForeignColumn         string `json:"foreign_column"`
    21  	ForeignColumnNullable bool   `json:"foreign_column_nullable"`
    22  	ForeignColumnUnique   bool   `json:"foreign_column_unique"`
    23  }
    24  
    25  // SQLColumnDef formats a column name and type like an SQL column definition.
    26  type SQLColumnDef struct {
    27  	Name string
    28  	Type string
    29  }
    30  
    31  // String for fmt.Stringer
    32  func (s SQLColumnDef) String() string {
    33  	return fmt.Sprintf("%s %s", s.Name, s.Type)
    34  }
    35  
    36  // SQLColumnDefs has small helper functions
    37  type SQLColumnDefs []SQLColumnDef
    38  
    39  // Names returns all the names
    40  func (s SQLColumnDefs) Names() []string {
    41  	names := make([]string, len(s))
    42  
    43  	for i, sqlDef := range s {
    44  		names[i] = sqlDef.Name
    45  	}
    46  
    47  	return names
    48  }
    49  
    50  // Types returns all the types
    51  func (s SQLColumnDefs) Types() []string {
    52  	types := make([]string, len(s))
    53  
    54  	for i, sqlDef := range s {
    55  		types[i] = sqlDef.Type
    56  	}
    57  
    58  	return types
    59  }
    60  
    61  // SQLColDefinitions creates a definition in sql format for a column
    62  func SQLColDefinitions(cols []Column, names []string) SQLColumnDefs {
    63  	ret := make([]SQLColumnDef, len(names))
    64  
    65  	for i, n := range names {
    66  		for _, c := range cols {
    67  			if n != c.Name {
    68  				continue
    69  			}
    70  
    71  			ret[i] = SQLColumnDef{Name: n, Type: c.Type}
    72  		}
    73  	}
    74  
    75  	return ret
    76  }