soql

package
v0.0.1-alpha.0...-700cb3a Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 8 Imported by: 0

README

SOQL (Salesforce Object Query Language)

Utilitzes method chaining and the builder design pattern to elegantly build SOQL queries.

In this context SOQL is a read-only API for querying data from the salesforce REST API. It does not support parameterized inputs. There are SQL Injection Vulnerabilities if you allow unsanitized input from the web.

One method of SQL Injection for example would be to unescape the single quotes which encapsulate strings in SOQL and inserting additional fields within a query. This is difficult but especially because this code is open sourced it is still a threat.

Sanitize your inputs. Even

builder := soql.
    Select("Id", "Name").
    From("Lead").
    Where(soql.And{
        soql.Eq{"FirstName": "Benjamin"},
        // salesforce datetime's use a custom format which types.Datetime accomodates 
        soql.Gt{"CreatedDate": types.NewDatetime(time.Now().Add(time.Hour).String())},
    }).
    Limit(1)

SOQL is intended to be used directly with the requests package.

type entityQuery struct {
    types.QueryResponse
    Records []*entitydefinitions.EntityDefinition `json:"records"`
}

var response entityQuery 
_, err := requests. 
    Sender(salesforce.DefaultClient). 
    URL("tooling/query"). 
    SQLizer(soql.
        Select("QualifiedApiName"). 
        From("EntityDefinition").
        Limit(10)).
    JSON(&response)

It has no issues querying parent records or child records (via subqueries). However you should use generated types with a recursion level of 1 so that they may be unmarshalled automatically.

go-salesforce-sdk generate Lead ./ leads 1
type leadQuery struct {
    types.QueryResponse
    Records []*leads.Lead `json:"records"`
}

var response2 leadQuery
subquery := soql.Select("Id", "Body").From("Attachments")
_, err = requests.
    Sender(salesforce.DefaultClient).
    URL("query").
    SQLizer(
        soql.
            Select("Id", "Name").
            Column(soql.SubQuery(subquery)).
            From("Lead").
            Limit(10),
    ).
    JSON(&response2)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Empty = sb

Empty is a empty builder useful for

Functions

This section is empty.

Types

type And

type And conj

And reuses conj.join to form AND clauses

func (And) ToSQL

func (a And) ToSQL() (sql string, err error)

ToSQL ...

type Builder

type Builder builder.Builder

Builder is used to compose highly dynamic and reusable SOQL queries

func Select

func Select(columns ...string) Builder

Select returns a SelectBuilder with the given columns

func String

func String(query string) Builder

String returns a SelectBuilder composed as a singular string. This builder should not be extended as the query is stored and prepended as a prefix to form the entire query. Its used to satisy the requests.SQLizer signature when a string is the only input.

func (Builder) Column

func (b Builder) Column(column interface{}) Builder

Column adds a result column to the query.

func (Builder) Columns

func (b Builder) Columns(columns ...string) Builder

Columns adds result columns to the query.

func (Builder) From

func (b Builder) From(from string) Builder

From sets the FROM clause of the query.

func (Builder) FromSelect

func (b Builder) FromSelect(from Builder, alias string) Builder

FromSelect sets a subquery into the FROM clause of the query.

func (Builder) GroupBy

func (b Builder) GroupBy(groupBys ...string) Builder

GroupBy appends group by claus(es) to selectData

func (Builder) Having

func (b Builder) Having(pred interface{}) Builder

Having appends a having clause to selectData

func (Builder) Limit

func (b Builder) Limit(limit int) Builder

Limit appends a limit to the query

func (Builder) MustSQL

func (b Builder) MustSQL() string

MustSQL calls ToSQL and panics instead of returning an error

func (Builder) Offset

func (b Builder) Offset(offset int) Builder

Offset appends an offset to the query

func (Builder) Options

func (b Builder) Options(options ...string) Builder

Options adds an additional clause between SELECT and COLUMNS, not sure if this has any application within SOQL but I'll keep it in case it proves useful one day (I don't think its unreasonable to expect Salesforce to port more features from Postgres/MariaDB/etc over time)

func (Builder) OrderBy

