`
// Generate the tree based on the database tables and columns
func TableTree(tree map[string][]model.Column) string {
html := TABLE_TREE_OPEN
var body string
for _, table := range getSortedKeys(tree) {
body += fmt.Sprintf(TABLE_TEMPLATE, table, table, table, table, table)
fields := fmt.Sprintf(TABLE_FIELDS_LIST_OPEN, table)
body += fields + generateFields(table, tree[table]) + TABLE_FIELDS_LIST_CLOSE
}
html += fmt.Sprintf(TABLE_TREE_BODY_TEMPLATE, body)
return html + TABLE_TREE_CLOSE
}
// Using a list of fields, generate the HTML for the fields
func generateFields(table string, fields []model.Column) string {
var html string
for _, field := range fields {
html += fmt.Sprintf(TABLE_FIELD_TEMPLATE, table, field.Name, field.Name, generateType(field))
}
return html
}
// Return a list of the keys in a map, sorted alphabetically
func getSortedKeys[T []model.Column | []string | string | [2]string](m map[string]T) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// Generate the string for the type of column base on the column definition
func generateType(col model.Column) string {
var str string
if col.PrimaryKey {
str = `PK, `
} else if col.ForeignKey.Column != "" {
str = fmt.Sprintf(`FK: %s(%s), `, col.ForeignKey.ForeignTable, col.ForeignKey.ForeignColumn)
}
if col.Nullable == "NO" {
str += `R, `
}
if col.Unique {
str += `U, `
}
if col.MaxLength.Valid {
str += fmt.Sprintf("%s(%d)", col.Type, col.MaxLength.Int64)
} else {
str += col.Type
}
return str
}
// Generate the HTML string for the enum tree
func EnumTree(enums map[string][]string) string {
html := ENUM_TREE_OPEN
var body string
for _, enum := range getSortedKeys(enums) {
body += fmt.Sprintf(ENUM_TEMPLATE, enum, enum, enum)
valuesList := fmt.Sprintf(ENUM_VALUES_LIST_OPEN, enum)
body += valuesList + generateEnumValues(enums[enum]) + ENUM_VALUES_LIST_CLOSE
}
html += fmt.Sprintf(ENUM_TREE_BODY_TEMPLATE, body)
return html + ENUM_TREE_CLOSE
}
// Convert a list of values into a list of HTML elements
func generateEnumValues(values []string) string {
var html string
for _, value := range values {
html += fmt.Sprintf(ENUM_VALUE_TEMPLATE, value)
}
return html
}