class Mysql::Stmt
@!visibility public Prepared statement @!attribute [r] affected_rows
@return [Integer]
@!attribute [r] insert_id
@return [Integer]
@!attribute [r] server_status
@return [Integer]
@!attribute [r] warning_count
@return [Integer]
@!attribute [r] param_count
@return [Integer]
@!attribute [r] fields
@return [Array<Mysql::Field>]
@!attribute [r] sqlstate
@return [String]
Constants
- CURSOR_TYPE_FOR_UPDATE
- CURSOR_TYPE_NO_CURSOR
-
Cursor type
- CURSOR_TYPE_READ_ONLY
- CURSOR_TYPE_SCROLLABLE
Attributes
Public Class Methods
Source
# File lib/mysql/stmt.rb, line 25 def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end
@private
Source
# 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
@private @param [Mysql::Protocol] protocol
Public Instance Methods
Source
# 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
Close prepared statement @return [void]
Source
# File lib/mysql/stmt.rb, line 184 def data_seek(n) @result.data_seek(n) end
Set record position @param [Integer] n record index @return [void]
Source
# 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
Iterate block with record. @yield [Array] record data @return [Mysql::Stmt] self @return [Enumerator] If block is not specified
Source
# 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
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
Source
# 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
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.
Source
# File lib/mysql/stmt.rb, line 138 def fetch(**opts) @result.fetch(**opts) end
@return [Array] current record data
Source
# File lib/mysql/stmt.rb, line 146 def fetch_hash(**opts) @result.fetch_hash(**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
Source
# File lib/mysql/stmt.rb, line 201 def field_count @fields.length end
@return [Integer] number of columns for last query
Source
# File lib/mysql/stmt.rb, line 207 def free_result # dummy end
ignore @return [void]
Source
# 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
Source
# File lib/mysql/stmt.rb, line 108 def more_results? @protocol.more_results? end
Source
# 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
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.
Source
# 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
@private parse prepared-statement and return {Mysql::Stmt} object @param [String] str query string @return self
Source
# File lib/mysql/stmt.rb, line 214 def result_metadata return nil if @fields.empty? Result.new @fields end
Returns Mysql::Result object that is empty. Use fields to get list of fields. @return [Mysql::Result]
Source
# File lib/mysql/stmt.rb, line 196 def row_seek(n) @result.row_seek(n) end
Set current position of record @param [Integer] n record index @return [Integer] previous position
Source
# File lib/mysql/stmt.rb, line 189 def row_tell @result.row_tell end
@return [Integer] current record position
Source
# File lib/mysql/stmt.rb, line 176 def size @result.size end
@return [Integer] number of record
Source
# 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