Project

metasql

0.0
No commit activity in last 3 years
No release in over 3 years
Resolve parameters of Metabase flavored query.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Metasql

Gem GitHub

Metasql is Metabase flavored query preprocessor. Provides parameter substitution, optional clause deletion, etc.

Getting Started

require 'metasql'

sql = <<~SQL
  SELECT
      *
  FROM
      items
  WHERE
      foo = TRUE
      AND bar = {{ bar }}
      [[
          AND
          CASE WHEN {{ baz }} < 10
          THEN baz = {{ baz }}
          ELSE TRUE
      ]]
SQL

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.