Categorygithub.com/pdk/crud
modulepackage
0.0.2
Repository: https://github.com/pdk/crud.git
Documentation: pkg.go.dev

# README

crud

Database CRUD operations in go using reflection

Attempt to support all CRUD (insert, update, delete, query) operations with a minimal amount of boilerplate code, either written or generated.

At system initialization, functions are created and stored in a Machine. The functions may be used directly, or methods can be written to encapsulate the needed type assertions.

Example:

import (
    "database/sql"
    "github.com/pdk/crud"
)

type Foo struct {
    ID      int64
    Name    string
    Age     int
    Address string
    Salary  float32
}

var (
    crudMachine = crud.NewMachineGetID("foo", Foo{}, "ID")
)

func (f Foo) Insert(db *sql.DB) (Foo, error) {
    var err error
    f.ID, err = crudMachine.InsertGetID(db, f)
    return f, err
}

func (f Foo) Update(db *sql.DB) (Foo, error) {
    err := crudMachine.Update(db, f)
    return f, err
}

func (f Foo) Delete(db *sql.DB) (Foo, error) {
    err := crudMachine.Delete(db, f)
    return f, err
}

func QueryFoo(db *sql.DB, querySQL string, queryParams ...interface{}) ([]Foo, error) {
    results, err := crudMachine.Query(db, querySQL, queryParams...)
    return results.([]Foo), err
}

func QueryOneRowFoo(db *sql.DB, querySQL string, queryParams ...interface{}) (Foo, error) {
    result, err := crudMachine.QueryOneRow(db, querySQL, queryParams...)
    return result.(Foo), err
}

Struct tags, ala db:"bar" are supported to specifying column names, if they differ from field names.

Limitations: Supports multiple bind marker styles, but InsertGetID only does postgres style ID retrieval.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

NewAutoIncrIDInserter generates a new Inserter.
NewDeleter creates a deleter CRUDFunc.
NewInserter generates a new Inserter.
NewMachine creates a new CRUD machine that supports standard operations.
NewMachineGetID creates a new CRUD machine that supports standard operations, and gets the ID value for autogen IDs.
NewQueryFunc creates a function to execute a query and return all results as a slice of the original struct type.
NewQueryOneRowFunc creates a function to execute a QueryOneRow and return an instance of the original struct type.
NewUpdater creates a new updater CRUDFunc.
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Machine is a holder of various CRUD functions.

# Type aliases

No description provided by the author
CRUDFunc is a function that executes some function in a database for a particular value.
CRUDFuncGetID is a function that executes some function (insert) in a database, and returns an integer result (the newly generated ID value).
QueryFunc is a function that executes a query, with some bind parameters, and returns a value.