Documentation ¶
Overview ¶
Package sqlutil provides utilities for database/sql for dealing with SQL queries and database records.
Index ¶
- type AssignmentList
- type DeleteStmt
- type InsertStmt
- func (s *InsertStmt) Fields(fields ...string) *InsertStmt
- func (s *InsertStmt) Into(table string) *InsertStmt
- func (s *InsertStmt) Literal(l string) *InsertStmt
- func (s *InsertStmt) QueryArgs() []interface{}
- func (s *InsertStmt) Select(stmt *SelectStmt) *InsertStmt
- func (s *InsertStmt) String() string
- func (s *InsertStmt) Values(records ...Record) *InsertStmt
- type NullTime
- type QueryConditionSet
- func (c *QueryConditionSet) And() *QueryConditionSet
- func (c *QueryConditionSet) Equal(k string, v interface{}) *QueryConditionSet
- func (c *QueryConditionSet) Group(cond *QueryConditionSet) *QueryConditionSet
- func (c *QueryConditionSet) In(k string, v interface{}) *QueryConditionSet
- func (c *QueryConditionSet) InSelect(k string, v *SelectStmt) *QueryConditionSet
- func (c *QueryConditionSet) IsNull(v string) *QueryConditionSet
- func (c *QueryConditionSet) Literal(l string) *QueryConditionSet
- func (c *QueryConditionSet) Not() *QueryConditionSet
- func (c *QueryConditionSet) Or() *QueryConditionSet
- func (c *QueryConditionSet) String() string
- func (c *QueryConditionSet) Values() []interface{}
- type Record
- type RecordType
- type Records
- type SelectStmt
- func (s *SelectStmt) From(tables ...string) *SelectStmt
- func (s *SelectStmt) Literal(l string) *SelectStmt
- func (s *SelectStmt) QueryArgs() []interface{}
- func (s *SelectStmt) Select(a *SelectStmt) *SelectStmt
- func (s *SelectStmt) SelectGroup(as string, g *SelectStmt) *SelectStmt
- func (s *SelectStmt) String() string
- func (s *SelectStmt) Where(cond *QueryConditionSet) *SelectStmt
- type UpdateStmt
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 (*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) 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 (*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) 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 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) 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 ¶
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
type QueryConditionSet ¶
type QueryConditionSet struct {
// contains filtered or unexported fields
}
QueryConditionSet represents a set of database query conditions.
func (*QueryConditionSet) And ¶
func (c *QueryConditionSet) And() *QueryConditionSet
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 ¶
func (c *QueryConditionSet) Group(cond *QueryConditionSet) *QueryConditionSet
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 ¶
func (c *QueryConditionSet) InSelect(k string, v *SelectStmt) *QueryConditionSet
InSelect adds k IN (select) to the condition set.
func (*QueryConditionSet) IsNull ¶
func (c *QueryConditionSet) IsNull(v string) *QueryConditionSet
IsNull adds v IS NULL to the query.
func (*QueryConditionSet) Literal ¶
func (c *QueryConditionSet) Literal(l string) *QueryConditionSet
Literal adds literal string l to the condition.
func (*QueryConditionSet) Not ¶
func (c *QueryConditionSet) Not() *QueryConditionSet
Not adds NOT to the set.
func (*QueryConditionSet) Or ¶
func (c *QueryConditionSet) Or() *QueryConditionSet
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.
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) 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) Where ¶
func (s *UpdateStmt) Where(cond *QueryConditionSet) *UpdateStmt
Where adds the WHERE part of the statement.