# 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())
			}
		}
	}

# Functions

NewOrderBy creates an OrderBy structure.
NewQuery creates a new builder.
NewResource forms the Salesforce SOQL resource.
WhereEquals forms the equals where expression.
WhereGreaterThan will form the greater or equal than expression.
WhereIn forms the field in a set expression.
WhereLessThan will form the less or equal than expression.
WhereLike will form the LIKE expression.
WhereNotEquals forms the not equals where expression.
WhereNotIn forms the field is not in a set expression.

# Constants

OrderAsc will place the results in ascending order.
OrderDesc will place the results in descending order.
OrderNullsFirst places the null values at the start of the ordering.
OrderNullsLast places the null values at the end of the ordering.

# Structs

OrderBy is the ordering structure of the SOQL query.
Query is the struture used to build a SOQL query.
QueryInput is used to provide SOQL inputs.
QueryRecord is the result of the SOQL record.
QueryResult is returned from the SOQL query.
Resource is the structure for the Salesforce SOQL API resource.
WhereClause is the structure that will contain a SOQL where clause.

# Interfaces

Orderer is the interface for returning the SOQL ordering.
QueryFormatter is the interface to return the SOQL query.
WhereClauser is an interface to return the where cause.
WhereExpression is an interface to return the where cause's expression.

# Type aliases

OrderNulls is where the null values are placed in the ordering.
OrderResult is the type of ordering of the query result.