morph

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2025 License: Apache-2.0 Imports: 16 Imported by: 1

README

morph

A compact package for organizing and maintaining your entity database metadata.

GoDoc Coverage Status Release License

What is it?

morph organizes and maintains the necessary metadata to map your entities to and from relational database tables. This is accomplished using the Metadata Mapping pattern popularized by Martin Fowler. With these metadata mappings, your application is empowered to construct SQL queries dynamically using the entities themselves.

Why use it?

With morph, your application reaps several benefits:

  • dynamic construction of queries using entities and their fields.
  • metadata generation using files in several formats, including YAML and JSON.
  • decoupling of code responsible for manufacturing queries from code tasked with SQL generation.

How to use it?

Using morph is super straightforward. You utilize Table and Column to organize metadata for your entities and their associated relational representations. Let's suppose you have a User entity in your application (user):

var idCol morph.Column
idCol.SetName("id")
idCol.SetField(Fields.ID)
idCol.SetFieldType("int")
idCol.SetStrategy("struct_field")
idCol.SetPrimaryKey(true)

var usernameCol morph.Column
usernameCol.SetName("username")
usernameCol.SetField(Fields.Username)
usernameCol.SetFieldType("string")
usernameCol.SetStrategy("struct_field")
usernameCol.SetPrimaryKey(false)

var passwordCol morph.Column
passwordCol.SetName("password")
passwordCol.SetField(Fields.Password)
passwordCol.SetFieldType("string")
passwordCol.SetStrategy("struct_field")
passwordCol.SetPrimaryKey(false)

var userTable morph.Table
userTable.SetName("user")
userTable.SetAlias("U")
userTable.SetType(user)
userTable.AddColumns(idCol, usernameCol, passwordCol)
Loading

Capturing the metadata mappings can be tedious, especially if your application has many entities with corresponding relational representations. Instead of constructing them manually, you can instead load a file that specifies the metadata mapping configuration:

{
  "tables": [
    {
      "typeName": "example.User",
      "name": "user",
      "alias": "U",
      "columns": [
        {
          "name": "id",
          "field": "ID",
          "fieldType": "string",
          "fieldStrategy": "struct_field",
          "primaryKey": true
        },
        {
          "name": "username",
          "field": "Username",
          "fieldType": "string",
          "fieldStrategy": "struct_field",
          "primaryKey": false
        },
        {
          "name": "password",
          "field": "Password",
          "fieldType": "string",
          "fieldStrategy": "struct_field",
          "primaryKey": false
        }
      ]
    }
  ]
}
configuration, err := morph.Load("./metadata.json")
if err != nil {
	panic(err)
}

tables := configuration.AsMetadata()
Custom Loader

At this time, we currently support YAML (.yaml, .yml) and JSON (.json) configuration files. However, if you would like to utilize a different file format, you can construct a type that implements morph.Loader and add the appropriate entries in morph.Loaders. The morph.Load function will leverage morph.Loaders by extracting the file extension using the path provided to it.

Reflection

If defining the metadata within files does not suit your fancy, you can leverage reflection to capture the metadata mappings:

razorcrest := Starship {
    ID: 123,
    Name: "Razorcrest",
    LastServicedAt: time.Now().Add(-3*time.Day),
}

table, err := morph.Reflect(razorcrest)
if err != nil {
    panic(err)
}

If you'd rather use a pointer, that works too:

table, err := morph.Reflect(&razorcrest)
if err != nil {
    panic(err)
}
Options

You can customize the reflection process by providing options to the morph.Reflect function. For example, you can specify the field tag to use when reflecting on the struct:

table, err := morph.Reflect(razorcrest, morph.WithTag("morph"))
if err != nil {
    panic(err)
}

There are many options available, so be sure to check out the morph.ReflectOptions type for more information!

Query Generation

Once you have your metadata mappings, you can use them to construct SQL queries. For example, you can generate a UPDATE query for the Starship entity like so:

razorcrest := Starship {
    ID: 123,
    Name: "Razorcrest",
    LastServicedAt: time.Date(1972, 12, 12, 0, 0, 0, 0, time.UTC),
}

table, err := morph.Reflect(razorcrest)
if err != nil {
    panic(err)
}