func (b Builder) OrderBy(orderBys ...string) Builder

OrderBy appends order by clauses to selectData

func (Builder) OrderByClause

func (b Builder) OrderByClause(pred interface{}) Builder

OrderByClause appends an OrderBy clause to selectData

func (Builder) Prefix

func (b Builder) Prefix(sql string) Builder

Prefix appends the given SQL to the beginning of a query

func (Builder) RemoveLimit

func (b Builder) RemoveLimit() Builder

RemoveLimit removes a limit from a query

func (Builder) RemoveOffset

func (b Builder) RemoveOffset() Builder

RemoveOffset removes an offset from the queery

func (Builder) SetColumns

func (b Builder) SetColumns(columns ...string) Builder

SetColumns is like Columns but it replaces existing columns rather then extending them

func (Builder) Suffix

func (b Builder) Suffix(sql string) Builder

Suffix adds an expression to the end of the query

func (Builder) ToSQL

func (b Builder) ToSQL() (string, error)

ToSQL marshalls the selectData builder into an SOQL string

func (Builder) Where

func (b Builder) Where(predicate interface{}) Builder

Where builds a where clause using nested SQLizers. Each SQLizer is responsible for serializing itself and any nested SQLizer, so all the SelectBuilder needs to do is maintain and apply a list of top-level SQLizers. See expressions.go for how the SQLizers work in practice.

The following types are accepted as a predicate (as seen in newWhereSQLizer()):

switch predicate.(type) { case nil:

// noop
return "", nil

case SQLizer:

return pred.ToSQL()

case map[string]interface{}:

return Eq(pred).ToSQL()

case string:

return pred, nil

type Eq

type Eq map[string]interface{}

Eq produces a complex clause for use by Where/Having methods. It may contain nested values and lists which are converted into IN queries.

func (Eq) ToSQL

func (eq Eq) ToSQL() (string, error)

ToSQL ...

type Fragment

type Fragment string

Fragment ...

func (*Fragment) ToSQL

func (f *Fragment) ToSQL() (sql string, err error)

ToSQL ...

type Gt

type Gt Lt

Gt k > v

func (Gt) ToSQL

func (gt Gt) ToSQL() (sql string, err error)

ToSQL ...

type GtOrEq

type GtOrEq Lt

GtOrEq k >= v

func (GtOrEq) ToSQL

func (gtOrEq GtOrEq) ToSQL() (sql string, err error)

ToSQL ...

type Like

type Like map[string]string

Like prepares a LIKE clause.

func (Like) ToSQL

func (lk Like) ToSQL() (sql string, err error)

ToSQL ...

type Lt

type Lt map[string]interface{}

Lt prepares a Less than clause. k < v

func (Lt) ToSQL

func (lt Lt) ToSQL() (sql string, err error)

ToSQL ...

type LtOrEq

type LtOrEq Lt

LtOrEq k <= v

func (LtOrEq) ToSQL

func (ltOrEq LtOrEq) ToSQL() (sql string, err error)

ToSQL ...

type NotEq

type NotEq Eq

NotEq is the inverse of Eq. See the documentation for Eq.

func (NotEq) ToSQL

func (neq NotEq) ToSQL() (sql string, err error)

ToSQL reuses Eq.toSQL() to convert NotEq to an sql string

type NotLike

type NotLike Like

NotLike is the inverse of Like.

func (NotLike) ToSQL

func (nlk NotLike) ToSQL() (sql string, err error)

ToSQL ...

type Or

type Or conj

Or reuses conj.Join to form OR clauses

func (Or) ToSQL

func (o Or) ToSQL() (sql string, err error)

ToSQL ...

type SQLizer

type SQLizer interface {
	ToSQL() (string, error)
}

SQLizer defines a component which can be converted to SQL. It is the base building block of this package.

func Alias

func Alias(expr SQLizer, alias string) SQLizer

Alias produces an SQL alias expression

func Expr

func Expr(sql string) SQLizer

Expr represents a sql expression that is already fully formed

func SubQuery

func SubQuery(expr SQLizer) SQLizer

SubQuery ...

Jump to

Keyboard shortcuts

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