query: github.com/fragmenta/query Index | Files | Directories

package query

import "github.com/fragmenta/query"


Package Files

database.go query.go textual.go


var Debug bool

Debug sets whether we output debug statements for SQL

func CloseDatabase Uses

func CloseDatabase() error

CloseDatabase closes the database opened by OpenDatabase

func Exec Uses

func Exec(sql string, args ...interface{}) (sql.Result, error)

Exec the given sql and args against the database directly Returning sql.Result (NB not rows)

func ExecSQL Uses

func ExecSQL(query string, args ...interface{}) (sql.Result, error)

ExecSQL executes the given sql against our database with arbitrary args NB returns sql.Result - not to be used when rows expected

func OpenDatabase Uses

func OpenDatabase(opts map[string]string) error

OpenDatabase opens the database with the given options

func QuerySQL Uses

func QuerySQL(query string, args ...interface{}) (*sql.Rows, error)

QuerySQL executes the given sql Query against our database, with arbitrary args

func Rows Uses

func Rows(sql string, args ...interface{}) (*sql.Rows, error)

Rows executes the given sql and args against the database directly Returning sql.Rows

func SetMaxOpenConns Uses

func SetMaxOpenConns(max int)

SetMaxOpenConns sets the maximum number of open connections

func TimeString Uses

func TimeString(t time.Time) string

TimeString returns a string formatted as a time for this db if the database is nil, an empty string is returned.

func ToCamel Uses

func ToCamel(text string, private ...bool) string

ToCamel converts a string from database column names to corresponding struct field names (e.g. field_name to FieldName).

func ToPlural Uses

func ToPlural(text string) (plural string)

ToPlural returns the plural version of an English word using some simple rules and a table of exceptions.

func ToSingular Uses

func ToSingular(word string) (singular string)

ToSingular converts a word to singular. NB reversal from plurals may fail

func ToSnake Uses

func ToSnake(text string) string

ToSnake converts a string from struct field names to corresponding database column names (e.g. FieldName to field_name).

func Truncate Uses

func Truncate(s string, length int) string

Truncate the given string to length using … as ellipsis.

func TruncateWithEllipsis Uses

func TruncateWithEllipsis(s string, length int, ellipsis string) string

TruncateWithEllipsis truncates the given string to length using provided ellipsis.

type Func Uses

type Func func(q *Query) *Query

Func is a function which applies effects to queries

type Query Uses

type Query struct {
    // contains filtered or unexported fields

Query provides all the chainable relational query builder methods

func New Uses

func New(t string, pk string) *Query

New builds a new Query, given the table and primary key

func (*Query) Apply Uses

func (q *Query) Apply(f Func) *Query

Apply the Func to this query, and return the modified Query This allows chainable finders from other packages e.g. q.Apply(status.Published) where status.Published is a Func

func (*Query) Conditions Uses

func (q *Query) Conditions(funcs ...Func) *Query

Conditions applies a series of query funcs to a query

func (*Query) Copy Uses

func (q *Query) Copy() *Query

Copy returns a new copy of this query which can be mutated without affecting the original

func (*Query) Count Uses

func (q *Query) Count() (int64, error)

Count fetches a count of model objects (executes SQL).

func (*Query) DebugString Uses

func (q *Query) DebugString() string

DebugString returns a query representation string useful for debugging

func (*Query) Delete Uses

func (q *Query) Delete() error

Delete one model specified in this relation

func (*Query) DeleteAll Uses

func (q *Query) DeleteAll() error

DeleteAll delets *all* models specified in this relation

func (*Query) FirstResult Uses

func (q *Query) FirstResult() (Result, error)

FirstResult executes the SQL and returrns the first result

func (*Query) Group Uses

func (q *Query) Group(sql string) *Query

Group defines GROUP BY sql

func (*Query) Having Uses

func (q *Query) Having(sql string) *Query

Having defines HAVING sql

func (*Query) Insert Uses

func (q *Query) Insert(params map[string]string) (int64, error)

Insert inserts a record in the database

func (*Query) InsertJoin Uses

func (q *Query) InsertJoin(a int64, b int64) error

InsertJoin inserts a join clause on the query

func (*Query) InsertJoins Uses

func (q *Query) InsertJoins(a []int64, b []int64) error

InsertJoins using an array of ids (more general version of above) This inserts joins for every possible relation between the ids

func (*Query) Join Uses

func (q *Query) Join(otherModel string) *Query

Join adds an inner join to the query

func (*Query) Limit Uses

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

Limit sets the sql LIMIT with an int

func (*Query) Offset Uses

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

Offset sets the sql OFFSET with an int

func (*Query) OrWhere Uses

func (q *Query) OrWhere(sql string, args ...interface{}) *Query

OrWhere defines a where clause on SQL - Additional calls add WHERE () OR () clauses

func (*Query) Order Uses

func (q *Query) Order(sql string) *Query

Order defines ORDER BY sql

func (*Query) QueryString Uses

func (q *Query) QueryString() string

QueryString builds a query string to use for results

func (*Query) Result Uses

func (q *Query) Result() (sql.Result, error)

Result executes the query against the database, returning sql.Result, and error (no rows) (Executes SQL)

func (*Query) ResultFloat64 Uses

func (q *Query) ResultFloat64(c string) (float64, error)

ResultFloat64 returns the first result from a query stored in the column named col as a float64.

func (*Query) ResultIDSets Uses

func (q *Query) ResultIDSets(a, b string) map[int64][]int64

ResultIDSets returns a map from a values to arrays of b values, the order of a,b is respected not the table key order

func (*Query) ResultIDs Uses

func (q *Query) ResultIDs() []int64

ResultIDs returns an array of ids as the result of a query FIXME - this should really use the query primary key, not "id" hardcoded

func (*Query) ResultInt64 Uses

func (q *Query) ResultInt64(c string) (int64, error)

ResultInt64 returns the first result from a query stored in the column named col as an int64.

func (*Query) Results Uses

func (q *Query) Results() ([]Result, error)

Results returns an array of results

func (*Query) Rows Uses

func (q *Query) Rows() (*sql.Rows, error)

Rows executes the query against the database, and return the sql rows result for this query (Executes SQL)

func (*Query) SQL Uses

func (q *Query) SQL(sql string) *Query

SQL defines sql manually and overrides all other setters Completely replaces all stored sql

func (*Query) Select Uses

func (q *Query) Select(sql string) *Query

Select defines SELECT sql

func (*Query) Update Uses

func (q *Query) Update(params map[string]string) error

Update one model specified in this query - the column names MUST be verified in the model

func (*Query) UpdateAll Uses

func (q *Query) UpdateAll(params map[string]string) error

UpdateAll updates all models specified in this relation

func (*Query) UpdateJoins Uses

func (q *Query) UpdateJoins(id int64, a []int64, b []int64) error

UpdateJoins updates the given joins, using the given id to clear joins first

func (*Query) Where Uses

func (q *Query) Where(sql string, args ...interface{}) *Query

Where defines a WHERE clause on SQL - Additional calls add WHERE () AND () clauses

func (*Query) WhereIn Uses

func (q *Query) WhereIn(col string, IDs []int64) *Query

WhereIn adds a Where clause which selects records IN() the given array If IDs is an empty array, the query limit is set to 0

type Result Uses

type Result map[string]interface{}

Result holds the results of a query as map[string]interface{}


adaptersPackage adapters offers adapters for pouplar databases

Package query imports 8 packages (graph) and is imported by 30 packages. Updated 2018-02-17. Refresh now. Tools for package owners.