Documentation ¶
Index ¶
- Constants
- Variables
- func New(c *Config) (*sqlx.DB, error)
- func WrapError(err error) error
- type Config
- type DB
- type Field
- type Logger
- type QueryOption
- type QueryOptions
- type Scanner
- type Selector
- type Table
- func (t *Table[T]) DeleteByID(ctx context.Context, db DB, ids ...interface{}) error
- func (t *Table[T]) GenerateAdditionalFields(coalesce bool) string
- func (t *Table[T]) GenerateDeleteByIDQuery() string
- func (t *Table[T]) GenerateGetByFieldsQuery(fields ...string) string
- func (t *Table[T]) GenerateGetByIDQuery() string
- func (t *Table[T]) GenerateInsertQuery() string
- func (t *Table[T]) GenerateSelectFields() string
- func (t *Table[T]) GenerateSelectorQuery() string
- func (t *Table[T]) GenerateUpdateQuery() string
- func (t *Table[T]) GenerateUpsertQuery() string
- func (t *Table[T]) GetByID(ctx context.Context, db DB, ids ...interface{}) (*T, error)
- func (t *Table[T]) GetByQuery(ctx context.Context, db DB, query string, values ...interface{}) (*T, error)
- func (t *Table[T]) Insert(ctx context.Context, db DB, record *T, opts ...QueryOption) error
- func (t *Table[T]) Update(ctx context.Context, db DB, record *T, opts ...QueryOption) error
- func (t *Table[T]) Upsert(ctx context.Context, db DB, record *T, opts ...QueryOption) error
Constants ¶
View Source
const Value = "$#"
Variables ¶
View Source
var DefaultQueryOptions = QueryOptions{ IgnoreReturn: false, }
Functions ¶
Types ¶
type Config ¶
type Config struct { Username string `conf:"username" default:"postgres"` Password string `conf:"password" default:"password"` Host string `conf:"host" default:"postgres"` Port string `conf:"port" default:"5432"` Database string `conf:"database" default:"postgres"` Schema string `conf:"schema" default:"public"` AutoCreate bool `conf:"auto_create" default:"false"` SearchPath string `conf:"search_path" default:""` SSLMode string `conf:"sslmode" default:"disable"` SSLCert string `conf:"sslcert" default:""` SSLKey string `conf:"sslkey" default:""` SSLRootCert string `conf:"sslrootcert" default:""` Retries int `conf:"retries" default:"5"` SleepBetweenRetries time.Duration `conf:"sleep_between_retries" default:"7s"` MaxConnections int `conf:"max_connections" default:"40"` WipeConfirm bool `conf:"wipe_confirm" default:"false"` Logger Logger QueryLogger Logger MigrationSource source.Driver }
type Field ¶
type Field[T any] struct { // The field name Name string // Is this field part of the record ID (a primary key). You can have // multiple ID fields on a record. ID bool // Should this field be used on a select statement. (used for auto // generating select statements.) Select bool // The value to use when inserting this field into the database. If you want // to use a positional argument, use the `Value` constant. Insert string // The value to use when updating this field in the database. If you want // to use a positional argument, use the `Value` constant. Update string // This function is used to fetch the value for insert or update from a record. Value func(*T) (driver.Value, error) // This is used to determine the value that should be returned if the // value is being returned in a COALESCED way. For example, if you left join this // table and there is no value, this would be the value returned if you use the // GenerateAdditionalFields(coalesce=true) to generate the AdditionalFields // string NullVal any }
Field is the field representation for each field in the table.
type Logger ¶
type Logger interface {
Printf(template string, args ...interface{})
}
The Logger Inferface for the database driver.
type QueryOption ¶
type QueryOption func(opt *QueryOptions) error
func QueryOptionIgnoreReturn ¶
func QueryOptionIgnoreReturn(v bool) QueryOption
type QueryOptions ¶
type QueryOptions struct { // Ignore return will cause the query to ignore the return value from a // Insert, Upsert or Update query. Normally these queries will return/update // in place the new value it is returning. IgnoreReturn bool }
type Selector ¶
type Selector[T any] struct { // The query to use Query string // OmitCount will not run the count query OmitCount bool // CountQuery allows you to override the query used to obtain counts with the same filter parameters. // If not specified it will wrap the Query provided in select count(*) CountQuery string // The fields you want to allow filtering on and the types FilterFieldTypes queryp.FilterFieldTypes // The fields you want to allow sorting on SortFields queryp.SortFields // If no sort is provided in the QueryParameters, the default sort to use. DefaultSort queryp.Sort // A callback to be used on every record to provide any transformations. PostProcessRecord func(*T) error // A callback to be used on the slice of records being returned before they are returned. PostProcessRecords func([]*T) error }
Selector is a tool for fetching slices of records based on any query.
func (*Selector[T]) SelectFirst ¶
type Table ¶
type Table[T any] struct { // Schema to use if you want to hard code it Schema string // Table name Table string // The fields you wish to reference for select, insert and update operations Fields []*Field[T] // Additional joins when fetching data from the table Joins string // Selector is a tool for fetching multiple rows from a table, using // queryp to filter results. Selector[T] // Scanner is used Scanner Scanner[T] // This is a callback that is used after fetching a row of data before // returning it. PostProcessRecord func(*T) error // The select portion of the query for just the fields in this table. // It should not include the SELECT keyword, just comma separated fields. // If this is not specified it will be built automatically based on the fields // provided above. SelectFields string // Additional fields you wish to select from the main query. Generally // associated with the Joins but could be anything. Just provide comma // separated field statements. SelectAdditionalFields string // The query used to get a record by ID. If not specified will be auto // generated. GetByIDQuery string // The query used to delete a record by ID. If not specified will be // auto generated. DeleteByIDQuery string // The query used to insert a record. If not specified will be auto generated. InsertQuery string // The query used to update a record. If not specified will be auto generated by ID. UpdateQuery string // The query used to upsert a record. If not specified will be auto generated by ID. UpsertQuery string }
Table is the query builder table representation.
func (*Table[T]) DeleteByID ¶
DeleteByID deletes a single record by ID(s)
func (*Table[T]) GenerateAdditionalFields ¶
func (*Table[T]) GenerateDeleteByIDQuery ¶
func (*Table[T]) GenerateGetByFieldsQuery ¶
func (*Table[T]) GenerateGetByIDQuery ¶
func (*Table[T]) GenerateInsertQuery ¶
func (*Table[T]) GenerateSelectFields ¶
func (*Table[T]) GenerateSelectorQuery ¶
func (*Table[T]) GenerateUpdateQuery ¶
func (*Table[T]) GenerateUpsertQuery ¶
func (*Table[T]) GetByQuery ¶
func (t *Table[T]) GetByQuery(ctx context.Context, db DB, query string, values ...interface{}) (*T, error)
GetByQuery fetches a single record by the given query and values
Click to show internal directories.
Click to hide internal directories.