query, err := table.UpdateQuery()
if err != nil {
    panic(err)
}

fmt.Println(query) // UPDATE ships SET name = ?, last_serviced_at = ? WHERE id = ?;
Options

You can customize the query generation process by providing options to the UpdateQuery, InsertQuery, and DeleteQuery methods. For example, you can change the placeholder used in the query:

query, err := table.UpdateQuery(morph.WithPlaceholder("$", true))
if err != nil {
    panic(err)
}

fmt.Println(query) // UPDATE ships SET name = $1, last_serviced_at = $2 WHERE id = $3;

There are many options available, so be sure to check out the morph.QueryOptions type for more information!

Contribute

Want to lend us a hand? Check out our guidelines for contributing.

License

We are rocking an Apache 2.0 license for this project.

Code of Conduct

Please check out our code of conduct to get up to speed how we do things.

Documentation

Index

Constants

View Source
const (
	// SnakeCaseStrategy is the snake case strategy.
	SnakeCaseStrategy CaseStrategy = "snake"

	// ScreamingSnakeCaseStrategy is the screaming (uppercase) snake case strategy.
	ScreamingSnakeCaseStrategy CaseStrategy = "screaming_snake"

	// CamelCaseStrategy is the camel case strategy.
	CamelCaseStrategy CaseStrategy = "camel"

	// LowerCaseStrategy is the lowercase strategy.
	LowerCaseStrategy CaseStrategy = "lower"

	// UpperCaseStrategy is the uppercase strategy.
	UpperCaseStrategy CaseStrategy = "upper"

	// DefaultTableAliasLength is the default table alias length.
	DefaultTableAliasLength = 1

	// DefaultTableNameStrategy is the default table name strategy.
	DefaultTableNameStrategy = SnakeCaseStrategy

	// DefaultTableAliasStrategy is the default table alias strategy.
	DefaultTableAliasStrategy = UpperCaseStrategy

	// DefaultColumnNameStrategy is the default column name strategy.
	DefaultColumnNameStrategy = SnakeCaseStrategy
)
View Source
const DefaultPlaceholder = "?"

DefaultPlaceholder represents the default placeholder value used for query generation.

Variables

