soql

package
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 9 Imported by: 0

README

SOQL APIs

back

The soql package is an implementation of the Salesforce APIs centered on SOQL operations. These operations include:

  • SOQL query formatter
  • SOQL query
  • SOQL query all

As a reference, see Salesforce API documentation

Examples

The following are examples to access the APIs. It is assumed that a go-sfdc session has been created.

SOQL Builder

The following examplas cenrter around SOQL builder. Although using the builder is not required to use the API, it is recommended as it generates the proper query statement.

SELECT Name, Id FROM Account WHERE Name = 'Golang'
	where, err := soql.WhereEquals("Name", "Golang")
	if err != nil {
		fmt.Printf("SOQL Query Where Statement Error %s\n", err.Error())
		return
	}
	input := soql.QueryInput{
		ObjectType: "Account",
		FieldList: []string{
			"Name",
			"Id",
		},
		Where: where,
	}
	queryStmt, err := soql.NewQuery(input)
	if err != nil {
		fmt.Printf("SOQL Query Statement Error %s\n", err.Error())
		return
	}
	stmt, err := queryStmt.Format()
	if err != nil {
		fmt.Printf("SOQL Query Statement Error %s\n", err.Error())
		return
	}

	fmt.Println("SOQL Query Statement")
	fmt.Println("-------------------")
	fmt.Println(stmt)
SELECT Name, Id, (SELECT LastName FROM Contacts) FROM Account
	subInput := soql.QueryInput{
		ObjectType: "Contacts",
		FieldList: []string{
			"LastName",
		},
	}
	subQuery, err := soql.NewQuery(subInput)
	if err != nil {
		fmt.Printf("SOQL Sub Query Error %s\n", err.Error())
		return
	}

	input := soql.QueryInput{
		ObjectType: "Account",
		FieldList: []string{
			"Name",
			"Id",
		},
		SubQuery: []soql.Querier{
			subQuery,
		},
	}
	queryStmt, err := soql.NewQuery(input)
	if err != nil {
		fmt.Printf("SOQL Query Statement Error %s\n", err.Error())
		return
	}

	stmt, err := queryStmt.Format()
	if err != nil {
		fmt.Printf("SOQL Query Statement Error %s\n", err.Error())
		return
	}

	fmt.Println("SOQL Query Statement")
	fmt.Println("-------------------")
	fmt.Println(stmt)
SOQL Query

The following example demostrates how to SOQL query. It is assumed that a session has need created and a SOQL statement has been built. The SOQL statement is as follows:

SELECT
  Name,
  Id,
  (
      SELECT
        LastName
      FROM
        Contacts  
  )
