d2/d2target/sqltable.go

71 lines
1.3 KiB
Go
Raw Permalink Normal View History

package d2target
2023-05-01 11:59:57 +00:00
import "strings"
2022-12-20 05:02:50 +00:00
const (
2023-06-05 18:33:34 +00:00
NamePadding = 10
TypePadding = 20
ConstraintPadding = 20
HeaderPadding = 10
2023-02-06 08:42:28 +00:00
// Setting table font size sets it for columns
// The header needs to be a little larger for visual hierarchy
HeaderFontAdd = 4
2022-12-20 05:02:50 +00:00
)
type SQLTable struct {
Columns []SQLColumn `json:"columns"`
}
type SQLColumn struct {
2023-05-01 11:59:57 +00:00
Name Text `json:"name"`
Type Text `json:"type"`
Constraint []string `json:"constraint"`
Reference string `json:"reference"`
}
2023-02-06 08:42:28 +00:00
func (c SQLColumn) Texts(fontSize int) []*MText {
2022-12-23 08:29:03 +00:00
return []*MText{
{
Text: c.Name.Label,
2023-02-06 08:42:28 +00:00
FontSize: fontSize,
2022-12-23 08:29:03 +00:00
IsBold: false,
IsItalic: false,
Shape: "sql_table",
},
{
Text: c.Type.Label,
2023-02-06 08:42:28 +00:00
FontSize: fontSize,
2022-12-23 08:29:03 +00:00
IsBold: false,
IsItalic: false,
Shape: "sql_table",
},
2023-06-05 18:33:34 +00:00
{
Text: c.ConstraintAbbr(),
FontSize: fontSize,
IsBold: false,
IsItalic: false,
Shape: "sql_table",
},
}
}
2022-12-22 19:06:57 +00:00
func (c SQLColumn) ConstraintAbbr() string {
2023-06-05 18:33:34 +00:00
constraints := make([]string, len(c.Constraint))
2023-05-01 11:59:57 +00:00
2023-06-05 18:33:34 +00:00
for i, constraint := range c.Constraint {
2023-05-01 11:59:57 +00:00
switch constraint {
case "primary_key":
2023-06-05 18:33:34 +00:00
constraint = "PK"
2023-05-01 11:59:57 +00:00
case "foreign_key":
2023-06-05 18:33:34 +00:00
constraint = "FK"
2023-05-01 11:59:57 +00:00
case "unique":
2023-06-05 18:33:34 +00:00
constraint = "UNQ"
2023-05-01 11:59:57 +00:00
}
2023-06-05 18:33:34 +00:00
constraints[i] = constraint
2022-12-22 19:06:57 +00:00
}
2023-05-01 11:59:57 +00:00
2023-06-05 18:33:34 +00:00
return strings.Join(constraints, ", ")
2022-12-22 19:06:57 +00:00
}