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_NO_CURSOR

Cursor type

CURSOR_TYPE_READ_ONLY

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/vendor/mysql.rb, line 815
def self.finalizer(protocol, statement_id)
  proc do
    protocol.gc_stmt statement_id
  end
end
new(protocol, charset) click to toggle source

@private @param [Mysql::Protocol] protocol @param [Mysql::Charset] charset

# File lib/vendor/mysql.rb, line 824
def initialize(protocol, charset)
  @protocol = protocol
  @charset = charset
  @statement_id = nil
  @affected_rows = @insert_id = @server_status = @warning_count = 0
  @sqlstate = "00000"
  @param_count = nil
  @bind_result = nil
end

Public Instance Methods

bind_result(*args) click to toggle source

Set retrieve type of value @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type @return [Mysql::Stmt] self

# File lib/vendor/mysql.rb, line 940
def bind_result(*args)
  if @fields.length != args.length
    raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})"
  end
  args.each do |a|
    raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a
  end
  @bind_result = args
  self
end
close() click to toggle source

Close prepared statement @return [void]

# File lib/vendor/mysql.rb, line 879
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/vendor/mysql.rb, line 985
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/vendor/mysql.rb, line 955
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/vendor/mysql.rb, line 968
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/vendor/mysql.rb, line 855
def execute(*values)
  raise ClientError, "not prepared" unless @param_count
  raise ClientError, "parameter count mismatch" if values.length != @param_count
  values = values.map{|v| @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, @charset
    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/vendor/mysql.rb, line 886
def fetch
  row = @result.fetch
  return row unless @bind_result
  row.zip(@bind_result).map do |col, type|
    if col.nil?
      nil
    elsif [Numeric, Integer, Fixnum].include? type
      col.to_i
    elsif type == String
      col.to_s
    elsif type == Float && !col.is_a?(Float)
      col.to_i.to_f
    elsif type == Mysql::Time && !col.is_a?(Mysql::Time)
      if col.to_s =~ /\A\d+\z/
        i = col.to_s.to_i
        if i < 100000000
          y = i/10000
          m = i/100%100
          d = i%100
          h, mm, s = 0
        else
          y = i/10000000000
          m = i/100000000%100
          d = i/1000000%100
          h = i/10000%100
          mm= i/100%100
          s = i%100
        end
        if y < 70
          y += 2000
        elsif y < 100
          y += 1900
        end
        Mysql::Time.new(y, m, d, h, mm, s)
      else
        Mysql::Time.new
      end
    else
      col
    end
  end
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/vendor/mysql.rb, line 933
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/vendor/mysql.rb, line 1002
def field_count
  @fields.length
end
free_result() click to toggle source

ignore @return [void]

# File lib/vendor/mysql.rb, line 1008
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/vendor/mysql.rb, line 838
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/vendor/mysql.rb, line 1014
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/vendor/mysql.rb, line 997
def row_seek(n)
  @result.row_seek(n)
end
row_tell() click to toggle source

@return [Integer] current record position

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

@return [Integer] number of record

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