ezsqlx

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: Apache-2.0 Imports: 10 Imported by: 1

README

ezsqlx

ezsqlx is a library which provides helpers for jmoiron/sqlx. It's goal is to improve your experience without incurring the penalties of an ORM.

Features
  • Insert
  • Update
  • Connection management
  • Get database fields from a struct

Examples

Given a basic model struct, ezsqlx simplifies basic operations.

type FooBar struct {
    Id      int        `db:"id"`
    Message string     `db:"message"`
    Flip    bool       `db:"flip"`
    Created *time.Time `db:"created"`
}
Insert
Interface
Insert(
    db *sqlx.DB,
    table string,
    model interface{},
    excludedFields []string
) (*sqlx.Rows, error)
Example
newRow := &FooBar{Message: "confused unga bunga"}
rows, err := Insert(db, "foobar", newRow, []string{"id", "created"})
Update
Interface
Update(
    db *sqlx.DB,
    table string,
    model interface{},
    where string,
    excludedFields []string
) (sql.Result, error) 
Example
updatedRow := &FooBar{Id: 1, Message: "pc master race", Flip: true}
where := fmt.Sprintf("id=%v", updatedRow.Id)
_, err = Update(db, "foobar", updatedRow, where, []string{"id", "created"})
Fields
Interface
Fields(
    values interface{}
) []string
Example
fields := Fields(model)
Connections

ezsqlx.ConnectionSettings abstracts away basic Postgres connection operations. Check connections.go for a full list of helpers.

Example
cs := &ConnectionSettings{
    Host: "localhost",
    Port: "1234",
    User: "postgres",
    Password: "postgres",
    Database: "my_database"
}

db := cs.Open()
defer db.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Columns added in v1.0.1

func Columns(sf []reflect.StructField) []string

func Insert

func Insert(db *sqlx.DB, table string, model interface{}, excludedFields []string) (*sqlx.Rows, error)

func InsertQuery added in v1.0.1

func InsertQuery(table string, model interface{}) string

func Update

func Update(db *sqlx.DB, table string, model interface{}, where string, excludedFields []string) (sql.Result, error)

func UpsertQuery added in v1.0.1

func UpsertQuery(table string, model interface{}, constraint string) string

Types

type ConnectionSettings

type ConnectionSettings struct {
	Driver       string
	Host         string
	Port         string
	User         string
	Password     string
	Database     string
	DisableSSL   bool
	MaxOpenConns int
}

func (*ConnectionSettings) Connect

func (cs *ConnectionSettings) Connect() (*sqlx.DB, error)

func (*ConnectionSettings) Copy

func (*ConnectionSettings) Init

func (cs *ConnectionSettings) Init() (*sqlx.DB, error)

func (*ConnectionSettings) Open

func (cs *ConnectionSettings) Open() (*sqlx.DB, error)

func (*ConnectionSettings) Ping

func (cs *ConnectionSettings) Ping() error

func (*ConnectionSettings) String

func (cs *ConnectionSettings) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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