Documentation ¶
Index ¶
- Variables
- type CountQuery
- func (cq *CountQuery) Exec() (int64, error)
- func (cq *CountQuery) GroupBy(bys ...string) *CountQuery
- func (cq *CountQuery) Having(condition string, args ...interface{}) *CountQuery
- func (cq *CountQuery) Join(join string) *CountQuery
- func (cq *CountQuery) LeftJoin(join string) *CountQuery
- func (cq *CountQuery) OrHaving(condition string, args ...interface{}) *CountQuery
- func (cq *CountQuery) OrWhere(condition string, args ...interface{}) *CountQuery
- func (cq *CountQuery) String() string
- func (cq *CountQuery) Where(condition string, args ...interface{}) *CountQuery
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Count(table string, count string) *CountQuery
- func (db *DB) Delete(obj interface{}) (sql.Result, error)
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Insert(obj interface{}) (sql.Result, error)
- func (db *DB) ManualDelete(table string) *DeleteQuery
- func (db *DB) ManualUpdate(table string) *UpdateQuery
- func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *DB) Register(structs ...interface{}) error
- func (db *DB) Select(fields ...string) *SelectQuery
- func (db *DB) Update(obj interface{}) (sql.Result, error)
- type DeleteQuery
- func (dq *DeleteQuery) Exec() (sql.Result, error)
- func (dq *DeleteQuery) Join(join string) *DeleteQuery
- func (dq *DeleteQuery) LeftJoin(join string) *DeleteQuery
- func (dq *DeleteQuery) OrWhere(condition string, args ...interface{}) *DeleteQuery
- func (dq *DeleteQuery) String() string
- func (dq *DeleteQuery) Where(condition string, args ...interface{}) *DeleteQuery
- type Execer
- type NullInt64
- type NullString
- type NullTime
- type Querier
- type QueryRower
- type SelectQuery
- func (sq *SelectQuery) Get(out interface{}) error
- func (sq *SelectQuery) GroupBy(bys ...string) *SelectQuery
- func (sq *SelectQuery) Having(condition string, args ...interface{}) *SelectQuery
- func (sq *SelectQuery) Join(join string) *SelectQuery
- func (sq *SelectQuery) LeftJoin(join string) *SelectQuery
- func (sq *SelectQuery) Limit(limit int64) *SelectQuery
- func (sq *SelectQuery) Offset(offset int64) *SelectQuery
- func (sq *SelectQuery) OrHaving(condition string, args ...interface{}) *SelectQuery
- func (sq *SelectQuery) OrWhere(condition string, args ...interface{}) *SelectQuery
- func (sq *SelectQuery) OrderBy(orderBy string) *SelectQuery
- func (sq *SelectQuery) String() string
- func (sq *SelectQuery) Where(condition string, args ...interface{}) *SelectQuery
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Count(table string, count string) *CountQuery
- func (t *Tx) Delete(obj interface{}) (sql.Result, error)
- func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (t *Tx) Insert(obj interface{}) (sql.Result, error)
- func (t *Tx) ManualDelete(table string) *DeleteQuery
- func (t *Tx) ManualUpdate(table string) *UpdateQuery
- func (t *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (t *Tx) QueryRow(query string, args ...interface{}) *sql.Row
- func (t *Tx) Rollback() error
- func (t *Tx) Select(fields ...string) *SelectQuery
- func (t *Tx) Update(obj interface{}) (sql.Result, error)
- type UpdateQuery
- func (uq *UpdateQuery) Exec() (sql.Result, error)
- func (uq *UpdateQuery) Join(join string) *UpdateQuery
- func (uq *UpdateQuery) LeftJoin(join string) *UpdateQuery
- func (uq *UpdateQuery) OrWhere(condition string, args ...interface{}) *UpdateQuery
- func (uq *UpdateQuery) Set(set string, args ...interface{}) *UpdateQuery
- func (uq *UpdateQuery) String() string
- func (uq *UpdateQuery) Where(condition string, args ...interface{}) *UpdateQuery
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("no result found")
ErrNotFound is returned when a query for one result returns no results.
Functions ¶
This section is empty.
Types ¶
type CountQuery ¶
type CountQuery struct {
// contains filtered or unexported fields
}
CountQuery is a query for counting rows in a table.
func (*CountQuery) GroupBy ¶ added in v1.3.0
func (cq *CountQuery) GroupBy(bys ...string) *CountQuery
GroupBy specifies how to group the results.
func (*CountQuery) Having ¶ added in v1.3.0
func (cq *CountQuery) Having(condition string, args ...interface{}) *CountQuery
Having specifies which rows will be returned.
func (*CountQuery) Join ¶
func (cq *CountQuery) Join(join string) *CountQuery
Join joins another table to this query.
func (*CountQuery) LeftJoin ¶ added in v1.3.1
func (cq *CountQuery) LeftJoin(join string) *CountQuery
LeftJoin joins another table to this query.
func (*CountQuery) OrHaving ¶ added in v1.3.0
func (cq *CountQuery) OrHaving(condition string, args ...interface{}) *CountQuery
OrHaving specifies which rows will be returned.
func (*CountQuery) OrWhere ¶
func (cq *CountQuery) OrWhere(condition string, args ...interface{}) *CountQuery
OrWhere specifies which rows will be returned.
func (*CountQuery) String ¶
func (cq *CountQuery) String() string
String returns the string representation of CountQuery.
func (*CountQuery) Where ¶
func (cq *CountQuery) Where(condition string, args ...interface{}) *CountQuery
Where specifies which rows will be returned.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a wrapper around sql.DB.
func (*DB) Count ¶
func (db *DB) Count(table string, count string) *CountQuery
Count starts a query for counting rows in a table.
func (*DB) Delete ¶
Delete deletes a row from the database.
Example ¶
package main import ( "database/sql" "fmt" "os" _ "github.com/mattn/go-sqlite3" "github.com/twharmon/gosql" ) func main() { os.Remove("/tmp/foo.db") sqliteDB, _ := sql.Open("sqlite3", "/tmp/foo.db") sqliteDB.Exec("create table user (id integer not null primary key, name text); delete from user") db := gosql.New(sqliteDB) type User struct { ID int `gosql:"primary"` Name string } db.Register(User{}) user := User{ID: 5, Name: "Gopher"} db.Insert(&user) db.Delete(&user) var foo User err := db.Select("*").Get(&foo) fmt.Println(err) }
Output: no result found
func (*DB) Insert ¶
Insert insterts a row in the database.
Example ¶
package main import ( "database/sql" "fmt" "os" _ "github.com/mattn/go-sqlite3" "github.com/twharmon/gosql" ) func main() { os.Remove("/tmp/foo.db") sqliteDB, _ := sql.Open("sqlite3", "/tmp/foo.db") sqliteDB.Exec("create table user (id integer not null primary key, name text); delete from user") db := gosql.New(sqliteDB) type User struct { ID int `gosql:"primary"` Name string } db.Register(User{}) db.Insert(&User{Name: "Gopher"}) var user User db.Select("*").Get(&user) fmt.Println(user.Name) }
Output: Gopher
func (*DB) ManualDelete ¶
func (db *DB) ManualDelete(table string) *DeleteQuery
ManualDelete starts a query for manually deleting rows in a table.
func (*DB) ManualUpdate ¶
func (db *DB) ManualUpdate(table string) *UpdateQuery
ManualUpdate starts a query for manually updating rows in a table.
func (*DB) Select ¶
func (db *DB) Select(fields ...string) *SelectQuery
Select selects columns of a table.
func (*DB) Update ¶
Update updates a row in the database.
Example ¶
package main import ( "database/sql" "fmt" "os" _ "github.com/mattn/go-sqlite3" "github.com/twharmon/gosql" ) func main() { os.Remove("/tmp/foo.db") sqliteDB, _ := sql.Open("sqlite3", "/tmp/foo.db") sqliteDB.Exec("create table user (id integer not null primary key, name text, email text); delete from user") db := gosql.New(sqliteDB) type User struct { ID int `gosql:"primary"` Name string Email string } db.Register(User{}) user := User{ID: 5, Name: "Gopher", Email: "gopher@example.com"} db.Insert(&user) user.Name = "Gofer" user.Email = "gofer@example.com" db.Update(&user) var foo User db.Select("*").Get(&foo) fmt.Println(foo.Name, foo.Email) }
Output: Gofer gofer@example.com
type DeleteQuery ¶
type DeleteQuery struct {
// contains filtered or unexported fields
}
DeleteQuery is a query for deleting rows from a table.
func (*DeleteQuery) Exec ¶
func (dq *DeleteQuery) Exec() (sql.Result, error)
Exec executes the query.
func (*DeleteQuery) Join ¶
func (dq *DeleteQuery) Join(join string) *DeleteQuery
Join joins another table to this query.
func (*DeleteQuery) LeftJoin ¶ added in v1.3.1
func (dq *DeleteQuery) LeftJoin(join string) *DeleteQuery
LeftJoin joins another table to this query.
func (*DeleteQuery) OrWhere ¶
func (dq *DeleteQuery) OrWhere(condition string, args ...interface{}) *DeleteQuery
OrWhere specifies which rows will be returned.
func (*DeleteQuery) String ¶
func (dq *DeleteQuery) String() string
String returns the string representation of DeleteQuery.
func (*DeleteQuery) Where ¶
func (dq *DeleteQuery) Where(condition string, args ...interface{}) *DeleteQuery
Where specifies which rows will be returned.
type NullInt64 ¶ added in v1.2.5
NullInt64 holds an int64 value that might be null in the database.
func (NullInt64) MarshalJSON ¶ added in v1.2.5
MarshalJSON implements the Marshaler interface.
func (*NullInt64) UnmarshalJSON ¶ added in v1.2.5
UnmarshalJSON implements the Unmarshaler interface.
type NullString ¶ added in v1.2.5
NullString holds an string value that might be null in the database.
func (NullString) MarshalJSON ¶ added in v1.2.5
func (n NullString) MarshalJSON() ([]byte, error)
MarshalJSON implements the Marshaler interface.
func (*NullString) Scan ¶ added in v1.2.5
func (n *NullString) Scan(value interface{}) error
Scan implements the Scanner interface.
func (*NullString) UnmarshalJSON ¶ added in v1.2.5
func (n *NullString) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the Unmarshaler interface.
type NullTime ¶ added in v1.2.5
NullTime holds an time.Time value that might be null in the database.
func (NullTime) MarshalJSON ¶ added in v1.2.5
MarshalJSON implements the Marshaler interface.
func (*NullTime) UnmarshalJSON ¶ added in v1.2.5
UnmarshalJSON implements the Unmarshaler interface.
type QueryRower ¶ added in v1.4.0
QueryRower .
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
SelectQuery holds information for a select query.
func (*SelectQuery) Get ¶ added in v1.2.6
func (sq *SelectQuery) Get(out interface{}) error
Get sets the result of the query to out. Get() can only take a pointer to a struct, a pointer to a slice of structs, or a pointer to a slice of pointers to structs.
func (*SelectQuery) GroupBy ¶ added in v1.2.8
func (sq *SelectQuery) GroupBy(bys ...string) *SelectQuery
GroupBy specifies how to group the results.
func (*SelectQuery) Having ¶ added in v1.2.8
func (sq *SelectQuery) Having(condition string, args ...interface{}) *SelectQuery
Having specifies which rows will be returned.
func (*SelectQuery) Join ¶
func (sq *SelectQuery) Join(join string) *SelectQuery
Join joins another table to this query.
func (*SelectQuery) LeftJoin ¶ added in v1.3.1
func (sq *SelectQuery) LeftJoin(join string) *SelectQuery
LeftJoin joins another table to this query.
func (*SelectQuery) Limit ¶
func (sq *SelectQuery) Limit(limit int64) *SelectQuery
Limit limits the number of results returned by the query.
func (*SelectQuery) Offset ¶
func (sq *SelectQuery) Offset(offset int64) *SelectQuery
Offset specifies the offset value in the query.
func (*SelectQuery) OrHaving ¶ added in v1.2.8
func (sq *SelectQuery) OrHaving(condition string, args ...interface{}) *SelectQuery
OrHaving specifies which rows will be returned.
func (*SelectQuery) OrWhere ¶
func (sq *SelectQuery) OrWhere(condition string, args ...interface{}) *SelectQuery
OrWhere specifies which rows will be returned.
func (*SelectQuery) OrderBy ¶
func (sq *SelectQuery) OrderBy(orderBy string) *SelectQuery
OrderBy orders the results by the given criteria.
func (*SelectQuery) String ¶
func (sq *SelectQuery) String() string
String returns the string representation of SelectQuery.
func (*SelectQuery) Where ¶
func (sq *SelectQuery) Where(condition string, args ...interface{}) *SelectQuery
Where specifies which rows will be returned.
type Tx ¶ added in v1.4.0
type Tx struct {
// contains filtered or unexported fields
}
Tx .
func (*Tx) Count ¶ added in v1.4.0
func (t *Tx) Count(table string, count string) *CountQuery
Count starts a query for counting rows in a table.
func (*Tx) ManualDelete ¶ added in v1.4.0
func (t *Tx) ManualDelete(table string) *DeleteQuery
ManualDelete starts a query for manually deleting rows in a table.
func (*Tx) ManualUpdate ¶ added in v1.4.0
func (t *Tx) ManualUpdate(table string) *UpdateQuery
ManualUpdate starts a query for manually updating rows in a table.
func (*Tx) Select ¶ added in v1.4.0
func (t *Tx) Select(fields ...string) *SelectQuery
Select selects columns of a table.
type UpdateQuery ¶
type UpdateQuery struct {
// contains filtered or unexported fields
}
UpdateQuery holds information for an update query.
func (*UpdateQuery) Exec ¶
func (uq *UpdateQuery) Exec() (sql.Result, error)
Exec executes the query.
func (*UpdateQuery) Join ¶
func (uq *UpdateQuery) Join(join string) *UpdateQuery
Join joins another table to this query.
func (*UpdateQuery) LeftJoin ¶ added in v1.3.1
func (uq *UpdateQuery) LeftJoin(join string) *UpdateQuery
LeftJoin joins another table to this query.
func (*UpdateQuery) OrWhere ¶
func (uq *UpdateQuery) OrWhere(condition string, args ...interface{}) *UpdateQuery
OrWhere specifies which rows will be returned.
func (*UpdateQuery) Set ¶
func (uq *UpdateQuery) Set(set string, args ...interface{}) *UpdateQuery
Set specifies how to update a row in a table.
func (*UpdateQuery) String ¶
func (uq *UpdateQuery) String() string
String returns the string representation of UpdateQuery.
func (*UpdateQuery) Where ¶
func (uq *UpdateQuery) Where(condition string, args ...interface{}) *UpdateQuery
Where specifies which rows will be returned.