module Nativeson

Constants

VERSION

Public Class Methods

fetch_json_by_query_hash(query_hash) click to toggle source
# File lib/nativeson.rb, line 5
def self.fetch_json_by_query_hash(query_hash)
  nativeson_hash = {}
  nativeson_hash[:query_hash] = query_hash
  nativeson_hash[:container] = NativesonContainer.new(container_type: :base, query: nativeson_hash[:query_hash], parent: nil)
  sql = nativeson_hash[:container].generate_sql
  nativeson_hash[:sql] = sql
  result = ActiveRecord::Base.connection.execute(sql)
  nativeson_hash[:json] = result.getvalue(0, 0)
  result.clear
  return nativeson_hash
end
fetch_json_by_rails_query(rails_query) click to toggle source
# File lib/nativeson.rb, line 17
def self.fetch_json_by_rails_query(rails_query)
  if rails_query.respond_to?(:to_sql)
    nativeson_hash = {}
    nativeson_hash[:sql] = "
    SELECT JSON_AGG(t)
      FROM (
        #{rails_query.to_sql}
      )
    t;"
    result = ActiveRecord::Base.connection.execute(nativeson_hash[:sql])
    nativeson_hash[:json] = result.getvalue(0, 0)
    result.clear
    return nativeson_hash
  else
    raise ArgumentError.new("#{__method__} input doesn't respond to :to_sql")
  end
end
fetch_json_by_string(string) click to toggle source
# File lib/nativeson.rb, line 35
def self.fetch_json_by_string(string)
  if string.is_a?(String)
    nativeson_hash = {}
    nativeson_hash[:sql] = "
    SELECT JSON_AGG(t)
      FROM (
        #{string}
      )
    t;"
    result = ActiveRecord::Base.connection.execute(nativeson_hash[:sql])
    nativeson_hash[:json] = result.getvalue(0, 0)
    result.clear
    return nativeson_hash
  else
    raise ArgumentError.new("#{__method__} input isn't a String")
  end
end