Giter Site home page Giter Site logo

ruipserra / soql-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fastly/sql-parser

1.0 2.0 1.0 412 KB

Ruby library for parsing and generating SOQL statements (Salesforce Object Query Language)

License: MIT License

Ruby 91.76% Shell 0.30% REXX 7.94%

soql-parser's Introduction

soql-parser

A Ruby library for parsing and generating SOQL (Salesforce Object Query Language) statements.

Features

  • Parse arbitrary SOQL strings into an AST (abstract syntax tree), which can then be traversed.

  • Allows your code to understand and manipulate SOQL in a deeper way than just using string manipulation.

Usage

TODO: Update these docs

Parsing a statement into an AST

>> require 'soql-parser'
>> parser = SOQLParser::Parser.new

# Build the AST from a SOQL statement
>> ast = parser.scan_str('SELECT * FROM users WHERE id = 1')

# Find which columns where selected in the FROM clause
>> ast.query_expression.list.to_soql
=> "*"

# Output the table expression as SOQL
>> ast.query_expression.table_expression.to_soql
=> "FROM users WHERE id = 1"

# Drill down into the WHERE clause, to examine every piece
>> ast.query_expression.table_expression.where_clause.to_soql
=> "WHERE id = 1"
>> ast.query_expression.table_expression.where_clause.search_condition.to_soql
=> "id = 1"
>> ast.query_expression.table_expression.where_clause.search_condition.left.to_soql
=> "id"
>> ast.query_expression.table_expression.where_clause.search_condition.right.to_soql
=> "1"

Manually building an AST

>> require 'soql-parser'

# Let's build a tree representing the SOQL statement
# "SELECT * FROM users WHERE id = 1"

# We'll start from the rightmost side, and work our way left as we go.

# First, the integer constant, "1"
>> integer_constant = SOQLParser::Statement::Integer.new(1)
>> integer_constant.to_soql
=> "1"

# Now the column reference, "id"
>> column_reference = SOQLParser::Statement::Column.new('id')
>> column_reference.to_soql
=> "id"

# Now we'll combine the two using an equals operator, to create a search
# condition
>> search_condition = SOQLParser::Statement::Equals.new(column_reference, integer_constant)
>> search_condition.to_soql
=> "id = 1"

# Next we'll feed that search condition to a where clause
>> where_clause = SOQLParser::Statement::WhereClause.new(search_condition)
>> where_clause.to_soql
=> "WHERE id = 1"

# Next up is the FROM clause.  First we'll build a table reference
>> users = SOQLParser::Statement::Table.new('users')
>> users.to_soql
=> "users"

# Now we'll feed that table reference to a from clause
>> from_clause = SOQLParser::Statement::FromClause.new(users)
>> from_clause.to_soql
=> "FROM users"

# Now to combine the FROM and WHERE clauses to form a table expression
>> table_expression = SOQLParser::Statement::TableExpression.new(from_clause, where_clause)
>> table_expression.to_soql
=> "FROM users WHERE id = 1"

# Now we need to represent the asterisk "*"
>> all = SOQLParser::Statement::All.new
>> all.to_soql
=> "*"

# Now we're ready to hand off these objects to a select statement
>> select_statement = SOQLParser::Statement::Select.new(all, table_expression)
>> select_statement.to_soql
=> "SELECT * FROM users WHERE id = 1"

Acknowledgments

This gem was adapted from sql-parser.

License

This software is released under the MIT license.

soql-parser's People

Contributors

flowerwrong avatar jackdanger avatar lanej avatar louismullie avatar ruipserra avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

asedge

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.