d2/d2target/sqltable.go

78 lines
1.5 KiB
Go
Raw Normal View History

package d2target
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 {
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 {
switch c.Constraint {
case "primary_key":
return "PK"
case "foreign_key":
return "FK"
case "unique":
return "UNQ"
default:
return ""
}
}
2023-03-27 18:24:18 +00:00
func (c SQLColumn) GetUniqueChars(uniqueMap map[rune]bool) string {
var uniqueChars string
for _, char := range c.Name.Label {
if _, exists := uniqueMap[char]; !exists {
uniqueMap[char] = true
uniqueChars = uniqueChars + string(char)
}
}
for _, char := range c.Type.Label {
if _, exists := uniqueMap[char]; !exists {
uniqueMap[char] = true
uniqueChars = uniqueChars + string(char)
}
}
for _, char := range c.ConstraintAbbr() {
if _, exists := uniqueMap[char]; !exists {
uniqueMap[char] = true
uniqueChars = uniqueChars + string(char)
}
}
return uniqueChars
}