View Source
var (
	// WithTableName specifies the table name, effectively overriding any
	// table name inference.
	WithTableName = func(name string) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.TableName = &name
			c.IsInferredTableName = false
		}
	}

	// WithTableAlias specifies the table alias, effectively overriding any
	// table alias inference.
	WithTableAlias = func(alias string) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.TableAlias = &alias
			c.IsInferredTableAlias = false
		}
	}

	// WithInferredTableName indicates tha the table name should be inferred
	// based on the provided strategy and plurality.
	WithInferredTableName = func(strategy CaseStrategy, plural bool) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.IsInferredTableName = true
			c.TableNameStrategy = &strategy
			c.IsTableNamePlural = plural
		}
	}

	// WithInferredTableAlias indicates that the table alias should be inferred
	// based on the provided strategy and length.
	WithInferredTableAlias = func(strategy CaseStrategy, length int) ReflectOption {
		return func(c *ReflectConfiguration) {
			if length < 1 {
				length = DefaultTableAliasLength
			}
			c.IsInferredTableAlias = true
			c.TableAliasStrategy = &strategy
			c.TableAliasLength = &length
		}
	}

	// WithInferredColumnNames indicates that the column names should be inferred
	// based on the provided strategy.
	WithInferredColumnNames = func(strategy CaseStrategy) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.IsInferredColumnNames = true
			c.ColumnNameStrategy = &strategy
		}
	}

	// WithTag specifies the struct tag to use for field reflection.
	WithTag = func(tag string) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.Tag = &tag
		}
	}

	// WithoutMethods specifies the methods by name to exclude from reflection.
	WithoutMethods = func(methods ...string) ReflectOption {
		return func(c *ReflectConfiguration) {
			if c.MethodExclusions == nil {
				c.MethodExclusions = []string{}
			}
			c.MethodExclusions = methods
		}
	}

	// WithoutMatchingMethods specifies a regular expression used to match against
	// method names, where matches are excluded from reflection.
	WithoutMatchingMethods = func(pattern string) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.MethodExclusionPattern = &pattern
		}
	}

	// WithoutFields specifies the struct fields by name to exclude from reflection.
	WithoutFields = func(fields ...string) ReflectOption {
		return func(c *ReflectConfiguration) {
			if c.FieldExclusions == nil {
				c.FieldExclusions = []string{}
			}
			c.FieldExclusions = fields
		}
	}

	// WithoutMatchingFields specifies a regular expression used to match against
	// struct field names, where matches are excluded from reflection.
	WithoutMatchingFields = func(pattern string) ReflectOption {
		return func(c *ReflectConfiguration) {
			c.FieldExclusionPattern = &pattern
		}
	}

	// WithPrimaryKeyColumn specifies the name of the column that acts as
	// the primary key.
	WithPrimaryKeyColumn = func(name string) ReflectOption {
		return func(c *ReflectConfiguration) {
			if c.PrimaryKeyColumns == nil {
				c.PrimaryKeyColumns = []string{}
			}
			c.PrimaryKeyColumns = []string{name}
		}
	}

	// WithPrimaryKeyColumns specifies the names of the column that acts together as
	// the primary key.
	WithPrimaryKeyColumns = func(names ...string) ReflectOption {
		return func(c *ReflectConfiguration) {
			if c.PrimaryKeyColumns == nil {
				c.PrimaryKeyColumns = []string{}
			}
			pks := append([]string{}, names...)
			c.PrimaryKeyColumns = pks
		}
	}

	// WithColumnNameMapping specifies a single column mapping between the provided
	// field name and column name. Both the field name and column name are case
	// sensitive, and the name provided will be used regardless of other options specified.
	WithColumnNameMapping = func(field, name string) ReflectOption {
		return func(c *ReflectConfiguration) {
			if c.ColumnNameMappings == nil {
				c.ColumnNameMappings = make(map[string]string)
			}
			c.ColumnNameMappings[field] = name
		}
	}
)
View Source
var (
	// ErrMissingTypeName represents an error encountered when evaluation is attempted but the
	// table does not have a type name configured.
	ErrMissingTypeName = errors.New("morph: must have table type name to evaluate")

	// ErrMissingTableName represents an error encountered when evaluation is attempted but the
	// table does not have a table name configured.
	ErrMissingTableName = errors.New("morph: must have table name to evaluate")

	// ErrMissingTableAlias represents an error encountered when evaluation is attempted but the
	// table does not have a table alias configured.
	ErrMissingTableAlias = errors.New("morph: must have table alias to evaluate")

	// ErrMissingColumns represents an error encountered when evaluation is attempted but the
	// table does not have any columns configured.
	ErrMissingColumns = errors.New("morph: must have columns to evaluate")

	// ErrMismatchingTypeName represents an error encountered when evaluation is attempted but the
	// table type name does not match the type name of the object being evaluated.
	ErrMismatchingTypeName = errors.New("morph: must have matching type names to evaluate")

	// ErrMissingPrimaryKey represents an error encountered when a table does not have any primary key columns.
	ErrMissingPrimaryKey = errors.New("morph: table must have at least one primary key column")

	// ErrMissingNonPrimaryKey represents an error encountered when a table does not have any non-primary key columns.
	ErrMissingNonPrimaryKey = errors.New("morph: table must have at least one non-primary key column")
)

Defines the various errors that can occur when interacting with tables.

View Source
var DefaultQueryOptions = []QueryOption{WithDefaultPlaceholder()}

DefaultQueryOptions represents the default query options used for query generation.

View Source
var (
	// DefaultReflectOptions represents the default reflection options used for reflection.
	DefaultReflectOptions = []ReflectOption{
		WithInferredTableName(DefaultTableNameStrategy, true),
		WithInferredTableAlias(DefaultTableAliasStrategy, DefaultTableAliasLength),
		WithInferredColumnNames(DefaultColumnNameStrategy),
		WithoutMatchingMethods("^Set.*"),
		WithPrimaryKeyColumn("id"),
	}
)
View Source
var (

	// ErrNotStruct is returned when the input being reflected is not a struct.
	// A struct is required to reflect the fields and methods to construct the
	// table metadata.
	ErrNotStruct = errors.New("morph: input must be a struct or pointer to a struct")
)
View Source
var Loaders = map[string]Loader{
	"yaml": YAMLLoader{},
	"yml":  YAMLLoader{},
	"json": JSONLoader{},
}

