Documentation ¶
Overview ¶
Sol is a SQL toolkit for Go - in the style of Python's SQLAlchemy Core
Index ¶
- Constants
- Variables
- func IntegrationTest(t *testing.T, conn *DB, ddlCommit bool)
- func NewTester(t *testing.T, d dialect.Dialect) *tester
- func NoMatchingFields(fields []Field) bool
- type Aliases
- type ArrayClause
- type BinaryClause
- type Clause
- type ColumnElem
- func Avg(col Columnar) ColumnElem
- func Column(name string, datatype types.Type) ColumnElem
- func Count(col Columnar) ColumnElem
- func Date(col Columnar) ColumnElem
- func DatePart(part string, col Columnar) ColumnElem
- func Function(name string, col Columnar) ColumnElem
- func InvalidColumn(name string, tabular Tabular) (column ColumnElem)
- func Max(col Columnar) ColumnElem
- func Min(col Columnar) ColumnElem
- func StdDev(col Columnar) ColumnElem
- func Sum(col Columnar) ColumnElem
- func Variance(col Columnar) ColumnElem
- func (col ColumnElem) AddOperator(op Operator) ColumnElem
- func (col ColumnElem) Alias() string
- func (col ColumnElem) As(alias string) ColumnElem
- func (col ColumnElem) Asc() OrderedColumn
- func (col ColumnElem) Between(a, b interface{}) Clause
- func (col ColumnElem) Column() ColumnElem
- func (col ColumnElem) Columns() []ColumnElem
- func (col ColumnElem) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (col ColumnElem) Create(d dialect.Dialect) (string, error)
- func (col ColumnElem) Desc() OrderedColumn
- func (col ColumnElem) DoesNotEqual(param interface{}) BinaryClause
- func (col ColumnElem) Equals(param interface{}) BinaryClause
- func (col ColumnElem) FullName() string
- func (col ColumnElem) GTE(param interface{}) BinaryClause
- func (col ColumnElem) GreaterThan(param interface{}) BinaryClause
- func (col ColumnElem) ILike(search string) BinaryClause
- func (col ColumnElem) In(args interface{}) BinaryClause
- func (col ColumnElem) IsInvalid() bool
- func (col ColumnElem) IsNotNull() UnaryClause
- func (col ColumnElem) IsNull() UnaryClause
- func (col ColumnElem) IsValid() bool
- func (col ColumnElem) LTE(param interface{}) BinaryClause
- func (col ColumnElem) LessThan(param interface{}) BinaryClause
- func (col ColumnElem) Like(search string) BinaryClause
- func (col ColumnElem) Modify(tabular Tabular) error
- func (col ColumnElem) Name() string
- func (col ColumnElem) NotBetween(a, b interface{}) Clause
- func (col ColumnElem) NotILike(search string) BinaryClause
- func (col ColumnElem) NotLike(search string) BinaryClause
- func (col ColumnElem) NullsFirst() OrderedColumn
- func (col ColumnElem) NullsLast() OrderedColumn
- func (col ColumnElem) Orderable() OrderedColumn
- func (col ColumnElem) Table() *TableElem
- func (col ColumnElem) Type() types.Type
- type ColumnSet
- func (set ColumnSet) Add(columns ...Columnar) (ColumnSet, error)
- func (set ColumnSet) All() []ColumnElem
- func (set ColumnSet) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (set ColumnSet) Exists() bool
- func (set ColumnSet) Filter(names ...string) (out ColumnSet)
- func (set ColumnSet) FullNames() []string
- func (set ColumnSet) Get(name string) ColumnElem
- func (set ColumnSet) Has(name string) bool
- func (set ColumnSet) IsEmpty() bool
- func (set ColumnSet) MakeUnique() (UniqueColumnSet, error)
- func (set ColumnSet) Names() []string
- func (set ColumnSet) Reject(names ...string) (out ColumnSet)
- type Columnar
- type Compiles
- type ConditionalStmt
- type Conn
- type Connection
- type CreateStmt
- type CreateViewStmt
- type DB
- type DeleteStmt
- type DropStmt
- type Executable
- type FKElem
- type Field
- type FuncClause
- type InsertStmt
- type JoinClause
- type Modifier
- type Operator
- type Orderable
- type OrderedColumn
- func (ord OrderedColumn) Asc() OrderedColumn
- func (ord OrderedColumn) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (ord OrderedColumn) Desc() OrderedColumn
- func (ord OrderedColumn) NullsFirst() OrderedColumn
- func (ord OrderedColumn) NullsLast() OrderedColumn
- func (ord OrderedColumn) Orderable() OrderedColumn
- func (ord OrderedColumn) String() string
- type PKArray
- type Parameter
- type Parameters
- type Result
- type Scanner
- type SelectStmt
- func (stmt SelectStmt) All() SelectStmt
- func (stmt SelectStmt) As(alias string) SelectStmt
- func (stmt SelectStmt) Columns() []ColumnElem
- func (stmt SelectStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (stmt SelectStmt) CrossJoin(table Tabular) SelectStmt
- func (stmt SelectStmt) Distinct(columns ...Columnar) SelectStmt
- func (stmt SelectStmt) From(tables ...Tabular) SelectStmt
- func (stmt SelectStmt) FullOuterJoin(table Tabular, clauses ...Clause) SelectStmt
- func (stmt SelectStmt) GroupBy(columns ...Columnar) SelectStmt
- func (stmt SelectStmt) Having(conditions ...Clause) SelectStmt
- func (stmt SelectStmt) InnerJoin(table Tabular, clauses ...Clause) SelectStmt
- func (stmt SelectStmt) LeftOuterJoin(table Tabular, clauses ...Clause) SelectStmt
- func (stmt SelectStmt) Limit(limit int) SelectStmt
- func (stmt SelectStmt) Name() string
- func (stmt SelectStmt) Offset(offset int) SelectStmt
- func (stmt SelectStmt) OrderBy(ords ...Orderable) SelectStmt
- func (stmt SelectStmt) RightOuterJoin(table Tabular, clauses ...Clause) SelectStmt
- func (stmt SelectStmt) String() string
- func (stmt SelectStmt) Table() *TableElem
- func (stmt SelectStmt) Where(conditions ...Clause) SelectStmt
- type Selectable
- type SelfFKElem
- type Stmt
- type String
- type TX
- type TableElem
- func (table TableElem) C(name string) ColumnElem
- func (table TableElem) Column(name string) ColumnElem
- func (table TableElem) Columns() []ColumnElem
- func (table TableElem) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (table *TableElem) Create() CreateStmt
- func (table *TableElem) Delete() DeleteStmt
- func (table *TableElem) Drop() DropStmt
- func (table *TableElem) ForeignKeys() []FKElem
- func (table *TableElem) Has(name string) bool
- func (table *TableElem) Insert() InsertStmt
- func (table *TableElem) Name() string
- func (table TableElem) PrimaryKey() PKArray
- func (table *TableElem) ReferencedBy() []FKElem
- func (table *TableElem) Select(selections ...Selectable) (stmt SelectStmt)
- func (table *TableElem) Table() *TableElem
- func (table *TableElem) Update() UpdateStmt
- type Tabular
- type TextStmt
- type Transaction
- type UnaryClause
- type UniqueArray
- type UniqueColumnSet
- type UpdateStmt
- type Values
- func (v Values) Compile(d dialect.Dialect, ps *Parameters) (string, error)
- func (v Values) Diff(other Values) Values
- func (v Values) Equals(other Values) bool
- func (v Values) Exclude(keys ...string) Values
- func (v Values) Filter(keys ...string) Values
- func (v Values) Keys() []string
- func (v Values) MarshalJSON() ([]byte, error)
- func (v Values) Merge(others ...Values) Values
- func (v Values) Reject(keys ...string) Values
- func (v Values) Values() []interface{}
- type ViewElem
Constants ¶
const ( Equal = "=" NotEqual = "<>" GreaterThan = ">" GreaterThanOrEqual = ">=" LessThan = "<" LessThanOrEqual = "<=" )
Conditionals ----
const ( NoAction fkAction = "NO ACTION" Restrict fkAction = "RESTRICT" Cascade fkAction = "CASCADE" SetNull fkAction = "SET NULL" SetDefault fkAction = "SET DEFAULT" )
The following constants represent possible foreign key actions that can be used in ON DELETE and ON UPDATE clauses.
const ( OmitEmpty = "omitempty" // Skip this field if it has a zero value OmitUpdate = "omitupdate" // Skip this field during updates )
Common options
const ( AVG = "AVG" COUNT = "COUNT" CROSSJOIN = "CROSS JOIN" DATE = "DATE" DATEPART = "DATE_PART" DELETE = "DELETE" DISTINCT = "DISTINCT" FROM = "FROM" FULLOUTERJOIN = "FULL OUTER JOIN" GROUPBY = "GROUP BY" HAVING = "HAVING" INNERJOIN = "INNER JOIN" INSERT = "INSERT" INTO = "INTO" LEFTOUTERJOIN = "LEFT OUTER JOIN" LIMIT = "LIMIT" MAX = "MAX" MIN = "MIN" OFFSET = "OFFSET" ORDERBY = "ORDER BY" RIGHTOUTERJOIN = "RIGHT OUTER JOIN" SELECT = "SELECT" SET = "SET" STDDEV = "STDDEV" SUM = "SUM" UPDATE = "UPDATE" VALUES = "VALUES" VARIANCE = "VARIANCE" WHERE = "WHERE" WHITESPACE = " " )
Tokens holds SQL tokens that are used for compilation / AST parsing
Variables ¶
var ErrNoColumns = errors.New(
"sol: cannot compile a statement without columns",
)
ErrNoColumns is returned when attempting to compile a query without any columns
Functions ¶
func IntegrationTest ¶
IntegrationTest runs a large, neutral dialect test
func NoMatchingFields ¶
NoMatchingFields returns true if no fields exist
Types ¶
type ArrayClause ¶
type ArrayClause struct {
// contains filtered or unexported fields
}
ArrayClause is any number of clauses with a column join
func (ArrayClause) Compile ¶
func (c ArrayClause) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile returns the ArrayClause as a compiled string using the given Dialect - possibly with an error. Any parameters will be appended to the given Parameters.
func (ArrayClause) String ¶
func (c ArrayClause) String() string
String returns the parameter-less ArrayClause in a neutral dialect.
func (ArrayClause) Wrap ¶
func (c ArrayClause) Wrap(str string) string
Wrap implements the Operator interface TODO Should Wrap operate on a clause and return a clause?
type BinaryClause ¶
BinaryClause is two clauses with a separator
func (BinaryClause) Compile ¶
func (c BinaryClause) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile returns the BinaryClause as a compiled string using the given Dialect - possibly with an error. Any parameters will be appended to the given Parameters.
func (BinaryClause) String ¶
func (c BinaryClause) String() string
String returns the parameter-less BinaryClause in a neutral dialect.
type Clause ¶
type Clause interface { Compiles }
Clause is the interface that all structural components of a statement must implement
type ColumnElem ¶
type ColumnElem struct {
// contains filtered or unexported fields
}
ColumnElem is a dialect neutral implementation of a SQL column
func Column ¶
func Column(name string, datatype types.Type) ColumnElem
Column is the constructor for a ColumnElem
func Count ¶
func Count(col Columnar) ColumnElem
Count returns a column wrapped in the COUNT() function
func DatePart ¶
func DatePart(part string, col Columnar) ColumnElem
DatePart returns a column wrapped in the DATE_PART() function TODO This method is unsafe - it should not accept direct user input
func Function ¶
func Function(name string, col Columnar) ColumnElem
Function adds a generic function to the column
func InvalidColumn ¶
func InvalidColumn(name string, tabular Tabular) (column ColumnElem)
InvalidColumn creates an invalid ColumnElem
func StdDev ¶
func StdDev(col Columnar) ColumnElem
StdDev returns a column wrapped in the STDDEV() function
func Variance ¶
func Variance(col Columnar) ColumnElem
Variance returns a column wrapped in the VARIANCE() function
func (ColumnElem) AddOperator ¶
func (col ColumnElem) AddOperator(op Operator) ColumnElem
func (ColumnElem) As ¶
func (col ColumnElem) As(alias string) ColumnElem
As sets an alias and returns a copy of the ColumnElem
func (ColumnElem) Asc ¶
func (col ColumnElem) Asc() OrderedColumn
Asc returns an OrderedColumn. It is the same as passing the column itself to an ORDER BY clause.
func (ColumnElem) Between ¶
func (col ColumnElem) Between(a, b interface{}) Clause
func (ColumnElem) Column ¶
func (col ColumnElem) Column() ColumnElem
Column returns a copy of its receiver Column. This method implements the Columnar interface.
func (ColumnElem) Columns ¶
func (col ColumnElem) Columns() []ColumnElem
Columns returns the ColumnElem itself in a slice of ColumnElem. This method implements the Selectable interface.
func (ColumnElem) Compile ¶
func (col ColumnElem) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile produces the dialect specific SQL and adds any parameters in the clause to the given Parameters instance
func (ColumnElem) Create ¶
func (col ColumnElem) Create(d dialect.Dialect) (string, error)
Create implements the Creatable interface that outputs a column of a CREATE TABLE statement.
func (ColumnElem) Desc ¶
func (col ColumnElem) Desc() OrderedColumn
Desc returns an OrderedColumn that will sort in descending order.
func (ColumnElem) DoesNotEqual ¶
func (col ColumnElem) DoesNotEqual(param interface{}) BinaryClause
DoesNotEqual creates a does not equal clause that can be used in conditional clauses.
table.Select().Where(table.C("id").DoesNotEqual(3))
func (ColumnElem) Equals ¶
func (col ColumnElem) Equals(param interface{}) BinaryClause
Equals creates an equals clause that can be used in conditional clauses.
table.Select().Where(table.C("id").Equals(3))
func (ColumnElem) FullName ¶
func (col ColumnElem) FullName() string
FullName prefixes the column name with the table name It does not include operators (such as 'max')
func (ColumnElem) GTE ¶
func (col ColumnElem) GTE(param interface{}) BinaryClause
GTE creates a greater than or equal to clause that can be used in conditional clauses.
table.Select().Where(table.C("id").GTE(3))
func (ColumnElem) GreaterThan ¶
func (col ColumnElem) GreaterThan(param interface{}) BinaryClause
GreaterThan creates a greater than clause that can be used in conditional clauses.
table.Select().Where(table.C("id").GreaterThan(3))
func (ColumnElem) ILike ¶
func (col ColumnElem) ILike(search string) BinaryClause
ILike creates a case insensitive pattern matching clause that can be used in conditional clauses.
table.Select().Where(table.C("name").ILike(`_b%`))
func (ColumnElem) In ¶
func (col ColumnElem) In(args interface{}) BinaryClause
In creates a comparison clause with an IN operator that can be used in conditional clauses. An interface is used because the args may be of any type: ints, strings...
table.Select().Where(table.C("id").In([]int64{1, 2, 3}))
func (ColumnElem) IsInvalid ¶
func (col ColumnElem) IsInvalid() bool
IsInvalid will return true when a column that does not exist was created by a table function - such as .Column() or .C()
func (ColumnElem) IsNotNull ¶
func (col ColumnElem) IsNotNull() UnaryClause
IsNotNull creates a comparison clause that can be used for checking absence of NULLs in conditional clauses.
table.Select().Where(table.C("name").IsNotNull())
func (ColumnElem) IsNull ¶
func (col ColumnElem) IsNull() UnaryClause
IsNull creates a comparison clause that can be used for checking existence of NULLs in conditional clauses.
table.Select().Where(table.C("name").IsNull())
func (ColumnElem) IsValid ¶
func (col ColumnElem) IsValid() bool
IsValid returns true unless the column has been marked invalid. It may be a false positive.
func (ColumnElem) LTE ¶
func (col ColumnElem) LTE(param interface{}) BinaryClause
LTE creates a less than or equal to clause that can be used in conditional clauses.
table.Select().Where(table.C("id").LTE(3))
func (ColumnElem) LessThan ¶
func (col ColumnElem) LessThan(param interface{}) BinaryClause
LessThan creates a less than clause that can be used in conditional clauses.
table.Select().Where(table.C("id").LessThan(3))
func (ColumnElem) Like ¶
func (col ColumnElem) Like(search string) BinaryClause
Like creates a pattern matching clause that can be used in conditional clauses.
table.Select().Where(table.C["name"].Like(`_b%`))
func (ColumnElem) Modify ¶
func (col ColumnElem) Modify(tabular Tabular) error
Modify implements the Modifier interface, allowing the ColumnElem to modify the given TableElem.
func (ColumnElem) Name ¶
func (col ColumnElem) Name() string
Name returns the name of the column - unescaped and without an alias
func (ColumnElem) NotBetween ¶
func (col ColumnElem) NotBetween(a, b interface{}) Clause
func (ColumnElem) NotILike ¶
func (col ColumnElem) NotILike(search string) BinaryClause
NotILike creates a negated, case insensitive pattern matching clause
func (ColumnElem) NotLike ¶
func (col ColumnElem) NotLike(search string) BinaryClause
NotLike creates a pattern matching clause that can be used in conditional clauses.
table.Select().Where(table.C("name").NotLike(`_b%`))
func (ColumnElem) NullsFirst ¶
func (col ColumnElem) NullsFirst() OrderedColumn
NullsFirst returns an OrderedColumn that will sort NULLs first.
func (ColumnElem) NullsLast ¶
func (col ColumnElem) NullsLast() OrderedColumn
NullsLast returns an OrderedColumn that will sort NULLs last.
func (ColumnElem) Orderable ¶
func (col ColumnElem) Orderable() OrderedColumn
Orderable implements the Orderable interface that allows the column to be used in an ORDER BY clause.
func (ColumnElem) Type ¶
func (col ColumnElem) Type() types.Type
Type returns the column's data type
type ColumnSet ¶
type ColumnSet struct {
// contains filtered or unexported fields
}
ColumnSet maintains a []ColumnElem. It includes a variety of getters, setters, and filters.
func (ColumnSet) All ¶
func (set ColumnSet) All() []ColumnElem
All returns all columns in their default order
func (ColumnSet) Filter ¶
Filter returns a ColumnSet of columns from the original set that match the given names
func (ColumnSet) Get ¶
func (set ColumnSet) Get(name string) ColumnElem
Get returns a ColumnElem - or an invalid ColumnElem if a column with the given name does not exist in the set
func (ColumnSet) MakeUnique ¶
func (set ColumnSet) MakeUnique() (UniqueColumnSet, error)
type Columnar ¶
type Columnar interface { // Two methods for neutral SQL element interfaces: // (1) Require the interface to return the neutral implementation // (2) Enumerate all the methods an implmentation would require // Columnar and Tabular both use method (1) // Name has been left as a legacy shortcut but may be removed Compiles Selectable Name() string Column() ColumnElem Table() *TableElem }
Columnar is the interface that all dialects of a SQL column must implement
type Compiles ¶
type Compiles interface {
Compile(dialect.Dialect, *Parameters) (string, error)
}
Compiles is the main interface that all SQL statements and clauses must implement in order to be queried.
type ConditionalStmt ¶
type ConditionalStmt struct { Stmt // contains filtered or unexported fields }
ConditionalStmt includes SELECT, DELETE, and UPDATE statements
func (*ConditionalStmt) AddConditional ¶
func (stmt *ConditionalStmt) AddConditional(where Clause)
AddConditional adds a conditional clause to the statement. If a conditional clause already exists, it will be logically joined to the given clause with AND. TODO Additional logical operators?
func (ConditionalStmt) Conditional ¶
func (stmt ConditionalStmt) Conditional() Clause
Conditional returns the statement's conditional Clause
type Conn ¶
type Conn interface { Begin() (TX, error) Close() error Query(stmt Executable, dest ...interface{}) error String(stmt Executable) string }
Conn is the common database connection interface. It can perform queries and dialect specific compilation. Since transactions also implement this interface, it is highly recommended to pass the Conn interface to functions that do not need to modify transactional state.
type CreateStmt ¶
type CreateStmt struct {
// contains filtered or unexported fields
}
CreateStmt is the internal representation of an CREATE TABLE statement.
func (CreateStmt) Compile ¶
func (stmt CreateStmt) Compile(d dialect.Dialect, p *Parameters) (string, error)
Compile outputs the CREATE TABLE statement using the given dialect and parameters. An error may be returned because of a pre-existing error or because an error occurred during compilation.
func (CreateStmt) IfNotExists ¶
func (stmt CreateStmt) IfNotExists() CreateStmt
func (CreateStmt) String ¶
func (stmt CreateStmt) String() string
String outputs the parameter-less CREATE TABLE statement in a neutral dialect.
func (CreateStmt) Temporary ¶
func (stmt CreateStmt) Temporary() CreateStmt
type CreateViewStmt ¶
type CreateViewStmt struct { Stmt // contains filtered or unexported fields }
CreateViewStmt is the internal representation of a CREATE VIEW statement.
func (CreateViewStmt) Compile ¶
func (stmt CreateViewStmt) Compile(d dialect.Dialect, p *Parameters) (string, error)
Compile outputs the CREATE VIEW statement using the given dialect and parameters. An error may be returned because of a pre-existing error or because an error occurred during compilation.
func (CreateViewStmt) OrReplace ¶
func (stmt CreateViewStmt) OrReplace() CreateViewStmt
func (CreateViewStmt) String ¶
func (stmt CreateViewStmt) String() string
String outputs the parameter-less CREATE View statement in a neutral dialect.
func (CreateViewStmt) Temporary ¶
func (stmt CreateViewStmt) Temporary() CreateViewStmt
type DB ¶
DB is a database connection pool. Most functions should use the Conn interface instead of this type.
func Open ¶
Open connects to the database using the given driver and credentials. It returns a database connection pool and an error if one occurred.
func (DB) PanicOnError ¶
PanicOnError will create a new connection that will panic on any error
func (*DB) Query ¶
func (c *DB) Query(stmt Executable, dest ...interface{}) error
Query executes an Executable statement
func (*DB) String ¶
func (c *DB) String(stmt Executable) string
String returns the compiled Executable using the DB's dialect. If an error is encountered during compilation, it will return the error instead.
type DeleteStmt ¶
type DeleteStmt struct { ConditionalStmt // contains filtered or unexported fields }
DeleteStmt is the internal representation of a DELETE statement.
func Delete ¶
func Delete(table *TableElem) (stmt DeleteStmt)
Delete creates a DELETE statement for the given table.
func (DeleteStmt) Compile ¶
func (stmt DeleteStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile outputs the DELETE statement using the given dialect and parameters. An error may be returned because of a pre-existing error or because an error occurred during compilation.
func (DeleteStmt) String ¶
func (stmt DeleteStmt) String() string
String outputs the parameter-less DELETE statement in a neutral dialect.
func (DeleteStmt) Where ¶
func (stmt DeleteStmt) Where(clauses ...Clause) DeleteStmt
Where adds a conditional WHERE clause to the DELETE statement.
type DropStmt ¶
type DropStmt struct {
// contains filtered or unexported fields
}
DropStmt is the internal representation of an DROP TABLE statement.
func (DropStmt) Compile ¶
Compile outputs the DROP TABLE statement using the given dialect and parameters.
type Executable ¶
type Executable interface { Compiles }
Executable is - for now - an alias of Compiles
type FKElem ¶
type FKElem struct {
// contains filtered or unexported fields
}
FKElem is an internal type representation. It implements the types.Type interface so it can be used in CREATE TABLE statements.
func ForeignKey ¶
func ForeignKey(name string, fk Selectable, datatypes ...types.Type) FKElem
ForeignKey creates a FKElem from the given name and column/table. If given a column, it must already have its table assigned. If given a table, it must have one and only one primary key
func (FKElem) ForeignName ¶
func (FKElem) Modify ¶
Modify implements the TableModifier interface. It creates a column and adds the same column to the create array.
func (FKElem) References ¶
References returns the table that this foreign key references.
type Field ¶
type Field struct { Value reflect.Value Type reflect.StructField Name string Options options }
Field holds value and type info on a struct field
func AlignFields ¶
AlignFields will reorder the given fields array to match the columns. Columns that do not match fields will be given empty field structs.
func DeepFields ¶
DeepFields returns value and type info on struct types. It will return nothing if the given object is not a struct or *struct type.
func (Field) IsIgnorable ¶
IsIgnorable returns true if the field can be ignored
func (Field) IsOmittable ¶
IsOmittable returns true if the field can be omitted
type FuncClause ¶
func (FuncClause) Compile ¶
func (c FuncClause) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile returns the FuncClause as a compiled string using the given Dialect - possibly with an error. Any parameters will be appended to the given Parameters.
func (FuncClause) String ¶
func (c FuncClause) String() string
String returns the parameter-less FuncClause in a neutral dialect.
func (FuncClause) Wrap ¶
func (c FuncClause) Wrap(str string) string
Wrap implements the Operator interface
type InsertStmt ¶
type InsertStmt struct { Stmt // contains filtered or unexported fields }
InsertStmt is the internal representation of an INSERT statement.
func Insert ¶
func Insert(selections ...Selectable) (stmt InsertStmt)
Insert creates an INSERT statement for the given columns and tables. There must be at least one column and all columns must belong to the same table.
func (InsertStmt) Compile ¶
func (stmt InsertStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile outputs the INSERT statement using the given dialect and parameters. An error may be returned because of a pre-existing error or because an error occurred during compilation.
func (InsertStmt) String ¶
func (stmt InsertStmt) String() string
String outputs the parameter-less INSERT statement in a neutral dialect.
func (InsertStmt) Table ¶
func (stmt InsertStmt) Table() Tabular
Table returns the INSERT statement's table
func (InsertStmt) Values ¶
func (stmt InsertStmt) Values(obj interface{}) InsertStmt
Values adds parameters to the INSERT statement. Accepted types: struc, Values, or a slice of either. Both pointers and values are accepted.
type JoinClause ¶
type JoinClause struct { ArrayClause // contains filtered or unexported fields }
JoinClause implements a variety of joins
func (JoinClause) Compile ¶
func (j JoinClause) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile compiles a JoinClause
func (JoinClause) String ¶
func (j JoinClause) String() string
String returns a default string representation of the JoinClause
type Modifier ¶
Modifier is the interface that all Table elements - such as columns and constraints - must implement in order to be added to the Table() constructor
type Orderable ¶
type Orderable interface {
Orderable() OrderedColumn
}
Orderable is implemented by both ColumnElem and OrderedColumn types. It allows those types to be used in ORDER BY clauses.
type OrderedColumn ¶
type OrderedColumn struct {
// contains filtered or unexported fields
}
OrderedColumn represents a ColumnElem that will be used in an ORDER BY clause within SELECT statements. It provides additional sorting features, such as ASC, DESC, NULLS FIRST, and NULLS LAST. If not specified, ASC is assumed by default. In Postgres: the default behavior is NULLS LAST when ASC is specified or implied, and NULLS FIRST when DESC is specified http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-ORDERBY
func (OrderedColumn) Asc ¶
func (ord OrderedColumn) Asc() OrderedColumn
Asc sets the column ordering to ascending - the default
func (OrderedColumn) Compile ¶
func (ord OrderedColumn) Compile(d dialect.Dialect, ps *Parameters) (string, error)
func (OrderedColumn) Desc ¶
func (ord OrderedColumn) Desc() OrderedColumn
Desc sets the column ordering to descending
func (OrderedColumn) NullsFirst ¶
func (ord OrderedColumn) NullsFirst() OrderedColumn
NullsFirst sets the column ordering to return NULL results first
func (OrderedColumn) NullsLast ¶
func (ord OrderedColumn) NullsLast() OrderedColumn
NullsLast sets the column ordering to return NULL results last
func (OrderedColumn) Orderable ¶
func (ord OrderedColumn) Orderable() OrderedColumn
Orderable returns the OrderedColumn itself
func (OrderedColumn) String ¶
func (ord OrderedColumn) String() string
String outputs the OrderedColumn in a neutral dialect.
type PKArray ¶
type PKArray []string
PKArray is a list of columns representing the table's primary key array. It is an array instead of a map because order is significant for primary keys.
func PrimaryKey ¶
PrimaryKey creates a new PKArray. Only one primary key is allowed per table.
type Parameter ¶
type Parameter struct {
Value interface{}
}
Parameter is a value that will be passed to the database using a dialect specific parameterization
type Parameters ¶
type Parameters []interface{}
Parameters aggregated values before parameterization
type Result ¶
type Result struct { Scanner // contains filtered or unexported fields }
Result is returned by a database query - it embeds a Scanner
type Scanner ¶
type Scanner interface { Close() error Columns() ([]string, error) Err() error Next() bool Scan(...interface{}) error }
Scanner is used for building mock result rows for testing
type SelectStmt ¶
type SelectStmt struct { ConditionalStmt // contains filtered or unexported fields }
SelectStmt is the internal representation of an SQL SELECT statement.
func Select ¶
func Select(selections ...Selectable) (stmt SelectStmt)
Select create a SELECT statement from the given columns and tables.
func SelectTable ¶
func SelectTable(table Tabular, selects ...Selectable) (stmt SelectStmt)
SelectTable creates a SELECT statement from the given table and its columns. Any additional selections will not have their table added to the SelectStmt's tables field - they must be added manually or through a join. To perform selections using cartesian logic, use Select() instead.
func (SelectStmt) All ¶
func (stmt SelectStmt) All() SelectStmt
All removes the DISTINCT clause from the SELECT statement.
func (SelectStmt) As ¶
func (stmt SelectStmt) As(alias string) SelectStmt
As sets the alias for this statement
func (SelectStmt) Columns ¶
func (stmt SelectStmt) Columns() []ColumnElem
Columns returns all the columns within the SELECT. This method implements the Tabular interface
func (SelectStmt) Compile ¶
func (stmt SelectStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error)
func (SelectStmt) CrossJoin ¶
func (stmt SelectStmt) CrossJoin(table Tabular) SelectStmt
CrossJoin adds a CROSS JOIN ... clause to the SELECT statement.
func (SelectStmt) Distinct ¶
func (stmt SelectStmt) Distinct(columns ...Columnar) SelectStmt
Distinct adds a DISTINCT clause to the SELECT statement. If any column are provided, the clause will be compiled as a DISTINCT ON.
func (SelectStmt) From ¶
func (stmt SelectStmt) From(tables ...Tabular) SelectStmt
From manually specifies the SelectStmt's FROM clause
func (SelectStmt) FullOuterJoin ¶
func (stmt SelectStmt) FullOuterJoin(table Tabular, clauses ...Clause) SelectStmt
FullOuterJoin adds a FULL OUTER JOIN ... ON ... clause to the SELECT statement. If no clauses are given, it will assume the clause is NATURAL.
func (SelectStmt) GroupBy ¶
func (stmt SelectStmt) GroupBy(columns ...Columnar) SelectStmt
GroupBy adds a GROUP BY to the SELECT statement. Only one GROUP BY is allowed per statement. Additional calls to GroupBy will overwrite the existing GROUP BY clause.
func (SelectStmt) Having ¶
func (stmt SelectStmt) Having(conditions ...Clause) SelectStmt
Having adds a conditional clause to the SELECT statement. Only one HAVING is allowed per statement. Additional calls to Having will overwrite the existing HAVING clause.
func (SelectStmt) InnerJoin ¶
func (stmt SelectStmt) InnerJoin(table Tabular, clauses ...Clause) SelectStmt
InnerJoin adds an INNER JOIN ... ON ... clause to the SELECT statement. If no clauses are given, it will assume the clause is NATURAL.
func (SelectStmt) LeftOuterJoin ¶
func (stmt SelectStmt) LeftOuterJoin(table Tabular, clauses ...Clause) SelectStmt
LeftOuterJoin adds a LEFT OUTER JOIN ... ON ... clause to the SELECT statement. If no clauses are given, it will assume the clause is NATURAL.
func (SelectStmt) Limit ¶
func (stmt SelectStmt) Limit(limit int) SelectStmt
Limit sets the limit of the SELECT statement.
func (SelectStmt) Name ¶
func (stmt SelectStmt) Name() string
Name returns the name of the statement's alias or the name of its first table. It will return nothing if neither an alias or any tables exist.
func (SelectStmt) Offset ¶
func (stmt SelectStmt) Offset(offset int) SelectStmt
Offset sets the offset of the SELECT statement.
func (SelectStmt) OrderBy ¶
func (stmt SelectStmt) OrderBy(ords ...Orderable) SelectStmt
OrderBy adds an ORDER BY to the SELECT statement. Only one ORDER BY is allowed per statement. Additional calls to OrderBy will overwrite the existing ORDER BY clause.
func (SelectStmt) RightOuterJoin ¶
func (stmt SelectStmt) RightOuterJoin(table Tabular, clauses ...Clause) SelectStmt
RightOuterJoin adds a RIGHT OUTER JOIN ... ON ... clause to the SELECT statement. If no clauses are given, it will assume the clause is NATURAL.
func (SelectStmt) String ¶
func (stmt SelectStmt) String() string
String outputs the parameter-less SELECT statement in a neutral dialect.
func (SelectStmt) Table ¶
func (stmt SelectStmt) Table() *TableElem
func (SelectStmt) Where ¶
func (stmt SelectStmt) Where(conditions ...Clause) SelectStmt
Where adds a conditional clause to the SELECT statement. Only one WHERE is allowed per statement. Additional calls to Where will overwrite the existing WHERE clause.
type Selectable ¶
type Selectable interface {
Columns() []ColumnElem
}
Selectable is an interface that allows both tables and columns to be selected. It is implemented by TableElem and ColumnElem.
type SelfFKElem ¶
type SelfFKElem struct { FKElem // contains filtered or unexported fields }
SelfFKElem allows a table to have a foreign key to itself. willReference is a placeholder for the column the self-referential foreign key will reference
func SelfForeignKey ¶
func SelfForeignKey(name, ref string, datatypes ...types.Type) SelfFKElem
SelfForeignKey creates a self-referential foreign key
func (SelfFKElem) Modify ¶
func (fk SelfFKElem) Modify(tabular Tabular) error
Modify implements the TableModifier interface. It creates a column and adds the same column to the create array, will adding the referencing table and column
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt is the base of all statements, including SELECT, UPDATE, DELETE, and INSERT statements
type String ¶
type String string
String implements the Clause interface for strings TODO dialect specific, safe-escape
type TX ¶
TX is the interface for a transaction. In addition to standard COMMIT and ROLLBACK behavior, the interface includes a generic Close method that can be used with defer. Close will rollback the transaction unless the IsSuccessful method has been called.
type TableElem ¶
type TableElem struct {
// contains filtered or unexported fields
}
TableElem is a dialect neutral implementation of a SQL table
func (TableElem) Column ¶
func (table TableElem) Column(name string) ColumnElem
Column returns the column as a ColumnElem. If the column does not exist it will return the ColumnElem in an invalid state that will be used to construct an error message
func (TableElem) Columns ¶
func (table TableElem) Columns() []ColumnElem
Columns returns all the table columns in the original schema order
func (*TableElem) Create ¶
func (table *TableElem) Create() CreateStmt
Create returns a CREATE statement for the table
func (*TableElem) Delete ¶
func (table *TableElem) Delete() DeleteStmt
Delete is an alias for Delete(table). It will generate a DELETE statement for the entire table
func (*TableElem) ForeignKeys ¶
ForeignKeys returns the table's foreign keys
func (*TableElem) Insert ¶
func (table *TableElem) Insert() InsertStmt
Insert is an alias for Insert(table). It will create an INSERT statement for the entire table. Specify the insert values with the method Values().
func (TableElem) PrimaryKey ¶
PrimaryKey returns the primary key array
func (*TableElem) ReferencedBy ¶
ReferencedBy returns the foreign keys that reference this table
func (*TableElem) Select ¶
func (table *TableElem) Select(selections ...Selectable) (stmt SelectStmt)
Select returns a SelectStmt for the entire table
func (*TableElem) Update ¶
func (table *TableElem) Update() UpdateStmt
Update is an alias for Update(table). It will create an UPDATE statement for the entire table. Specify the update values with the method Values().
type Tabular ¶
type Tabular interface { // Two methods for neutral SQL element interfaces: // (1) Require the interface to return the neutral implementation // (2) Enumerate all the methods an implmentation would require // Columnar and Tabular both use method (1) // Name has been left as a legacy shortcut but may be removed Compiles Selectable Name() string Table() *TableElem }
Tabular is the interface that all dialects of a SQL table must implement
type TextStmt ¶
type TextStmt struct { Stmt // contains filtered or unexported fields }
TextStmt allows the creation of custom SQL statements
type UnaryClause ¶
func (UnaryClause) Compile ¶
func (c UnaryClause) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile returns the UnaryClause as a compiled string using the given Dialect - possibly with an error. Any parameters will be appended to the given Parameters.
func (UnaryClause) String ¶
func (c UnaryClause) String() string
String returns the parameter-less UnaryClause in a neutral dialect.
type UniqueArray ¶
type UniqueArray []string
UniqueArray is a list of columns representing a table UNIQUE constraint.
func Unique ¶
func Unique(names ...string) UniqueArray
func (UniqueArray) Create ¶
func (unique UniqueArray) Create(d dialect.Dialect) (string, error)
Create returns the proper syntax for CREATE TABLE commands.
func (UniqueArray) Has ¶
func (unique UniqueArray) Has(name string) bool
Has returns true if the UniqueArray contains the given column name.
func (UniqueArray) Modify ¶
func (unique UniqueArray) Modify(tabular Tabular) error
Modify implements the TableModifier interface. It confirms that every column given exists in the parent table.
type UniqueColumnSet ¶
type UniqueColumnSet struct{ ColumnSet }
UniqueColumnSet is a ColumnSet, but column names must be unique
func UniqueColumns ¶
func UniqueColumns() (set UniqueColumnSet)
UniqueColumns creates a new ColumnSet that can only hold columns with unique names
func (UniqueColumnSet) Add ¶
func (set UniqueColumnSet) Add(columns ...Columnar) (UniqueColumnSet, error)
Add adds any number of Columnar types to the set and returns the new set. Adding a column with the same name as an existing column in the set will return an error.
func (UniqueColumnSet) EmptyValues ¶
func (set UniqueColumnSet) EmptyValues() Values
EmptyValues creates an empty Values from the UniqueColumnSet
func (UniqueColumnSet) Filter ¶
func (set UniqueColumnSet) Filter(names ...string) (out UniqueColumnSet)
Filter returns a UniqueColumnSet of columns from the original set that match the given names
func (UniqueColumnSet) Reject ¶
func (set UniqueColumnSet) Reject(names ...string) (out UniqueColumnSet)
Reject removes the given column names and returns a new ColumnSet
type UpdateStmt ¶
type UpdateStmt struct { ConditionalStmt // contains filtered or unexported fields }
UpdateStmt is the internal representation of an SQL UPDATE statement.
func Update ¶
func Update(table *TableElem) (stmt UpdateStmt)
Update creates an UPDATE statement for the given table.
func (UpdateStmt) Compile ¶
func (stmt UpdateStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error)
Compile outputs the UPDATE statement using the given dialect and parameters.
func (UpdateStmt) String ¶
func (stmt UpdateStmt) String() string
String outputs the parameter-less UPDATE statement in a neutral dialect.
func (UpdateStmt) Values ¶
func (stmt UpdateStmt) Values(values Values) UpdateStmt
Values attaches the given values to the statement. The keys of values must match columns in the table. TODO Allow structs to be used if a primary key is specified in the schema
func (UpdateStmt) Where ¶
func (stmt UpdateStmt) Where(clauses ...Clause) UpdateStmt
Where adds a conditional WHERE clause to the UPDATE statement.
type Values ¶
type Values map[string]interface{}
Values is a map of column names to values. It can be used both as a source of values in INSERT, UPDATE, and Text statements, or as a destination for SELECT.
func (Values) Compile ¶
Compile outputs the Values in a format for UPDATE using the given dialect and parameters.
func (Values) Diff ¶
Diff returns the values in v that differ from the values in other. ISO 31-11: v \ other
func (Values) Equals ¶
Equals returns true if both the receiver and parameter Values have equal keys and values
func (Values) Filter ¶
Filter returns a Values type with key-values from the original Values that match the given keys
func (Values) MarshalJSON ¶
MarshalJSON converts Values to JSON after converting all byte slices to a string type. By default, byte slices are JSON unmarshaled as base64. This is an issue since the postgres driver will scan string/varchar types as byte slices - the current solution is to convert before output.
Source Files ¶
- clause.go
- column.go
- columnset.go
- compiles.go
- conn.go
- create.go
- delete.go
- drop.go
- errors.go
- executable.go
- fields.go
- foreignkey.go
- function.go
- insert.go
- join.go
- modifier.go
- names.go
- options.go
- order.go
- parameters.go
- primarykey.go
- result.go
- select.go
- sol.go
- stmt.go
- table.go
- tester.go
- text.go
- tokens.go
- unique.go
- update.go
- values.go
- view.go