Class: Json2sql::SelectRunner
- Inherits:
-
Object
- Object
- Json2sql::SelectRunner
- Defined in:
- lib/json2sql/select_runner.rb
Overview
Builds a top-level SELECT statement from a Hash of table → params.
Usage:
sql = Json2sql::SelectRunner.build(
"users" => {
"columns" => ["id", "name", "email"],
"and" => { "active" => 1, "role" => { "in" => [1, 2] } },
"order" => { "created_at" => "desc" },
"limit" => 20,
"offset" => 0,
"options" => ["total"]
}
)
Output wraps every table in JSON_OBJECT so the client receives a single JSON document:
SELECT JSON_OBJECT('users', (...));
Class Method Summary collapse
Class Method Details
.build(input) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/json2sql/select_runner.rb', line 23 def self.build(input) sql = +"" separator = false input = Json2sql.normalize(input) relation = WhereRelation.none("") sql << "SELECT JSON_OBJECT(" input.each do |table, value| next unless value.is_a?(Hash) sql << ", " if separator separator = true sql << Sanitizer.keyword_wrap(table.to_s, "'") sql << ", (" SelectModel.new(sql, table.to_s, relation).(value) sql << ")" end sql << ");\n" sql end |