Loaders is the collection of loaders by file extension.

Functions

func Must added in v1.3.0

func Must[T any](v T, err error) T

Must panics if the error is not nil.

Types

type CaseStrategy added in v1.1.0

type CaseStrategy string

CaseStrategy is an enumeration of the available case strategies.

type Column

type Column struct {
	// contains filtered or unexported fields
}

Column represents a mapping between an entity field and a database column.

func (*Column) Field

func (c *Column) Field() string

Field retrieves the field name associated to the column.

func (*Column) FieldType added in v1.1.0

func (c *Column) FieldType() string

FieldType retrieves the field type associated to the column.

func (*Column) Name

func (c *Column) Name() string

Name retrieves the name of the column.

func (*Column) PrimaryKey added in v1.1.0

func (c *Column) PrimaryKey() bool

PrimaryKey indicates if the column plays a role in the primary key for a table. Tables with composite primary keys will have multiple columns that indicate a true value for this method.

func (*Column) SetField

func (c *Column) SetField(field string)

SetField modifies the field name associated to the column.

func (*Column) SetFieldType added in v1.1.0

func (c *Column) SetFieldType(fieldType string)

SetFieldType modifies the field type associated to the column.

func (*Column) SetName

func (c *Column) SetName(name string)

SetName modifies the name of the column.

func (*Column) SetPrimaryKey added in v1.1.0

func (c *Column) SetPrimaryKey(pKey bool)

func (*Column) SetStrategy added in v1.1.0

func (c *Column) SetStrategy(strategy FieldStrategy)

SetStrategy modifies the field strategy associated to the column.

func (*Column) Strategy added in v1.1.0

func (c *Column) Strategy() FieldStrategy

Strategy retrieves the field strategy associated to the column.

func (*Column) UsingMethodStrategy added in v1.1.0

func (c *Column) UsingMethodStrategy() bool

UsingMethodStrategy determines if the column is using the method field strategy.

func (*Column) UsingStructFieldStrategy added in v1.1.0

func (c *Column) UsingStructFieldStrategy() bool

UsingStructFieldStrategy determines if the column is using the struct field strategy.

type ColumnConfiguration

type ColumnConfiguration struct {
	Name          string        `json:"name" yaml:"name"`
	Field         string        `json:"field" yaml:"field"`
	FieldType     string        `json:"fieldType" yaml:"fieldType"`
	FieldStrategy FieldStrategy `json:"fieldStrategy" yaml:"fieldStrategy"`
	PrimaryKey    bool          `json:"primaryKey" yaml:"primaryKey"`
}

ColumnConfiguration represents the configuration used to construct a single column mapping.

type ColumnPredicate added in v1.1.0

type ColumnPredicate func(Column) bool

ColumnPredicate represents a condition used to match columns within find operations.

type Configuration

type Configuration struct {
	Tables []TableConfiguration `json:"tables" yaml:"tables"`
}

Configuration represents the configuration used to construct the table and column mappings.

func Load

func Load(path string) (Configuration, error)

Load loads the configuration from the provided file.

func (Configuration) AsMetadata

func (c Configuration) AsMetadata() []Table

AsMetadata converts the configuration to metadata mappings.

type EvaluationResult added in v1.1.0

type EvaluationResult map[string]any

EvaluationResult represents the result of evaluating a table against an object.

func (EvaluationResult) Empties added in v1.1.0

func (r EvaluationResult) Empties() []string

Empties retrieves all of the keys in the result that have nil values.

func (EvaluationResult) NonEmpties added in v1.1.0

func (r EvaluationResult) NonEmpties() []string

NonEmpties retrieves all of the keys in the result that have non-nil values.

type FieldStrategy added in v1.1.0

type FieldStrategy string
const (
	FieldStrategyStructField FieldStrategy = "struct_field"
	FieldStrategyMethod      FieldStrategy = "method"
)

