codegen: add unique index lookup funcs

This commit is contained in:
2026-02-19 21:40:02 -08:00
parent d6426bba14
commit 3e0a85fcfa
5 changed files with 149 additions and 20 deletions

View File

@@ -41,7 +41,23 @@ func (c Column) GoFieldName() string {
return textutils.SnakeToCamel(c.Name)
}
func (c Column) GoType() string {
// GoVarName returns the name of a local variable for this column, e.g., when used as a function parameter.
func (c Column) GoVarName() string {
if c.Name == "rowid" {
return strings.ToLower(c.TableName)[0:1] + "ID"
// TODO: Or should it just be "id"??
}
// For foreign keys, use first letter of the target type and "ID". "UserID" => "uID"
if c.IsNonCodeTableForeignKey() {
return strings.ToLower(c.ForeignKeyTargetTable)[0:1] + "ID"
}
// Otherwise, just lowercase the field name
fieldname := c.GoFieldName()
return strings.ToLower(fieldname)[0:1] + fieldname[1:]
}
func (c Column) GoTypeName() string {
if c.IsNonCodeTableForeignKey() {
return TypenameFromTablename(c.ForeignKeyTargetTable) + "ID"
}
@@ -101,6 +117,15 @@ func (t Table) PrimaryKeyColumns() []Column {
return pks
}
func (t Table) GetColumnByName(name string) Column {
for _, c := range t.Columns {
if c.Name == name {
return c
}
}
panic("no such column: " + name)
}
func (t Table) HasAutoTimestamps() (hasCreatedAt bool, hasUpdatedAt bool) {
for _, c := range t.Columns {
if c.Name == "created_at" && c.Type == "integer" {