sqlutil

package
v0.0.0-...-e85a4bd Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

sqlutil

Package sqlutil provides utilities for Go's database/sql for dealing with SQL queries and database records.

Supports the following features:

  • Mapping structs to sets of columns ([]string) and values ([]interface{})
  • Using structs to build queries with bindings for safe Exec and Query
  • Binding struct fields to query response records
  • Embedding SQL schema as Go code along with an InitSchema helper function

Documentation

Overview

Package sqlutil provides utilities for database/sql for dealing with SQL queries and database records.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssignmentList

type AssignmentList struct {
	// contains filtered or unexported fields
}

AssignmentList represents a list of assignments for the UPDATE command.

func Assign

func Assign() *AssignmentList

Assign returns an empty AssignmentList.

func (*AssignmentList) Equal

func (al *AssignmentList) Equal(k string, v interface{}) *AssignmentList

Equal adds k=v to the list.

func (*AssignmentList) Literal

func (al *AssignmentList) Literal(l string) *AssignmentList

Literal adds literal string l to the list.

func (*AssignmentList) String

func (al *AssignmentList) String() string

String returns the query.

func (*AssignmentList) Values

func (al *AssignmentList) Values() []interface{}

Values returns the values associated to each assignment.

type DeleteStmt

type DeleteStmt struct {
	// contains filtered or unexported fields
}

DeleteStmt represents a DELETE statement.

func Delete

func Delete() *DeleteStmt

Delete creates and initializes a new DELETE statement.

func (*DeleteStmt) From

func (s *DeleteStmt) From(tables ...string) *DeleteStmt

From adds the FROM part of the statement.

func (*DeleteStmt) Literal

func (s *DeleteStmt) Literal(l string) *DeleteStmt

Literal adds literal string l to the statement.

func (*DeleteStmt) QueryArgs

func (s *DeleteStmt) QueryArgs() []interface{}

QueryArgs returns the values corresponding to bindings (?, ?) from all calls to Where. e.g. db.Exec(stmt.String(), stmt.QueryArgs()...)

func (*DeleteStmt) String

func (s *DeleteStmt) String() string

String returns the statement.

func (*DeleteStmt) Where

func (s *DeleteStmt) Where(cond *QueryConditionSet) *DeleteStmt

Where adds the WHERE statement followed by conditions to the statement.

type InsertStmt

type InsertStmt struct {
	// contains filtered or unexported fields
}

InsertStmt represents the INSERT statement.

func Insert

func Insert() *InsertStmt

Insert creates and initializes a new INSERT statement.

func Replace

func Replace() *InsertStmt

Replace creates and initializes a new REPLACE statement. Backed by InsertStmt to be used interchangeably.

func (*InsertStmt) Fields

func (s *InsertStmt) Fields(fields ...string) *InsertStmt

Fields adds the (f1, fN) part of the insert part of the statement.

func (*InsertStmt) Into

func (s *InsertStmt) Into(table string) *InsertStmt

Into adds the INTO part of the statement.

func (*InsertStmt) Literal

func (s *InsertStmt) Literal(l string) *InsertStmt

Literal adds literal string l to the statement.

func (*InsertStmt) QueryArgs

func (s *InsertStmt) QueryArgs() []interface{}

QueryArgs returns the values corresponding to bindings (?, ?) from all calls to Values. e.g. db.Exec(stmt.String(), stmt.QueryArgs()...)

func (*InsertStmt) Select

func (s *InsertStmt) Select(stmt *SelectStmt) *InsertStmt

Select adds a Select to the statement.

func (*InsertStmt) String

func (s *InsertStmt) String() string

String returns the statement.

func (*InsertStmt) Values

func (s *InsertStmt) Values(records ...Record) *InsertStmt

Values adds the (v1, vN) part of the insert part of the statement. Each record is added to the statement as a set of bindings (?, ?), and their values recorded. Use QueryArgs to get the values recorded.

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool // Valid is true if Time is not NULL
}

NullTime represents a time.Time that may be null. NullTime implements the sql.Scanner interface so it can be used as a scan destination, similar to sql.NullString.

code borrowed from github.com/lib/pq

func (*NullTime) Scan

func (nt *NullTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullTime) Value