type JSONLoader

type JSONLoader struct{}

func (JSONLoader) Load

func (l JSONLoader) Load(path string) (c Configuration, err error)

Load loads the configuration from the JSON file provided.

type Loader

type Loader interface {
	Load(path string) (Configuration, error)
}

Loader loads the cofiguration from the provided file.

type QueryOption added in v1.1.0

type QueryOption func(*QueryOptions)

QueryOption represents a function that modifies the query options.

func WithDefaultPlaceholder added in v1.1.0

func WithDefaultPlaceholder() QueryOption

func WithNamedParameters added in v1.1.0

func WithNamedParameters() QueryOption

WithNamedParameters sets the query to use named parameters.

func WithPlaceholder added in v1.1.0

func WithPlaceholder(p string, o bool) QueryOption

WithPlaceholder sets the placeholder value and whether the parameter should have a sequence number appended to it.

func WithoutEmptyValues added in v1.1.0

func WithoutEmptyValues(obj any) QueryOption

WithoutEmptyValues indicates that columns with no value should be omitted from the query.

type QueryOptions added in v1.1.0

type QueryOptions struct {
	Placeholder string
	Ordered     bool
	Named       bool
	OmitEmpty   bool
	// contains filtered or unexported fields
}

QueryOptions represents the options available for generating a query.

type ReflectConfiguration added in v1.1.0

type ReflectConfiguration struct {
	TableName              *string
	TableAlias             *string
	IsInferredTableName    bool
	IsInferredTableAlias   bool
	IsInferredColumnNames  bool
	TableNameStrategy      *CaseStrategy
	TableAliasStrategy     *CaseStrategy
	ColumnNameStrategy     *CaseStrategy
	Tag                    *string
	MethodExclusions       []string
	FieldExclusions        []string
	MethodExclusionPattern *string
	FieldExclusionPattern  *string
	IsTableNamePlural      bool
	TableAliasLength       *int
	PrimaryKeyColumns      []string
	ColumnNameMappings     map[string]string
}

ReflectConfiguration is the configuration leveraged when constructing tables via reflection.

func (*ReflectConfiguration) CamelCaseColumnName added in v1.1.0

func (c *ReflectConfiguration) CamelCaseColumnName() bool

CamelCaseColumnName indicates if the column name strategy is camel case.

func (*ReflectConfiguration) CamelCaseTableName added in v1.1.0

func (c *ReflectConfiguration) CamelCaseTableName() bool

CamelCaseTableName indicates if the table name strategy is camel case.

func (*ReflectConfiguration) HasColumnNameMappings added in v1.2.0

func (c *ReflectConfiguration) HasColumnNameMappings() bool

HasColumnNameMappings indicates if any explicit column name mappings have been set.

func (*ReflectConfiguration) HasColumnNameStrategy added in v1.1.0

func (c *ReflectConfiguration) HasColumnNameStrategy() bool

HasColumnNameStrategy indicates if the column name strategy is set.

func (*ReflectConfiguration) HasFieldExclusionPattern added in v1.1.0

func (c *ReflectConfiguration) HasFieldExclusionPattern() bool

HasFieldExclusionPattern indicates if the field exclusion pattern is set.

func (*ReflectConfiguration) HasFieldExclusions added in v1.1.0

func (c *ReflectConfiguration) HasFieldExclusions() bool

HasFieldExclusions indicates if field exclusions are set.

func (*ReflectConfiguration) HasMethodExclusionPattern added in v1.1.0

func (c *ReflectConfiguration) HasMethodExclusionPattern() bool

HasMethodExclusionPattern indicates if the method exclusion pattern is set.

func (*ReflectConfiguration) HasMethodExclusions added in v1.1.0

func (c *ReflectConfiguration) HasMethodExclusions() bool

HasMethodExclusions indicates if method exclusions are set.

func (*ReflectConfiguration) HasTableAliasLength added in v1.1.0

func (c *ReflectConfiguration) HasTableAliasLength() bool

HasTableAliasLength indicates if the table alias length is set.

func (*ReflectConfiguration) HasTableAliasStrategy added in v1.1.0

func (c *ReflectConfiguration) HasTableAliasStrategy() bool

