Documentation
¶
Index ¶
- Constants
- func LowerCaseIdentifier(s string) (i string)
- func UpperCaseIdentifier(s string) (i string)
- type Column
- func (c *Column) CompareGen(a, b string, equal bool) string
- func (c *Column) DecimalPrecision() uint64
- func (c *Column) DecimalScale() uint64
- func (c *Column) DefaultConstantName() string
- func (c *Column) DefaultValueAsValue() string
- func (c *Column) HasDefaultValue() bool
- func (c *Column) HasSetter() bool
- func (c *Column) IsAPrimaryKey() bool
- func (c *Column) IsAutoPK() bool
- func (c *Column) IsDecimal() bool
- func (c *Column) IsEnum() bool
- func (c *Column) IsReference() bool
- func (c *Column) IsThePrimaryKey() bool
- func (c *Column) JsonKey() string
- func (c *Column) MaxInt() int64
- func (c *Column) MaxLength() uint64
- func (c *Column) MinInt() int64
- func (c *Column) QueryKey() string
- func (c *Column) String() string
- type ConstValue
- type Database
- type Enum
- type EnumField
- type Index
- type ManyManyReference
- func (m *ManyManyReference) JsonKey() string
- func (m *ManyManyReference) PkField() string
- func (m *ManyManyReference) PrimaryKeyColumnName() string
- func (m *ManyManyReference) PrimaryKeyType() string
- func (m *ManyManyReference) QueryKey() string
- func (m *ManyManyReference) SourceColumnName() string
- func (m *ManyManyReference) SourceColumnReceiverType() query.ReceiverType
- func (m *ManyManyReference) SourcePrimaryKeyName() string
- func (m *ManyManyReference) TableIdentifier() string
- func (m *ManyManyReference) Type() string
- func (m *ManyManyReference) TypePlural() string
- type Reference
- type Table
- func (t *Table) AllColumns() (out []*Column)
- func (t *Table) ColumnByName(name string) *Column
- func (t *Table) FileName() string
- func (t *Table) HasAutoPK() bool
- func (t *Table) HasGetterName(name string) (hasName bool, desc string)
- func (t *Table) HasManyManyReferences() bool
- func (t *Table) HasReferences() bool
- func (t *Table) HasReverseReferences() bool
- func (t *Table) HasUniqueIndexes() bool
- func (t *Table) LockColumnIdentifier() string
- func (t *Table) LockColumnQueryName() string
- func (t *Table) PrimaryKeyColumn() *Column
- func (t *Table) PrimaryKeyColumns() []*Column
- func (t *Table) PrimaryKeyType() string
- func (t *Table) ReadTimeoutConst() string
- func (t *Table) SettableColumns() (out []*Column)
- func (t *Table) VariableNamePlural() string
- func (t *Table) WriteTimeoutConst() string
Constants ¶
const ( CreatedTime = "now" ModifiedTime = "update" )
Variables ¶
This section is empty.
Functions ¶
func LowerCaseIdentifier ¶
func UpperCaseIdentifier ¶
Types ¶
type Column ¶
type Column struct {
// QueryName is the name of the column in the database.
QueryName string
// Table is a pointer back to the table that this column is part of
Table *Table
// Identifier is the name of the accessor function in go code. e.g. "FirstName".
Identifier string
// Field is the name of the field in the Table struct that will hold the column's value.
// It is also used as the name for a function parameter.
Field string
// FieldPlural is the plural form of Field.
FieldPlural string
// Label is the name that identifies the value to humans. e.g. "First Name".
Label string
// Type is the Go type that the column represents to the rest of the Go application.
Type string
// SchemaType is the type info specified in the schema description
SchemaType schema2.ColumnType
// SchemaSubType further describes the schema type.
SchemaSubType schema2.ColumnSubType
// ReceiverType is the Go type that will be received from the database.
ReceiverType ReceiverType
// Size is the maximum length of runes to allow in the column if a string type column.
// If a byte array, it is the number of bytes permitted.
// If an attempt at entering more than this amount occurs, it is considered a programming bug
// and we will panic.
// If an integer or float type, it is the number of bits in the data type.
Size uint64
// DefaultValue is the default value as specified by the database. We will initialize new ORM objects
// with this value.
DefaultValue interface{}
// IsNullable is true if the column can be given a NULL value.
IsNullable bool
// If this is an enum type, Enum will point to the Enum object.
Enum *Enum
// If this column is a reference, a pointer to the Reference object.
Reference *Reference
// Options are the options extracted from the comments string
Options map[string]interface{}
}
Column describes a database column. The information is derived from the schema structure.
func (*Column) CompareGen ¶
CompareGen will treat a and b as variables of the same type as the column's schema type, and will generate code that will compare them for equality (if equal), or inequality (if equal is false)
func (*Column) DecimalPrecision ¶
DecimalPrecision returns the precision value of a decimal number that is packed into the Size value.
func (*Column) DecimalScale ¶
DecimalScale returns the scale value of a decimal number when it is packed into the Size value.
func (*Column) DefaultConstantName ¶
DefaultConstantName returns the name of the default value constant that will be used to refer to the default value
func (*Column) DefaultValueAsValue ¶
DefaultValueAsValue returns the default value of the column as a GO value
func (*Column) HasDefaultValue ¶
HasDefaultValue returns true if the column has a default value.
func (*Column) HasSetter ¶
HasSetter returns true if the column should be allowed to be set by the programmer. Some columns should not be alterable, including time based columns that automatically set or update their times.
func (*Column) IsAPrimaryKey ¶
IsAPrimaryKey returns true if this is one of the primary keys.
func (*Column) IsDecimal ¶
IsDecimal returns true if the column is a Decimal (Numeric) string value, meaning is a variable precision decimal number.
func (*Column) IsReference ¶
IsReference returns true if the column is a foreign key.
func (*Column) IsThePrimaryKey ¶
IsThePrimaryKey returns true if this is the single primary key of its table.
func (*Column) MaxInt ¶
MaxInt returns the maximum integer that the column can hold if it is an integer type. Returns 0 if not.
func (*Column) MaxLength ¶
MaxLength returns the maximum length of the column, which normally is Column.Size, but in certain situations might be something else.
func (*Column) MinInt ¶
MinInt returns the minimum integer that the column can hold if it is an integer type. Returns 0 if not.
type ConstValue ¶
type ConstValue struct {
// Value is the integer that will represent the value
Value int
// Name is the CamelCase name that represents the value (without the table name prefix)
Name string
// Key is the snake_case key that will represent the value.
Key string
// FieldValues are the additional values associated with the field. At a minimum, it will have a "label" entry.
// This should correspond to the entries in Enum.Fields.
FieldValues map[string]any
}
type Database ¶
type Database struct {
// The database key corresponding to its key in the global database cluster
Key string
// ImportPath is the module path to the output directory.
ImportPath string
// WriteTimeout is used to wrap write transactions with a timeout on their contexts.
// Leaving it as zero will turn off timeout wrapping, allowing you to wrap individual calls as you
// see fit.
WriteTimeout time.Duration
// ReadTimeout is used to wrap read transactions with a timeout on their contexts.
// Leaving it as zero will turn off timeout wrapping, allowing you to wrap individual calls as you
// see fit.
ReadTimeout time.Duration
// Tables are the tables in the database, keyed by database table name
Tables map[string]*Table
// Enums contains a description of the enumerated types linked to the database, keyed by database table name
Enums map[string]*Enum
// EnumTableSuffix is the text to string off the end of an enum table when converting it to a type name.
// Defaults to "_enum".
EnumTableSuffix string
// Defaults to _assn
AssnTableSuffix string
}
Database is the top level struct that contains a description of a database modeled as objects. It is used in code generation.
func FromSchema ¶
func (*Database) IsEnumTable ¶
IsEnumTable returns true if the given name is the name of an enum table in the database
func (*Database) MarshalOrder ¶
MarshalOrder returns an array of tables in the order they should be marshaled such that if they then get unmarshalled in the same order, there will not be problems with foreign keys not existing when an object is eventually saved. Note that it cannot do this for circular references, and so if your database has circular references, including self references, any foreign key checking will need to be turned off while importing the database.
func (*Database) UniqueManyManyReferences ¶
func (m *Database) UniqueManyManyReferences() []*ManyManyReference
UniqueManyManyReferences returns all the many-many references, but returning only one per association table.
type Enum ¶
type Enum struct {
// DbKey is the key used to find the database in the global database cluster.
DbKey string
// QueryName is the name of the table to use in querying the database.
QueryName string
// Label is the english name of the object when describing it to the world.
Label string
// LabelPlural is the plural english name of the object.
LabelPlural string
// Identifier is the name of the item as a go type name.
Identifier string
// IdentifierPlural is the plural of the go type name.
IdentifierPlural string
// DecapIdentifier is the Identifier with the first letter lower case.
DecapIdentifier string
// Fields are the names of accessor functions associated with the type.
// The first field name MUST be "label". There may be additional fields.
Fields []EnumField
// Constants are the constant identifiers that will be used for each enumerated value.
// These are in ascending order by keys.
Constants []ConstValue
}
Enum describes a structure that represents a fixed, enumerated type that will not change during the application's use. This will generate types that can be used as values for fields in the database.
func (*Enum) FieldIdentifier ¶
FieldIdentifier returns the go name corresponding to the given field offset, or an empty string if out of bounds.
func (*Enum) FieldIdentifierPlural ¶
FieldIdentifierPlural returns the go plural name corresponding to the given field offset, or an empty string if out of bounds.
func (*Enum) FieldQueryName ¶
func (*Enum) FieldReceiverType ¶
FieldReceiverType returns the ReceiverType corresponding to the given field offset
type EnumField ¶
type EnumField struct {
// QueryName is the name of the field in the database.
// QueryNames should be lower_snake_case.
QueryName string
// Identifier is the name used in Go code to access the data.
Identifier string
// IdentifierPlural is the plural form of Identifier.
IdentifierPlural string
// Type is the ReceiverType of the column.
Type ReceiverType
}
type Index ¶
type Index struct {
// IsUnique indicates whether the index is unique
IsUnique bool
// Columns are the columns that are part of the index
Columns []*Column
Name string
Identifier string
}
Index will create accessor functions related to Columns.
type ManyManyReference ¶
type ManyManyReference struct {
// TableQueryName is the database table that links the two associated tables together.
TableQueryName string
// ForeignKeyName is the database column in the association table that points at the referenced table's primary key.
ForeignKeyName string
// ForeignKeyReceiverType is the query.ReceiverType of the destination column.
ForeignKeyReceiverType query.ReceiverType
// ReferencedTable is the table being linked.
ReferencedTable *Table
// Label is the human-readable label of the objects pointed to.
Label string
// LabelPlural is the plural of Label
LabelPlural string
// Identifier is the name used to refer to an object on the other end of the reference.
// It is not the same as the object type. For example TeamMember would refer to a Person type.
// This is derived from the ForeignKeyName but can be overridden.
Identifier string
// IdentifierPlural is the name used to refer to the group of objects on the other end of the reference.
// For example, TeamMembers. This is derived from the ForeignKeyName but can be overridden by
// a comment in the table.
IdentifierPlural string
// Field is the go identifier that will be used in the Table struct and parameters.
// Since this always points to many objects, it will be a plural name.
Field string
// MM is the many-many reference on the other end of the relationship that points back to this one.
MM *ManyManyReference
}
The ManyManyReference structure is used by the templates during the codegen process to describe a many-to-many relationship. Underlying the structure is an association table that has two foreign keys pointing to the records that are linked. For each relationship, two ManyManyReference structures are created.
func (*ManyManyReference) JsonKey ¶
func (m *ManyManyReference) JsonKey() string
JsonKey returns the key used when referring to the associated objects in JSON.
func (*ManyManyReference) PkField ¶
func (m *ManyManyReference) PkField() string
PkField returns the identifier used for the variable listing the primary keys of the associated objects.
func (*ManyManyReference) PrimaryKeyColumnName ¶
func (m *ManyManyReference) PrimaryKeyColumnName() string
PrimaryKeyColumnName returns the database name of the primary key of the object the association links to.
func (*ManyManyReference) PrimaryKeyType ¶
func (m *ManyManyReference) PrimaryKeyType() string
PrimaryKeyType returns the type of the referenced table's primary key.
func (*ManyManyReference) QueryKey ¶
func (m *ManyManyReference) QueryKey() string
func (*ManyManyReference) SourceColumnName ¶
func (m *ManyManyReference) SourceColumnName() string
func (*ManyManyReference) SourceColumnReceiverType ¶
func (m *ManyManyReference) SourceColumnReceiverType() query.ReceiverType
func (*ManyManyReference) SourcePrimaryKeyName ¶
func (m *ManyManyReference) SourcePrimaryKeyName() string
func (*ManyManyReference) TableIdentifier ¶
func (m *ManyManyReference) TableIdentifier() string
TableIdentifier identifies the association table.
func (*ManyManyReference) Type ¶
func (m *ManyManyReference) Type() string
Type returns the name of the object type the association links to.
func (*ManyManyReference) TypePlural ¶
func (m *ManyManyReference) TypePlural() string
TypePlural returns the plural name of the object type the association links to.
type Reference ¶
type Reference struct {
// Table is a pointer back to the table this reference is part of.
Table *Table
// ReferencedTable is the table being pointed to by ForeignKey.
ReferencedTable *Table
// ForeignKey is the local column that is referring to the referenced table
ForeignKey *Column
// Identifier is the go identifier used as an accessor to the forward referenced object.
// Example: ProjectManager
Identifier string
// Field is the name used in the struct representing a pointer to the referenced object.
// Also used as a function parameter name and the query identifier.
// Example: projectManager
Field string
// The human-readable label of the object referred to.
// Example: Project Manager
Label string
// ReverseIdentifier is the name ReferencedTable should use to refer to the singular related object.
ReverseIdentifier string
// ReverseIdentifierPlural is the name ReferencedTable should use to refer to the plural related objects
// if this is not a unique reference.
// Example: Projects
ReverseIdentifierPlural string
// ReverseLabel is the human-readable label of the object of the reverse relationship.
// Example: Project
ReverseLabel string
// ReverseLabelPlural is the plural of ReverseLabel.
// Example: Projects
ReverseLabelPlural string
// ReverseField is the name used in the struct for the reverse object(s)
ReverseField string
// IsUnique is true if this is a one-to-one relationship
IsUnique bool
// IsNullable is true if the foreign keys are nullable. This also indicates that the
// reference is not required when the table is saved.
IsNullable bool
}
Reference describes a forward relationship to a related Table. Relationships are through a foreign key column in this table, that contains a copy of the primary key from the related table. Cross database references are not supported. References will generate code that refers to an object through the accessor function Identifier, and reverse code in the referenced Table that will refer to objects in this table as ReverseIdentifier.
func (*Reference) JsonKey ¶
JsonKey returns the key that will be used for the referenced object in JSON.
func (*Reference) ReverseJsonKey ¶
ReverseJsonKey returns the key that will be used for the reverse referenced object in JSON.
func (*Reference) ReverseNodeIdentifier ¶
ReverseNodeIdentifier is the identifier to use for reverse nodes. Depending on IsUnique, this will be a singular or plural word.
func (*Reference) ReverseQueryKey ¶
type Table ¶
type Table struct {
// DbKey is the key used to find the database in the global database cluster
DbKey string
// WriteTimeout is used to wrap write transactions with a timeout on their contexts.
// Leaving it as zero will use the timeout in the database, if one is set.
WriteTimeout time.Duration
// ReadTimeout is used to protect read transactions with a timeout on their contexts.
// Leaving it as zero will use the timeout in the database, if one is set.
ReadTimeout time.Duration
// NoTest indicates that the table should NOT have an automated test generated for it.
NoTest bool
// QueryName is the database's identifier for the table.
QueryName string
// Label is the name of the object when describing it to the world. Should be lower case.
Label string
// LabelPlural is the plural name of the object.
LabelPlural string
// Identifier is the name of the struct type when referring to it in go code.
Identifier string
// IdentifierPlural is the name of a collection of these objects when referring to them in go code.
IdentifierPlural string
// DecapIdentifier is the same as Identifier, but the first letter is lower case.
DecapIdentifier string
// Columns is a list of Columns, one for each column in the table, but does not include columns in references.
Columns []*Column
// Indexes are all the indexes defined on the table, single and multi-column, but not primary key.
Indexes []Index
// Options are key-value pairs of values that can be used to customize how code generation is performed
Options map[string]interface{}
// References are all the foreign keys. References contains additional columns.
References []*Reference
// ReverseReferences are the columns from other tables, or even this table,
// that point to this table.
ReverseReferences []*Reference
// ManyManyReferences describe the many-to-many references pointing to this table
ManyManyReferences []*ManyManyReference
// The cached optimistic locking column, if one is present
LockColumn *Column
// contains filtered or unexported fields
}
func (*Table) AllColumns ¶
AllColumns returns all the columns in the table, including foreign keys
func (*Table) ColumnByName ¶
ColumnByName returns a Column given the query name of the column, or nil if not found.
func (*Table) FileName ¶
FileName is the base name of generated file names that correspond to this database table. Typically, Go files are lower case snake case by convention.
func (*Table) HasAutoPK ¶
HasAutoPK returns true if the table has an automatically generated primary key
func (*Table) HasGetterName ¶
HasGetterName returns true if the given name is in use by one of the getters. This is used for detecting naming conflicts. Will also return an error string to display if there is a conflict.
func (*Table) HasManyManyReferences ¶
HasManyManyReferences returns true if the table has at least one many-many reference.
func (*Table) HasReferences ¶
HasReferences returns true if the table has at least one forward reference.
func (*Table) HasReverseReferences ¶
HasReverseReferences returns true if the table has at least one reverse reference.
func (*Table) HasUniqueIndexes ¶
HasUniqueIndexes returns true if the table has at least one unique index.
func (*Table) LockColumnIdentifier ¶
LockColumnIdentifier returns the Column.Identifier of the lock column, or an empty string if there is no lock column.
func (*Table) LockColumnQueryName ¶
LockColumnQueryName returns the Column.QueryName of the lock column, or an empty string if there is no lock column.
func (*Table) PrimaryKeyColumn ¶
PrimaryKeyColumn returns a single primary key column if the table is keyed on one column. If not, nil is returned.
func (*Table) PrimaryKeyColumns ¶
PrimaryKeyColumns returns a slice of the primary key columns in the table. Note that some of these columns may be part of a reference.
func (*Table) PrimaryKeyType ¶
PrimaryKeyType returns a type name for the primary key. If the primary key is a single column, this will be a go type. If a composite key, this will be the name of a struct type that should be generated to house the primary key values.
func (*Table) ReadTimeoutConst ¶
func (*Table) SettableColumns ¶
SettableColumns returns an array of columns that are settable, including foreign keys.