Class: Mysql::Stmt

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mysql/stmt.rb,
lib/mysql/constants.rb

Overview

Prepared statement

Constant Summary collapse

CURSOR_TYPE_NO_CURSOR =

Cursor type

0
CURSOR_TYPE_READ_ONLY =
1
CURSOR_TYPE_FOR_UPDATE =
2
CURSOR_TYPE_SCROLLABLE =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol, **opts) ⇒ Stmt

Returns a new instance of Stmt.

Parameters:



33
34
35
36
37
38
39
40
# File 'lib/mysql/stmt.rb', line 33

def initialize(protocol, **opts)
  @protocol = protocol
  @opts = opts
  @statement_id = nil
  @affected_rows = @insert_id = @server_status = @warning_count = 0
  @sqlstate = "00000"
  @param_count = nil
end

Instance Attribute Details

#affected_rowsInteger (readonly)

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#fieldsArray<Mysql::Field> (readonly)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#infoObject (readonly)

Returns the value of attribute info.



21
22
23
# File 'lib/mysql/stmt.rb', line 21

def info
  @info
end

#insert_idInteger (readonly)

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#param_countInteger (readonly)

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#server_statusInteger (readonly)

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#sqlstateString (readonly)

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

#warning_countInteger (readonly)

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mysql/stmt.rb', line 18

class Stmt
  include Enumerable

  attr_reader :affected_rows, :info, :insert_id, :server_status, :warning_count
  attr_reader :param_count, :fields, :sqlstate

  # @private
  def self.finalizer(protocol, statement_id)
    proc do
      protocol.gc_stmt statement_id
    end
  end

  # @private
  # @param [Mysql::Protocol] protocol
  def initialize(protocol, **opts)
    @protocol = protocol
    @opts = opts
    @statement_id = nil
    @affected_rows = @insert_id = @server_status = @warning_count = 0
    @sqlstate = "00000"
    @param_count = nil
  end

  # @private
  # parse prepared-statement and return {Mysql::Stmt} object
  # @param [String] str query string
  # @return self
  def prepare(str)
    raise ClientError, 'MySQL client is not connected' unless @protocol
    close
    begin
      @sqlstate = "00000"
      @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
    self
  end

  # Execute prepared statement.
  # @param [Object] values values passed to query
  # @return [Mysql::Result] if return_result is true and the query returns result set.
  # @return [nil] if return_result is true and the query does not return result set.
  # @return [self] if return_result is false or block is specified.
  def execute(*values, **opts, &block)
    raise ClientError, "Invalid statement handle" unless @statement_id
    raise ClientError, "not prepared" unless @param_count
    raise ClientError, "parameter count mismatch" if values.length != @param_count
    values = values.map{|v| @protocol.charset.convert v}
    opts = @opts.merge(opts)
    begin
      @sqlstate = "00000"
      @protocol.stmt_execute_command @statement_id, values
      @fields = @result = nil
      if block
        while true
          get_result
          res = store_result(**opts)
          block.call res if res || opts[:yield_null_result]
          break unless more_results?
        end
        return self
      end
      get_result
      return self unless opts[:return_result]
      return store_result(**opts)
    rescue ServerError => e
      @last_error = e
      @sqlstate = e.sqlstate
      raise
    end
  end

  def get_result
    @protocol.get_result
    @affected_rows, @insert_id, @server_status, @warning_count, @info =
      @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
  end

  def store_result(**opts)
    return nil if @protocol.field_count.nil? || @protocol.field_count == 0
    @fields = @protocol.retr_fields
    opts = @opts.merge(opts)
    @result = StatementResult.new(@fields, @protocol, **opts)
  end

  def more_results?
    @protocol.more_results?
  end

  # execute next query if precedure is called.
  # @return [Mysql::StatementResult] result set of query if return_result is true.
  # @return [true] if return_result is false and result exists.
  # @return [nil] query returns no results or no more results.
  def next_result(**opts)
    return nil unless more_results?
    opts = @opts.merge(opts)
    @fields = @result = nil
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end

  # Close prepared statement
  # @return [void]
  def close
    ObjectSpace.undefine_finalizer(self)
    @protocol.stmt_close_command @statement_id if @statement_id
    @statement_id = nil
  end

  # @return [Array] current record data
  def fetch(**opts)
    @result.fetch(**opts)
  end

  # Return data of current record as Hash.
  # The hash key is field name.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @return [Hash] record data
  def fetch_hash(**opts)
    @result.fetch_hash(**opts)
  end

  # Iterate block with record.
  # @yield [Array] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each(**opts, &block)
    return enum_for(:each, **opts) unless block
    while (rec = fetch(*opts))
      block.call rec
    end
    self
  end

  # Iterate block with record as Hash.
  # @param [Boolean] with_table if true, hash key is "table_name.field_name".
  # @yield [Hash] record data
  # @return [Mysql::Stmt] self
  # @return [Enumerator] If block is not specified
  def each_hash(**opts, &block)
    return enum_for(:each_hash, **opts) unless block
    while (rec = fetch_hash(**opts))
      block.call rec
    end
    self
  end

  # @return [Integer] number of record
  def size
    @result.size
  end
  alias num_rows size

  # Set record position
  # @param [Integer] n record index
  # @return [void]
  def data_seek(n)
    @result.data_seek(n)
  end

  # @return [Integer] current record position
  def row_tell
    @result.row_tell
  end

  # Set current position of record
  # @param [Integer] n record index
  # @return [Integer] previous position
  def row_seek(n)
    @result.row_seek(n)
  end

  # @return [Integer] number of columns for last query
  def field_count
    @fields.length
  end

  # ignore
  # @return [void]
  def free_result
    # dummy
  end

  # Returns Mysql::Result object that is empty.
  # Use fields to get list of fields.
  # @return [Mysql::Result]
  def 
    return nil if @fields.empty?
    Result.new @fields
  end