HasTableAliasStrategy indicates if the table alias strategy is set.

func (*ReflectConfiguration) HasTableName added in v1.1.0

func (c *ReflectConfiguration) HasTableName() bool

HasTableName indicates if the table name is set.

func (*ReflectConfiguration) HasTableNameStrategy added in v1.1.0

func (c *ReflectConfiguration) HasTableNameStrategy() bool

HasTableNameStrategy indicates if the table name strategy is set.

func (*ReflectConfiguration) HasTag added in v1.1.0

func (c *ReflectConfiguration) HasTag() bool

HasTag indicates if the tag is set.

func (*ReflectConfiguration) LowercaseColumnName added in v1.2.0

func (c *ReflectConfiguration) LowercaseColumnName() bool

LowercaseColumnName indicates if the column name strategy is uppercase.

func (*ReflectConfiguration) LowercaseTableAlias added in v1.1.0

func (c *ReflectConfiguration) LowercaseTableAlias() bool

LowercaseTableAlias indicates if the table alias strategy is lowercase.

func (*ReflectConfiguration) PluralTableName added in v1.1.0

func (c *ReflectConfiguration) PluralTableName() bool

PluralTableName indicates if the table name is plural.

func (*ReflectConfiguration) ScreamingSnakeCaseColumnName added in v1.2.0

func (c *ReflectConfiguration) ScreamingSnakeCaseColumnName() bool

ScreamingSnakeCaseColumnName indicates if the column name strategy is scream snake case.

func (*ReflectConfiguration) ScreamingSnakeCaseTableName added in v1.2.0

func (c *ReflectConfiguration) ScreamingSnakeCaseTableName() bool

ScreamingSnakeCaseTableName indicates if the table name strategy is screaming snake case.

func (*ReflectConfiguration) SnakeCaseColumnName added in v1.1.0

func (c *ReflectConfiguration) SnakeCaseColumnName() bool

SnakeCaseColumnName indicates if the column name strategy is snake case.

func (*ReflectConfiguration) SnakeCaseTableName added in v1.1.0

func (c *ReflectConfiguration) SnakeCaseTableName() bool

SnakeCaseTableName indicates if the table name strategy is snake case.

func (*ReflectConfiguration) UppercaseColumnName added in v1.2.0

func (c *ReflectConfiguration) UppercaseColumnName() bool

UppercaseColumnName indicates if the column name strategy is uppercase.

func (*ReflectConfiguration) UppercaseTableAlias added in v1.1.0

func (c *ReflectConfiguration) UppercaseTableAlias() bool

UppercaseTableAlias indicates if the table alias strategy is uppercase.

type ReflectOption added in v1.1.0

type ReflectOption func(*ReflectConfiguration)

ReflectOption is a function that configures the reflection configuration.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table represents a mapping between an entity and a database table.

func Reflect added in v1.1.0

func Reflect(obj any, options ...ReflectOption) (Table, error)

Reflect observes the provided object and generates metadata from it using the provided options.

func (*Table) AddColumn

func (t *Table) AddColumn(column Column) error

AddColumn adds a column to the table.

func (*Table) AddColumns

func (t *Table) AddColumns(columns ...Column) error

AddColumns adds all of the provided columns to the table.

func (*Table) Alias

func (t *Table) Alias() string

Alias retrieves the alias for the table.

func (*Table) ColumnName

func (t *Table) ColumnName(field string) (string, error)

ColumnName retrieves the column name associated to the provide field name.

func (*Table) ColumnNames

func (t *Table) ColumnNames() []string

ColumnNames retrieves all of the column names for the table.

func (*Table) Columns

func (t *Table) Columns() (columns []Column)

Columns retrieves all of the columns for the table.

func (*Table) DeleteQuery added in v1.1.0

func (t *Table) DeleteQuery(options ...QueryOption) (string, error)

DeleteQuery generates a DELETE query for the table.

func (*Table) DeleteQueryWithArgs added in v1.3.0

func (t *Table) DeleteQueryWithArgs(obj any, options ...QueryOption) (string, []any, error)

DeleteQueryWithArgs generates a DELETE query for the table along with arguments derived from the provided object.

