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

affected_rows[R]
fields[R]
insert_id[R]
param_count[R]
server_status[R]
sqlstate[R]
warning_count[R]

Public Class Methods

finalizer(protocol, statement_id) click to toggle source

@private

# File lib/mysql.rb, line 801
def self.finalizer(protocol, statement_id)
  proc do
    protocol.gc_stmt statement_id
  end
end
new(protocol) click to toggle source

@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() click to toggle source

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
data_seek(n) click to toggle source

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
each(&block) click to toggle source

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
each_hash(with_table=nil, &block) click to toggle source

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(*values) click to toggle source

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
fetch() click to toggle source

@return [Array] current record data

# File lib/mysql.rb, line 869
def fetch
  @result.fetch
end
fetch_hash(with_table=nil) click to toggle source

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
field_count() click to toggle source

@return [Integer] number of columns for last query

# File lib/mysql.rb, line 932
def field_count
  @fields.length
end
free_result() click to toggle source

ignore @return [void]

# File lib/mysql.rb, line 938
def free_result
end
num_rows()
Alias for: size
prepare(str) click to toggle source

@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
result_metadata() click to toggle source

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
row_seek(n) click to toggle source

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
row_tell() click to toggle source

@return [Integer] current record position

# File lib/mysql.rb, line 920
def row_tell
  @result.row_tell
end
size() click to toggle source

@return [Integer] number of record

# File lib/mysql.rb, line 907
def size
  @result.size
end
Also aliased as: num_rows