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
@private
# File lib/mysql.rb, line 801 def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end
@private @param [Mysql::Protocol] protocol
# File lib/mysql.rb, line 809 def initialize(protocol) @protocol = protocol @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil end
Public Instance Methods
Close prepared statement @return [void]
# File lib/mysql.rb, line 862 def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end
Set record position @param [Integer] n record index @return [void]
# File lib/mysql.rb, line 915 def data_seek(n) @result.data_seek(n) end
Iterate block with record. @yield [Array] record data @return [Mysql::Stmt] self @return [Enumerator] If block is not specified
# File lib/mysql.rb, line 885 def each(&block) return enum_for(:each) unless block while rec = fetch 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
# File lib/mysql.rb, line 898 def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end
Execute prepared statement. @param [Object] values values passed to query @return [Mysql::Stmt] self
# File lib/mysql.rb, line 838 def execute(*values) 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} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol.message end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end
@return [Array] current record data
# File lib/mysql.rb, line 869 def fetch @result.fetch 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
# File lib/mysql.rb, line 877 def fetch_hash(with_table=nil) @result.fetch_hash with_table end
@return [Integer] number of columns for last query
# File lib/mysql.rb, line 932 def field_count @fields.length end
ignore @return [void]
# File lib/mysql.rb, line 938 def free_result end
@private parse prepared-statement and return {Mysql::Stmt} object @param [String] str query string @return self
# File lib/mysql.rb, line 821 def prepare(str) 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
Returns Mysql::Result
object that is empty. Use fetch_fields to get list of fields. @return [Mysql::Result]
# File lib/mysql.rb, line 944 def result_metadata return nil if @fields.empty? Result.new @fields end
Set current position of record @param [Integer] n record index @return [Integer] previous position
# File lib/mysql.rb, line 927 def row_seek(n) @result.row_seek(n) end
@return [Integer] current record position
# File lib/mysql.rb, line 920 def row_tell @result.row_tell end
@return [Integer] number of record
# File lib/mysql.rb, line 907 def size @result.size end