func (*Table) Evaluate added in v1.1.0

func (t *Table) Evaluate(obj any) (EvaluationResult, error)

Evaluate applies the table to the provided object to produce a result containing the column names and their respective values. The result can then be subsequently used to execute queries.

func (*Table) FieldName

func (t *Table) FieldName(name string) (string, error)

FieldName retrieves the field name associated to the provided column name.

func (Table) FindColumns added in v1.1.0

func (t Table) FindColumns(p ColumnPredicate) []Column

FindColumn retrieves the columns that matches the provided predicate.

func (*Table) InsertQuery added in v1.1.0

func (t *Table) InsertQuery(options ...QueryOption) (string, error)

InsertQuery generates an INSERT query for the table.

func (*Table) InsertQueryWithArgs added in v1.3.0

func (t *Table) InsertQueryWithArgs(obj any, options ...QueryOption) (string, []any, error)

InsertQueryWithArgs generates an INSERT query for the table along with arguments derived from the provided object.

func (*Table) MustDeleteQuery added in v1.3.0

func (t *Table) MustDeleteQuery(options ...QueryOption) string

MustDeleteQuery performs the same operation as DeleteQuery but panics if an error occurs.

func (*Table) MustEvaluate added in v1.1.0

func (t *Table) MustEvaluate(obj any) EvaluationResult

MustEvaluate performs the same operation as Evaluate but panics if an error occurs.

func (*Table) MustInsertQuery added in v1.3.0

func (t *Table) MustInsertQuery(options ...QueryOption) string

MustInsertQuery performs the same operation as InsertQuery but panics if an error occurs.

func (*Table) MustSelectQuery added in v1.4.0

func (t *Table) MustSelectQuery(options ...QueryOption) string

MustSelectQuery performs the same operation as SelectQuery but panics if an error occurs.

func (*Table) MustUpdateQuery added in v1.3.0

func (t *Table) MustUpdateQuery(options ...QueryOption) string

MustUpdateQuery performs the same operation as UpdateQuery but panics if an error occurs.

func (*Table) Name

func (t *Table) Name() string

Name retrieves the the table name.

func (*Table) SelectQuery added in v1.4.0

func (t *Table) SelectQuery(options ...QueryOption) (string, error)

SelectQuery generates a SELECT query for the table.

func (*Table) SelectQueryWithArgs added in v1.4.0

func (t *Table) SelectQueryWithArgs(obj any, options ...QueryOption) (string, []any, error)

SelectQueryWithArgs generates a SELECT query for the table along with arguments derived from the provided object.

func (*Table) SetAlias

func (t *Table) SetAlias(alias string)

SetAlias modifies the alias of the table.

func (*Table) SetName

func (t *Table) SetName(name string)

SetName modifies the name of the table.

func (*Table) SetType

func (t *Table) SetType(entity any)

SetType associates the entity type to the table.

func (*Table) SetTypeName

func (t *Table) SetTypeName(typeName string)

SetTypeName modifies the entity type name for the table.

func (*Table) TypeName

func (t *Table) TypeName() string

TypeName retrieves the type name of the entity associated to the table.

func (*Table) UpdateQuery added in v1.1.0

func (t *Table) UpdateQuery(options ...QueryOption) (string, error)

UpdateQuery generates an UPDATE query for the table.

func (*Table) UpdateQueryWithArgs added in v1.3.0

func (t *Table) UpdateQueryWithArgs(obj any, options ...QueryOption) (string, []any, error)

UpdateQueryWithArgs generates an UPDATE query for the table along with arguments derived from the provided object.

type TableConfiguration

type TableConfiguration struct {
	TypeName string                `json:"typeName" yaml:"typeName"`
	Name     string                `json:"name" yaml:"name"`
	Alias    string                `json:"alias" yaml:"alias"`
	Columns  []ColumnConfiguration `json:"columns" yaml:"columns"`
}

TableConfiguration represents the configuration used to construct a single table mapping.

type YAMLLoader

type YAMLLoader struct{}

func (YAMLLoader) Load

func (l YAMLLoader) Load(path string) (c Configuration, err error)

Load loads the configuration from the YAML file provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL