class SP::Duh::JSONAPI::Adapters::RawDb

Protected Instance Methods

do_request(method, path, param) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 26
def do_request(method, path, param)
  process_result(do_request_on_the_db(method, path, params))
end
explicit_do_request(accounting_schema, method, path, param) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 30
def explicit_do_request(accounting_schema, method, path, param)
  process_result(explicit_do_request_on_the_db(accounting_schema, method, path, params))
end
get_error_response(path, error) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 24
def get_error_response(path, error) ; error_response(path, error).to_json ; end
unwrap_response(response) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 10
def unwrap_response(response)
  # As the method request() is EXACTLY the same as request!(), and it cannot be reverted without affecting lots of changes already made in the app's controllers...
  # Allow for response being both a [ status, result ] pair (as of old) OR a single result (as of now)
  if response.is_a?(Array)
    status = response[0].to_i
    result = response[1]
    raise SP::Duh::JSONAPI::Exceptions::GenericModelError.new(result) if status != SP::Duh::JSONAPI::Status::OK
    result
  else
    # No raise here, we do not know the status...
    response
  end
end

Private Instance Methods

accounting_prefix() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 40
def accounting_prefix ; service.parameters.accounting_prefix.nil? ? 'NULL' : "'#{service.parameters.accounting_prefix}'" ; end
accounting_schema() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 39
def accounting_schema ; service.parameters.accounting_schema.nil? ? 'NULL' : "'#{service.parameters.accounting_schema}'" ; end
company_id() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 36
def company_id        ; "'#{service.parameters.company_id}'" ; end
company_schema() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 37
def company_schema    ; service.parameters.company_schema.nil? ? 'NULL' : "'#{service.parameters.company_schema}'" ; end
do_request_on_the_db(method, path, params) click to toggle source

Implement the JSONAPI request by direct querying of the JSONAPI function in the database

# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 48
def do_request_on_the_db(method, path, params)
  jsonapi_query = if method == 'GET'
    %Q[ SELECT * FROM public.jsonapi('#{method}', '#{url_with_params_for_query(path, params)}', '', #{user_id}, #{company_id}, #{company_schema}, #{sharded_schema}, #{accounting_schema}, #{accounting_prefix}) ]
  else
    %Q[ SELECT * FROM public.jsonapi('#{method}', '#{url(path)}', '#{params_for_body(params)}', #{user_id}, #{company_id}, #{company_schema}, #{sharded_schema}, #{accounting_schema}, #{accounting_prefix}) ]
  end
  response = service.connection.exec jsonapi_query
  response.first if response.first
end
explicit_do_request_on_the_db(exp_accounting_schema, exp_accounting_prefix, method, path, params) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 58
def explicit_do_request_on_the_db(exp_accounting_schema, exp_accounting_prefix, method, path, params)
  _accounting_schema = "'#{exp_accounting_schema}'"
  _accounting_prefix = "'#{exp_accounting_prefix}'"

  jsonapi_query = if method == 'GET'
    %Q[ SELECT * FROM public.jsonapi('#{method}', '#{url_with_params_for_query(path, params)}', '', #{user_id}, #{company_id}, #{company_schema}, #{sharded_schema}, #{_accounting_schema}, #{_accounting_prefix}) ]
  else
    %Q[ SELECT * FROM public.jsonapi('#{method}', '#{url(path)}', '#{params_for_body(params)}', #{user_id}, #{company_id}, #{company_schema}, #{sharded_schema}, #{_accounting_schema}, #{_accounting_prefix}) ]
  end
  response = service.connection.exec jsonapi_query
  response.first if response.first
end
is_error?(result) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 71
def is_error?(result) ; result =~ /^\s*{\s*"errors"\s*:/ ; end
process_result(result) click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 42
def process_result(result)
  raise SP::Duh::JSONAPI::Exceptions::GenericModelError.new(result) if is_error?(result)
  [ SP::Duh::JSONAPI::Status::OK, result ]
end
sharded_schema() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 38
def sharded_schema    ; service.parameters.sharded_schema.nil? ? 'NULL' : "'#{service.parameters.sharded_schema}'" ; end
user_id() click to toggle source
# File lib/sp/duh/jsonapi/adapters/raw_db.rb, line 35
def user_id           ; "'#{service.parameters.user_id}'" ; end