...

Source file src/oss.terrastruct.com/d2/d2target/sqltable.go

Documentation: oss.terrastruct.com/d2/d2target

     1  package d2target
     2  
     3  import "strings"
     4  
     5  const (
     6  	NamePadding       = 10
     7  	TypePadding       = 20
     8  	ConstraintPadding = 20
     9  	HeaderPadding     = 10
    10  
    11  	// Setting table font size sets it for columns
    12  	// The header needs to be a little larger for visual hierarchy
    13  	HeaderFontAdd = 4
    14  )
    15  
    16  type SQLTable struct {
    17  	Columns []SQLColumn `json:"columns"`
    18  }
    19  
    20  type SQLColumn struct {
    21  	Name       Text     `json:"name"`
    22  	Type       Text     `json:"type"`
    23  	Constraint []string `json:"constraint"`
    24  	Reference  string   `json:"reference"`
    25  }
    26  
    27  func (c SQLColumn) Texts(fontSize int) []*MText {
    28  	return []*MText{
    29  		{
    30  			Text:     c.Name.Label,
    31  			FontSize: fontSize,
    32  			IsBold:   false,
    33  			IsItalic: false,
    34  			Shape:    "sql_table",
    35  		},
    36  		{
    37  			Text:     c.Type.Label,
    38  			FontSize: fontSize,
    39  			IsBold:   false,
    40  			IsItalic: false,
    41  			Shape:    "sql_table",
    42  		},
    43  		{
    44  			Text:     c.ConstraintAbbr(),
    45  			FontSize: fontSize,
    46  			IsBold:   false,
    47  			IsItalic: false,
    48  			Shape:    "sql_table",
    49  		},
    50  	}
    51  }
    52  
    53  func (c SQLColumn) ConstraintAbbr() string {
    54  	constraints := make([]string, len(c.Constraint))
    55  
    56  	for i, constraint := range c.Constraint {
    57  		switch constraint {
    58  		case "primary_key":
    59  			constraint = "PK"
    60  		case "foreign_key":
    61  			constraint = "FK"
    62  		case "unique":
    63  			constraint = "UNQ"
    64  		}
    65  
    66  		constraints[i] = constraint
    67  	}
    68  
    69  	return strings.Join(constraints, ", ")
    70  }
    71  

View as plain text