Class: Pinot::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pinot/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(transport:, broker_selector:, use_multistage_engine: false, logger: nil) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
# File 'lib/pinot/connection.rb', line 5

def initialize(transport:, broker_selector:, use_multistage_engine: false, logger: nil)
  @transport = transport
  @broker_selector = broker_selector
  @use_multistage_engine = use_multistage_engine
  @trace = false
  @logger = logger
end

Instance Method Details

#close_traceObject



21
22
23
# File 'lib/pinot/connection.rb', line 21

def close_trace
  @trace = false
end

#execute_sql(table, query) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/pinot/connection.rb', line 25

def execute_sql(table, query)
  logger.debug "Executing SQL on table=#{table}: #{query}"
  broker = @broker_selector.select_broker(table)
  @transport.execute(broker, build_request(query))
rescue => e
  raise "unable to execute SQL on table #{table}: #{e.message}"
end

#execute_sql_with_params(table, query_pattern, params) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/pinot/connection.rb', line 33

def execute_sql_with_params(table, query_pattern, params)
  query = format_query(query_pattern, params)
  execute_sql(table, query)
rescue => e
  # Re-raise format errors directly (they already have the right message)
  raise e
end

#format_arg(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pinot/connection.rb', line 68

def format_arg(value)
  case value
  when String
    "'#{value.gsub("'", "''")}'"
  when Integer
    value.to_s
  when Float
    value.to_s
  when TrueClass, FalseClass
    value.to_s
  when BigDecimal
    s = value.to_s("F")
    # Strip trailing .0 for whole numbers (mirrors Go's big.Int quoted format)
    s = s.sub(/\.0\z/, "") if s.end_with?(".0")
    "'#{s}'"
  when Time
    "'#{value.utc.strftime("%Y-%m-%d %H:%M:%S.") + format("%03d", value.utc.subsec * 1000)}'"
  else
    raise "unsupported type: #{value.class}"
  end
end

#format_query(pattern, params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pinot/connection.rb', line 49

def format_query(pattern, params)
  params ||= []
  placeholders = pattern.count("?")
  if placeholders != params.length
    raise "failed to format query: number of placeholders in queryPattern (#{placeholders}) does not match number of params (#{params.length})"
  end
  parts = pattern.split("?", -1)
  result = ""
  params.each_with_index do |param, i|
    formatted = begin
      format_arg(param)
    rescue => e
      raise "failed to format query: failed to format parameter: #{e.message}"
    end
    result += parts[i] + formatted
  end
  result + parts.last
end

#open_traceObject



17
18
19
# File 'lib/pinot/connection.rb', line 17

def open_trace
  @trace = true
end

#prepare(table, query_template) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/pinot/connection.rb', line 41

def prepare(table, query_template)
  raise ArgumentError, "table name cannot be empty" if table.nil? || table.strip.empty?
  raise ArgumentError, "query template cannot be empty" if query_template.nil? || query_template.strip.empty?
  count = query_template.count("?")
  raise ArgumentError, "query template must contain at least one parameter placeholder (?)" if count == 0
  PreparedStatementImpl.new(connection: self, table: table, query_template: query_template)
end

#use_multistage_engine=(val) ⇒ Object



13
14
15
# File 'lib/pinot/connection.rb', line 13

def use_multistage_engine=(val)
  @use_multistage_engine = val
end