end

Class Method Details

.finalizer(protocol, statement_id) ⇒ Object



25
26
27
28
29
# File 'lib/mysql/stmt.rb', line 25

def self.finalizer(protocol, statement_id)
  proc do
    protocol.gc_stmt statement_id
  end
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close prepared statement



131
132
133
134
135
# File 'lib/mysql/stmt.rb', line 131

def close
  ObjectSpace.undefine_finalizer(self)
  @protocol.stmt_close_command @statement_id if @statement_id
  @statement_id = nil
end

#data_seek(n) ⇒ void

This method returns an undefined value.

Set record position

Parameters:

  • n (Integer)

    record index



184
185
186
# File 'lib/mysql/stmt.rb', line 184

def data_seek(n)
  @result.data_seek(n)
end

#each(**opts) {|Array| ... } ⇒ Mysql::Stmt, Enumerator

Iterate block with record.

Yields:

  • (Array)

    record data

Returns:

  • (Mysql::Stmt)

    self

  • (Enumerator)

    If block is not specified



154
155
156
157
158
159
160
# File 'lib/mysql/stmt.rb', line 154

def each(**opts, &block)
  return enum_for(:each, **opts) unless block
  while (rec = fetch(*opts))
    block.call rec
  end
  self
end

#each_hash(**opts) {|Hash| ... } ⇒ Mysql::Stmt, Enumerator

Iterate block with record as Hash.

Parameters:

  • with_table (Boolean)

    if true, hash key is “table_name.field_name”.

Yields:

  • (Hash)

    record data

Returns:

  • (Mysql::Stmt)

    self

  • (Enumerator)

    If block is not specified



167
168
169
170
171
172
173
# File 'lib/mysql/stmt.rb', line 167

def each_hash(**opts, &block)
  return enum_for(:each_hash, **opts) unless block
  while (rec = fetch_hash(**opts))
    block.call rec
  end
  self
end

#execute(*values, **opts, &block) ⇒ Mysql::Result, ...

Execute prepared statement.

Parameters:

  • values (Object)

    values passed to query

Returns:

  • (Mysql::Result)

    if return_result is true and the query returns result set.

  • (nil)

    if return_result is true and the query does not return result set.

  • (self)

    if return_result is false or block is specified.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mysql/stmt.rb', line 66

