class MysqlPR::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<MysqlPR::Field>]
@!attribute [r] sqlstate
@return [String]
Constants
- CURSOR_TYPE_NO_CURSOR
Cursor type
- CURSOR_TYPE_READ_ONLY
Attributes
Public Class Methods
@private
# File lib/mysql-pr.rb, line 821 def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end
@private @param [MysqlPR::Protocol] protocol @param [MysqlPR::Charset] charset
# File lib/mysql-pr.rb, line 830 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
Set retrieve type of value @param [Numeric / Fixnum / Integer / Float / String / MysqlPR::Time
/ nil] args value type @return [MysqlPR::Stmt] self
# File lib/mysql-pr.rb, line 946 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, MysqlPR::Time, nil].include? a end @bind_result = args self end
Close prepared statement @return [void]
# File lib/mysql-pr.rb, line 885 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-pr.rb, line 991 def data_seek(n) @result.data_seek(n) end
Iterate block with record. @yield [Array] record data @return [MysqlPR::Stmt] self @return [Enumerator] If block is not specified
# File lib/mysql-pr.rb, line 961 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 [MysqlPR::Stmt] self @return [Enumerator] If block is not specified
# File lib/mysql-pr.rb, line 974 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 [MysqlPR::Stmt] self
# File lib/mysql-pr.rb, line 861 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
@return [Array] current record data
# File lib/mysql-pr.rb, line 892 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 == MysqlPR::Time && !col.is_a?(MysqlPR::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 MysqlPR::Time.new(y, m, d, h, mm, s) else MysqlPR::Time.new end else col end end 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-pr.rb, line 939 def fetch_hash(with_table=nil) @result.fetch_hash with_table end
@return [Integer] number of columns for last query
# File lib/mysql-pr.rb, line 1008 def field_count @fields.length end
ignore @return [void]
# File lib/mysql-pr.rb, line 1014 def free_result end
@private parse prepared-statement and return {MysqlPR::Stmt} object @param [String] str query string @return self
# File lib/mysql-pr.rb, line 844 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 MysqlPR::Result
object that is empty. Use fetch_fields to get list of fields. @return [MysqlPR::Result]
# File lib/mysql-pr.rb, line 1020 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-pr.rb, line 1003 def row_seek(n) @result.row_seek(n) end
@return [Integer] current record position
# File lib/mysql-pr.rb, line 996 def row_tell @result.row_tell end
@return [Integer] number of record
# File lib/mysql-pr.rb, line 983 def size @result.size end