Documentation ¶
Index ¶
- func CreateParameterPlaceholders(count int, dialect SQLDialect) string
- func ParameterizeFields(fields []string, dialect SQLDialect, useAnd bool, startCountFrom ...int) string
- type Crudiator
- type DataForm
- type DbRow
- type Editor
- func (e *Editor) Build() Crudiator
- func (e Editor) Create(form DataForm, db *sql.DB) (DbRow, error)
- func (e *Editor) Debug(b bool) *Editor
- func (e Editor) Delete(form DataForm, db *sql.DB) (DbRow, error)
- func (e *Editor) MustPaginate(strategy PaginationStrategy, fields ...string) *Editor
- func (e *Editor) OnPostCreate(f PostActionCallback) *Editor
- func (e *Editor) OnPostDelete(f PostActionCallback) *Editor
- func (e *Editor) OnPostRead(f PostActionCallback) *Editor
- func (e *Editor) OnPostUpdate(f PostActionCallback) *Editor
- func (e *Editor) OnPreCreate(f PreActionCallback) *Editor
- func (e *Editor) OnPreDelete(f PreActionCallback) *Editor
- func (e *Editor) OnPreRead(f PreActionCallback) *Editor
- func (e *Editor) OnPreUpdate(f PreActionCallback) *Editor
- func (e Editor) Read(form DataForm, db *sql.DB, pageable ...Pageable) ([]DbRow, error)
- func (e *Editor) SetLogger(l Logger) *Editor
- func (e Editor) SingleRead(form DataForm, db *sql.DB) (DbRow, error)
- func (e *Editor) SoftDelete(v bool) *Editor
- func (e Editor) Update(form DataForm, db *sql.DB) (DbRow, error)
- func (e Editor) UsesKeysetPagination() bool
- type Field
- type FieldNullCheck
- type FieldOption
- type FieldType
- type KeysetPaging
- type Level
- type Logger
- type MapBackedDataForm
- type OffsetPaging
- type Pageable
- type PaginationStrategy
- type PostActionCallback
- type PreActionCallback
- type SQLDialect
- type StdOutLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateParameterPlaceholders ¶
func CreateParameterPlaceholders(count int, dialect SQLDialect) string
func ParameterizeFields ¶
func ParameterizeFields(fields []string, dialect SQLDialect, useAnd bool, startCountFrom ...int) string
Types ¶
type Crudiator ¶
type Crudiator interface { Create(form DataForm, db *sql.DB) (DbRow, error) Read(form DataForm, db *sql.DB, pageable ...Pageable) ([]DbRow, error) // Reads a single database row. May return nil,nil if no row exists SingleRead(form DataForm, db *sql.DB) (DbRow, error) // Updates the specified record and returns the updated row. // // A single statement is executed for postgres (using the RETURNING keyword) and for // all others, two statements are executed; one to update and one for the query. Update(form DataForm, db *sql.DB) (DbRow, error) Delete(form DataForm, db *sql.DB) (DbRow, error) }
type DataForm ¶
type DataForm interface { Has(name string) bool Get(name string) any Set(name string, value any) Remove(name string) Iterate(func(key string, value any)) }
func FromJsonStruct ¶
convert struct to DataForm
func FromStruct ¶
Create form from a struct. Pass an additional form to append/overwrite values
func FromXmlStruct ¶
type DbRow ¶
Represents a database row (column set)
Since this is an aliased map type, it can readily be serialized.
Call DbRow.HasData() to check if any data was read into the row
type Editor ¶
type Editor struct {
// contains filtered or unexported fields
}
Editor is the object that interacts with the underlying object.
One instance can be used multiple times concurrently as no state is stored
func MustNewEditor ¶
func MustNewEditor(table string, dialect SQLDialect, fields ...Field) *Editor
MustNewEditor Creates a table editor instance used for CRUD operations on that table.
The function will panic if fields is empty or if a duplicate field is found
func (*Editor) MustPaginate ¶
func (e *Editor) MustPaginate(strategy PaginationStrategy, fields ...string) *Editor
MustPaginate configures selection query pagination.
The function will panic if strategy is not 'KEYSET' and no fields have been defined
func (*Editor) OnPostCreate ¶
func (e *Editor) OnPostCreate(f PostActionCallback) *Editor
func (*Editor) OnPostDelete ¶ added in v0.0.5
func (e *Editor) OnPostDelete(f PostActionCallback) *Editor
func (*Editor) OnPostRead ¶
func (e *Editor) OnPostRead(f PostActionCallback) *Editor
func (*Editor) OnPostUpdate ¶
func (e *Editor) OnPostUpdate(f PostActionCallback) *Editor
func (*Editor) OnPreCreate ¶
func (e *Editor) OnPreCreate(f PreActionCallback) *Editor
func (*Editor) OnPreDelete ¶ added in v0.0.5
func (e *Editor) OnPreDelete(f PreActionCallback) *Editor
func (*Editor) OnPreRead ¶
func (e *Editor) OnPreRead(f PreActionCallback) *Editor
func (*Editor) OnPreUpdate ¶
func (e *Editor) OnPreUpdate(f PreActionCallback) *Editor
func (*Editor) SoftDelete ¶
SoftDelete Indicates whether records in this table should be soft deleted.
If true, a call to 'Delete' is converted to an 'Update', with only the columns marked for soft deletion being updated.
func (Editor) UsesKeysetPagination ¶
type Field ¶
type Field struct { PrimaryKey bool Name string Create bool Read bool Update bool Unique bool // Mark this field as a selection filter when 'Read()' is called // // Example // studentEditor := MustNewEditor( // "students", // POSTGRESQL, // NewField("name", IncludeAlways), // NewField("school_id", IncludeAlways, IsSelectionFilter) // ) // editor.Read() // SELECT "name", school_id FROM students WHERE school_id = $1 SelectionFilter bool NullCheck FieldNullCheck SoftDelete bool // Indicates whether this field should be used when soft-deleting records SoftDeleteType FieldType // Indicates the type of the soft deletion field. }
func NewField ¶
func NewField(name string, options ...FieldOption) Field
type FieldNullCheck ¶ added in v0.0.3
type FieldNullCheck int
Indicates whether a field should be checked againt the 'NULL' constant value.
For most DBMS, the following query will yield nothing:
SELECT * FROM foo WHERE col1 = NULL
Rather, the following returns data:
SELECT * FROM foo WHERE col1 IS NULL
This type's values control this behavior. This is especially useful for filter fields
const ( // Indicates that the field should always be checked normally '= ?' or '= $1'. // This is the default behavior NoFieldNullCheck FieldNullCheck = iota // Indicates that the field should always be checked as 'IS NULL' FieldMustBeNull // Indicates that the field should always be checked as 'IS NOT NULL' FieldMustNotBeNull )
type FieldOption ¶
type FieldOption func(f *Field)
var ( IsPrimaryKey FieldOption = func(f *Field) { f.PrimaryKey = true } IncludeAlways FieldOption = func(f *Field) { f.Read = true f.Create = true f.Update = true } IncludeOnCreate FieldOption = func(f *Field) { f.Create = true } IncludeOnUpdate FieldOption = func(f *Field) { f.Update = true } IncludeOnRead FieldOption = func(f *Field) { f.Read = true } IsUnique FieldOption = func(f *Field) { f.Unique = true } IsSelectionFilter FieldOption = func(f *Field) { f.SelectionFilter = true } IsNullConstant FieldOption = func(f *Field) { f.NullCheck = FieldMustBeNull } IsNotNullConstant FieldOption = func(f *Field) { f.NullCheck = FieldMustNotBeNull } SoftDeleteAs = func(t FieldType) FieldOption { return func(f *Field) { f.SoftDelete = true f.SoftDeleteType = t } } )
type FieldType ¶ added in v0.0.5
type FieldType int
Indicates the type of column the field represents, mainly used when soft-deleting.
The value for this field will automatically be set to true or its equivalent.
(int) = 1 (bool) = true (timestamp/datetime) = time.Now()
type KeysetPaging ¶
func (KeysetPaging) KeysetValue ¶
func (kp KeysetPaging) KeysetValue() any
func (KeysetPaging) Offset ¶
func (kp KeysetPaging) Offset() int
func (KeysetPaging) Size ¶
func (kp KeysetPaging) Size() int
type Logger ¶
type Logger interface { Error(e error) Info(m string, args ...any) Warn(m string, args ...any) Debug(m string, args ...any) }
func NewNopLogger ¶
func NewNopLogger() Logger
func NewStdOutLogger ¶
type MapBackedDataForm ¶
func (MapBackedDataForm) Get ¶
func (f MapBackedDataForm) Get(name string) any
func (MapBackedDataForm) Has ¶
func (f MapBackedDataForm) Has(name string) bool
func (MapBackedDataForm) Iterate ¶ added in v0.0.3
func (f MapBackedDataForm) Iterate(fn func(key string, value any))
func (MapBackedDataForm) Remove ¶
func (f MapBackedDataForm) Remove(name string)
func (MapBackedDataForm) Set ¶
func (f MapBackedDataForm) Set(name string, value any)
type OffsetPaging ¶
func (OffsetPaging) KeysetValue ¶
func (of OffsetPaging) KeysetValue() any
func (OffsetPaging) Offset ¶
func (of OffsetPaging) Offset() int
func (OffsetPaging) Size ¶
func (of OffsetPaging) Size() int
type Pageable ¶
type Pageable interface { // Returns the calculated offset. Applicable in 'OFFSET' mode Offset() int // Returns the page size Size() int // Returns the indexable value to be used. Applicable in 'KEYSET' mode KeysetValue() any }
Passed to the 'Retrieve()' function to be used for pagination
Only applicable if pagination has been configured through 'MustPaginate()' function.
func NewKeysetPaging ¶
func NewOffsetPaging ¶
type PaginationStrategy ¶
type PaginationStrategy int
Defines how to query a table. i.e select everything at once or paginate.
Default is NONE
const ( // Do not paginate the query NONE PaginationStrategy = iota // Use offset pagination where an entire table is scanned in order to get to a specific offset. // // The larger the offset, the slower the query. OFFSET // Use keyset pagination where an indexed column is used for comparison. // // Provides faster pagination better than OFFSET based. KEYSET )
type PostActionCallback ¶
type PreActionCallback ¶
type SQLDialect ¶
type SQLDialect int
The dialect to use when interacting with the underlying database
const ( MYSQL SQLDialect = iota + 1 POSTGRESQL SQLITE )
type StdOutLogger ¶
type StdOutLogger struct {
// contains filtered or unexported fields
}
func (StdOutLogger) Debug ¶
func (l StdOutLogger) Debug(m string, args ...any)
func (StdOutLogger) Error ¶
func (l StdOutLogger) Error(e error)
func (StdOutLogger) Info ¶
func (l StdOutLogger) Info(m string, args ...any)
func (StdOutLogger) Warn ¶
func (l StdOutLogger) Warn(m string, args ...any)