def execute(*values, **opts, &block)
  raise ClientError, "Invalid statement handle" unless @statement_id
  raise ClientError, "not prepared" unless @param_count
  raise ClientError, "parameter count mismatch" if values.length != @param_count
  values = values.map{|v| @protocol.charset.convert v}
  opts = @opts.merge(opts)
  begin
    @sqlstate = "00000"
    @protocol.stmt_execute_command @statement_id, values
    @fields = @result = nil
    if block
      while true
        get_result
        res = store_result(**opts)
        block.call res if res || opts[:yield_null_result]
        break unless more_results?
      end
      return self
    end
    get_result
    return self unless opts[:return_result]
    return store_result(**opts)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end
end

#fetch(**opts) ⇒ Array

Returns current record data.

Returns:

  • (Array)

    current record data



138
139
140
# File 'lib/mysql/stmt.rb', line 138

def fetch(**opts)
  @result.fetch(**opts)
end

#fetch_hash(**opts) ⇒ Hash

Return data of current record as Hash. The hash key is field name.

Parameters:

  • with_table (Boolean)

    if true, hash key is “table_name.field_name”.

Returns:

  • (Hash)

    record data



146
147
148
# File 'lib/mysql/stmt.rb', line 146

def fetch_hash(**opts)
  @result.fetch_hash(**opts)
end

#field_countInteger

Returns number of columns for last query.

Returns:

  • (Integer)

    number of columns for last query



201
202
203
# File 'lib/mysql/stmt.rb', line 201

def field_count
  @fields.length
end

#free_resultvoid

This method returns an undefined value.

ignore



207
208
209
# File 'lib/mysql/stmt.rb', line 207

def free_result
  # dummy
end

#get_resultObject



95
96
97
98
99
# File 'lib/mysql/stmt.rb', line 95

def get_result
  @protocol.get_result
  @affected_rows, @insert_id, @server_status, @warning_count, @info =
    @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message
end

#more_results?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/mysql/stmt.rb', line 108

def more_results?
  @protocol.more_results?
end

#next_result(**opts) ⇒ Mysql::StatementResult, ...

execute next query if precedure is called.

Returns:

  • (Mysql::StatementResult)

    result set of query if return_result is true.

  • (true)

    if return_result is false and result exists.

  • (nil)

    query returns no results or no more results.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mysql/stmt.rb', line 116

def next_result(**opts)
  return nil unless more_results?
  opts = @opts.merge(opts)
  @fields = @result = nil
  get_result
  return self unless opts[:return_result]
  return store_result(**opts)
rescue ServerError => e
  @last_error = e
  @sqlstate = e.sqlstate
  raise
end

#prepare(str) ⇒ Object

parse prepared-statement and return Mysql::Stmt object

Parameters:

  • str (String)

    query string

Returns:

  • self

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mysql/stmt.rb', line 46

def prepare(str)
  raise ClientError, 'MySQL client is not connected' unless @protocol
  close
  begin
    @sqlstate = "00000"
    @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str)
  rescue ServerError => e
    @last_error = e
    @sqlstate = e.sqlstate
    raise
  end
  ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
  self
end

#result_metadataMysql::Result

Returns Mysql::Result object that is empty. Use fields to get list of fields.

Returns:



214
215
216
217
# File 'lib/mysql/stmt.rb', line 214

def 
  return nil if @fields.empty?
  Result.new @fields
end

#row_seek(n) ⇒ Integer

Set current position of record

Parameters:

  • n (Integer)

    record index

Returns:

  • (Integer)

    previous position



196
197
198
# File 'lib/mysql/stmt.rb', line 196

def row_seek(n)
  @result.row_seek(n)
end

#row_tellInteger

Returns current record position.

Returns:

  • (Integer)

    current record position



189
190
191
# File 'lib/mysql/stmt.rb', line 189

def row_tell
  @result.row_tell
end

#sizeInteger Also known as: num_rows

Returns number of record.

Returns:

  • (Integer)

    number of record



176
177
178
# File 'lib/mysql/stmt.rb', line 176

def size
  @result.size
end

#store_result(**opts) ⇒ Object



101
102
103
104
105
106
# File 'lib/mysql/stmt.rb', line 101

def store_result(**opts)
  return nil if @protocol.field_count.nil? || @protocol.field_count == 0
  @fields = @protocol.retr_fields
  opts = @opts.merge(opts)
  @result = StatementResult.new(@fields, @protocol, **opts)
end