gosql: github.com/twharmon/gosql Index | Examples | Files

package gosql

import "github.com/twharmon/gosql"

Index

Examples

Package Files

count_query.go db.go delete_query.go gosql.go model.go null_int64.go null_string.go null_time.go select_query.go update_query.go

Variables

var ErrNotFound = errors.New("no result found")

ErrNotFound is returned when a query for one result returns no results.

type CountQuery Uses

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

CountQuery is a query for counting rows in a table.

func (*CountQuery) Exec Uses

func (cq *CountQuery) Exec() (int64, error)

Exec executes the query.

func (*CountQuery) Join Uses

func (cq *CountQuery) Join(join string) *CountQuery

Join joins another table to this query.

func (*CountQuery) OrWhere Uses

func (cq *CountQuery) OrWhere(condition string, args ...interface{}) *CountQuery

OrWhere specifies which rows will be returned.

func (*CountQuery) String Uses

func (cq *CountQuery) String() string

String returns the string representation of CountQuery.

func (*CountQuery) Where Uses

func (cq *CountQuery) Where(condition string, args ...interface{}) *CountQuery

Where specifies which rows will be returned.

type DB Uses

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

DB is a wrapper around sql.DB.

func New Uses

func New(db *sql.DB) *DB

New returns a reference to DB.

func (*DB) Begin Uses

func (db *DB) Begin() (*sql.Tx, error)

Begin starts a transaction.

func (*DB) Count Uses

func (db *DB) Count(table string, count string) *CountQuery

Count starts a query for counting rows in a table.

func (*DB) Delete Uses

func (db *DB) Delete(obj interface{}) (sql.Result, error)

Delete deletes a row from the database.

Code:

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) Exec Uses

func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)

Exec is a wrapper around sql.DB.Exec().

func (*DB) Insert Uses

func (db *DB) Insert(obj interface{}) (sql.Result, error)

Insert insterts a row in the database.

Code:

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 Uses

func (db *DB) ManualDelete(table string) *DeleteQuery

ManualDelete starts a query for manually deleting rows in a table.

func (*DB) ManualUpdate Uses

func (db *DB) ManualUpdate(table string) *UpdateQuery

ManualUpdate starts a query for manually updating rows in a table.

func (*DB) Query Uses

func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

Query is a wrapper around sql.DB.Query().

func (*DB) QueryRow Uses

func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow is a wrapper around sql.DB.QueryRow().

func (*DB) Register Uses

func (db *DB) Register(structs ...interface{}) error

Register registers structs for database queries.

func (*DB) Select Uses

func (db *DB) Select(fields ...string) *SelectQuery

Select selects columns of a table.

func (*DB) Update Uses

func (db *DB) Update(obj interface{}) (sql.Result, error)

Update updates a row in the database.

Code:

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 Uses

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

DeleteQuery is a query for deleting rows from a table.

func (*DeleteQuery) Exec Uses

func (dq *DeleteQuery) Exec() (sql.Result, error)

Exec executes the query.

func (*DeleteQuery) Join Uses

func (dq *DeleteQuery) Join(join string) *DeleteQuery

Join joins another table to this query.

func (*DeleteQuery) OrWhere Uses

func (dq *DeleteQuery) OrWhere(condition string, args ...interface{}) *DeleteQuery

OrWhere specifies which rows will be returned.

func (*DeleteQuery) String Uses

func (dq *DeleteQuery) String() string

String returns the string representation of DeleteQuery.

func (*DeleteQuery) Where Uses

func (dq *DeleteQuery) Where(condition string, args ...interface{}) *DeleteQuery

Where specifies which rows will be returned.

type NullInt64 Uses

type NullInt64 struct {
    Int64 int64
    Valid bool
}

NullInt64 holds an int64 value that might be null in the database.

func (NullInt64) MarshalJSON Uses

func (n NullInt64) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface.

func (*NullInt64) Scan Uses

func (n *NullInt64) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*NullInt64) UnmarshalJSON Uses

func (n *NullInt64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler interface.

func (NullInt64) Value Uses

func (n NullInt64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullString Uses

type NullString struct {
    String string
    Valid  bool
}

NullString holds an string value that might be null in the database.

func (NullString) MarshalJSON Uses

func (n NullString) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface.

func (*NullString) Scan Uses

func (n *NullString) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*NullString) UnmarshalJSON Uses

func (n *NullString) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler interface.

func (NullString) Value Uses

func (n NullString) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullTime Uses

type NullTime struct {
    Time  time.Time
    Valid bool
}

NullTime holds an time.Time value that might be null in the database.

func (NullTime) MarshalJSON Uses

func (n NullTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface.

func (*NullTime) Scan Uses

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

Scan implements the Scanner interface.

func (*NullTime) UnmarshalJSON Uses

func (n *NullTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler interface.

func (NullTime) Value Uses

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

Value implements the driver Valuer interface.

type SelectQuery Uses

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

SelectQuery holds information for a select query.

func (*SelectQuery) Get Uses

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 Uses

func (sq *SelectQuery) GroupBy(bys ...string) *SelectQuery

GroupBy specifies how to group the results.

func (*SelectQuery) Having Uses

func (sq *SelectQuery) Having(condition string, args ...interface{}) *SelectQuery

Having specifies which rows will be returned.

func (*SelectQuery) Join Uses

func (sq *SelectQuery) Join(join string) *SelectQuery

Join joins another table to this query.

func (*SelectQuery) Limit Uses

func (sq *SelectQuery) Limit(limit int64) *SelectQuery

Limit limits the number of results returned by the query.

func (*SelectQuery) Offset Uses

func (sq *SelectQuery) Offset(offset int64) *SelectQuery

Offset specifies the offset value in the query.

func (*SelectQuery) OrHaving Uses

func (sq *SelectQuery) OrHaving(condition string, args ...interface{}) *SelectQuery

OrHaving specifies which rows will be returned.

func (*SelectQuery) OrWhere Uses

func (sq *SelectQuery) OrWhere(condition string, args ...interface{}) *SelectQuery

OrWhere specifies which rows will be returned.

func (*SelectQuery) OrderBy Uses

func (sq *SelectQuery) OrderBy(orderBy string) *SelectQuery

OrderBy orders the results by the given criteria.

func (*SelectQuery) String Uses

func (sq *SelectQuery) String() string

String returns the string representation of SelectQuery.

func (*SelectQuery) Where Uses

func (sq *SelectQuery) Where(condition string, args ...interface{}) *SelectQuery

Where specifies which rows will be returned.

type UpdateQuery Uses

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

UpdateQuery holds information for an update query.

func (*UpdateQuery) Exec Uses

func (uq *UpdateQuery) Exec() (sql.Result, error)

Exec executes the query.

func (*UpdateQuery) Join Uses

func (uq *UpdateQuery) Join(join string) *UpdateQuery

Join joins another table to this query.

func (*UpdateQuery) OrWhere Uses

func (uq *UpdateQuery) OrWhere(condition string, args ...interface{}) *UpdateQuery

OrWhere specifies which rows will be returned.

func (*UpdateQuery) Set Uses

func (uq *UpdateQuery) Set(set string, args ...interface{}) *UpdateQuery

Set specifies how to update a row in a table.

func (*UpdateQuery) String Uses

func (uq *UpdateQuery) String() string

String returns the string representation of UpdateQuery.

func (*UpdateQuery) Where Uses

func (uq *UpdateQuery) Where(condition string, args ...interface{}) *UpdateQuery

Where specifies which rows will be returned.

Package gosql imports 11 packages (graph). Updated 2020-07-20. Refresh now. Tools for package owners.