qb

package module
v0.0.0-...-8a55f81 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 12 Imported by: 1

README

qb

Yet another query builder for golang assuming sqlite

Go Reference Go Report Card

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPointer indicates that you passed an invalid pointer into a function
	ErrInvalidPointer = errors.New("qb: attempt to load into an invalid pointer")
)

Functions

func JSONScan

func JSONScan(destination interface{}, value interface{}) error

JSONScan is a convenient function to implement the Scanner interface for json columns

func JSONValue

func JSONValue(value interface{}) (driver.Value, error)

JSONValue is a convenient function to implement the Valuer interface for json columns

func WitLogger

func WitLogger(ctx context.Context, logger Logger) context.Context

WitLogger adds a qb.Logger to the context

func WitTx

func WitTx(ctx context.Context, tx *Tx) context.Context

WitTx adds a qb.Tx to the context

Types

type Builder

type Builder interface {
	Build(buf *bytes.Buffer) error
	Params() []interface{}
}

Builder is the interface for all query builders

type DB

type DB struct {
	*sql.DB
}

DB represents the database

func Open

func Open(ctx context.Context, conn string) (*DB, error)

Open initializes the database

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx starts a transaction. The default isolation level is dependent on the driver

func (*DB) Delete

func (db *DB) Delete(ctx context.Context) *DeleteQuery

Delete creates and returns a new instance of DeleteQuery for the specified table

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query

func (*DB) Insert

func (db *DB) Insert(ctx context.Context) *InsertQuery

Insert creates and returns a new instance of InsertQuery for the specified table

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) Select

func (db *DB) Select(ctx context.Context) *SelectQuery

Select creates and returns a new instance of SelectQuery for the specified table

func (*DB) Update

func (db *DB) Update(ctx context.Context) *UpdateQuery

Update creates and returns a new instance of UpdateQuery for the specified table

type DeleteQuery

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

DeleteQuery represents a DELETE sql query

func (*DeleteQuery) Build

func (q *DeleteQuery) Build(buf *bytes.Buffer) error

Build renders the DELETE query as a string

func (*DeleteQuery) Exec

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

Exec executes the query

func (*DeleteQuery) From

func (q *DeleteQuery) From(table string) *DeleteQuery

From is used to set the table to delete from

func (*DeleteQuery) Params

func (q *DeleteQuery) Params() []interface{}

Params returns the parameters for this query

func (*DeleteQuery) Where

func (q *DeleteQuery) Where(condition string, params ...interface{}) *DeleteQuery

Where adds a where clause to the select query using *AND* strategy

type InsertQuery

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

InsertQuery represents a INSERT sql query

func (*InsertQuery) Build

func (q *InsertQuery) Build(buf *bytes.Buffer) error

Build renders the INSERT query as a string

func (*InsertQuery) Columns

func (q *InsertQuery) Columns(columns ...string) *InsertQuery

Columns determines the columns to insert

func (*InsertQuery) Exec

func (q *InsertQuery) Exec() (sql.Result, error)

Exec executes the query

func (*InsertQuery) InTo

func (q *InsertQuery) InTo(table string) *InsertQuery

InTo is used to set the table to insert into

func (*InsertQuery) OnConflict

func (q *InsertQuery) OnConflict(column string, sets string) *InsertQuery

OnConflict specifies what to do if there is conflict

func (*InsertQuery) OrIgnore

func (q *InsertQuery) OrIgnore() *InsertQuery

OrIgnore make the query behave using INSERT OR IGNORE INTO

func (*InsertQuery) Params

func (q *InsertQuery) Params() []interface{}

Params returns all parameters for the query

func (*InsertQuery) Record

func (q *InsertQuery) Record(structValue interface{})

Record scans the result of the query into the given struct

func (*InsertQuery) Returning

func (q *InsertQuery) Returning(returning ...string) *InsertQuery

Returning specifies with columns to return after the INSERT is successful

func (*InsertQuery) Values

func (q *InsertQuery) Values(values ...interface{}) *InsertQuery

Values determines the columns to insert

type Logger

type Logger func(ctx context.Context, duration time.Duration, format string, v ...interface{})

Logger logs

func GetLoggerCtx

func GetLoggerCtx(ctx context.Context) Logger

GetLoggerCtx extracts a qb.Logger from the context

type NullInt64

type NullInt64 struct {
	sql.NullInt64
}

NullInt64 is a wrapper around sql.NullInt64 that plays nice with JSON

func (NullInt64) MarshalJSON

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

MarshalJSON serializes a NullInt64 to JSON

