`
// This is not implemented yet
const PRIMARY_KEY string = ``
// Generate the tree based on the database tables and columns
func TableTree(tree map[string][]model.Column) string {
html := TREE_OPEN
var body string
for _, table := range getSortedKeys(tree) {
body += fmt.Sprintf(TABLE_TEMPLATE, table, table, table, table, table)
fields := fmt.Sprintf(FIELDS_LIST_OPEN, table)
body += fields + generateFields(table, tree[table]) + FIELDS_LIST_CLOSE
}
html += fmt.Sprintf(TREE_BODY_TEMPLATE, body)
return html + 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(FIELD_TEMPLATE, table, field.Name, field.Name, generateType(field))
}
return html
}
// Return a list of the keys in a map, sorted alphabetically
func getSortedKeys(m map[string][]model.Column) []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
}