Documentation
¶
Index ¶
- Constants
- func ColumnToField(s string) string
- func FieldToColumn(s string) string
- func FiltersInterfaces(filters *Filters) []interface{}
- func IsFieldKindSupported(k reflect.Kind) bool
- func IsStructField(u interface{}, field string) bool
- func LimitOffset(limit int, offset int) string
- func MapInterfaces(mapObj map[string]interface{}) []interface{}
- func PrettifyCreateTable(sql string) string
- func QuoteColumn(column string) string
- func SetObjFields(obj interface{}, values *Filters) error
- func StructFieldValueFromString(obj interface{}, name string, value string) (bool, interface{})
- type Builder
- func (b *Builder) CreateTable() (string, error)
- func (b *Builder) DatabaseColumnToFieldName(n string) string
- func (b *Builder) Delete(filters *Filters) (string, error)
- func (b *Builder) DeleteByID() (string, error)
- func (b *Builder) DeleteReturningID(filters *Filters) (string, error)
- func (b *Builder) DropTable() (string, error)
- func (b *Builder) Err() error
- func (b *Builder) HasAliasedColumnNames() (bool, error)
- func (b *Builder) HasModificationFields() (bool, error)
- func (b *Builder) Insert() (string, error)
- func (b *Builder) InsertOnConflictUpdate() (string, error)
- func (b *Builder) PasswordFields() ([]string, error)
- func (b *Builder) Select(order []string, limit int, offset int, filters *Filters) (string, error)
- func (b *Builder) SelectByID() (string, error)
- func (b *Builder) SelectCount(filters *Filters) (string, error)
- func (b *Builder) UniqueFields() ([]string, error)
- func (b *Builder) Update(values map[string]interface{}, filters *Filters) (string, error)
- func (b *Builder) UpdateByID() (string, error)
- type BuilderError
- type FieldInfo
- type Filters
- type OpVal
- type Options
- type QueryContainer
- type StructInfo
Constants ¶
const ( OpOR = iota * 1 OpAND )
const ( OpEqual = iota * 1 OpNotEqual OpLike OpMatch OpGreater OpLower OpGreaterOrEqual OpLowerOrEqual OpBit )
const (
DefaultTagName = "sql"
)
const (
Raw = "_"
)
Variables ¶
This section is empty.
Functions ¶
func ColumnToField ¶
ColumnToField converts database column name to struct field name.
func FieldToColumn ¶
FieldToColumn converts struct field name to database column name.
func FiltersInterfaces ¶
func FiltersInterfaces(filters *Filters) []interface{}
FiltersInterfaces returns a list of interfaces from a filters map (used in querying)
func IsFieldKindSupported ¶
IsFieldKindSupported checks if a specific reflect kind of the field is supported by the Builder.
func IsStructField ¶
IsStructField checks if a field exists in a struct.
func LimitOffset ¶
LimitOffset returns a LIMIT and OFFSET clause for a query.
func MapInterfaces ¶
func MapInterfaces(mapObj map[string]interface{}) []interface{}
MapInterfaces returns a slice of interfaces from a map.
func PrettifyCreateTable ¶
PrettifyCreateTable prettifies SQL query to make it more human-readable.
func SetObjFields ¶
func StructFieldValueFromString ¶
StructFieldValueFromString takes a field value as string and converts it (if possible) to a value type of that field.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder reflects the object to generate and cache PostgreSQL queries (CREATE TABLE, INSERT, UPDATE, etc.). Database table and column names are lowercase with an underscore, and they are generated from field names.
func (*Builder) CreateTable ¶
CreateTable returns an SQL query for creating the table.
func (*Builder) DatabaseColumnToFieldName ¶
DatabaseColumnToFieldName takes a database column and converts it to a struct field name.
func (*Builder) Delete ¶
Delete returns a DELETE query with a WHERE condition built from 'filters' (field-value pairs). Struct fields in the 'filters' argument are sorted alphabetically. Hence, when used with a database connection, their values (or pointers to it) must be sorted as well.
func (*Builder) DeleteByID ¶
DeleteByID returns an SQL query for deleting an object by its ID.
func (*Builder) DeleteReturningID ¶
DeleteReturningID returns a DELETE query with a WHERE condition built from 'filters' (field-value pairs) with RETURNING id. Struct fields in the 'filters' argument are sorted alphabetically. Hence, when used with a database connection, their values (or pointers to it) must be sorted as well.
func (*Builder) HasAliasedColumnNames ¶
HasAliasedColumnNames returns true if any of the fields have column names in the format of "alias.column_name".
func (*Builder) HasModificationFields ¶
HasModificationFields returns true if all the following int64 fields are present: CreatedAt, CreatedBy, ModifiedAt, ModifiedBy.
func (*Builder) InsertOnConflictUpdate ¶
InsertOnConflictUpdate returns an SQL query for inserting when a conflict is detected.
func (*Builder) PasswordFields ¶
PasswordFields returns a list with field names that are passwords.
func (*Builder) Select ¶
Select returns a SELECT query with a WHERE condition built from 'filters' (field-value pairs). Struct fields in the 'filters' argument are sorted alphabetically. Hence, when used with a database connection, their values (or pointers to it) must be sorted as well. Columns in the SELECT query are ordered the same way as they are defined in the struct: SELECT field1_column, field2_column, ... etc.
func (*Builder) SelectByID ¶
SelectByID returns an SQL query for selecting an object by its ID.
func (*Builder) SelectCount ¶
SelectCount returns a SELECT COUNT(*) query to count rows with a WHERE condition built from 'filters' (field-value pairs). Struct fields in the 'filters' argument are sorted alphabetically. Hence, when used with a database connection, their values (or pointers to it) must be sorted as well.
func (*Builder) UniqueFields ¶
UniqueFields returns a list with field names that are unique.
func (*Builder) Update ¶
Update returns an UPDATE query where specified struct fields (columns) are updated and rows match specific WHERE condition built from 'filters' (field-value pairs). Struct fields in 'values' and the 'filters' arguments are sorted alphabetically. Hence, when used with a database connection, their values (or pointers to it) must be sorted as well.
func (*Builder) UpdateByID ¶
UpdateByID returns an SQL query for updating an object by their ID.
type BuilderError ¶
func (*BuilderError) Error ¶
func (e *BuilderError) Error() string
type FieldInfo ¶
type FieldInfo struct {
// ColumnName is the name of the column in the database table.
ColumnName string
// ColumnType is the type of the column in the database table.
ColumnType string
// Default contains default value for the field.
Default string
// Unique is set to true if the field is marked with an "uniq" tag.
Unique bool
// NotString is set to true if the field is not a string.
NotString bool
// Password is set to true if the field is marked with a "password" tag.
Password bool
// Ignored is set to true if the field is marked with a "-" tag.
Ignored bool
}
type QueryContainer ¶
type StructInfo ¶
type StructInfo struct {
// Name of the struct.
Name string
// ModificationFields indicates whether the struct has the following fields: CreatedAt, CreatedBy, ModifiedAt, ModifiedBy.
ModificationFields bool
// TableName is a name of the table in the database.
TableName string
// AliasedColumnNames set to true indicates that some column names are in format of "alias.column_name".
AliasedColumnNames bool
}