Class: Mysql::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql.rb,
lib/mysql/compat.rb,
lib/mysql/constants.rb

Overview

Prepared statement

Constant Summary collapse

CURSOR_TYPE_NO_CURSOR =

Cursor type

0
CURSOR_TYPE_READ_ONLY =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql) ⇒ Statement

Returns a new instance of Statement.



648
649
650
651
652
653
654
655
# File 'lib/mysql.rb', line 648

def initialize(mysql)
  @mysql = mysql
  @protocol = mysql.protocol
  @statement_id = nil
  @affected_rows = @insert_id = @server_status = @warning_count = 0
  @sqlstate = "00000"
  @param_count = nil
end

Instance Attribute Details

#affected_rowsObject (readonly)

Returns the value of attribute affected_rows.



634
635
636
# File 'lib/mysql.rb', line 634

def affected_rows
  @affected_rows
end

#fieldsObject (readonly)

Returns the value of attribute fields.



635
636
637
# File 'lib/mysql.rb', line 635

def fields
  @fields
end

#insert_idObject (readonly)

Returns the value of attribute insert_id.



634
635
636
# File 'lib/mysql.rb', line 634

def insert_id
  @insert_id
end

#param_countObject (readonly)

Returns the value of attribute param_count.



635
636
637
# File 'lib/mysql.rb', line 635

def param_count
  @param_count
end

#server_statusObject (readonly)

Returns the value of attribute server_status.



634
635
636
# File 'lib/mysql.rb', line 634

def server_status
  @server_status
end

#sqlstateObject (readonly)

Returns the value of attribute sqlstate.



635
636
637
# File 'lib/mysql.rb', line 635

def sqlstate
  @sqlstate
end

#warning_countObject (readonly)

Returns the value of attribute warning_count.



634
635
636
# File 'lib/mysql.rb', line 634

def warning_count
  @warning_count
end

Class Method Details

.finalizer(protocol, statement_id) ⇒ Object



637
638
639
640
641
642
643
644
645
646
# File 'lib/mysql.rb', line 637

def self.finalizer(protocol, statement_id)
  proc do
    Thread.new do
      protocol.synchronize do
        protocol.reset
        protocol.send_packet Protocol::StmtClosePacket.new(statement_id)
      end
    end
  end
end

Instance Method Details

#closeObject



722
723
724
725
726
727
728
729
730
731
# File 'lib/mysql.rb', line 722

def close
  ObjectSpace.undefine_finalizer(self)
  @protocol.synchronize do
    @protocol.reset
    if @statement_id
      @protocol.send_packet Protocol::StmtClosePacket.new(@statement_id)
      @statement_id = nil
    end
  end
end

#data_seek(n) ⇒ Object



237
238
239
# File 'lib/mysql/compat.rb', line 237

def data_seek(n)
  @res.data_seek(n)
end

#each(*args, &block) ⇒ Object



229
230
231
# File 'lib/mysql/compat.rb', line 229

def each(*args, &block)
  @res.each(*args, &block)
end

#execute(*args) ⇒ Object

execute prepared-statement.

Return

Mysql::Result

Raises:



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/mysql.rb', line 695

def execute(*values)
  raise ClientError, "not prepared" unless @param_count
  raise ClientError, "parameter count mismatch" if values.length != @param_count
  values = values.map{|v| @mysql.charset.convert v}
  @protocol.synchronize do
    begin
      @sqlstate = "00000"
      @protocol.reset
      @protocol.send_packet Protocol::ExecutePacket.new(@statement_id, CURSOR_TYPE_NO_CURSOR, values)
      res_packet = @protocol.read_result_packet
      raise ProtocolError, "invalid field_count" unless res_packet.field_count == @fields.length
      @fieldname_with_table = nil
      if res_packet.field_count == 0
        @affected_rows, @insert_id, @server_status, @warning_conut =
          res_packet.affected_rows, res_packet.insert_id, res_packet.server_status, res_packet.warning_count
        return nil
      end
      @fields = Array.new(res_packet.field_count).map{Field.new @protocol.read_field_packet}
      @protocol.read_eof_packet
      return StatementResult.new(@mysql, @fields)
    rescue ServerError => e
      @sqlstate = e.sqlstate
      raise
    end
  end
end

#execute_origObject

execute prepared-statement.

Return

Mysql::Result

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mysql/compat.rb', line 219

def execute(*values)
  raise ClientError, "not prepared" unless @param_count
  raise ClientError, "parameter count mismatch" if values.length != @param_count
  values = values.map{|v| @mysql.charset.convert v}
  @protocol.synchronize do
    begin
      @sqlstate = "00000"
      @protocol.reset
      @protocol.send_packet Protocol::ExecutePacket.new(@statement_id, CURSOR_TYPE_NO_CURSOR, values)
      res_packet = @protocol.read_result_packet
      raise ProtocolError, "invalid field_count" unless res_packet.field_count == @fields.length
      @fieldname_with_table = nil
      if res_packet.field_count == 0
        @affected_rows, @insert_id, @server_status, @warning_conut =
          res_packet.affected_rows, res_packet.insert_id, res_packet.server_status, res_packet.warning_count
        return nil
      end
      @fields = Array.new(res_packet.field_count).map{Field.new @protocol.read_field_packet}
      @protocol.read_eof_packet
      return StatementResult.new(@mysql, @fields)
    rescue ServerError => e
      @sqlstate = e.sqlstate
      raise
    end
  end
end

#fetchObject Also known as: fetch_row



224
225
226
# File 'lib/mysql/compat.rb', line 224

def fetch
  @res.fetch
end

#field_countObject



249
250
251
# File 'lib/mysql/compat.rb', line 249

def field_count
  @fields.length
end

#free_resultObject



253
254
255
# File 'lib/mysql/compat.rb', line 253

def free_result
  # do nothing
end

#num_rowsObject



233
234
235
# File 'lib/mysql/compat.rb', line 233

def num_rows
  @res.num_rows
end

#prepare(str) ⇒ Object

parse prepared-statement and return Mysql::Statement object

Argument

str
String

query string

Return

self



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/mysql.rb', line 662

def prepare(str)
  close
  @protocol.synchronize do
    begin
      @sqlstate = "00000"
      @protocol.reset
      @protocol.send_packet Protocol::PreparePacket.new(@mysql.charset.convert(str))
      res_packet = @protocol.read_prepare_result_packet
      if res_packet.param_count > 0
        res_packet.param_count.times{@protocol.read}   # skip parameter packet
        @protocol.read_eof_packet
      end
      if res_packet.field_count > 0
        fields = Array.new(res_packet.field_count).map{Field.new @protocol.read_field_packet}
        @protocol.read_eof_packet
      else
        fields = []
      end
      @statement_id = res_packet.statement_id
      @param_count = res_packet.param_count
      @fields = fields
    rescue ServerError => e
      @sqlstate = e.sqlstate
      raise
    end
  end
  ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id))
  self
end

#result_metadataObject



257
258
259
260
261
262
263
264
# File 'lib/mysql/compat.rb', line 257

def 
  return nil if @fields.empty?
  res = Result.allocate
  res.instance_variable_set :@mysql, @mysql
  res.instance_variable_set :@fields, @fields
  res.instance_variable_set :@records, []
  res
end

#row_seek(n) ⇒ Object



245
246
247
# File 'lib/mysql/compat.rb', line 245

def row_seek(n)
  @res.row_seek(n)
end

#row_tellObject



241
242
243
# File 'lib/mysql/compat.rb', line 241

def row_tell
  @res.row_tell
end