sqlxselect

package module
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2019 License: MIT Imports: 6 Imported by: 0

README

sqlx-selector

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

Installation

go get github.com/cs3238-tsuzu/sqlx-selector/v2

Usage

with sqlx

package main

import (
	"fmt"
	"time"

	sqlxselect "github.com/cs3238-tsuzu/sqlx-selector/v2"
)

func main() {
	type User struct {
		ID             string `db:"id"`
		Name           string `db:"name"`
		OrganizationID string `db:"org_id"`
	}
	type Organization struct {
		ID   string `db:"id"`
		Name string `db:"name"`
	}

	var joined struct {
		User          *User         `db:"u"`
		Organization  *Organization `db:"org"`
		UserUpdatedAt time.Time     `db:"updated_at"`
	}

	fmt.Println(
		`SELECT ` +
			sqlxselect.New(&joined).
				SelectAs("u.updated_at", "updated_at").
				SelectStructAs("u.*", "u.*", "id", "name"). // select only id and name
				SelectStructAs("org.*", "org.*").
				String() +
			`FROM users AS u INNER JOIN organizations AS org ON u.org_id = org.id LIMIT 1`,
	)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultColumnEscaper = Backquote

DefaultColumnEscaper is used in New/NewWithMapper

Functions

This section is empty.

Types

type ColumnEscaper added in v2.2.0

type ColumnEscaper func(s string) string

ColumnEscaper is a helper to escape column names

var (
	// Backquote for MySQL
	Backquote ColumnEscaper = func(s string) string {
		spl := strings.Split(s, ".")

		for i := range spl {
			spl[i] = "`" + spl[i] + "`"
		}
		return strings.Join(spl, ".")
	}

	// Doublequote for SQLite and PostgreSQL
	Doublequote ColumnEscaper = func(s string) string {
		spl := strings.Split(s, ".")

		for i := range spl {
			spl[i] = doubleQuote(spl[i])
		}
		return strings.Join(spl, ".")
	}
)

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

New generates SqlxSelector with default mapper

func NewWithMapper

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

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

func (*SqlxSelector) WithColumnEscaper added in v2.2.0

func (s *SqlxSelector) WithColumnEscaper(ce ColumnEscaper) *SqlxSelector

WithColumnEscaper specifies a function to escape column names

Jump to

Keyboard shortcuts

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