HashSql

Create SQL statements using Hashes. (Currently only supports SELECT statements.)

Installation

Add this line to your application's Gemfile:

gem 'hash_sql'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_sql

Usage

# Basic query
# Will produce:
#   SELECT UID,profile.firstName,profile.lastName,profile.email 
#   FROM accounts 
#   WHERE profile.email='[email protected]' 
#      AND (profile.firstName='Five' AND profile.lastName='Zero')
#
query = HashSql.select_statement(:accounts,
    fields: ['UID', 'profile.firstName', 'profile.lastName', 'profile.email'],
    filter: {
      "profile.email" => "='[email protected]'", 
      and: { "profile.firstName" => "='Five'", "profile.lastName" => "='Zero'" }
    }
)

# Nested conditions
# Will produce:
#   SELECT UID,profile.firstName,profile.lastName,profile.email 
#    FROM accounts 
#    WHERE profile.email='[email protected]' 
#      AND (profile.firstName='Five' 
#        OR (profile.lastName='Zero' AND isActive=true))
#
query = HashSql.select_statement(:accounts,
    fields: ['UID', 'profile.firstName', 'profile.lastName', 'profile.email'],
    filter: {
      "profile.email" => "='[email protected]'", 
      and: { "profile.firstName" => "='Five'", or: { "profile.lastName" => "='Zero'", and: { "isActive" => "=true" } } }
    }
)

Filter Hash

Comparison

The filter to use for searching accounts. The key is the field name to set the filter on, and the value is the concatenation of the operator to use for comparison and the value used for comparing. Example:

{ email: "='[email protected]'"}

AND and ORs:

The logical operations between filters can be AND or OR and the it could be set by setting the key to either "and:" or "or:" Example:

{ email: "='[email protected]", and: {firstname: "='Nick'"} }

Will be translated to: email='[email protected]' AND firstname = 'Nick'

If multiple entries are placed inside an "and:" or "or:" they will "ANDed" or "ORed" together. Example:

{ email: "='[email protected]", and: {firstname: "='Nick'", lastname: "='DS'"} }

Will be translated to email='[email protected]' AND (firstname = 'Nick' AND lastname = 'DS')

Example:

{ email: "='[email protected]", or: {firstname: "='Nick'", lastname: "='DS'"} }

Will be translated to email='[email protected]' OR (firstname = 'Nick' OR lastname = 'DS')

NESTING

Example:

{ email: "='[email protected]", and: {firstname: "='Nick'", or: {lastname: "='DS'"}} }

Will be translated to email='[email protected]' AND (firstname = 'Nick' OR lastname = 'DS')

Contributing

  1. Fork it ( https://github.com/nicknux/hash_sql/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request