Class: Pinot::PreparedStatementImpl
- Inherits:
-
Object
- Object
- Pinot::PreparedStatementImpl
- Includes:
- PreparedStatement
- Defined in:
- lib/pinot/prepared_statement.rb
Instance Method Summary collapse
- #build_query(params) ⇒ Object
- #clear_parameters ⇒ Object
- #close ⇒ Object
- #execute ⇒ Object
- #execute_with_params(*params) ⇒ Object
- #get_parameter_count ⇒ Object
- #get_query ⇒ Object
-
#initialize(connection:, table:, query_template:) ⇒ PreparedStatementImpl
constructor
A new instance of PreparedStatementImpl.
- #set(index, value) ⇒ Object
- #set_bool(index, value) ⇒ Object
- #set_float64(index, value) ⇒ Object
- #set_int(index, value) ⇒ Object
- #set_int64(index, value) ⇒ Object
- #set_string(index, value) ⇒ Object
Constructor Details
#initialize(connection:, table:, query_template:) ⇒ PreparedStatementImpl
Returns a new instance of PreparedStatementImpl.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pinot/prepared_statement.rb', line 13 def initialize(connection:, table:, query_template:) @connection = connection @table = table @query_template = query_template @parts = query_template.split("?", -1) @param_count = @parts.length - 1 @parameters = Array.new(@param_count) @mutex = Mutex.new @closed = false end |
Instance Method Details
#build_query(params) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pinot/prepared_statement.rb', line 107 def build_query(params) if params.length != @param_count raise "expected #{@param_count} parameters, got #{params.length}" end result = "" params.each_with_index do |param, i| formatted = begin @connection.format_arg(param) rescue => e raise "failed to format parameter: #{e.}" end result += @parts[i] + formatted end result + @parts.last end |
#clear_parameters ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/pinot/prepared_statement.rb', line 91 def clear_parameters @mutex.synchronize do raise "prepared statement is closed" if @closed @parameters.fill(nil) end nil end |
#close ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/pinot/prepared_statement.rb', line 99 def close @mutex.synchronize do @closed = true @parameters = nil end nil end |
#execute ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pinot/prepared_statement.rb', line 63 def execute @mutex.synchronize do raise "prepared statement is closed" if @closed @parameters.each_with_index do |p, i| raise "parameter at index #{i + 1} is not set" if p.nil? end end query = begin build_query(@parameters) rescue => e raise "failed to build query: #{e.}" end @connection.execute_sql(@table, query) end |
#execute_with_params(*params) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pinot/prepared_statement.rb', line 78 def execute_with_params(*params) @mutex.synchronize { raise "prepared statement is closed" if @closed } if params.length != @param_count raise "expected #{@param_count} parameters, got #{params.length}" end query = begin build_query(params) rescue => e raise "failed to build query: #{e.}" end @connection.execute_sql(@table, query) end |
#get_parameter_count ⇒ Object
28 29 30 |
# File 'lib/pinot/prepared_statement.rb', line 28 def get_parameter_count @param_count end |
#get_query ⇒ Object
24 25 26 |
# File 'lib/pinot/prepared_statement.rb', line 24 def get_query @query_template end |
#set(index, value) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pinot/prepared_statement.rb', line 52 def set(index, value) @mutex.synchronize do raise "prepared statement is closed" if @closed unless index >= 1 && index <= @param_count raise "parameter index #{index} is out of range [1, #{@param_count}]" end @parameters[index - 1] = value end nil end |
#set_bool(index, value) ⇒ Object
48 49 50 |
# File 'lib/pinot/prepared_statement.rb', line 48 def set_bool(index, value) set(index, !!value) end |
#set_float64(index, value) ⇒ Object
44 45 46 |
# File 'lib/pinot/prepared_statement.rb', line 44 def set_float64(index, value) set(index, value.to_f) end |
#set_int(index, value) ⇒ Object
36 37 38 |
# File 'lib/pinot/prepared_statement.rb', line 36 def set_int(index, value) set(index, value.to_i) end |
#set_int64(index, value) ⇒ Object
40 41 42 |
# File 'lib/pinot/prepared_statement.rb', line 40 def set_int64(index, value) set(index, value.to_i) end |
#set_string(index, value) ⇒ Object
32 33 34 |
# File 'lib/pinot/prepared_statement.rb', line 32 def set_string(index, value) set(index, value.to_s) end |