FROM 
  Account
	resource := soql.NewResource(session)
	result, err := resource.Query(queryStmt, false)
	if err != nil {
		fmt.Printf("SOQL Query Error %s\n", err.Error())
		return
	}
	fmt.Println("SOQL Query")
	fmt.Println("-------------------")
	fmt.Printf("Done: %t\n", result.Done())
	fmt.Printf("Total Size: %d\n", result.TotalSize())
	fmt.Printf("Next Records URL: %t\n\n", result.MoreRecords())

	for _, rec := range result.Records() {
		r := rec.Record()
		fmt.Printf("SObject: %s\n", r.SObject())
		fmt.Printf("Fields: %v\n", r.Fields())
		for obj, subResult := range rec.Subresults() {
			fmt.Printf("Sub Result: %s\n", obj)
			fmt.Printf("Done: %t\n", subResult.Done())
			fmt.Printf("Total Size: %d\n", subResult.TotalSize())
			fmt.Printf("Next Records URL: %t\n", subResult.MoreRecords())
			fmt.Println()
			for _, subRec := range subResult.Records() {
				sr := subRec.Record()
				fmt.Printf("SObject: %s\n", sr.SObject())
				fmt.Printf("Fields: %v\n", sr.Fields())
			}
		}
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderBy

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

OrderBy is the ordering structure of the SOQL query.

func NewOrderBy

func NewOrderBy(result OrderResult) (*OrderBy, error)

NewOrderBy creates an OrderBy structure. If the order results is not ASC or DESC, an error will be returned.

func (*OrderBy) FieldOrder

func (o *OrderBy) FieldOrder(fields ...string)

FieldOrder is a list of fields in the ordering.

func (*OrderBy) NullOrdering

func (o *OrderBy) NullOrdering(nulls OrderNulls) error

NullOrdering sets the ordering, first or last, of the null values.

func (*OrderBy) Order

func (o *OrderBy) Order() (string, error)

Order returns the order by SOQL string.

type OrderNulls

type OrderNulls string

OrderNulls is where the null values are placed in the ordering.

const (
	// OrderNullsLast places the null values at the end of the ordering.
	OrderNullsLast OrderNulls = "NULLS LAST"
	// OrderNullsFirst places the null values at the start of the ordering.
	OrderNullsFirst OrderNulls = "NULLS FIRST"
)

type OrderResult

type OrderResult string

OrderResult is the type of ordering of the query result.

const (
	// OrderAsc will place the results in ascending order.
	OrderAsc OrderResult = "ASC"
	// OrderDesc will place the results in descending order.
	OrderDesc OrderResult = "DESC"
)

type Orderer

type Orderer interface {
	Order() (string, error)
}

Orderer is the interface for returning the SOQL ordering.

type Query

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

Query is the struture used to build a SOQL query.

func NewQuery

func NewQuery(input QueryInput) (*Query, error)

NewQuery creates a new builder. If the object is an empty string, then an error is returned.

func (*Query) Format

func (b *Query) Format() (string, error)

Format will return the SOQL query. If the builder has an empty string or the field list is zero, an error is returned.

type QueryFormatter

type QueryFormatter interface {
	Format() (string, error)
}

QueryFormatter is the interface to return the SOQL query.

Format returns the SOQL query.

type QueryInput

type QueryInput struct {
	FieldList  []string
	ObjectType string
	SubQuery   []QueryFormatter
	Where      WhereClauser
	Order      Orderer
	Limit      int
	Offset     int
}

QueryInput is used to provide SOQL inputs.

ObjectType is the Salesforce Object, like Account

FieldList is the Salesforce Object's fields to query

SubQuery is the inner query

Where is the SOQL where cause

Order is the SOQL ordering

Limit is the SOQL record limit

Offset is the SOQL record offset

type QueryRecord

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

QueryRecord is the result of the SOQL record. If the query statement contains an inner query, there will be a group of subresults.

func (*QueryRecord) Record

func (rec *QueryRecord) Record() *sfdc.Record

Record returns the SOQL record.

func (*QueryRecord) Subresult

func (rec *QueryRecord) Subresult(sub string) (*QueryResult, bool)

Subresult returns a specific inner query result.

func (*QueryRecord) Subresults

func (rec *QueryRecord) Subresults() map[string]*QueryResult

Subresults returns all of the inner query results.

type QueryResult

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

QueryResult is returned from the SOQL query. This will allow for retrieving all of the records and query the next round of records if available.

func (*QueryResult) Done

func (result *QueryResult) Done() bool

Done will indicate if the result does not contain any more records.

func (*QueryResult) MoreRecords

func (result *QueryResult) MoreRecords() bool

MoreRecords will indicate if the remaining records require another Saleforce service callout.

func (*QueryResult) Next

func (result *QueryResult) Next() (*QueryResult, error)

Next will query the next set of records.

func (*QueryResult) Records

func (result *QueryResult) Records() []*QueryRecord

Records returns the records from the query request.

func (*QueryResult) TotalSize

func (result *QueryResult) TotalSize() int

TotalSize is the total size of the query result. This may or may not be the size of the records in the result.

type Resource

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

Resource is the structure for the Salesforce SOQL API resource.

func NewResource

func NewResource(session session.ServiceFormatter) (*Resource, error)

NewResource forms the Salesforce SOQL resource. The session formatter is required to form the proper URLs and authorization header.

func (*Resource) Query

func (r *Resource) Query(querier QueryFormatter, all bool) (*QueryResult, error)

Query will call out to the Salesforce org for a SOQL. The results will be the result of the query. The all parameter is for querying all records, which include deleted records that are in the recycle bin.

type WhereClause

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

WhereClause is the structure that will contain a SOQL where clause.

func WhereEquals

func WhereEquals(field string, value interface{}) (*WhereClause, error)

WhereEquals forms the equals where expression.

func WhereGreaterThan

func WhereGreaterThan(field string, value interface{}, equals bool) (*WhereClause, error)

WhereGreaterThan will form the greater or equal than expression. If the value is a string or boolean, an error is returned.

func WhereIn

func WhereIn(field string, values []interface{}) (*WhereClause, error)

WhereIn forms the field in a set expression.

func WhereLessThan

func WhereLessThan(field string, value interface{}, equals bool) (*WhereClause, error)

WhereLessThan will form the less or equal than expression. If the value is a string or boolean, an error is returned.

func WhereLike

func WhereLike(field string, value string) (*WhereClause, error)

WhereLike will form the LIKE expression.

func WhereNotEquals

func WhereNotEquals(field string, value interface{}) (*WhereClause, error)

WhereNotEquals forms the not equals where expression.

func WhereNotIn

func WhereNotIn(field string, values []interface{}) (*WhereClause, error)

WhereNotIn forms the field is not in a set expression.

func (*WhereClause) And

func (wc *WhereClause) And(where WhereExpression)

And will logical AND the expressions.

func (*WhereClause) Clause

func (wc *WhereClause) Clause() string

Clause returns the where cluase.

func (*WhereClause) Expression

func (wc *WhereClause) Expression() string

Expression will return the where expression.

func (*WhereClause) Group

func (wc *WhereClause) Group()

Group will form a grouping around the expression.

func (*WhereClause) Or

func (wc *WhereClause) Or(where WhereExpression)

Or will logical OR the expressions.

type WhereClauser

type WhereClauser interface {
	Clause() string
}

WhereClauser is an interface to return the where cause.

type WhereExpression

type WhereExpression interface {
	Expression() string
}

WhereExpression is an interface to return the where cause's expression.

Jump to

Keyboard shortcuts

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