Module: SQLHelpers

Extended by:
SQLHelpers
Included in:
SQLHelpers
Defined in:
lib/sql/statement.rb,
lib/sql/expression.rb

Instance Method Summary collapse

Instance Method Details

#sql_func(&block) ⇒ Object

this function takes a block containing an expression, and converts it to a proper SQL expression.

The rules for doing so are quite complex:

  • all Ruby binary operaters are converted to their SQL equivalents
  • the + operator on strings is never converted to the concat() function. Use the concat() function directly.
  • the count_distinct(...) function is converted to count(distinct ...).
  • all Ruby variable references are evaluated and their values substituted, with the exception of the special sql logical method reciever.
  • method calls on ruby objects are evaluated, with the exception of the logical sql method reciever.
  • free functions (for example count(:field)) that are undefined in the surrounding scope are returned as SQL function calls.
  • free functions that are defined in the surrounding scope are called by Ruby. To override this behavior, specify the function as a method call on the virtual reciever sql (for example, sql.object_id becomes object_id(), because in ordinary Ruby contexts, object_id is almost always defined, and will therefore be evaluated.)
  • Symbols are interpreted as field names, just like everywhere else in this library. Array indexes evaluate to tablename.fieldname references. The special symbol : converts to a literal SQL
  • #{...} interpolated strings are not supported (yet).
  • Other objects (except Arrays) are treated as themselves.


60
61
62
# File 'lib/sql/expression.rb', line 60

def sql_func(&block)
   sxp(&block)
end

#string_func(str, placeheld = []) ⇒ Object

This helper method takes a string, and encapsulates it as a proper SQL expression, so that it won't get passed to the database as though it were a (potentially hostile) string value.



58
59
60
# File 'lib/sql/statement.rb', line 58

def string_func(str,placeheld=[])
   SQLStatement::Function.new(str,placeheld)
end