Class: Mxrb::Oql::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/oql/server.rb

Overview

Loopback-only JSON API for read-only queries against a synchronized Mendix Runtime database.

Constant Summary collapse

MAX_BODY_BYTES =
1_048_576

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path, host: '127.0.0.1', port: 4567, database_port: 55_432, workspace: nil, logger: nil) ⇒ Server

rubocop:disable Metrics/ParameterLists

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/mxrb/oql/server.rb', line 16

def initialize(project_path, host: '127.0.0.1', port: 4567, database_port: 55_432,
               workspace: nil, logger: nil)
  @host = host
  raise ArgumentError, 'the query server must bind to a loopback address' \
    unless %w[127.0.0.1 ::1 localhost].include?(@host)

  @port = Integer(port)
  @workspace = workspace || Runtime::DatabaseWorkspace.new(project_path, port: database_port)
  @logger = logger
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/mxrb/oql/server.rb', line 13

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/mxrb/oql/server.rb', line 13

def port
  @port
end

Instance Method Details

#execute(payload) ⇒ Object

rubocop:disable Metrics/MethodLength



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mxrb/oql/server.rb', line 37

def execute(payload)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  sql, warnings, params = query_sql(payload)
  rows = @workspace.query_rows(sql, params:)
  {
    ok: true, rows:, row_count: rows.size, elapsed_ms: elapsed_ms(started),
    warnings:
  }
rescue ArgumentError, JSON::ParserError => e
  error_payload(e, started, 'invalid_request')
rescue StandardError => e
  error_payload(e, started, 'query_failed')
end

#shutdownObject



34
# File 'lib/mxrb/oql/server.rb', line 34

def shutdown = @server&.shutdown

#start(prepare: true) ⇒ Object

rubocop:enable Metrics/ParameterLists



28
29
30
31
32
# File 'lib/mxrb/oql/server.rb', line 28

def start(prepare: true)
  @workspace.up if prepare
  @server = Http::Server.new(host:, port:, logger: @logger) { dispatch(_1, _2) }
  @server.start { yield(_1) if block_given? }
end