func (nt NullTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type QueryConditionSet

type QueryConditionSet struct {
	// contains filtered or unexported fields
}

QueryConditionSet represents a set of database query conditions.

func Cond

func Cond() *QueryConditionSet

Cond returns an empty QueryConditioSet.

func (*QueryConditionSet) And

And adds AND to the set.

func (*QueryConditionSet) Equal

func (c *QueryConditionSet) Equal(k string, v interface{}) *QueryConditionSet

Equal adds k=v to the set.

func (*QueryConditionSet) Group

Group adds (cond) to the set.

func (*QueryConditionSet) In

func (c *QueryConditionSet) In(k string, v interface{}) *QueryConditionSet

In adds k IN (v) to the condition set. v must be a slice of any type or a *SelectStmt.

func (*QueryConditionSet) InSelect

InSelect adds k IN (select) to the condition set.

func (*QueryConditionSet) IsNull

IsNull adds v IS NULL to the query.

func (*QueryConditionSet) Literal

Literal adds literal string l to the condition.

func (*QueryConditionSet) Not

Not adds NOT to the set.

func (*QueryConditionSet) Or

Or adds OR to the set.

func (*QueryConditionSet) String

func (c *QueryConditionSet) String() string

String returns the query.

func (*QueryConditionSet) Values

func (c *QueryConditionSet) Values() []interface{}

Values returns the values associated to each condition.

type Record

type Record interface {
	Subset(...string) Record // Subset returns a subset of the record fields.
	Fields() []string        // Fields return the struct field name to be used as table column
	Values() []interface{}   // Values return the struct field value to be used in Scan
}

Record represents a database record. See RecordType for mapping structs to Records.

type RecordType

type RecordType struct {
	T interface{}
}

RecordType implements the Record interface for any struct with 'sql' tags as T.

func NewRecordType

func NewRecordType(T interface{}) RecordType

NewRecordType creates and initializes a new RecordType.

func (RecordType) Fields

func (r RecordType) Fields() []string

Fields returns a list of table column names from struct fields. Uses the 'db' tag if available.

func (RecordType) Subset

func (r RecordType) Subset(fields ...string) Record

Subset returns a subset of the struct fields from r.T.

func (RecordType) Values

func (r RecordType) Values() []interface{}

Values returns a list of table column values from struct fields. These values are suitable for Row.Scan from Exec (e.g. INSERT, REPLACE) calls.

type Records

type Records []Record

Records implements the Record interface for a slice of Records.

func NewRecords

func NewRecords(T interface{}) Records

NewRecords creates and initializes new Records from slice of any struct.

func (Records) Fields

func (r Records) Fields() []string

Fields returns a list of table column names from the first Record.

func (Records) Subset

func (r Records) Subset(fields ...string) Record

Subset returns Records with a subset of their fields.

In order to use the Subset of Records (a []Record), one needs to type assert it: rows.Scan(NewRecords(&r).Subset("a", "b").(Records)...).

func (Records) Values

func (r Records) Values() []interface{}

Values returns a list of table column values from all Records.

type SelectStmt

type SelectStmt struct {
	// contains filtered or unexported fields
}

SelectStmt represents a SELECT statement.

func Select

func Select(fields ...string) *SelectStmt

Select creates and initializes a new SELECT statement.

func (*SelectStmt) From

func (s *SelectStmt) From(tables ...string) *SelectStmt

From adds the FROM part of the statement.

func (*SelectStmt) Literal

func (s *SelectStmt) Literal(l string) *SelectStmt

Literal adds literal string l to the statement. Useful for e.g. UNION, JOIN, LIMIT.

func (*SelectStmt) QueryArgs

func (s *SelectStmt) QueryArgs() []interface{}

QueryArgs returns the values corresponding to bindings (?, ?) from all calls to Where. e.g. db.Query(stmt.String(), stmt.QueryArgs()...)

func (*SelectStmt) Select

func (s *SelectStmt) Select(a *SelectStmt) *SelectStmt

Select adds another Select statement to the statement. e.g. Select("*").From().Select(...)

func (*SelectStmt) SelectGroup

func (s *SelectStmt) SelectGroup(as string, g *SelectStmt) *SelectStmt

SelectGroup adds another SelectStmt to the statement, as a group, with an optional alias.

func (*SelectStmt) String

func (s *SelectStmt) String() string

String returns the statement.

func (*SelectStmt) Where

func (s *SelectStmt) Where(cond *QueryConditionSet) *SelectStmt

Where adds the WHERE statement followed by conditions to the statement.

type UpdateStmt

type UpdateStmt struct {
	// contains filtered or unexported fields
}

UpdateStmt represents the UPDATE statement.

func Update

func Update(tables ...string) *UpdateStmt

Update creates and initializes a new UPDATE statement.

func (*UpdateStmt) QueryArgs

func (s *UpdateStmt) QueryArgs() []interface{}

QueryArgs returns the values corresponding to bindings (?, ?) from all calls to Set and Where. e.g. db.Exec(stmt.String(), stmt.QueryArgs()...)

func (*UpdateStmt) Set

func (s *UpdateStmt) Set(al *AssignmentList) *UpdateStmt

Set adds the SET part of the statement.

func (*UpdateStmt) String

func (s *UpdateStmt) String() string

String returns the statement.

func (*UpdateStmt) Where

func (s *UpdateStmt) Where(cond *QueryConditionSet) *UpdateStmt

Where adds the WHERE part of the statement.

Directories

Path Synopsis
b64schema converts a SQL schema file into base64 encoded strings as Go code.
b64schema converts a SQL schema file into base64 encoded strings as Go code.

Jump to

Keyboard shortcuts

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