d2/d2target/sqltable.go

65 lines
1.1 KiB
Go
Raw Normal View History

package d2target
2023-05-01 11:59:57 +00:00
import "strings"
2022-12-20 05:02:50 +00:00
const (
NamePadding = 10
TypePadding = 20
2023-02-06 08:42:28 +00:00
HeaderPadding = 10
// 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",
},
}
}
2022-12-22 19:06:57 +00:00
func (c SQLColumn) ConstraintAbbr() string {
2023-05-01 11:59:57 +00:00
var abbrs []string
for _, constraint := range c.Constraint {
var abbr string
switch constraint {
case "primary_key":
abbr = "PK"
case "foreign_key":
abbr = "FK"
case "unique":
abbr = "UNQ"
}
abbrs = append(abbrs, abbr)
2022-12-22 19:06:57 +00:00
}
2023-05-01 11:59:57 +00:00
2023-06-03 11:54:06 +00:00
return strings.Join(abbrs, ", ")
2022-12-22 19:06:57 +00:00
}