sqlxselect

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 6 Imported by: 0

README

sqlx-selector

  • SELECT columns helper library for sqlx
  • Maybe useful for queries using JOIN

Usage

with sqlx

type User struct {
    ID string `db:"id"`
    Name string `db:"name"`
    GroupID string `db:"group_id"`
}
type Group struct {
    ID string `db:"id"`
    Name string `db:"name"`
}

type join struct {
    User *User `db:"user"`
    Group *Group `db:"group"`
    UserUpdatedAt time.Time `db:"user_updated_at"`
}

var j join
selector, _ := sqlxselect.New(j) # Read #1
db.QueryRowx(
    `SELECT` + 
        selector.
            SelectAs("users.updated_at", "user_updated_at").
            SelectStructAs("users.*", "user.*", "id". "name"). // select only id and name
            SelectStructAs("groups.*", "group.*").
            String() +
        `FROM users INNER JOIN groups ON users.group_id = groups.id LIMIT 1`,
).StructScan(&j)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SqlxSelector

type SqlxSelector struct {
	Errors []error
	// contains filtered or unexported fields
}

SqlxSelector is a generator of columns in SELECT query

func New

func New(dst interface{}) (*SqlxSelector, error)

New generates SqlxSelector with default mapper

func NewWithMapper

func NewWithMapper(dst interface{}, mapper *reflectx.Mapper) (*SqlxSelector, error)

NewWithMapper generates SqlxSelector with specified mapper

func (*SqlxSelector) Select

func (s *SqlxSelector) Select(column string) *SqlxSelector

Select adds the column directly to query

func (*SqlxSelector) SelectAs

func (s *SqlxSelector) SelectAs(column, as string) *SqlxSelector

SelectAs adds the column and 'AS' name directly to query

func (*SqlxSelector) SelectStruct

func (s *SqlxSelector) SelectStruct(column string, limit ...string) *SqlxSelector

SelectStruct adds columns specified as 'column' to query to store values 'limit' can specify columns to add ex. SelectStruct("users.*" /* table name */, "id", "name" /* columns to select */)

func (*SqlxSelector) SelectStructAs

func (s *SqlxSelector) SelectStructAs(column, as string, limit ...string) *SqlxSelector

SelectStructAs adds columns specified as 'column' to query to store values specified as 'as' in struct 'limit' can specify columns to add ex. SelectStructAs("users.*" /* table name */, "user." /* 'db:""' name */, "id", "name" /* columns to select */)

func (*SqlxSelector) String

func (s *SqlxSelector) String() string

func (*SqlxSelector) StringWithError

func (s *SqlxSelector) StringWithError() (string, error)

StringWithError returns columns for SELECT as string, but may return an error if something went wrong

Jump to

Keyboard shortcuts

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