Metasql
Metasql is Metabase flavored query preprocessor. Provides parameter substitution, optional clause deletion, etc.
Getting Started
require 'metasql'
sql = "SELECT\n *\nFROM\n items\nWHERE\n foo = TRUE\n AND bar = {{ bar }}\n [[\n AND\n CASE WHEN {{ baz }} < 10\n THEN baz = {{ baz }}\n ELSE TRUE\n ]]\n"
query = Metasql::Parser.parse(sql)
parameters = {
bar: 'hi',
baz: 10
}
# with parameter
print query.with(parameters).deparse
# => SELECT
# *
# FROM
# items
# WHERE
# foo = TRUE
# AND bar = 'hi'
#
# AND
# CASE WHEN 10 < 10
# THEN baz = 10
# ELSE TRUE
# without parameter
print query.deparse
# => Metasql::ParameterMissing: Required parameters missing: bar
# partially supply parameter
print query.with({ bar: 'hi' }).deparse
# => SELECT
# *
# FROM
# items
# WHERE
# foo = TRUE
# AND bar = 'hi'
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nobuyo/metasql.
License
The gem is available as open source under the terms of the MIT License.