Documentation
¶
Index ¶
- Constants
- Variables
- func AutoMigrateModel(ctx context.Context, conn internal.Connection, model *mapping.ModelStruct) error
- func AutoMigrateModels(ctx context.Context, conn internal.Connection, models ...*mapping.ModelStruct) error
- func ConstraintSetter(field *mapping.StructField, tag *mapping.FieldTag) error
- func CreateForeignKeysView(ctx context.Context, conn internal.Connection)
- func DataTypeSetter(field *mapping.StructField, t *mapping.FieldTag) error
- func FieldColumnName(field *mapping.StructField) (string, error)
- func GetKeyWords(ctx context.Context, conn internal.Connection, version int) (map[string]KeyWordType, error)
- func GetQuotedWord(word string, pgVersion int) string
- func GetVersion(ctx context.Context, conn internal.Connection) (int, error)
- func HasColumn(ctx context.Context, conn internal.Connection, t *Table, c *Column) bool
- func HasForeignKey(ctx context.Context, conn internal.Connection, t *Table, c *Column) bool
- func HasIndex(ctx context.Context, conn internal.Connection, t *Table, i *Index) bool
- func HasNotNullConstraint(ctx context.Context, conn internal.Connection, t *Table, c *Column) bool
- func HasPrimaryKey(ctx context.Context, conn internal.Connection, t *Table, c *Column) bool
- func HasTable(ctx context.Context, conn internal.Connection, t *Table) bool
- func HasUniqueConstraint(ctx context.Context, conn internal.Connection, t *Table, col *Column) bool
- func IndexSetter(field *mapping.StructField, t *mapping.FieldTag) error
- func ModelsTableName(m *mapping.ModelStruct) (string, error)
- func NameSetter(f *mapping.StructField, t *mapping.FieldTag) error
- func PrepareModel(model *mapping.ModelStruct) error
- func PrepareModels(models ...*mapping.ModelStruct) error
- func RegisterDataType(dt DataTyper) error
- func RegisterRefTypeDT(t reflect.Type, dt DataTyper, override ...bool) error
- func RegisterTagSetter(key string, setter TagSetterFunc) error
- func WriteQuotedWord(b *strings.Builder, word string, pgVersion int)
- type BasicDataType
- type Column
- type ColumnCreator
- type Constraint
- type DataType
- type DataTyper
- type ExternalDataTyper
- type Index
- type IndexType
- type KeyWordType
- type OptionalParameterDataType
- type ParameteredDataType
- type SchemaNamer
- type Table
- type TableNamer
- type TagSetterFunc
Constants ¶
const ( // TagDB is the tag used for the model structfields that defines 'pq' options. TagDB string = "db" // TagDatabaseName is the tag key that defines database column name. TagDatabaseName string = "name" // TagColumnIndex is the tag key that defines database column name. TagColumnIndex string = "index" // TagDataType is the tag used to set the column's data type. TagDataType string = "type" // ModelAlreadyPrepared is the key used. ModelAlreadyPrepared string = "pq:model_already_prepared" // SchemaNameKey is the models store key used to save the schema name. SchemaNameKey string = "pq:schema_name" // TableKey is the key for the ModelStruct Store that contains table definition. TableKey string = "pq:table" // ColumnKey is the key for the StructField's Store that contains a column definition. ColumnKey string = "pq:column" // OmitKey is the key for the StructField's Store that OmitKey string = "pq:omit" // NotNullKey is the StructField's Store key that defines the NotNull Constraint for the field. NotNullKey string = "pq:not_null" // DataTypeParametersStoreKey is the key used in the field's stores to the Data type paremeters. DataTypeParametersStoreKey string = "pq:data_type_parameters" )
const ( // BTreeTag is the BTree index type tag. BTreeTag = "btree" // HashTag is the Hash index type tag. HashTag = "hash" // GiSTTag is the GiST index type tag. GiSTTag = "gist" // GINTag is the GIN index type tag. GINTag = "gin" )
Variables ¶
var ( // CNotNull is the not null constraint CNotNull = &Constraint{Name: cNotNull, SQLName: func(t *Table, c *Column) (string, error) { return fmt.Sprintf("ALTER TABLE %s.%s ALTER COLUMN %s SET NOT NULL;", quoteIdentifier(t.Schema), quoteIdentifier(t.Name), c.Name), nil }, DBChecker: HasNotNullConstraint, } // CUnique is the 'unique' constraint. CUnique = &Constraint{Name: cUnique, SQLName: func(t *Table, c *Column) (string, error) { return fmt.Sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s UNIQUE (%s);", quoteIdentifier(t.Schema), quoteIdentifier(t.Name), uniqueConstraintName(c), c.Name, ), nil }, DBChecker: HasUniqueConstraint, } // CPrimaryKey is the Primary key constraint. CPrimaryKey = &Constraint{Name: "primary", SQLName: func(t *Table, c *Column) (string, error) { return fmt.Sprintf("ALTER TABLE %s.%s ADD PRIMARY KEY (%s);", quoteIdentifier(t.Schema), quoteIdentifier(t.Name), c.Name), nil }, DBChecker: HasPrimaryKey, } // CForeignKey is the Foreign key constraint. CForeignKey = &Constraint{Name: "foreign", SQLName: func(t *Table, c *Column) (string, error) { relatedField := c.Field().Relationship().Struct().Primary() relatedTable, err := modelsTable(relatedField.ModelStruct()) if err != nil { return "", err } relatedPrimaryColumn, err := fieldsColumn(relatedField) if err != nil { return "", err } return fmt.Sprintf("ALTER TABLE %s.%s ADD FOREIGN KEY (%s) REFERENCES %s.%s(%s);", quoteIdentifier(t.Schema), quoteIdentifier(t.Name), c.Name, quoteIdentifier(relatedTable.Schema), quoteIdentifier(relatedTable.Name), relatedPrimaryColumn.Name, ), nil }, DBChecker: HasForeignKey, } )
var ( // FChar is the 'char' field type. FChar = &ParameteredDataType{SQLName: "char", DataType: DataType{Name: "char"}} // FVarChar is the 'varchar' field type. FVarChar = &ParameteredDataType{SQLName: "varchar", DataType: DataType{Name: "varchar"}} // FText is the 'text' field type. FText = &BasicDataType{SQLName: "text", DataType: DataType{Name: "text"}} // FSmallInt is the 2 bytes signed 'smallint' - int16. FSmallInt = &BasicDataType{SQLName: "smallint", DataType: DataType{Name: "smallint"}} // FInteger is the 4 bytes signed 'integer' type - int32. FInteger = &BasicDataType{SQLName: "integer", DataType: DataType{Name: "integer"}} // FBigInt is the 8 bytes signed 'bigint' type - int64. FBigInt = &BasicDataType{SQLName: "bigint", DataType: DataType{Name: "bigint"}} // FDecimal is the variable 'decimal' type. FDecimal = &ParameteredDataType{SQLName: "decimal", DataType: DataType{Name: "decimal"}} // FNumeric is the ariable 'numeric' type. FNumeric = &ParameteredDataType{SQLName: "numeric", DataType: DataType{Name: "numeric"}} // FReal is the 4 bytes - 6 decimal digits precision 'real' type. FReal = &BasicDataType{SQLName: "real", DataType: DataType{Name: "real"}} // FDouble is the 8 bytes - 15 decimal digits precision 'double precision' type. FDouble = &BasicDataType{SQLName: "double precision", DataType: DataType{Name: "double"}} // FSerial is the 4 bytes - autoincrement integer 'serial' type. FSerial = &BasicDataType{SQLName: "serial", DataType: DataType{Name: "serial"}} // FBigSerial is the 8 bytes autoincrement big integer - 'bigserial' type. FBigSerial = &BasicDataType{SQLName: "bigserial", DataType: DataType{Name: "bigserial"}} // FBytea is the 1 or 4 bytes plus the actual binary string data type 'bytea'. FBytea = &ParameteredDataType{SQLName: "bytea", DataType: DataType{Name: "bytea"}} // FBoolean is the 'boolean' pq data type. FBoolean = &BasicDataType{SQLName: "boolean", DataType: DataType{Name: "boolean"}} // FDate is the 'date' field kind. FDate = &BasicDataType{SQLName: "date", DataType: DataType{Name: "date"}} // FTimestamp is the 'timestamp' without time zone data type. FTimestamp = &OptionalParameterDataType{SQLNames: []string{"timestamp"}, ParameterIndex: 1, DataType: DataType{Name: "timestamp"}} // FTimestampTZ is the 'timestamp with time zone' data type. FTimestampTZ = &OptionalParameterDataType{SQLNames: []string{"timestamp", "with time zone"}, ParameterIndex: 1, DataType: DataType{Name: "timestamptz"}} // FTime is the 'time' without time zone data type. FTime = &OptionalParameterDataType{SQLNames: []string{"time"}, ParameterIndex: 1, DataType: DataType{Name: "time"}} // FTimeTZ is the 'time with time zone' data type. FTimeTZ = &OptionalParameterDataType{SQLNames: []string{"time", "with time zone"}, ParameterIndex: 1, DataType: DataType{Name: "timetz"}} )
var TagSetterFunctions = map[string]TagSetterFunc{}
TagSetterFunctions is the mapping for the tags with their TagSetterFunc.
Functions ¶
func AutoMigrateModel ¶
func AutoMigrateModel(ctx context.Context, conn internal.Connection, model *mapping.ModelStruct) error
AutoMigrateModel prepares the model's struct and automatically migrates it's table into the provided database structure.
func AutoMigrateModels ¶
func AutoMigrateModels(ctx context.Context, conn internal.Connection, models ...*mapping.ModelStruct) error
AutoMigrateModels migrates the provided model definitions.
func ConstraintSetter ¶
func ConstraintSetter(field *mapping.StructField, tag *mapping.FieldTag) error
ConstraintSetter is the TagSetterFunc for the constraints.
func CreateForeignKeysView ¶
func CreateForeignKeysView(ctx context.Context, conn internal.Connection)
CreateForeignKeysView creates a sql View for the foreign keys per table.
func DataTypeSetter ¶
func DataTypeSetter(field *mapping.StructField, t *mapping.FieldTag) error
DataTypeSetter is the TagSetter function that sets the proper data type for given tag value or field.
func FieldColumnName ¶
func FieldColumnName(field *mapping.StructField) (string, error)
FieldColumnName gets the column name for the provided field.
func GetKeyWords ¶
func GetKeyWords(ctx context.Context, conn internal.Connection, version int) (map[string]KeyWordType, error)
GetKeyWords gets and stores the keywords for the provided postgres 'version' from the current database 'db' connection.
func GetQuotedWord ¶
GetQuotedWord gets the quoted 'word' if it is on the lists of the keywords. The postgres version 'pgVersion' is the numeric version of the postgres server.
func GetVersion ¶
GetVersion gets the numerical version of the postgres server.
func HasForeignKey ¶
HasForeignKey checks if the foreign key exists.
func HasNotNullConstraint ¶
HasNotNullConstraint checks if the column has a not null constraint.
func HasPrimaryKey ¶
HasPrimaryKey checks if the table contains primary key.
func HasUniqueConstraint ¶
HasUniqueConstraint checks if the table contains constraint.
func IndexSetter ¶
func IndexSetter(field *mapping.StructField, t *mapping.FieldTag) error
IndexSetter is the TagSetter function that sets the Column's Index.
func ModelsTableName ¶
func ModelsTableName(m *mapping.ModelStruct) (string, error)
ModelsTableName gets the table name for the provided model
func NameSetter ¶
func NameSetter(f *mapping.StructField, t *mapping.FieldTag) error
NameSetter is the TagSetter function that sets the Column's DBName
func PrepareModel ¶
func PrepareModel(model *mapping.ModelStruct) error
PrepareModel prepares the model for the pq db tags.
func PrepareModels ¶
func PrepareModels(models ...*mapping.ModelStruct) error
PrepareModels prepares multiple models, get's their db tags.
func RegisterDataType ¶
RegisterDataType registers the provided datatype assigning it next id.
func RegisterRefTypeDT ¶
RegisterRefTypeDT registers default data type for provided reflect.Type.
func RegisterTagSetter ¶
func RegisterTagSetter(key string, setter TagSetterFunc) error
RegisterTagSetter registers the TagSetter function for given tag key.
func WriteQuotedWord ¶
WriteQuotedWord surrounds the 'word' with quotations '"' signs if it is one of reserved keywords. The postgres version 'pgVersion' is the numeric version of the postgres server. The result is being written into provided 'b' strings.Builder.
Types ¶
type BasicDataType ¶
BasicDataType is the InlineDataTyper that sets the basic columns on the base of it's SQLName.
func (*BasicDataType) GetName ¶
func (b *BasicDataType) GetName(*mapping.StructField) string
GetName creates the inline column definition on the base of it's SQLName.
type Column ¶
type Column struct { // Name defines the column name Name string // Type is the column data type Type DataTyper // Variables defines the data type specific variables Variables []string // Constraints defines column constrains Constraints []*Constraint // Indexes defines the column indexes Indexes []*Index // Table is the pointer to the column's table Table *Table // contains filtered or unexported fields }
Column is a postgres field kind.
func FieldsColumn ¶
func FieldsColumn(field *mapping.StructField) (*Column, error)
FieldsColumn gets the column name for the provided field.
func (*Column) Field ¶
func (c *Column) Field() *mapping.StructField
Field is the column's related *mapping.StructField.
type ColumnCreator ¶
ColumnCreator is the function that creates custom.
type Constraint ¶
type Constraint struct { Name string SQLName func(t *Table, c *Column) (string, error) DBChecker func(context.Context, internal.Connection, *Table, *Column) bool }
Constraint defines the postgres constraint type.
type DataType ¶
type DataType struct {
Name string
}
DataType is the pq base model defininig the data type.
type DataTyper ¶
type DataTyper interface { KeyName() string // GetName creates the column string used within the table definition GetName(field *mapping.StructField) string }
DataTyper is the interface for basic data type methods.
type ExternalDataTyper ¶
type ExternalDataTyper interface { DataTyper // ExternalFunction is the method used to create the column outside of the table definition. ExternalFunction(field *mapping.StructField) string }
ExternalDataTyper is the interface that defines the columns that sets the column outside the table definition.
type Index ¶
type Index struct { // Name is the index name. Name string // Type defines the index type. Type IndexType // Columns are the columns specified for the index. Columns []*Column }
Index defines the postgres Index.
func FieldIndexes ¶
func FieldIndexes(field *mapping.StructField) ([]*Index, error)
FieldIndexes gets the column's indexes.
type KeyWordType ¶
type KeyWordType int
KeyWordType is the postgres default key word type.
const ( // KWUnknown is the unknown keyword type. KWUnknown KeyWordType = iota // KWUnreservedU is the unreserved key word type. KWUnreservedU // KWUnreservedC is the unreserved key word type that cannot be a function or type name. KWUnreservedC // KWReservedR is the reserved keyword type. KWReservedR // KWReservedT is the reserved keyword that can be a function or type name. KWReservedT )
func GetKeyWordType ¶
func GetKeyWordType(word string, pgVersion int) KeyWordType
GetKeyWordType gets the keyword type for given 'word' and postgres version 'pgVersion'. If field is not found returns 'KWUnknown'.
func (KeyWordType) IsReserved ¶
func (k KeyWordType) IsReserved() bool
IsReserved checks if the current keyword is reserved.
type OptionalParameterDataType ¶
OptionalParameterDataType is the data type that contains optional parameters.
func (*OptionalParameterDataType) GetName ¶
func (p *OptionalParameterDataType) GetName(field *mapping.StructField) string
GetName creates the inline column definition on the base of it's SQLName and Parameters. nolint:gocritic
type ParameteredDataType ¶
ParameteredDataType is the data type that contains the variable parameters. i.e. varchar(2) has a single parameter '2'.
func (*ParameteredDataType) GetName ¶
func (p *ParameteredDataType) GetName(field *mapping.StructField) string
GetName creates the inline column definition on the base of it's SQLName and Parameters.
type SchemaNamer ¶
type SchemaNamer interface {
PQSchemaName() string
}
SchemaNamer is the interface for the models that needs non default 'postgres' schema name.
type Table ¶
type Table struct { Schema string QuotedSchema string Name string QuotedName string Columns []*Column Indexes []*Index // contains filtered or unexported fields }
Table is the model for the SQL table definition.
func ModelsTable ¶
func ModelsTable(m *mapping.ModelStruct) (*Table, error)
ModelsTable gets the model table
func (*Table) FindColumn ¶
FindColumn finds the column by it's name.
func (*Table) Model ¶
func (t *Table) Model() *mapping.ModelStruct
Model is the *mapping.ModelStruct for which the Table was defined.
type TableNamer ¶
type TableNamer interface {
TableName() string
}
TableNamer is the interface that allows to get the table name for given model
type TagSetterFunc ¶
type TagSetterFunc func(*mapping.StructField, *mapping.FieldTag) error
TagSetterFunc is the function that sets the proper column info for the given tag. I.e. having set the 'name' - tag key to a 'NameSetter' function allows to set the Column name with tag value.