Class: TrinoSqlParser::SupportProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/trino_sql_parser/support_process.rb

Instance Method Summary collapse

Constructor Details

#initialize(idle_timeout:, with_tokens:, with_statement:) ⇒ SupportProcess

Returns a new instance of SupportProcess.



5
6
7
8
9
10
11
12
13
# File 'lib/trino_sql_parser/support_process.rb', line 5

def initialize(idle_timeout:, with_tokens:, with_statement:)
  @idle_timeout = idle_timeout
  @with_tokens = with_tokens
  @with_statement = with_statement
  @mutex = Mutex.new
  @last_used_pid = nil
  @pipe = nil
  @pid = nil
end

Instance Method Details

#kill!Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/trino_sql_parser/support_process.rb', line 36

def kill!
  @mutex.synchronize do
    if @pid
      Process.kill("KILL", @pid)
      @pipe.close rescue nil
      @pipe = nil
      @pid = nil
    end
  end
end

#send_and_receive_line(line) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/trino_sql_parser/support_process.rb', line 47

def send_and_receive_line(line)
  @mutex.synchronize do  # block kill! during execution
    start! unless @pipe
    @pipe.puts line
    @last_used_pid = @pipe.pid
    @pipe.gets
  end
end

#start!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trino_sql_parser/support_process.rb', line 15

def start!
  return if @pipe

  cmd = (
    [TrinoSqlParser.java_cmd] +
    TrinoSqlParser.java_args.map {|arg| Shellwords.escape(arg) } +
    ["-jar", Shellwords.escape(TrinoSqlParser.jar_path)]
  ).join(' ')

  if @with_tokens
    cmd << " --with-tokens"
  end
  if @with_statement
    cmd << " --with-statement"
  end

  @pipe = IO.popen(TrinoSqlParser.java_env, cmd, "r+", external_encoding: 'UTF-8')
  @pid = @pipe.pid
  Thread.new(@pid, &method(:monitor_thread))
end