func (*NullInt64) UnmarshalJSON

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

UnmarshalJSON deserializes a NullInt64 from JSON

type NullString

type NullString struct {
	sql.NullString
}

NullString is a wrapper around sql.NullString that plays nice with JSON

func (NullString) MarshalJSON

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

MarshalJSON serializes a NullString to JSON

func (*NullString) UnmarshalJSON

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

UnmarshalJSON deserializes a NullString from JSON

type NullTime

type NullTime struct {
	sql.NullTime
}

NullTime is a wrapper around time.Time that plays nice with SQL and JSON

func (NullTime) MarshalJSON

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

MarshalJSON serializes a NullTime to JSON

func (*NullTime) UnmarshalJSON

func (nt *NullTime) UnmarshalJSON(b []byte) error

UnmarshalJSON deserializes a NullTime from JSON

type SelectQuery

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

SelectQuery represents a SELECT sql query

func (*SelectQuery) Build

func (q *SelectQuery) Build(buf *bytes.Buffer) error

Build renders the SELECT query as a string

func (*SelectQuery) Columns

func (q *SelectQuery) Columns(columns ...string) *SelectQuery

Columns determines with columns to select

func (*SelectQuery) From

func (q *SelectQuery) From(table string) *SelectQuery

From is used to set the table to select from

func (*SelectQuery) GroupBy

func (q *SelectQuery) GroupBy(column string) *SelectQuery

GroupBy adds a GROUP BY clause to the SELECT query

func (*SelectQuery) Join

func (q *SelectQuery) Join(join string, params ...interface{}) *SelectQuery

Join adds a join to the select query

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(limit int) *SelectQuery

Limit adds a LIMIT clause to the SELECT query

func (*SelectQuery) Load

func (q *SelectQuery) Load(value interface{}) (int, error)

Load will execute the query and scan the result into the given struct

func (*SelectQuery) LoadValue

func (q *SelectQuery) LoadValue(value interface{}) error

LoadValue will execute the query and scan the scalar result into the given variable

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(offset int) *SelectQuery

Offset adds a OFFSET clause to the SELECT query

func (*SelectQuery) OrderBy

func (q *SelectQuery) OrderBy(column string, direction string) *SelectQuery

OrderBy adds an ORDER BY clause to the SELECT query

func (*SelectQuery) Params

func (q *SelectQuery) Params() []interface{}

Params returns the parameters for this query

func (*SelectQuery) Where

func (q *SelectQuery) Where(condition string, params ...interface{}) *SelectQuery

Where adds a where clause to the select query using *AND* strategy

type Tx

type Tx struct {
	*sql.Tx
}

Tx represents a transaction in a database

func GetTxCtx

func GetTxCtx(ctx context.Context) *Tx

GetTxCtx extracts a qb.Tx from the context

func (*Tx) Delete

func (tx *Tx) Delete(ctx context.Context) *DeleteQuery

Delete creates and returns a new instance of DeleteQuery for the specified table

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query

func (*Tx) Insert

func (tx *Tx) Insert(ctx context.Context) *InsertQuery

Insert creates and returns a new instance of InsertQuery for the specified table

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Tx) Select

func (tx *Tx) Select(ctx context.Context) *SelectQuery

Select creates and returns a new instance of SelectQuery for the specified table

func (*Tx) Update

func (tx *Tx) Update(ctx context.Context) *UpdateQuery

Update creates and returns a new instance of UpdateQuery for the specified table

type UpdateQuery

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

UpdateQuery represents a UPDATE sql query

func (*UpdateQuery) Build

func (q *UpdateQuery) Build(buf *bytes.Buffer) error

Build renders the UPDATE query as a string

func (*UpdateQuery) Exec

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

Exec executes the query

func (*UpdateQuery) Params

func (q *UpdateQuery) Params() []interface{}

Params returns all parameters for the query

func (*UpdateQuery) Returning

func (q *UpdateQuery) Returning(returning ...string) *UpdateQuery

Returning specifies with columns to return after the UPDATE is successful

func (*UpdateQuery) Set

func (q *UpdateQuery) Set(column string, values ...interface{}) *UpdateQuery

Set adds a column = value statement to the UPDATE querie's SET clause

func (*UpdateQuery) Table

func (q *UpdateQuery) Table(table string) *UpdateQuery

Table is used to set the table to update from

func (*UpdateQuery) Where

func (q *UpdateQuery) Where(condition string, params ...interface{}) *UpdateQuery

Where adds a where clause to the update query using *AND* strategy

Jump to

Keyboard shortcuts

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